-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
221 lines (192 loc) · 4.41 KB
/
main.go
File metadata and controls
221 lines (192 loc) · 4.41 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"sort"
"strings"
"text/tabwriter"
"time"
"github.com/BurntSushi/toml"
"github.com/Sirupsen/logrus"
"github.com/libgit2/git2go"
)
const (
grey = "\x1b[1;30m"
red = "\x1b[1;31m"
green = "\x1b[1;32m"
reset = "\x1b[0m"
)
var (
gitPathEnv = os.Getenv("GIT")
gitReposPath = flag.String("g", gitPathEnv, "Path to git repositories")
branch = flag.String("b", "current", "Only repositories in specified branch")
configPath = path.Join(os.Getenv("HOME"), ".gitwork")
configFile = "config"
activeWork = flag.Int("d", 90, "Set days to mark abandoned a git repository")
)
type gitinfo struct {
Name string
Date time.Time
Branch string
}
type gitInfoSorter struct {
repos []gitinfo
}
type Config struct {
Global Global
}
type Global struct {
//TODO: Provide branch option
DaysAgo int `toml:"daysago"`
}
func (r gitInfoSorter) Len() int {
return len(r.repos)
}
func (r gitInfoSorter) Swap(i, j int) {
r.repos[i], r.repos[j] = r.repos[j], r.repos[i]
}
func (r gitInfoSorter) Less(i, j int) bool {
// Sort from recent to older change
return r.repos[j].Date.Before(r.repos[i].Date)
}
// get the repositories list
func getRepositories(gitpath string) ([]string, error) {
repos := make([]string, 0)
if _, err := os.Stat(gitpath); os.IsNotExist(err) {
return nil, err
}
// path is absolute
absPath, err := filepath.Abs(gitpath)
if err != nil {
return nil, err
}
// read the files on the git path
files, err := ioutil.ReadDir(gitpath)
if err != nil {
return nil, err
}
for _, f := range files {
if !f.IsDir() {
continue
}
absGitPath := path.Join(absPath, f.Name())
repos = append(repos, absGitPath)
}
return repos, nil
}
func gitRepository(repo string) (*gitinfo, error) {
r, err := git.OpenRepository(repo)
if err != nil {
return nil, err
}
head, err := r.Head()
if err != nil {
return nil, err
}
// Last commit form HEAD, do not assume
// HEAD points to master.
lastCommit, err := r.LookupCommit(head.Target())
if err != nil {
return nil, err
}
author := lastCommit.Author()
branch, err := head.Branch().Name()
if err != nil {
return nil, err
}
gitInfo := &gitinfo{
Date: author.When,
Branch: branch,
}
return gitInfo, nil
}
func listGitInfo(repos gitInfoSorter) {
var message string
w := tabwriter.NewWriter(os.Stdout, 25, 8, 0, ' ', 0)
fmt.Fprintln(w, "GIT\tBRANCH\tLAST CHANGE")
for _, r := range repos.repos {
if *branch != "current" && *branch != r.Branch {
continue
}
date := time.Now().Sub(r.Date)
daysAgo := int(date.Hours()) / 24
message = fmt.Sprintf("%d %sdays ago%s %s(abandoned)%s", daysAgo, grey, reset, red, reset)
if daysAgo < *activeWork {
message = fmt.Sprintf("%d %sdays ago%s", daysAgo, green, reset)
}
fmt.Fprintf(w, "%s\t%s\t%s\n", r.Name, r.Branch, message)
}
w.Flush()
}
func createConfigFile() error {
var config Config
config.Global.DaysAgo = *activeWork
file := path.Join(configPath, configFile)
var f *os.File
// create config path
if _, err := os.Stat(configPath); os.IsNotExist(err) {
err := os.Mkdir(configPath, 0755)
if err != nil {
return err
}
}
// create config file
if _, err := os.Stat(file); os.IsNotExist(err) {
f, err = os.Create(file)
if err != nil {
return err
}
}
toml.NewEncoder(f).Encode(&config)
return nil
}
func readConfigFile() (*Config, error) {
var config *Config
file := path.Join(configPath, configFile)
_, err := toml.DecodeFile(file, &config)
if err != nil {
return nil, err
}
return config, nil
}
func main() {
flag.Parse()
// check if the config file exits or create an empty one
err := createConfigFile()
if err != nil {
logrus.Fatal(err)
}
config, err := readConfigFile()
if err != nil {
logrus.Fatal(err)
}
if config.Global.DaysAgo != 0 && *activeWork == 90 {
*activeWork = config.Global.DaysAgo
}
gitReposInfo := make([]gitinfo, 0)
if *gitReposPath == "" {
fmt.Fprintf(os.Stderr, "usage: gitwork [OPTIONS]\n")
flag.PrintDefaults()
os.Exit(1)
}
repos, err := getRepositories(*gitReposPath)
if err != nil {
logrus.Fatal(err)
}
for _, r := range repos {
info, err := gitRepository(r)
if err != nil {
continue
}
indexLastSlash := strings.LastIndex(r, "/") + 1
info.Name = r[indexLastSlash:]
gitReposInfo = append(gitReposInfo, *info)
}
reposSorted := gitInfoSorter{repos: gitReposInfo}
sort.Sort(reposSorted)
listGitInfo(reposSorted)
}