From 07912df2f1935c7dd4e08f8e675ed989dbc97b31 Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Sat, 9 Jan 2021 15:42:32 +0300 Subject: [PATCH] Fix dependencies relative paths determination --- README | 2 +- dep.go | 23 +++++++++++++++-------- main.go | 10 ++++++++-- run.go | 5 ++++- t/goredo-deps.t | 40 ++++++++++++++++++++++++++++++++++++++++ usage.go | 2 +- 6 files changed, 69 insertions(+), 13 deletions(-) create mode 100755 t/goredo-deps.t diff --git a/README b/README index a35eab3..d7bae44 100644 --- a/README +++ b/README @@ -30,7 +30,7 @@ problems with the authenticity on your side, then build it manually: > $ git clone git://git.cypherpunks.ru/goredo.git $ cd goredo - $ git tag -v v0.8.0 + $ git tag -v v0.9.0 $ git clone git://git.cypherpunks.ru/gorecfile.git $ ( cd gorecfile ; git tag -v v0.4.0 ) $ echo "replace go.cypherpunks.ru/recfile => `pwd`/gorecfile" >> go.mod diff --git a/dep.go b/dep.go index 38d8bb7..df5ae27 100644 --- a/dep.go +++ b/dep.go @@ -27,13 +27,15 @@ import ( "io" "os" "path" - "strings" + "path/filepath" "go.cypherpunks.ru/recfile" "golang.org/x/crypto/blake2b" "golang.org/x/sys/unix" ) +var DirPrefix string + func recfileWrite(fdDep *os.File, fields ...recfile.Field) error { w := recfile.NewWriter(fdDep) if _, err := w.RecordStart(); err != nil { @@ -129,17 +131,22 @@ func writeDeps(fdDep *os.File, tgts []string) error { trace(CDebug, "no opened fdDep: %s", tgts) return nil } - ups := []string{} - upLevels := strings.Count(os.Getenv(EnvDirPrefix), "/") - for i := 0; i < upLevels; i++ { - ups = append(ups, "..") - } - up := path.Join(ups...) for _, tgt := range tgts { if _, err := os.Stat(tgt); err == nil { - if err = writeDep(fdDep, Cwd, path.Join(up, tgt)); err != nil { + tgtAbs, err := filepath.Abs(tgt) + if err != nil { + panic(err) + } + tgtDir := path.Join(Cwd, DirPrefix) + tgtRel, err := filepath.Rel(tgtDir, tgtAbs) + if err != nil { + panic(err) + } + if err = writeDep(fdDep, tgtDir, tgtRel); err != nil { return err } + } else { + trace(CDebug, "skipping dep record, can not stat: %s", tgt) } } return nil diff --git a/main.go b/main.go index 1262c57..ed82856 100644 --- a/main.go +++ b/main.go @@ -112,6 +112,7 @@ func main() { panic(err) } } + DirPrefix = os.Getenv(EnvDirPrefix) if *flagStderrKeep { mustSetenv(EnvStderrKeep, "1") @@ -199,7 +200,10 @@ func main() { ok := true err = nil cmdName := path.Base(os.Args[0]) - trace(CDebug, "[%s] run: %s %s [%s]", BuildUUID, cmdName, tgts, Cwd) + trace( + CDebug, "[%s] run: %s %s cwd:%s dirprefix:%s", + BuildUUID, cmdName, tgts, Cwd, DirPrefix, + ) CmdSwitch: switch cmdName { @@ -212,7 +216,9 @@ CmdSwitch: } case "redo-ifchange": ok, err = ifchange(tgts, false, traced) - writeDeps(fdDep, tgts) + if err == nil { + err = writeDeps(fdDep, tgts) + } case "redo-ifcreate": if fdDep == nil { log.Fatalln("no", EnvDepFd) diff --git a/run.go b/run.go index aab8e9a..4724870 100644 --- a/run.go +++ b/run.go @@ -379,7 +379,10 @@ func runScript(tgtOrig string, errs chan error, traced bool) error { } fdStderr.Truncate(0) } - shCtx := fmt.Sprintf("sh: %s: %s %s [%s]", tgtOrig, cmdName, args, cwd) + shCtx := fmt.Sprintf( + "sh: %s: %s %s cwd:%s dirprefix:%s", + tgtOrig, cmdName, args, cwd, dirPrefix, + ) trace(CDebug, "%s", shCtx) Jobs.Add(1) diff --git a/t/goredo-deps.t b/t/goredo-deps.t new file mode 100755 index 0000000..a0a58d5 --- /dev/null +++ b/t/goredo-deps.t @@ -0,0 +1,40 @@ +#!/bin/sh + +testname=`basename "$0"` +test_description="Check that the following use-case won't rebuild everytime and has correct dependencies" +. $SHARNESS_TEST_SRCDIR/sharness.sh + +tmp=`mktemp -d` +trap "rm -fr $tmp" HUP PIPE INT QUIT TERM EXIT +cd $tmp +mkdir -p sub +cat > default.html.do < default.pre.do < footer.inc +echo "" > sub/index.html.in + +test_expect_success Build "redo sub/index.html" +stat1=`stat sub/index.html` +test_expect_success Rebuild "redo-ifchange sub/index.html" +stat2=`stat sub/index.html` +test_expect_success "Was not rebuild" '[ "$stat1" = "$stat2" ]' + +tgts=`sed -n "s/^Target: //p" sub/.redo/index.html.dep | sort` +tgts=`echo $tgts` +tgts_expected="../default.html.do" # .do itself +tgts_expected="$tgts_expected default.do" # ifcreate +tgts_expected="$tgts_expected default.html.do" # ifcreate +tgts_expected="$tgts_expected index.html" # check for user-modification +tgts_expected="$tgts_expected index.html.do" # ifcreate +tgts_expected="$tgts_expected index.pre" # source +test_expect_success "Correct dependencies" '[ "$tgts" = "$tgts_expected" ]' + +test_done diff --git a/usage.go b/usage.go index 8f0b0e5..caa905b 100644 --- a/usage.go +++ b/usage.go @@ -26,7 +26,7 @@ import ( ) const ( - Version = "0.8.0" + Version = "0.9.0" Warranty = `This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3 of the License. -- 2.44.0