Skip to content

Commit 74b9fe7

Browse files
committed
lint-staged support --status
1 parent 51b7410 commit 74b9fe7

5 files changed

Lines changed: 49 additions & 7 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ You can also target other file states:
7676
```shell
7777
kitty @lint-staged --status unstaged
7878
kitty @lint-staged --status tracked
79+
kitty @lint-staged --status changed
7980
kitty @lint-staged --status all
8081
```
8182

internal/ext/lint-staged/git.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,11 @@ func resolveGitRepo(cwd string) (gitDir, gitConfigDir string, err error) {
8383
func getDiffCommand(diff, diffFilter string) []string {
8484
diffFilter = normalizeDiffFilter(diffFilter)
8585

86-
// Use `--diff branch1...branch2` or `--diff="branch1 branch2"`.
8786
diffArgs := strings.Fields(strings.TrimSpace(diff))
87+
if len(diffArgs) == 0 {
88+
// Fall back to staged files when no explicit diff range is provided.
89+
diffArgs = []string{"--staged"}
90+
}
8891

8992
// Docs for -z option:
9093
// https://git-scm.com/docs/git-diff#Documentation/git-diff.txt--z
@@ -126,8 +129,10 @@ func getSelectedFiles(options *Options, gitDir string) ([]string, error) {
126129
return getUntrackedFiles(gitDir)
127130
case SelectionModeTracked:
128131
return getTrackedFiles(options.DiffFilter, gitDir)
132+
case SelectionModeChanged:
133+
return getChangedFiles(options.DiffFilter, gitDir)
129134
case SelectionModeAll:
130-
tracked, err := getTrackedFiles(options.DiffFilter, gitDir)
135+
tracked, err := getCachedFiles(gitDir)
131136
if err != nil {
132137
return nil, err
133138
}
@@ -196,6 +201,19 @@ func getTrackedFiles(diffFilter string, gitDir string) ([]string, error) {
196201
return uniqueStrings(append(staged, unstaged...)), nil
197202
}
198203

204+
func getChangedFiles(diffFilter string, gitDir string) ([]string, error) {
205+
tracked, err := getTrackedFiles(diffFilter, gitDir)
206+
if err != nil {
207+
return nil, err
208+
}
209+
untracked, err := getUntrackedFiles(gitDir)
210+
if err != nil {
211+
return nil, err
212+
}
213+
214+
return uniqueStrings(append(tracked, untracked...)), nil
215+
}
216+
199217
func getUntrackedFiles(gitDir string) ([]string, error) {
200218
return execGitZ([]string{"ls-files", "-z", "--full-name", "--others", "--exclude-standard"}, gitDir)
201219
}

internal/ext/lint-staged/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func Commands() []*cobra.Command {
3232
flags.StringVarP(&o.ConfigPath, "config", "c", "", "path to configuration file")
3333
flags.StringVar(&o.Diff, "diff", "", `override the default "--staged" flag of "git diff" to get list of files. Implies "--stash=false"`)
3434
flags.StringVar(&o.DiffFilter, "diff-filter", "", `override the default "--diff-filter=ACMR" flag of "git diff" to get list of files`)
35-
flags.StringVar(&o.Status, "status", string(SelectionModeStaged), "select files by git status: staged, unstaged, untracked, tracked, or all")
35+
flags.StringVar(&o.Status, "status", string(SelectionModeStaged), "select files by git status: staged, unstaged, untracked, tracked, changed, or all")
3636
flags.BoolVar(&o.Stash, "stash", true, "enable the backup stash, and revert in case of errors")
3737
flags.StringVarP(&o.Shell, "shell", "x", "", "use a custom shell to execute tasks with; defaults to the shell specified in the environment variable $SHELL, or /bin/sh if not set")
3838
flags.BoolVarP(&o.Verbose, "verbose", "v", false, "show task output even when tasks succeed; by default only failed output is shown")

internal/ext/lint-staged/selection.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const (
99
SelectionModeUnstaged SelectionMode = "unstaged"
1010
SelectionModeUntracked SelectionMode = "untracked"
1111
SelectionModeTracked SelectionMode = "tracked"
12+
SelectionModeChanged SelectionMode = "changed"
1213
SelectionModeAll SelectionMode = "all"
1314
)
1415

@@ -26,10 +27,10 @@ func (o *Options) UsesIndex() bool {
2627

2728
func (o *Options) ValidateSelectionMode() error {
2829
switch o.SelectionMode() {
29-
case SelectionModeStaged, SelectionModeUnstaged, SelectionModeUntracked, SelectionModeTracked, SelectionModeAll:
30+
case SelectionModeStaged, SelectionModeUnstaged, SelectionModeUntracked, SelectionModeTracked, SelectionModeChanged, SelectionModeAll:
3031
// ok
3132
default:
32-
return fmt.Errorf("invalid --status %q (must be one of: staged, unstaged, untracked, tracked, all)", o.Status)
33+
return fmt.Errorf("invalid --status %q (must be one of: staged, unstaged, untracked, tracked, changed, all)", o.Status)
3334
}
3435

3536
if o.Diff != "" && o.SelectionMode() != SelectionModeStaged {
@@ -62,8 +63,10 @@ func (o *Options) SelectedFilesLabel() string {
6263
return "untracked files"
6364
case SelectionModeTracked:
6465
return "tracked changed files"
65-
case SelectionModeAll:
66+
case SelectionModeChanged:
6667
return "changed files"
68+
case SelectionModeAll:
69+
return "all files"
6770
default:
6871
return "staged files"
6972
}

internal/ext/lint-staged/selection_test.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,26 @@ func TestOptionsSelectionMode(t *testing.T) {
2727
assert.Equal(t, "tracked changed files", options.SelectedFilesLabel())
2828
})
2929

30+
t.Run("changed status describes diff set", func(t *testing.T) {
31+
options := &Options{Status: string(SelectionModeChanged)}
32+
33+
require.NoError(t, options.ValidateSelectionMode())
34+
assert.Equal(t, SelectionModeChanged, options.SelectionMode())
35+
assert.False(t, options.UsesIndex())
36+
assert.Equal(t, "`--status=changed` was used", options.SelectionReason())
37+
assert.Equal(t, "changed files", options.SelectedFilesLabel())
38+
})
39+
40+
t.Run("all status describes full repo set", func(t *testing.T) {
41+
options := &Options{Status: string(SelectionModeAll)}
42+
43+
require.NoError(t, options.ValidateSelectionMode())
44+
assert.Equal(t, SelectionModeAll, options.SelectionMode())
45+
assert.False(t, options.UsesIndex())
46+
assert.Equal(t, "`--status=all` was used", options.SelectionReason())
47+
assert.Equal(t, "all files", options.SelectedFilesLabel())
48+
})
49+
3050
t.Run("diff uses working tree only", func(t *testing.T) {
3151
options := &Options{Diff: "HEAD"}
3252

@@ -45,7 +65,7 @@ func TestOptionsSelectionMode(t *testing.T) {
4565
t.Run("diff cannot combine with non default status", func(t *testing.T) {
4666
options := &Options{
4767
Diff: "HEAD",
48-
Status: string(SelectionModeAll),
68+
Status: string(SelectionModeChanged),
4969
}
5070

5171
require.Error(t, options.ValidateSelectionMode())

0 commit comments

Comments
 (0)