Skip to content

Commit f714763

Browse files
committed
Get the file path from --name-only
1 parent 756d648 commit f714763

File tree

3 files changed

+74
-29
lines changed

3 files changed

+74
-29
lines changed

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ Or you can download the binary from [release](https://github.com/yasukotelin/git
2525
```
2626
$ git diffs
2727
=== Staged files ===
28-
[1] D git.go
29-
[2] M main.go
28+
29+
[1] D git.go
30+
[2] M main.go
3031
3132
=== Unstaged files ===
32-
[3] M README.md
33-
[4] M git/diff.go
34-
[5] A images/git-diffs-demo.gif
33+
34+
[3] M README.md
35+
[4] M git/diff.go
36+
[5] A images/git-diffs-demo.gif
3537
3638
Select number (empty is cancel) =>
3739
```

git/diff.go

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,42 @@ func Diff(path string, isStaged bool) error {
2020
}
2121

2222
type DiffFile struct {
23-
Text string
2423
Status string
2524
Path string
2625
IsStaged bool
2726
}
2827

29-
func DiffNameStatus(isStaged bool) ([]DiffFile, error) {
28+
func DiffFiles(isStaged bool) ([]DiffFile, error) {
29+
nameOnly, err := DiffNameOnly(isStaged)
30+
if err != nil {
31+
return nil, err
32+
}
33+
nameStatus, err := DiffNameStatus(isStaged)
34+
if err != nil {
35+
return nil, err
36+
}
37+
38+
// len(nameStatuses) equal len(nameOnlies)
39+
rowLen := len(nameOnly)
40+
41+
diffFile := make([]DiffFile, rowLen)
42+
for i := 0; i < rowLen; i++ {
43+
path := nameOnly[i]
44+
status := strings.Fields(nameStatus[i])
45+
46+
diffFile[i] = DiffFile{
47+
Status: status[0],
48+
Path: path,
49+
IsStaged: isStaged,
50+
}
51+
}
52+
53+
return diffFile, nil
54+
}
55+
56+
// DiffNameStatus runs `git diff --name-status` git command.
57+
// If isStaged is true, add `--staged`.
58+
func DiffNameStatus(isStaged bool) ([]string, error) {
3059
var cmd *exec.Cmd
3160
if isStaged {
3261
cmd = exec.Command("git", "diff", "--staged", "--name-status")
@@ -41,19 +70,25 @@ func DiffNameStatus(isStaged bool) ([]DiffFile, error) {
4170

4271
rows := strings.Split(string(out), "\n")
4372
// Remove the latest empty row.
44-
rows = rows[0 : len(rows)-1]
73+
return rows[0 : len(rows)-1], nil
74+
}
4575

46-
// Divide the status(M, D etc) and file path
47-
diffFile := make([]DiffFile, len(rows))
48-
for i, row := range rows {
49-
splited := strings.Fields(row)
50-
diffFile[i] = DiffFile{
51-
Text: row,
52-
Status: splited[0],
53-
Path: splited[1],
54-
IsStaged: isStaged,
55-
}
76+
// DiffNameOnly runs `git diff --name-only` git commnad.
77+
// If isStaged is true, add `--staged`.
78+
func DiffNameOnly(isStaged bool) ([]string, error) {
79+
var cmd *exec.Cmd
80+
if isStaged {
81+
cmd = exec.Command("git", "diff", "--staged", "--name-only")
82+
} else {
83+
exec.Command("git", "add", "-A", "-N").Run()
84+
cmd = exec.Command("git", "diff", "--name-only")
85+
}
86+
out, err := cmd.Output()
87+
if err != nil {
88+
return nil, err
5689
}
5790

58-
return diffFile, nil
91+
rows := strings.Split(string(out), "\n")
92+
// Remove the latest empty row.
93+
return rows[0 : len(rows)-1], nil
5994
}

main.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,38 @@ func main() {
2626

2727
func mainAction(c *cli.Context) error {
2828
// get staged files.
29-
stagedFiles, err := git.DiffNameStatus(true)
29+
stagedFiles, err := git.DiffFiles(true)
3030
if err != nil {
3131
return err
3232
}
33+
// get unstaged files.
34+
unstagedFiles, err := git.DiffFiles(false)
35+
if err != nil {
36+
return err
37+
}
38+
3339
fmt.Println("=== Staged files ===")
40+
fmt.Println()
3441
stagedFilesLen := len(stagedFiles)
42+
if stagedFilesLen == 0 {
43+
fmt.Println("No staged files.")
44+
}
3545
for i, file := range stagedFiles {
36-
fmt.Printf("[%d] %v\n", i+1, file.Text)
46+
fmt.Printf(" [%d]\t%v\t%v\n", i+1, file.Status, file.Path)
3747
}
38-
3948
fmt.Println()
4049

41-
// get unstaged files.
42-
unstagedFiles, err := git.DiffNameStatus(false)
43-
if err != nil {
44-
return err
45-
}
4650
fmt.Println("=== Unstaged files ===")
51+
fmt.Println()
4752
unstagedFilesLen := len(unstagedFiles)
53+
if unstagedFilesLen == 0 {
54+
fmt.Println("No unstaged files.")
55+
}
4856
for i, file := range unstagedFiles {
49-
fmt.Printf("[%d] %v\n", i+1+stagedFilesLen, file.Text)
57+
fmt.Printf(" [%d]\t%v\t%v\n", i+1+stagedFilesLen, file.Status, file.Path)
5058
}
51-
5259
fmt.Println()
60+
5361
fmt.Print("Select number (empty is cancel) => ")
5462

5563
var selNumStr string

0 commit comments

Comments
 (0)