diff --git a/internal/cli/add.go b/internal/cli/add.go index 46283be..1d4c9e1 100644 --- a/internal/cli/add.go +++ b/internal/cli/add.go @@ -31,6 +31,7 @@ browser || module || main) from the package's package.json.`, if err != nil { return err } + warnGitIgnored(cmd.ErrOrStderr(), opts.Dir, res.SyncResult) out := cmd.OutOrStdout() fmt.Fprintf(out, "added %s %s (resolved %s)\n", res.Entry.Name, res.Entry.Version, res.Resolved) if res.SyncResult != nil { diff --git a/internal/cli/gitignore.go b/internal/cli/gitignore.go new file mode 100644 index 0000000..e3b56a2 --- /dev/null +++ b/internal/cli/gitignore.go @@ -0,0 +1,59 @@ +package cli + +import ( + "bytes" + "fmt" + "io" + "os/exec" + "path" + "strings" + + pin "github.com/git-pkgs/pin" +) + +// warnGitIgnored warns when git ignores files that sync just wrote, since +// git status then hides them. +func warnGitIgnored(w io.Writer, dir string, res *pin.SyncResult) { + if res == nil || res.Lock == nil || len(res.Written) == 0 { + return + } + var in bytes.Buffer + for _, f := range res.Written { + in.WriteString(path.Join(res.Lock.OutDir, f)) + in.WriteByte(0) + } + cmd := exec.Command("git", "-C", dir, "check-ignore", "--verbose", "-z", "--stdin") + cmd.Stdin = &in + out, err := cmd.Output() + if err != nil { + // Exit 1 means nothing is ignored and 128 means dir is not in a + // work tree. Neither needs a warning, and sync works without git. + return + } + // --verbose -z prints source, line, pattern and path, each NUL-terminated. + fields := strings.Split(strings.TrimSuffix(string(out), "\x00"), "\x00") + type rule struct{ where, pattern string } + var order []rule + paths := map[rule][]string{} + for i := 0; i+3 < len(fields); i += 4 { + pattern := fields[i+2] + if strings.HasPrefix(pattern, "!") { + // --verbose also reports negated matches, which un-ignore the path. + continue + } + r := rule{where: fields[i] + ":" + fields[i+1], pattern: pattern} + if _, ok := paths[r]; !ok { + order = append(order, r) + } + paths[r] = append(paths[r], fields[i+3]) + } + for _, r := range order { + p := paths[r] + more, them := "", "it" + if len(p) > 1 { + more, them = fmt.Sprintf(" and %d more", len(p)-1), "them" + } + fmt.Fprintf(w, "warning: git ignores %s%s (%s %q), which pin.lock lists; commit %s with git add -f or change out: in the manifest\n", + p[0], more, r.where, r.pattern, them) + } +} diff --git a/internal/cli/gitignore_test.go b/internal/cli/gitignore_test.go new file mode 100644 index 0000000..4ee84fb --- /dev/null +++ b/internal/cli/gitignore_test.go @@ -0,0 +1,169 @@ +package cli + +import ( + "os" + "os/exec" + "path/filepath" + "strings" + "testing" +) + +const ignoredWarning = "warning: git ignores" + +// gitRepo makes dir a git work tree with the given .gitignore. Global and +// system config are cut off so a user's own excludes can't change results. +func gitRepo(t *testing.T, dir, gitignore string) { + t.Helper() + if _, err := exec.LookPath("git"); err != nil { + t.Skip("git not on PATH") + } + t.Setenv("GIT_CONFIG_GLOBAL", os.DevNull) + t.Setenv("GIT_CONFIG_NOSYSTEM", "1") + t.Setenv("XDG_CONFIG_HOME", t.TempDir()) + git(t, dir, "init", "-q") + if err := os.WriteFile(filepath.Join(dir, ".gitignore"), []byte(gitignore), 0o644); err != nil { + t.Fatal(err) + } +} + +func git(t *testing.T, dir string, args ...string) { + t.Helper() + out, err := exec.Command("git", append([]string{"-C", dir}, args...)...).CombinedOutput() + if err != nil { + t.Fatalf("git %v: %v\n%s", args, err, out) + } +} + +const vendorManifest = `out: "static/vendor" +assets: + - name: "demo" + version: "1.0.0" + files: ["dist/x.js"] +` + +func TestCLI_SyncWarnsWhenGitIgnoresOutput(t *testing.T) { + srv := fakeRegistry(t, "demo", "1.0.0", map[string]string{"dist/x.js": "x"}) + dir := t.TempDir() + gitRepo(t, dir, "vendor/\n") + writeManifest(t, dir, vendorManifest) + + out, err := runCmd(t, "sync", "-C", dir, "--registry", srv.URL) + if err != nil { + t.Fatalf("sync: %v\n%s", err, out) + } + want := `warning: git ignores static/vendor/demo/x.js (.gitignore:1 "vendor/"), which pin.lock lists; commit it with git add -f` + if !strings.Contains(out, want) { + t.Errorf("missing %q in:\n%s", want, out) + } + if _, err := os.Stat(filepath.Join(dir, "static/vendor/demo/x.js")); err != nil { + t.Errorf("the warning must not stop the write: %v", err) + } +} + +func TestCLI_SyncWarningCountsFilesPerRule(t *testing.T) { + srv := fakeRegistry(t, "demo", "1.0.0", map[string]string{"dist/a.js": "a", "dist/b.js": "b", "dist/c.css": "c"}) + dir := t.TempDir() + gitRepo(t, dir, "*.js\n") + writeManifest(t, dir, `out: "assets" +assets: + - name: "demo" + version: "1.0.0" + files: ["dist/a.js", "dist/b.js", "dist/c.css"] +`) + out, err := runCmd(t, "sync", "-C", dir, "--registry", srv.URL) + if err != nil { + t.Fatalf("sync: %v\n%s", err, out) + } + var warnings []string + for line := range strings.Lines(out) { + if strings.HasPrefix(line, ignoredWarning) { + warnings = append(warnings, line) + } + } + if len(warnings) != 1 { + t.Fatalf("want one warning for the one rule, got %d:\n%s", len(warnings), out) + } + if !strings.Contains(warnings[0], ` and 1 more (.gitignore:1 "*.js"), which pin.lock lists; commit them `) || strings.Contains(warnings[0], "c.css") { + t.Errorf("want the two .js files under one rule and no c.css: %s", warnings[0]) + } +} + +func TestCLI_SyncNoGitIgnoreWarning(t *testing.T) { + cases := []struct { + name string + setup func(t *testing.T, dir string) + }{ + {"not a git work tree", func(t *testing.T, dir string) {}}, + {"output not ignored", func(t *testing.T, dir string) { gitRepo(t, dir, "node_modules/\n") }}, + {"negated rule", func(t *testing.T, dir string) { gitRepo(t, dir, "*.js\n!x.js\n") }}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + srv := fakeRegistry(t, "demo", "1.0.0", map[string]string{"dist/x.js": "x"}) + dir := t.TempDir() + tc.setup(t, dir) + writeManifest(t, dir, vendorManifest) + out, err := runCmd(t, "sync", "-C", dir, "--registry", srv.URL) + if err != nil { + t.Fatalf("sync: %v\n%s", err, out) + } + if strings.Contains(out, ignoredWarning) { + t.Errorf("unexpected warning:\n%s", out) + } + }) + } +} + +func TestCLI_SyncNoWarningOnceFilesAreTracked(t *testing.T) { + srv := fakeRegistry(t, "demo", "1.0.0", map[string]string{"dist/x.js": "x"}) + dir := t.TempDir() + gitRepo(t, dir, "vendor/\n") + writeManifest(t, dir, vendorManifest) + if out, err := runCmd(t, "sync", "-C", dir, "--registry", srv.URL); err != nil || !strings.Contains(out, ignoredWarning) { + t.Fatalf("first sync: want the warning, got %v\n%s", err, out) + } + git(t, dir, "add", "-f", "static/vendor/demo/x.js") + out, err := runCmd(t, "sync", "-C", dir, "--registry", srv.URL) + if err != nil { + t.Fatalf("second sync: %v\n%s", err, out) + } + if strings.Contains(out, ignoredWarning) { + t.Errorf("tracked files are not ignored, but got:\n%s", out) + } +} + +func TestCLI_SyncDryRunDoesNotCheckIgnore(t *testing.T) { + srv := fakeRegistry(t, "demo", "1.0.0", map[string]string{"dist/x.js": "x"}) + dir := t.TempDir() + gitRepo(t, dir, "vendor/\n") + writeManifest(t, dir, vendorManifest) + out, err := runCmd(t, "sync", "-C", dir, "--registry", srv.URL, "--dry-run") + if err != nil { + t.Fatalf("sync --dry-run: %v\n%s", err, out) + } + if strings.Contains(out, ignoredWarning) { + t.Errorf("dry-run writes nothing, so nothing to warn about:\n%s", out) + } +} + +func TestCLI_AddAndUpdateWarnWhenGitIgnoresOutput(t *testing.T) { + srv := fakeRegistry(t, "demo", "1.0.0", map[string]string{"dist/x.js": "x"}) + dir := t.TempDir() + gitRepo(t, dir, "vendor/\n") + writeManifest(t, dir, "out: static/vendor\nassets: []\n") + + out, err := runCmd(t, "add", "-C", dir, "--registry", srv.URL, "demo@1.0.0", "dist/x.js") + if err != nil { + t.Fatalf("add: %v\n%s", err, out) + } + if !strings.Contains(out, ignoredWarning) { + t.Errorf("add: missing warning:\n%s", out) + } + out, err = runCmd(t, "update", "-C", dir, "--registry", srv.URL) + if err != nil { + t.Fatalf("update: %v\n%s", err, out) + } + if !strings.Contains(out, ignoredWarning) { + t.Errorf("update: missing warning:\n%s", out) + } +} diff --git a/internal/cli/sync.go b/internal/cli/sync.go index a10b888..f9da6ca 100644 --- a/internal/cli/sync.go +++ b/internal/cli/sync.go @@ -64,6 +64,7 @@ Common CI flags: if err != nil { return err } + warnGitIgnored(cmd.ErrOrStderr(), opts.Dir, res) out := cmd.OutOrStdout() if jsonOut { encoded, err := pin.EncodeLock(res.Lock) diff --git a/internal/cli/update.go b/internal/cli/update.go index 3f48bb1..b13f91f 100644 --- a/internal/cli/update.go +++ b/internal/cli/update.go @@ -22,6 +22,7 @@ func newUpdateCmd() *cobra.Command { if err != nil { return err } + warnGitIgnored(cmd.ErrOrStderr(), opts.Dir, res) out := cmd.OutOrStdout() fmt.Fprintf(out, "updated %d assets (%d changed, %d unchanged)\n", len(res.Lock.Assets), len(res.Changes.Updated)+len(res.Changes.Added), len(res.Changes.Unchanged))