|
| 1 | +package shellshape |
| 2 | + |
| 3 | +import "testing" |
| 4 | + |
| 5 | +func TestGit(t *testing.T) { |
| 6 | + tests := []struct { |
| 7 | + name string |
| 8 | + input string |
| 9 | + want string |
| 10 | + }{ |
| 11 | + // commit |
| 12 | + {"commit with message", "git commit -m 'fix login bug'", "git commit -m <str>"}, |
| 13 | + {"commit with long message flag", "git commit --message 'update readme'", "git commit --message <str>"}, |
| 14 | + {"commit with author", "git commit --author 'John Doe <john@example.com>'", "git commit --author <val>"}, |
| 15 | + {"commit amend no edit", "git commit --amend --no-edit", "git commit --amend --no-edit"}, |
| 16 | + {"commit specific files", "git commit -m 'fix' src/main.go src/util.go", "git commit -m <str> <path>+"}, |
| 17 | + {"commit all with message", "git commit -am 'quick fix'", "git commit -am <str>"}, |
| 18 | + |
| 19 | + // add |
| 20 | + {"add single file", "git add src/main.go", "git add <path>"}, |
| 21 | + {"add multiple files", "git add file1.js file2.js file3.js", "git add <path>+"}, |
| 22 | + {"add all", "git add -A", "git add -A"}, |
| 23 | + {"add patch", "git add -p src/main.go", "git add -p <path>"}, |
| 24 | + |
| 25 | + // push / pull / fetch |
| 26 | + {"push simple", "git push", "git push"}, |
| 27 | + {"push remote branch", "git push origin main", "git push origin main"}, |
| 28 | + {"push force", "git push --force-with-lease origin feature/foo", "git push --force-with-lease origin <path>"}, |
| 29 | + {"pull", "git pull origin main", "git pull origin main"}, |
| 30 | + {"pull rebase", "git pull --rebase", "git pull --rebase"}, |
| 31 | + {"fetch", "git fetch --all", "git fetch --all"}, |
| 32 | + {"fetch prune", "git fetch --prune origin", "git fetch --prune origin"}, |
| 33 | + |
| 34 | + // log |
| 35 | + {"log simple", "git log --oneline", "git log --oneline"}, |
| 36 | + {"log with count", "git log -n 10 --oneline", "git log -n N --oneline"}, |
| 37 | + {"log with format", "git log --format '%H %s'", "git log --format <val>"}, |
| 38 | + {"log with pretty", "git log --pretty=oneline", "git log --pretty=<val>"}, |
| 39 | + {"log with author filter", "git log --author john", "git log --author <val>"}, |
| 40 | + {"log since", "git log --since '2024-01-01'", "git log --since <val>"}, |
| 41 | + |
| 42 | + // diff |
| 43 | + {"diff simple", "git diff", "git diff"}, |
| 44 | + {"diff staged", "git diff --staged", "git diff --staged"}, |
| 45 | + {"diff with file", "git diff HEAD src/main.go", "git diff HEAD <path>"}, |
| 46 | + {"diff rev range", "git diff main...feature", "git diff <range>"}, |
| 47 | + {"diff context lines", "git diff -U 5", "git diff -U N"}, |
| 48 | + |
| 49 | + // clone |
| 50 | + {"clone https", "git clone https://github.com/user/repo.git", "git clone <https-uri>"}, |
| 51 | + {"clone ssh", "git clone git@github.com:user/repo.git", "git clone <git-uri>"}, |
| 52 | + {"clone with dir", "git clone https://github.com/user/repo.git mydir", "git clone <https-uri> mydir"}, |
| 53 | + {"clone depth", "git clone --depth 1 https://github.com/user/repo.git", "git clone --depth N <https-uri>"}, |
| 54 | + |
| 55 | + // checkout / switch / branch |
| 56 | + {"checkout branch", "git checkout main", "git checkout main"}, |
| 57 | + {"checkout new branch", "git checkout -b new-feature", "git checkout -b new-feature"}, |
| 58 | + {"switch branch", "git switch main", "git switch main"}, |
| 59 | + {"branch list", "git branch", "git branch"}, |
| 60 | + {"branch delete", "git branch -d old-branch", "git branch -d old-branch"}, |
| 61 | + |
| 62 | + // rebase / merge / reset |
| 63 | + {"rebase onto", "git rebase --onto main feature", "git rebase --onto <val> feature"}, |
| 64 | + {"rebase interactive", "git rebase -i HEAD~3", "git rebase -i <rev>"}, |
| 65 | + {"merge branch", "git merge feature-branch", "git merge feature-branch"}, |
| 66 | + {"reset soft", "git reset --soft HEAD~1", "git reset --soft <rev>"}, |
| 67 | + {"reset file", "git reset HEAD src/main.go", "git reset HEAD <path>"}, |
| 68 | + |
| 69 | + // stash |
| 70 | + {"stash push", "git stash push -m 'work in progress'", "git stash push -m <str>"}, |
| 71 | + {"stash pop", "git stash pop", "git stash pop"}, |
| 72 | + {"stash drop index", "git stash drop stash@{2}", "git stash drop stash@{N}"}, |
| 73 | + |
| 74 | + // tag |
| 75 | + {"tag annotated", "git tag -a v1.0.0 -m 'release 1.0'", "git tag -a v1.0.0 -m <str>"}, |
| 76 | + {"tag delete", "git tag -d v1.0.0", "git tag -d v1.0.0"}, |
| 77 | + |
| 78 | + // remote |
| 79 | + {"remote add", "git remote add origin git@github.com:user/repo.git", "git remote add origin <git-uri>"}, |
| 80 | + {"remote remove", "git remote remove upstream", "git remote remove upstream"}, |
| 81 | + |
| 82 | + // config |
| 83 | + {"config set", "git config user.name 'John Doe'", "git config <dotted-id> <val>"}, |
| 84 | + {"config get", "git config --global user.email", "git config --global <dotted-id>"}, |
| 85 | + |
| 86 | + // show |
| 87 | + {"show commit", "git show abc1234", "git show <hash>"}, |
| 88 | + {"show head", "git show HEAD", "git show HEAD"}, |
| 89 | + |
| 90 | + // cherry-pick / revert |
| 91 | + {"cherry-pick", "git cherry-pick abc1234def", "git cherry-pick <hash>"}, |
| 92 | + {"revert", "git revert HEAD~2", "git revert <rev>"}, |
| 93 | + |
| 94 | + // rm |
| 95 | + {"rm file", "git rm src/old.go", "git rm <path>"}, |
| 96 | + {"rm cached", "git rm --cached secrets.env", "git rm --cached <path>"}, |
| 97 | + |
| 98 | + // global flags |
| 99 | + {"global -C flag", "git -C /path/to/repo status", "git -C <path> status"}, |
| 100 | + |
| 101 | + // redirects |
| 102 | + {"log with redirect", "git log --oneline > output.txt", "git log --oneline > <path>"}, |
| 103 | + } |
| 104 | + |
| 105 | + for _, tt := range tests { |
| 106 | + t.Run(tt.name, func(t *testing.T) { |
| 107 | + got := Normalize(tt.input) |
| 108 | + if got != tt.want { |
| 109 | + t.Errorf("Normalize(%q) = %q, want %q", tt.input, got, tt.want) |
| 110 | + } |
| 111 | + }) |
| 112 | + } |
| 113 | + |
| 114 | + // COLLISION TESTS |
| 115 | + t.Run("different commit messages collide", func(t *testing.T) { |
| 116 | + a := Normalize("git commit -m 'fix login bug'") |
| 117 | + b := Normalize("git commit -m 'update readme section'") |
| 118 | + if a != b { |
| 119 | + t.Errorf("expected %q == %q", a, b) |
| 120 | + } |
| 121 | + }) |
| 122 | + |
| 123 | + t.Run("different clone URLs collide", func(t *testing.T) { |
| 124 | + a := Normalize("git clone https://github.com/user/repo1.git") |
| 125 | + b := Normalize("git clone https://github.com/other/repo2.git") |
| 126 | + if a != b { |
| 127 | + t.Errorf("expected %q == %q", a, b) |
| 128 | + } |
| 129 | + }) |
| 130 | + |
| 131 | + t.Run("different file paths in add collide", func(t *testing.T) { |
| 132 | + a := Normalize("git add src/main.go") |
| 133 | + b := Normalize("git add lib/util.ts") |
| 134 | + if a != b { |
| 135 | + t.Errorf("expected %q == %q", a, b) |
| 136 | + } |
| 137 | + }) |
| 138 | + |
| 139 | + // SAFETY TEST |
| 140 | + t.Run("subshell not collapsed", func(t *testing.T) { |
| 141 | + benign := Normalize("git add literal-arg") |
| 142 | + subshell := Normalize("git add $(dangerous-command)") |
| 143 | + if benign == subshell { |
| 144 | + t.Error("subshell must produce different shape than literal") |
| 145 | + } |
| 146 | + }) |
| 147 | +} |
0 commit comments