-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommand_list.go
89 lines (74 loc) · 1.75 KB
/
command_list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"fmt"
"sort"
"strings"
ct "github.com/daviddengcn/go-colortext"
"github.com/dustin/go-humanize"
"github.com/urfave/cli"
)
var flagsOfList = []cli.Flag{
cli.BoolFlag{
Name: "short, s",
Usage: "only prints path strings",
},
}
var commandList = cli.Command{
Name: "list",
Action: doList,
Flags: flagsOfList,
}
func doList(c *cli.Context) error {
if c.Args().Present() {
cli.ShowAppHelpAndExit(c, 0)
}
ghqPath := verifyGhqPath()
reposChannel := searchForRepos(ghqPath)
shortExpression := c.Bool("short")
// Sort by time
repos := []Repository{}
for repo := range reposChannel {
repos = append(repos, repo)
}
sort.Sort(RepositoriesByModTime{repos})
// Listing repos
for _, repo := range repos {
uncommitedChanges, ccErr := GitStatus(repo.Path)
unpushedCommits, pcErr := GitLog(repo.Path)
if ccErr != nil && pcErr != nil {
continue
}
if shortExpression {
fmt.Println(repo.Path)
continue
}
printlnWithColor(repo.Path+" ("+humanize.Time(repo.ModTime)+")", ct.Cyan)
// print uncommited changes
if ccErr == nil {
printlnWithColor("uncommitted changes", ct.Magenta)
for _, changes := range uncommitedChanges {
staged := changes[:1]
unstaged := changes[1:2]
filename := changes[3:]
if staged == "?" {
printWithColor(staged, ct.Red)
} else {
printWithColor(staged, ct.Green)
}
printWithColor(unstaged, ct.Red)
fmt.Println("", filename)
}
}
// print unpushed commits
if pcErr == nil {
printlnWithColor("unpushed commits", ct.Magenta)
for _, commit := range unpushedCommits {
line := strings.Split(commit, " ")
printWithColor(line[0], ct.Yellow)
fmt.Println(" " + strings.Join(line[1:], " "))
}
}
fmt.Println()
}
return nil
}