Skip to content

Commit d47dff4

Browse files
committed
Implemented!
0 parents  commit d47dff4

File tree

8 files changed

+179
-0
lines changed

8 files changed

+179
-0
lines changed

.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Binaries for programs and plugins
2+
*.exe
3+
*.exe~
4+
*.dll
5+
*.so
6+
*.dylib
7+
8+
# Test binary, build with `go test -c`
9+
*.test
10+
11+
# Output of the go coverage tool, specifically when used with LiteIDE
12+
*.out

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 yasukotelin
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# git-diffs
2+
3+
git-diffs is the git subcommand that is diff files selector.
4+
5+
## Description
6+
7+
You can use the `git-diffs` as a git subcommand like `git diffs`.<br>
8+
This shows diff file names and show diff when you select one.
9+
10+
## Install
11+
12+
```
13+
go get -u github.com/yasukotelin/git-diffs
14+
```
15+
16+
## Usage
17+
18+
```
19+
$ 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
30+
```
31+
32+
## Licence
33+
34+
MIT
35+
36+
## Author
37+
38+
yasukotelin

git.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"os/exec"
6+
"strings"
7+
)
8+
9+
func execDiffNameOnly() ([]string, error) {
10+
// git add -N すると実際にはaddしていないがaddしたときと同じような結果が得られるようになる
11+
// つまり、つぎにgit diffしたときに新規作成ファイルもdiff表示されるようになる
12+
err := exec.Command("git", "add", "-N", "-A").Run()
13+
if err != nil {
14+
return nil, err
15+
}
16+
17+
out, err := exec.Command("git", "diff", "--name-only").Output()
18+
if err != nil {
19+
return nil, err
20+
}
21+
22+
files := strings.Split(string(out), "\n")
23+
24+
// 最後の空行は抜き出して返却
25+
return files[0:len(files)-1], nil
26+
}
27+
28+
func execDiff(path string) error {
29+
cmd := exec.Command("git", "diff", path)
30+
cmd.Stdout = os.Stdout
31+
cmd.Stderr = os.Stderr
32+
33+
return cmd.Run()
34+
}

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/yasukotelin/git-diffs
2+
3+
go 1.13
4+
5+
require github.com/urfave/cli v1.22.1

go.sum

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
2+
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
3+
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
6+
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
7+
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
8+
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
9+
github.com/urfave/cli v1.22.1 h1:+mkCCcOFKPnCmVYVcURKps1Xe+3zP90gSYGNfRkjoIY=
10+
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
11+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
12+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

main.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
"strconv"
8+
"errors"
9+
10+
"github.com/urfave/cli"
11+
)
12+
13+
func main() {
14+
app := cli.NewApp()
15+
app.Name = "git-diffs"
16+
app.Version = "0.1.0"
17+
app.Description = "The git subcommand that is diff files selector."
18+
app.Action = mainAction
19+
20+
err := app.Run(os.Args)
21+
if err != nil {
22+
log.Fatal(err)
23+
}
24+
}
25+
26+
func mainAction(c *cli.Context) error {
27+
files, err := execDiffNameOnly()
28+
if err != nil {
29+
return err
30+
}
31+
32+
for i, f := range files {
33+
fmt.Printf("[%d] %s\n", i+1, f)
34+
}
35+
fmt.Println()
36+
fmt.Print("Select number (empty is cancel) => ")
37+
38+
var selNumStr string
39+
fmt.Scanln(&selNumStr)
40+
41+
if selNumStr == "" {
42+
return nil
43+
}
44+
selNum, err := strconv.Atoi(selNumStr)
45+
if err != nil {
46+
return errors.New("your input is not number.")
47+
}
48+
if selNum > len(files) || selNum < 1 {
49+
return errors.New("your input is out of range numbers")
50+
}
51+
execDiff(files[selNum-1])
52+
53+
return nil
54+
}

test/sample.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
AAA
2+
BBB
3+
CCC

0 commit comments

Comments
 (0)