Skip to content

Commit 756d648

Browse files
authored
Merge pull request #1 from yasukotelin/v1.1.0
V1.1.0
2 parents c1d1a93 + 15847be commit 756d648

File tree

5 files changed

+110
-51
lines changed

5 files changed

+110
-51
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: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package git
2+
3+
import (
4+
"os"
5+
"os/exec"
6+
"strings"
7+
)
8+
9+
func Diff(path string, isStaged bool) error {
10+
var cmd *exec.Cmd
11+
if isStaged {
12+
cmd = exec.Command("git", "diff", "--staged", path)
13+
} else {
14+
cmd = exec.Command("git", "diff", path)
15+
}
16+
cmd.Stdout = os.Stdout
17+
cmd.Stderr = os.Stderr
18+
19+
return cmd.Run()
20+
}
21+
22+
type DiffFile struct {
23+
Text string
24+
Status string
25+
Path string
26+
IsStaged bool
27+
}
28+
29+
func DiffNameStatus(isStaged bool) ([]DiffFile, error) {
30+
var cmd *exec.Cmd
31+
if isStaged {
32+
cmd = exec.Command("git", "diff", "--staged", "--name-status")
33+
} else {
34+
exec.Command("git", "add", "-A", "-N").Run()
35+
cmd = exec.Command("git", "diff", "--name-status")
36+
}
37+
out, err := cmd.Output()
38+
if err != nil {
39+
return nil, err
40+
}
41+
42+
rows := strings.Split(string(out), "\n")
43+
// Remove the latest empty row.
44+
rows = rows[0 : len(rows)-1]
45+
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+
}
56+
}
57+
58+
return diffFile, nil
59+
}

images/git-diffs-demo.gif

437 KB
Loading

main.go

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
package main
22

33
import (
4+
"errors"
45
"fmt"
56
"log"
67
"os"
78
"strconv"
8-
"errors"
99

1010
"github.com/urfave/cli"
11+
"github.com/yasukotelin/git-diffs/git"
1112
)
1213

1314
func main() {
1415
app := cli.NewApp()
1516
app.Name = "git-diffs"
16-
app.Version = "1.0.0"
17+
app.Version = "1.1.0"
1718
app.Description = "The git subcommand that is diff files selector."
1819
app.Action = mainAction
1920

@@ -24,14 +25,30 @@ func main() {
2425
}
2526

2627
func mainAction(c *cli.Context) error {
27-
files, err := execDiffNameOnly()
28+
// get staged files.
29+
stagedFiles, err := git.DiffNameStatus(true)
2830
if err != nil {
2931
return err
3032
}
33+
fmt.Println("=== Staged files ===")
34+
stagedFilesLen := len(stagedFiles)
35+
for i, file := range stagedFiles {
36+
fmt.Printf("[%d] %v\n", i+1, file.Text)
37+
}
3138

32-
for i, f := range files {
33-
fmt.Printf("[%d] %s\n", i+1, f)
39+
fmt.Println()
40+
41+
// get unstaged files.
42+
unstagedFiles, err := git.DiffNameStatus(false)
43+
if err != nil {
44+
return err
3445
}
46+
fmt.Println("=== Unstaged files ===")
47+
unstagedFilesLen := len(unstagedFiles)
48+
for i, file := range unstagedFiles {
49+
fmt.Printf("[%d] %v\n", i+1+stagedFilesLen, file.Text)
50+
}
51+
3552
fmt.Println()
3653
fmt.Print("Select number (empty is cancel) => ")
3754

@@ -45,10 +62,20 @@ func mainAction(c *cli.Context) error {
4562
if err != nil {
4663
return errors.New("your input is not number.")
4764
}
48-
if selNum > len(files) || selNum < 1 {
65+
if selNum > stagedFilesLen + unstagedFilesLen || selNum < 1 {
4966
return errors.New("your input is out of range numbers")
5067
}
51-
execDiff(files[selNum-1])
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+
}
5279

5380
return nil
5481
}

0 commit comments

Comments
 (0)