forked from smarty/gitreview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
188 lines (141 loc) · 5.58 KB
/
config.go
File metadata and controls
188 lines (141 loc) · 5.58 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
package main
import (
"cmp"
"flag"
"fmt"
"log"
"os"
"path/filepath"
"strings"
)
type Config struct {
GitDefaultBranch string
GitFetch bool
GitRepositoryRoot string
GitGUILauncher string
}
func ReadConfig(version string) *Config {
log.SetFlags(log.Ltime | log.Lshortfile)
config := new(Config)
flags := flag.NewFlagSet(fmt.Sprintf("%s @ %s", filepath.Base(os.Args[0]), version), flag.ExitOnError)
flags.Usage = func() {
_, _ = fmt.Fprintln(os.Stderr, doc(version))
_, _ = fmt.Fprintln(os.Stderr)
_, _ = fmt.Fprintln(os.Stderr, "```")
flags.PrintDefaults()
_, _ = fmt.Fprintln(os.Stderr, "```")
}
flags.StringVar(&config.GitDefaultBranch,
"default-branch", cmp.Or(os.Getenv("GITREVIEWBRANCH"), "main"), ""+
"The default branch to use. Defaults to the value of the\n"+
"GITREVIEWBRANCH environment variable, if declared,\n"+
"otherwise 'main'.\n"+
"-->",
)
flags.StringVar(&config.GitGUILauncher,
"gui", "smerge", ""+
"The external git GUI application to use for visual reviews."+"\n"+
"-->",
)
flags.BoolVar(&config.GitFetch,
"fetch", true, ""+
"When false, suppress all git fetch operations via --dry-run."+"\n"+
"Repositories with updates will still be included in the review."+"\n"+
"-->",
)
err := flags.Parse(os.Args[1:])
if err != nil {
log.Fatal(err)
}
if !config.GitFetch {
log.Println("Running git fetch with --dry-run (updated repositories will not be reviewed).")
gitFetchCommand += " --dry-run"
}
config.GitRepositoryRoot = gitRepositoryRoot(flags.Arg(0))
return config
}
func gitRepositoryRoot(pathFlag string) string {
if pathFlag != "" {
absolute, err := filepath.Abs(pathFlag)
if err != nil {
log.Fatal(err)
}
return absolute
}
home, err := os.UserHomeDir()
if err != nil {
log.Fatal(err)
}
return filepath.Join(home, "src")
}
const rawDoc = `# gitreview @ %s
WARNING: This README file is built from the source code. To modify its contents:
1. Edit the string values in |config.go|
2. Run |make docs| for the changes to show up in this file
|gitreview| facilitates visual inspection (code review) of git
repositories that meet any of the following criteria:
1. New content was fetched
2. Behind origin/<default-branch>
3. Ahead of origin/<default-branch>
4. Messy (have uncommitted state)
5. Throw errors for the required git operations (listed below)
We use variants of the following commands to ascertain the
status of each repository:
- |git remote| (shows remote address)
- |git status| (shows uncommitted files)
- |git fetch| (finds new commits/tags/branches)
- |git rev-list| (lists commits behind/ahead-of <default-branch>)
- |git config --get ...| (show config parameters of a repo)
...all of which should be safe enough.
Each repository that meets any criteria above will be
presented for review.
Repositories are gathered recursively from the path described by the first non-flag command-line argument or $HOME/src.
## Prerequisites:
1. [Git](https://git-scm.com/)
- The |gitreview| tool will invoke the aforementioned git commands.
1. A Git GUI (recommended: [Sublime Merge](https://www.sublimemerge.com/) or [SourceTree](https://www.sourcetreeapp.com/))
- The |gitreview| tool invokes your git GUI according to the 'gui' flag (details below).
- The git GUI used must support invocation from the command line in the form "<git-gui-command> /path/to/repository"
1. A go compiler (v1.16 or higher)
- download [installer](https://golang.org/dl/)
- or use brew: "brew install golang"
## Compilation/Installation:
git clone [email protected]:mdw-tools/gitreview
cd gitreview
make install
The above |make install| runs |go install| which installs executables
"in the directory named by the |GOBIN| environment variable, which defaults
to |$GOPATH/bin| or |$HOME/go/bin| if the |GOPATH| environment variable is not set."
See the Makefile for other helpful targets/operations.
## Usage Description
After installation, the |gitreview| executable, when invoked, will commence
running |git fetch| for all repositories it finds based on what is provided
to the |-roots| flag. It uses a fan-out strategy to fetch multiple repositories
concurrently.
As repositories are scanned a brief summary of the status of each is emitted.
After scanning all repositories a report of which repositories require a review.
The |gitreview| program will then halt, waiting for the user to press |<ENTER>|.
Upon receiving |<ENTER>|, |gitreview| will invoke the git GUI program specified
by the |-gui| flag for each of the repositories previously listed. At this point
the |gitreview| tool's execution is concluded.
All that remains is for the user to review each opened repository in the git GUI.
It is the user's responsibility to review each new tag, branch, and commit as well
as to run |git pull| as desired to fully synchronize the local repository with the
remote.
## Pre-repository Settings
Skipping Repositories:
If you have repositories in your list that you would rather not review,
you can mark them to be skipped by adding a config variable to the
repository. The following command will produce this result:
git config --add review.skip true
Specifying the 'default' branch:
This tool assumes that the default branch of all repositories is 'main'.
If a repository uses a non-standard default branch (ie. 'master', 'trunk')
and you want this tool to focus reviews on commits pushed to that branch
instead, run the following command:
git config --add review.branch <branch-name>
CLI Flags:
`
func doc(version string) string {
return strings.ReplaceAll(fmt.Sprintf(rawDoc, version), "|", "`")
}