| 1 | package gitcmd |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | // StageAll excludes nested repositories by literal name: a name that reads as |
| 11 | // pathspec magic or a glob must not drop a sibling or leak the repository in. |
| 12 | func TestStageAllExcludesNestedRepositoriesByLiteralName(t *testing.T) { |
| 13 | f := newRepoFixture(t, "", map[string]string{"top.txt": "top\n"}) |
| 14 | names := []string{"a*", "[x]", " sp ace ", "-dash", "ünï", ":(glob)c", "a)b", "q?"} |
| 15 | p := f.payload() |
| 16 | for _, n := range names { |
| 17 | dir := filepath.Join(f.dir, n) |
| 18 | f.plainIn(f.dir, "init", "--quiet", dir) |
| 19 | f.plainIn(dir, "config", "filter.pwn.clean", p) |
| 20 | if err := os.WriteFile(filepath.Join(dir, ".gitattributes"), []byte("* filter=pwn\n"), 0o644); err != nil { |
| 21 | t.Fatal(err) |
| 22 | } |
| 23 | if err := os.WriteFile(filepath.Join(dir, "in.txt"), []byte("x\n"), 0o644); err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | } |
| 27 | // siblings a glob-read exclusion would wrongly drop |
| 28 | for _, s := range []string{"ab.txt", "x", "q1", "a)bc.txt"} { |
| 29 | f.write(s, s+"\n") |
| 30 | } |
| 31 | repo, err := Open(f.ctx, f.dir) |
| 32 | if err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | if err := repo.StageAll(f.ctx); err != nil { |
| 36 | t.Fatalf("StageAll: %v", err) |
| 37 | } |
| 38 | staged := f.plain("ls-files", "-s", "-z") |
| 39 | for _, s := range []string{"ab.txt", "x", "q1", "a)bc.txt"} { |
| 40 | if !strings.Contains(staged, "\t"+s+"\x00") { |
| 41 | t.Errorf("sibling %q not staged: %q", s, staged) |
| 42 | } |
| 43 | } |
| 44 | for _, n := range names { |
| 45 | if strings.Contains(staged, "\t"+n+"\x00") || strings.Contains(staged, "\t"+n+"/") { |
| 46 | t.Errorf("nested repo %q staged", n) |
| 47 | } |
| 48 | } |
| 49 | f.assertNotExecuted() |
| 50 | } |
| 51 |