1
1
package git
2
2
3
3
import (
4
- "bufio"
5
4
"context"
6
5
"encoding/json"
7
6
"fmt"
8
7
"git.medlinker.com/wanghouwei/autoAddCommit/file"
9
8
"git.medlinker.com/wanghouwei/autoAddCommit/util"
10
- "io"
11
9
"math/rand"
12
- "os"
13
10
"strconv"
11
+ "strings"
14
12
"time"
15
13
)
16
14
@@ -20,20 +18,27 @@ var (
20
18
startTime time.Time
21
19
maxTimes = 100
22
20
baseDir = ""
23
- commitTimes = 50
21
+ // commit 的数量
22
+ commitTimes = 70
23
+ // 前面100 天 去分 commit
24
+ seconds = 100 * 24 * 60 * 60
24
25
)
25
26
26
27
type commit struct {
27
28
Date string `json:"date"`
28
29
Hash string `json:"hash"`
29
- Msg string `json:"msg"`
30
+ Msg string `json:"msg"`
30
31
}
31
32
32
- func changeDate () {
33
- id , _ := getCommitID ()
33
+ func changeDate (hash , t string ) {
34
+ if hash == "" {
35
+ hash , _ = getCommitID ()
36
+ }
37
+ if t == "" {
38
+ t = startTime .Format ("2006-01-02 15:04:05" )
39
+ }
34
40
cmd := `if [ $GIT_COMMIT = %s ]; then export GIT_AUTHOR_DATE="%s" export GIT_COMMITTER_DATE="%s"; fi`
35
- t := startTime .Format ("2006-01-02 15:04:05" )
36
- cmdString := fmt .Sprintf (cmd , id , t , t )
41
+ cmdString := fmt .Sprintf (cmd , hash , t , t )
37
42
ret , err := util .RunCmdRet ("git" , "filter-branch" , "-f" , "--env-filter" , cmdString )
38
43
if err != nil {
39
44
util .Errorf ("filter-branch err (%+v)" , err )
@@ -45,17 +50,21 @@ func changeDate() {
45
50
func Run (dir string ) {
46
51
now := time .Now ()
47
52
// 前推4个月
48
- startTime = now .Add (time .Second * - 1 * 60 * 60 * 24 * 30 * 4 )
53
+ startTime = now .Add (time .Second * time . Duration ( - 1 * seconds ) )
49
54
//changeDate()
50
55
//return
51
56
//dir = "/Users/med/mine/goPkgLearn"
52
57
// 最后50次
53
58
log := getCommitLog (commitTimes )
54
- if len (log ) = = commitTimes {
59
+ if len (log ) > = commitTimes {
55
60
util .Infof ("commit log 已经够分配" )
56
- deviceCommit (log )
61
+ //deviceCommitTime(log)
62
+ commitTime := deviceCommitTime (log )
63
+ pushCommitTime (log , commitTime )
64
+ push (0 )
57
65
return
58
66
}
67
+ util .Infof ("commit log 共 (%d) 不够分配,自动生成提交记录中..." , len (log ))
59
68
baseDir = dir
60
69
if err := getBranch (); err != nil {
61
70
util .Errorf ("getBranch err (%+v)" , err )
@@ -72,10 +81,11 @@ func Run(dir string) {
72
81
// continue
73
82
//}
74
83
getTime ()
75
- if err := gitPush ("./" , branch , f ); err != nil {
84
+ if err := addCommit ("./" , branch , f ); err != nil {
76
85
return
77
86
}
78
87
}
88
+ push (0 )
79
89
}
80
90
81
91
func addFile () (f file.File , err error ) {
@@ -92,7 +102,7 @@ func addFile() (f file.File, err error) {
92
102
}
93
103
94
104
// 提交修改内容到git
95
- func gitPush (medSdkDir , branch string , f file.File ) (err error ) {
105
+ func addCommit (medSdkDir , branch string , f file.File ) (err error ) {
96
106
util .Infof ("正在提交代码..." )
97
107
err = util .RunCmd ("git" , "add" , "-A" )
98
108
if err != nil {
@@ -106,7 +116,7 @@ func gitPush(medSdkDir, branch string, f file.File) (err error) {
106
116
date := startTime .Unix ()
107
117
dateString := fmt .Sprintf ("--date=%d" , date )
108
118
err = util .RunCmd ("git" , "commit" , msgString , dateString )
109
- changeDate ()
119
+ changeDate ("" , "" )
110
120
if err != nil {
111
121
util .Errorf ("git commit err (%+v)" , err )
112
122
return
@@ -115,7 +125,6 @@ func gitPush(medSdkDir, branch string, f file.File) (err error) {
115
125
util .Infof ("无改动文件" )
116
126
return
117
127
}
118
- push (0 )
119
128
return
120
129
}
121
130
@@ -125,8 +134,8 @@ func push(restart int64) {
125
134
return
126
135
}
127
136
restart ++
128
- // 10s 内没push 成功就重试
129
- ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
137
+ // 30s 内没push 成功就重试
138
+ ctx , cancel := context .WithTimeout (context .Background (), 30 * time .Second )
130
139
defer cancel ()
131
140
err := util .RunCmdCtx (ctx , "git" , "push" , "-f" , "origin" , branch )
132
141
if err != nil {
@@ -204,25 +213,18 @@ func getTime() {
204
213
// 如果以前的commit 够用了,则直接随机分配
205
214
//git log -n 5 --pretty=format:"%cI | %H | %s" > abc.txt
206
215
func getCommitLog (times int ) (res []commit ) {
207
- err := util .RunCmd ("git" , "log" , "-n" , strconv .Itoa (times ), "--pretty=format:\" {\\ \" date\\ \" :\\ \" %cI\\ \" ,\\ \" hash\\ \" :\\ \" %H\\ \" ,\\ \" msg\\ \" :\\ \" %s\\ \" }\" > commit.log" )
216
+ pwd , _ := file .GetCurrentPath ()
217
+ pretty := fmt .Sprintf ("--pretty=format:{\" date\" :\" %%cI\" ,\" hash\" :\" %%H\" }" )
218
+ r , err := util .RunCmdRetCD (pwd , "git" , "log" , "-n" , strconv .Itoa (times ), pretty )
208
219
if err != nil {
209
220
util .Errorf ("git log err (%+v)" , err )
210
221
return
211
222
}
212
- // 读取
213
- f , err := os .Open ("commit.log" )
214
- if err != nil {
215
- panic (err )
216
- }
217
- defer f .Close ()
218
- rd := bufio .NewReader (f )
219
- for {
220
- line , err := rd .ReadBytes ('\n' ) //以'\n'为结束符读入一行
221
- if err != nil || io .EOF == err {
222
- break
223
- }
223
+ split := strings .Split (r , "\n " )
224
+ for i , s := range split {
225
+ util .Infof ("commit %d: %s)" , i , s )
224
226
r := commit {}
225
- if err = json .Unmarshal (line , & r ); err != nil {
227
+ if err = json .Unmarshal ([] byte ( s ) , & r ); err != nil {
226
228
util .Errorf ("Unmarshal err (%+v)" , err )
227
229
return
228
230
}
@@ -231,16 +233,47 @@ func getCommitLog(times int) (res []commit) {
231
233
return
232
234
}
233
235
234
- func deviceCommit (list []commit ) {
235
- l := len (list )
236
+ func deviceCommitTime (list []commit ) ( timeList [] int ) {
237
+ l := len (list )
236
238
// 4个月
237
- total := 60 * 60 * 24 * 30 * 4
238
- timeList : = []int {total }
239
+ total := seconds
240
+ timeList = []int {}
239
241
for i := 0 ; i < len (list ); i ++ {
242
+ timeList = append (timeList , total )
240
243
reduce := util .DoubleAverage (l , total )
241
- total -= reduce
244
+ total -= reduce
242
245
l --
243
- println (reduce )
244
- timeList = append (timeList , total )
245
246
}
246
- }
247
+ fmt .Printf ("%+v" , timeList )
248
+ if timeList [len (timeList )- 1 ] > 60 * 60 * 24 * 3 || timeList [len (timeList )- 1 ] < 60 * 60 {
249
+ util .Infof ("最后一次分配超过3天,重新分配..." )
250
+ return deviceCommitTime (list )
251
+ }
252
+ util .Infof ("时间切分完成" )
253
+ return
254
+ }
255
+
256
+ func pushCommitTime (commitList []commit , timeList []int ) {
257
+ lc , lt := len (commitList ), len (timeList )
258
+ if lc != lt {
259
+ util .Errorf ("lc(%d) != lt (%d)" , lc , lt )
260
+ panic ("出错啦,长度咋不相等呢?" )
261
+ return
262
+ }
263
+ // 时间反着得
264
+ // 这里不能开协程
265
+ //eg := errgroup.Group{}
266
+ for i , c := range timeList {
267
+ util .Infof ("=========== 修改第 %d 次开始 ===========" , i + 1 )
268
+ format := time .Now ().Add (time .Second * time .Duration (c * - 1 )).Format ("2006-01-02 15:04:05" )
269
+ //eg.Go(func() error {
270
+ changeDate (commitList [i ].Hash , format )
271
+ //return nil
272
+ //})
273
+ }
274
+ //if err := eg.Wait(); err != nil {
275
+ // util.Errorf("errgroup err (%+v)", err)
276
+ // return
277
+ //}
278
+ return
279
+ }
0 commit comments