Skip to content

Commit 15847be

Browse files
committed
Add function that can select from staged and unstaged files
1 parent 14bd163 commit 15847be

File tree

5 files changed

+35
-59
lines changed

5 files changed

+35
-59
lines changed

README.md

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,33 @@ git-diffs is the git subcommand that is diff files selector.
77
You can use the `git-diffs` as a git subcommand like `git diffs`.<br>
88
This shows diff file names and show diff when you select one.
99

10+
<img src="images/git-diffs-demo.gif" alt="git-diffs demo">
11+
1012
## Install
1113

14+
If you have go environment, you can use `go get` and install.
15+
1216
```
1317
go get -u github.com/yasukotelin/git-diffs
1418
```
1519

20+
Or you can download the binary from [release](https://github.com/yasukotelin/git-diffs/releases) page. (only windows)
21+
22+
1623
## Usage
1724

1825
```
1926
$ git diffs
20-
[1] .gitignore
21-
[2] LICENSE
22-
[3] README.md
23-
[4] git.go
24-
[5] go.mod
25-
[6] go.sum
26-
[7] main.go
27-
[8] test/sample.txt
28-
29-
Select number (empty is cancel) => 5
27+
=== Staged files ===
28+
[1] D git.go
29+
[2] M main.go
30+
31+
=== Unstaged files ===
32+
[3] M README.md
33+
[4] M git/diff.go
34+
[5] A images/git-diffs-demo.gif
35+
36+
Select number (empty is cancel) =>
3037
```
3138

3239
## Licence

git.go

Lines changed: 0 additions & 34 deletions
This file was deleted.

git/diff.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func DiffNameStatus(isStaged bool) ([]DiffFile, error) {
3131
if isStaged {
3232
cmd = exec.Command("git", "diff", "--staged", "--name-status")
3333
} else {
34+
exec.Command("git", "add", "-A", "-N").Run()
3435
cmd = exec.Command("git", "diff", "--name-status")
3536
}
3637
out, err := cmd.Output()

images/git-diffs-demo.gif

437 KB
Loading

main.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func mainAction(c *cli.Context) error {
3131
return err
3232
}
3333
fmt.Println("=== Staged files ===")
34+
stagedFilesLen := len(stagedFiles)
3435
for i, file := range stagedFiles {
3536
fmt.Printf("[%d] %v\n", i+1, file.Text)
3637
}
@@ -43,19 +44,11 @@ func mainAction(c *cli.Context) error {
4344
return err
4445
}
4546
fmt.Println("=== Unstaged files ===")
47+
unstagedFilesLen := len(unstagedFiles)
4648
for i, file := range unstagedFiles {
47-
fmt.Printf("[%d] %v\n", i+1, file.Text)
49+
fmt.Printf("[%d] %v\n", i+1+stagedFilesLen, file.Text)
4850
}
4951

50-
// files, err := execDiffNameOnly()
51-
// if err != nil {
52-
// return err
53-
// }
54-
55-
// for i, f := range files {
56-
// fmt.Printf("[%d] %s\n", i+1, f)
57-
// }
58-
5952
fmt.Println()
6053
fmt.Print("Select number (empty is cancel) => ")
6154

@@ -69,11 +62,20 @@ func mainAction(c *cli.Context) error {
6962
if err != nil {
7063
return errors.New("your input is not number.")
7164
}
72-
fmt.Println(selNum)
73-
// if selNum > len(files) || selNum < 1 {
74-
// return errors.New("your input is out of range numbers")
75-
// }
76-
// execDiff(files[selNum-1])
65+
if selNum > stagedFilesLen + unstagedFilesLen || selNum < 1 {
66+
return errors.New("your input is out of range numbers")
67+
}
68+
69+
if selNum <= stagedFilesLen {
70+
// User selected staged file number
71+
err = git.Diff(stagedFiles[selNum-1].Path, true)
72+
} else {
73+
// User selected unstaged file number
74+
err = git.Diff(unstagedFiles[selNum-stagedFilesLen-1].Path, false)
75+
}
76+
if err != nil {
77+
return err
78+
}
7779

7880
return nil
7981
}

0 commit comments

Comments
 (0)