Skip to content

Commit 79bff32

Browse files
authored
Git command (#3)
* add glog command for extend git log * 测试根据目录下所有git仓库列表 * 增加多仓库解析 * feature:新增控制台&文件表格形式输出 * fix:windows下陆路径混用&输出多引号特殊字符 * 以表格形式输出提交记录更换为go-pretty库 * 优化build后的二进制文件大小 * 完善命令提示&交互提示 * 更换分隔符&增加glog文档 * 修改文档格式、排版 --------- Co-authored-by: yesand<wangyingjie>
1 parent d5259c6 commit 79bff32

File tree

11 files changed

+547
-21
lines changed

11 files changed

+547
-21
lines changed

Makefile

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
BINARY_NAME=lwe
22

33
build:
4-
GOARCH=amd64 GOOS=darwin go build -o ${BINARY_NAME}_mac main.go
5-
GOARCH=amd64 GOOS=linux go build -o ${BINARY_NAME}_linux main.go
6-
GOARCH=amd64 GOOS=windows go build -o ${BINARY_NAME}_win64.exe main.go
4+
GOARCH=amd64 GOOS=darwin go build -ldflags "-s -w" -o ${BINARY_NAME}_mac main.go
5+
GOARCH=amd64 GOOS=linux go build -ldflags "-s -w" -o ${BINARY_NAME}_linux main.go
6+
GOARCH=amd64 GOOS=windows go build -ldflags "-s -w" -o ${BINARY_NAME}_win64.exe main.go
77
run:
88
./${BINARY_NAME}
99

@@ -13,19 +13,19 @@ release:
1313
rm -rf ./*.gz
1414

1515
# Build for mac
16-
GOARCH=amd64 GOOS=darwin go build -o ${BINARY_NAME}_mac main.go
16+
GOARCH=amd64 GOOS=darwin go build -ldflags "-s -w" -o ${BINARY_NAME}_mac main.go
1717
tar czvf ${BINARY_NAME}_mac_${VERSION}.tar.gz ./${BINARY_NAME}_mac
1818
rm -rf ./${BINARY_NAME}_mac
1919

2020
# Build for linux
2121
go clean
22-
GOARCH=amd64 GOOS=linux go build -o ${BINARY_NAME}_linux main.go
22+
GOARCH=amd64 GOOS=linux go build -ldflags "-s -w" -o ${BINARY_NAME}_linux main.go
2323
tar czvf ${BINARY_NAME}_linux_${VERSION}.tar.gz ./${BINARY_NAME}_linux
2424
rm -rf ./${BINARY_NAME}_linux
2525

2626
# Build for win
2727
go clean
28-
GOARCH=amd64 GOOS=windows go build -o ${BINARY_NAME}_win64.exe main.go
28+
GOARCH=amd64 GOOS=windows go build -ldflags "-s -w" -o ${BINARY_NAME}_win64.exe main.go
2929
tar czvf ${BINARY_NAME}_win64_${VERSION}.tar.gz ./${BINARY_NAME}_win64.exe
3030
rm -rf ./${BINARY_NAME}_win64.exe
3131
go clean

README.md

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ lwe是leave work early的缩写,也就是"早点下班"!🤣🤣🤣
1010

1111
[3.获取Navicat连接配置中的密码](#3)
1212

13-
[4.获取给定值的md5值](#4)
13+
[4.增强Git日志功能:查看给定目录下所有git仓库提交日志](#4)
1414

1515
## 安装
1616

@@ -55,6 +55,7 @@ Available Commands:
5555
completion Generate the autocompletion script for the specified shell
5656
es Translate SQL to elasticsearch's DSL
5757
fmt Generate the specified file based on SQL
58+
glog Get all git repository commit log under the given dir
5859
help Help about any command
5960
md5 Get a md5 for the given value or a random md5 value
6061
ncx Decrypt password of connection in .ncx file
@@ -237,25 +238,54 @@ Connection username: root
237238
Connection password: This is a test
238239
```
239240

240-
<h3 id="4">4、获取给定值的md5值</h3>
241-
这个命令非常的简单,返回给定值的md5值,如果未给定值则随机返回一个md5值
242-
241+
<h3 id="4">4、增强Git日志功能:查看给定目录下所有git仓库提交日志</h3>
242+
开发人员可能同时维护多个项目或者一个项目中多个模块在不同git仓库,如果有跨仓库查看多个仓库提交日志的需求,glog子命令就派上用场了。
243243
```bash
244-
lwe md5 [给定的串]
244+
lwe glog <仓库所在目录> [可选参数]
245245
```
246-
如:
246+
> 查询结果对应的是每个git仓库当前使用分支的提交记录
247+
248+
> 如果未指定目录,则在当前目录下搜寻git仓库,另,如果目录层级过深,可能会影响性能
249+
250+
如:写周报时,需要查看自己近一周在哪些仓库提交了哪些代码,来辅助我写总结,假定我的工作目录在/Users/yesand/work/
251+
247252
```bash
248-
lwe md5 yesAnd
253+
lwe glog /Users/yesand/work/ -a=yesand -f=false -n=20 -s=2023-05-15 -e=2023-05-19
254+
```
255+
其中:
256+
257+
`-a, --author string`,可选参数,该参数用于指定提交者,未指定查询所有提交者。\
258+
`-f, --file bool`,可选参数,该参数决定将查询结果写到文件中,默认在控制台输出。\
259+
`-n, --recentN int16`,可选参数,该参数指定每个仓库查询最近N条的提交记录。\
260+
`-s, --start string`,可选参数,该参数指定筛选提交记录的开始日期,格式:'yyyy-MM-dd'。\
261+
`-e, --end string`,可选参数,该参数指定筛选提交记录的结束日期,格式:'yyyy-MM-dd'。\
262+
263+
结果:示例
264+
265+
```text
266+
#1 Git Repo >> /Users/yesand/work/lwe
267+
+---------+--------+-----------------------------------------+---------------------+
268+
| HASH | AUTHOR | COMMIT | TIME |
269+
+---------+--------+-----------------------------------------+---------------------+
270+
| bf67fcd | yesand | 完善命令提示&交互提示 | 2023-05-19 17:21:34 |
271+
| 3739c60 | yesand | 优化build后的二进制文件大小 | 2023-05-19 09:44:14|
272+
| 7a2ca47 | yesand | 以表格形式输出提交记录更换为go-pretty库 | 2023-05-19 09:21:26 |
273+
+---------+--------+-----------------------------------------+---------------------+
274+
275+
#2 Git Repo >> /Users/yesand/work/xxx
276+
...
249277
```
250278

251279

280+
252281
## 说明
253282
1.使用[spf13/cobra](github.com/spf13/cobra)库来方便的构建命令行工具
254283

255284
2.es子命令实现借助了[sqlparser](github.com/xwb1989/sqlparser)库来解析SQL语句,一个库很优秀的解析SQL
256285

257286
3.sql转换成dsl,曹大的[elasticsql](https://github.com/cch123/elasticsql)项目已经是一个很成熟好用的轮子了,lwe也大量借鉴了它的实现思路;没直接调用这个库的原因是想自己练手,同时后续增减功能也更加灵活
258287

288+
4.glog结果输出时使用了[go-pretty](https://github.com/jedib0t/go-pretty)库来表格化提交信息
259289
## RoadMap
260290
- fmt 根据需求支持更多类型的转换
261291
- es 按需增加对insert、update、delete

cmd/git.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package cmd
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
"lwe/handler/gitcmd"
6+
)
7+
8+
/**
9+
* git命令相关功能
10+
*/
11+
var (
12+
detail = false //是否展示每次提交变动的文件 !!!没找到合适的展示方式,且性能稳定性不可控,暂时先不开放
13+
file bool //日志结果控制台输出,或者生成文件,默认控制台输出
14+
committer string //指定查询提交者,不指定查所有
15+
recentN int16 //指定查询仓库
16+
start string //开始时间
17+
end string //结束时间
18+
19+
gitCmd = &cobra.Command{
20+
Use: "glog",
21+
Short: "Get all git repository commit log under the given dir ",
22+
Long: `Get all git repository commit log under the given dir ,and specify author,date etc. supported!`,
23+
Args: cobra.MatchAll(),
24+
Run: func(cmd *cobra.Command, args []string) {
25+
26+
var dir = "."
27+
if len(args) > 0 {
28+
dir = args[0]
29+
}
30+
31+
if recentN > int16(200) {
32+
cobra.CheckErr("recentN can't exceed 200")
33+
}
34+
35+
commitLogs, err := gitcmd.GetAllGitRepoCommitLog(detail, recentN, dir, committer, start, end)
36+
if err != nil {
37+
cobra.CheckErr(err)
38+
}
39+
40+
var output gitcmd.OutputFormatter = &gitcmd.ConsoleOutput{}
41+
if file {
42+
output = &gitcmd.FileOutput{}
43+
}
44+
output.Output(commitLogs)
45+
46+
},
47+
}
48+
)
49+
50+
func init() {
51+
52+
//gitCmd.PersistentFlags().BoolVarP(&detail, "detail", "d", false, "")
53+
gitCmd.PersistentFlags().BoolVarP(&file, "file", "f", false, "result output to file,default value is false (meaning output to console). ")
54+
gitCmd.PersistentFlags().StringVarP(&committer, "author", "a", "", "specify name of committer ")
55+
gitCmd.PersistentFlags().StringVarP(&start, "start", "s", "", "specify the start of commit date. eg.'yyyy-MM-dd'")
56+
gitCmd.PersistentFlags().StringVarP(&end, "end", "e", "", "specify the end of commit date. eg.'yyyy-MM-dd'")
57+
gitCmd.PersistentFlags().Int16VarP(&recentN, "recentN", "n", 10, "specify the number of commit log for each git repo.")
58+
}

cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ func init() {
2424
rootCmd.AddCommand(md5Cmd)
2525
rootCmd.AddCommand(esCmd)
2626
rootCmd.AddCommand(navicatCmd)
27+
rootCmd.AddCommand(gitCmd)
2728
}

cmd/version.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,6 @@ var versionCmd = &cobra.Command{
1111
Short: "Print the version number of lwe",
1212
Long: `All software has versions. This is lwe's`,
1313
Run: func(cmd *cobra.Command, args []string) {
14-
fmt.Println("0.1beta")
14+
fmt.Println("v.0.0.3.beta")
1515
},
1616
}
17-
18-
func init() {
19-
20-
//versionCmd.PersistentFlags().StringP("abc", "c", "YOUR NAME", "author name for copyright attribution")
21-
//versionCmd.PersistentFlags().Bool("viper", true, "use Viper for configuration")
22-
//rootCmd.AddCommand(versionCmd)
23-
}

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,9 @@ require (
99

1010
require (
1111
github.com/inconshreveable/mousetrap v1.1.0 // indirect
12+
github.com/jedib0t/go-pretty/v6 v6.4.6 // indirect
13+
github.com/mattn/go-runewidth v0.0.13 // indirect
14+
github.com/rivo/uniseg v0.2.0 // indirect
1215
github.com/spf13/pflag v1.0.5 // indirect
16+
golang.org/x/sys v0.1.0 // indirect
1317
)

go.sum

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,34 @@
11
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
24
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
35
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
46
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
7+
github.com/jedib0t/go-pretty/v6 v6.4.6 h1:v6aG9h6Uby3IusSSEjHaZNXpHFhzqMmjXcPq1Rjl9Jw=
8+
github.com/jedib0t/go-pretty/v6 v6.4.6/go.mod h1:Ndk3ase2CkQbXLLNf5QDHoYb6J9WtVfmHZu9n8rk2xs=
9+
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
10+
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
11+
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
12+
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
13+
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
14+
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
15+
github.com/pkg/profile v1.6.0/go.mod h1:qBsxPvzyUincmltOk6iyRVxHYg4adc0OFOv72ZdLa18=
16+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
17+
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
18+
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
519
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
620
github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA=
721
github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY=
822
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
923
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
24+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
25+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
26+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
27+
github.com/stretchr/testify v1.7.4/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
1028
github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2 h1:zzrxE1FKn5ryBNl9eKOeqQ58Y/Qpo3Q9QNxKHX5uzzQ=
1129
github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2/go.mod h1:hzfGeIUDq/j97IG+FhNqkowIyEcD88LrW6fyU3K3WqY=
30+
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
31+
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1232
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
33+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
1334
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)