Skip to content

Commit cff7c3a

Browse files
committed
add commit.log
1 parent 40e24b2 commit cff7c3a

File tree

6 files changed

+130
-3
lines changed

6 files changed

+130
-3
lines changed

commit.log

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{"date":"2021-08-12T06:03:26+08:00","hash":"40e24b24faa6ba51d1defbebc34ccc27dedcef93","msg":"完工"}
2+
{"date":"2021-08-12T05:04:24+08:00","hash":"3312b45d69d94d338e20ce48294feb2235df6e22","msg":"opt"}
3+
{"date":"2021-05-18T06:54:27+08:00","hash":"f332ed7f4e119b1cee6788ecfed79a93fe9366fc","msg":"cut ChangeLog.txt"}
4+
{"date":"2021-05-16T00:08:56+08:00","hash":"a58945a39b6b436e5e7ffd1bf8aa366b0b660395","msg":"add README.md"}
5+
{"date":"2021-08-01T23:49:36+08:00","hash":"ece6ddf8a4b2a53c247add6ec7f13d8c566711d2","msg":"可以用了"}
6+
{"date":"2021-08-11T23:48:37+08:00","hash":"2b21160d3008b17135289a81e53d3dad1ca1b9c2","msg":"modify go.sum"}
7+
{"date":"2021-08-11T23:47:16+08:00","hash":"59c784473a8cad2b99242436a46ed09f75f84bad","msg":"increase go.mod"}
8+
{"date":"2021-08-11T23:47:13+08:00","hash":"176e7fc09d961a2522042ae5c6620a0e0d714ddd","msg":"edit README.md"}
9+
{"date":"2021-08-11T23:43:21+08:00","hash":"87c44bdf6cbe6166615c962180d22910877f24fb","msg":"edit README.md"}
10+
{"date":"2021-08-11T23:38:31+08:00","hash":"2f51259d8417ade03b3297d85ebc7f7bb9025f73","msg":"increase client.go"}

git/commit.log

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{"date":"2021-08-12T06:03:26+08:00","hash":"40e24b24faa6ba51d1defbebc34ccc27dedcef93","msg":"完工"}
2+
{"date":"2021-08-12T05:04:24+08:00","hash":"3312b45d69d94d338e20ce48294feb2235df6e22","msg":"opt"}
3+
{"date":"2021-05-18T06:54:27+08:00","hash":"f332ed7f4e119b1cee6788ecfed79a93fe9366fc","msg":"cut ChangeLog.txt"}
4+
{"date":"2021-05-16T00:08:56+08:00","hash":"a58945a39b6b436e5e7ffd1bf8aa366b0b660395","msg":"add README.md"}
5+
{"date":"2021-08-01T23:49:36+08:00","hash":"ece6ddf8a4b2a53c247add6ec7f13d8c566711d2","msg":"可以用了"}
6+
{"date":"2021-08-11T23:48:37+08:00","hash":"2b21160d3008b17135289a81e53d3dad1ca1b9c2","msg":"modify go.sum"}
7+
{"date":"2021-08-11T23:47:16+08:00","hash":"59c784473a8cad2b99242436a46ed09f75f84bad","msg":"increase go.mod"}
8+
{"date":"2021-08-11T23:47:13+08:00","hash":"176e7fc09d961a2522042ae5c6620a0e0d714ddd","msg":"edit README.md"}
9+
{"date":"2021-08-11T23:43:21+08:00","hash":"87c44bdf6cbe6166615c962180d22910877f24fb","msg":"edit README.md"}
10+
{"date":"2021-08-11T23:38:31+08:00","hash":"2f51259d8417ade03b3297d85ebc7f7bb9025f73","msg":"increase client.go"}

git/git.go

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package git
22

33
import (
4+
"bufio"
45
"context"
6+
"encoding/json"
57
"fmt"
68
"git.medlinker.com/wanghouwei/autoAddCommit/file"
79
"git.medlinker.com/wanghouwei/autoAddCommit/util"
10+
"io"
811
"math/rand"
12+
"os"
13+
"strconv"
914
"time"
1015
)
1116

@@ -15,8 +20,15 @@ var (
1520
startTime time.Time
1621
maxTimes = 100
1722
baseDir = ""
23+
commitTimes = 50
1824
)
1925

26+
type commit struct {
27+
Date string `json:"date"`
28+
Hash string `json:"hash"`
29+
Msg string `json:"msg"`
30+
}
31+
2032
func changeDate() {
2133
id, _ := getCommitID()
2234
cmd := `if [ $GIT_COMMIT = %s ]; then export GIT_AUTHOR_DATE="%s" export GIT_COMMITTER_DATE="%s"; fi`
@@ -31,13 +43,20 @@ func changeDate() {
3143
}
3244

3345
func Run(dir string) {
46+
now := time.Now()
47+
// 前推4个月
48+
startTime = now.Add(time.Second * -1 * 60 * 60 * 24 * 30 * 4)
3449
//changeDate()
3550
//return
3651
//dir = "/Users/med/mine/goPkgLearn"
52+
// 最后50次
53+
log := getCommitLog(commitTimes)
54+
if len(log) == commitTimes {
55+
util.Infof("commit log 已经够分配")
56+
deviceCommit(log)
57+
return
58+
}
3759
baseDir = dir
38-
now := time.Now()
39-
// 前推4个月
40-
startTime = now.Add(time.Second * -1 * 60 * 60 * 24 * 30 * 4)
4160
if err := getBranch(); err != nil {
4261
util.Errorf("getBranch err (%+v)", err)
4362
return
@@ -181,3 +200,47 @@ func getTime() {
181200
}
182201
startTime = startTime.Add(time.Second * time.Duration(randNum))
183202
}
203+
204+
// 如果以前的commit 够用了,则直接随机分配
205+
//git log -n 5 --pretty=format:"%cI | %H | %s" > abc.txt
206+
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")
208+
if err != nil {
209+
util.Errorf("git log err (%+v)", err)
210+
return
211+
}
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+
}
224+
r := commit{}
225+
if err = json.Unmarshal(line, &r); err != nil {
226+
util.Errorf("Unmarshal err (%+v)", err)
227+
return
228+
}
229+
res = append(res, r)
230+
}
231+
return
232+
}
233+
234+
func deviceCommit(list []commit) {
235+
l := len(list)
236+
// 4个月
237+
total := 60 * 60 * 24 * 30 * 4
238+
timeList := []int{total}
239+
for i := 0; i < len(list); i++ {
240+
reduce := util.DoubleAverage(l, total)
241+
total-= reduce
242+
l--
243+
println(reduce)
244+
timeList = append(timeList, total)
245+
}
246+
}

git/git_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package git
2+
3+
import "testing"
4+
5+
func Test_getCommitLog(t *testing.T) {
6+
getCommitLog(10)
7+
}
8+
9+
func Test_Run(t *testing.T) {
10+
Run("./")
11+
}

git/mainFile/mit.log

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{"date":"2021-08-12T06:03:26+08:00","hash":"40e24b24faa6ba51d1defbebc34ccc27dedcef93","msg":"完工"}
2+
{"date":"2021-08-12T05:04:24+08:00","hash":"3312b45d69d94d338e20ce48294feb2235df6e22","msg":"opt"}
3+
{"date":"2021-05-18T06:54:27+08:00","hash":"f332ed7f4e119b1cee6788ecfed79a93fe9366fc","msg":"cut ChangeLog.txt"}
4+
{"date":"2021-05-16T00:08:56+08:00","hash":"a58945a39b6b436e5e7ffd1bf8aa366b0b660395","msg":"add README.md"}
5+
{"date":"2021-08-01T23:49:36+08:00","hash":"ece6ddf8a4b2a53c247add6ec7f13d8c566711d2","msg":"可以用了"}
6+
{"date":"2021-08-11T23:48:37+08:00","hash":"2b21160d3008b17135289a81e53d3dad1ca1b9c2","msg":"modify go.sum"}
7+
{"date":"2021-08-11T23:47:16+08:00","hash":"59c784473a8cad2b99242436a46ed09f75f84bad","msg":"increase go.mod"}
8+
{"date":"2021-08-11T23:47:13+08:00","hash":"176e7fc09d961a2522042ae5c6620a0e0d714ddd","msg":"edit README.md"}
9+
{"date":"2021-08-11T23:43:21+08:00","hash":"87c44bdf6cbe6166615c962180d22910877f24fb","msg":"edit README.md"}
10+
{"date":"2021-08-11T23:38:31+08:00","hash":"2f51259d8417ade03b3297d85ebc7f7bb9025f73","msg":"increase client.go"}

util/util.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"context"
55
"fmt"
66
"github.com/fatih/color"
7+
"math/rand"
78
"os/exec"
89
"strings"
10+
"time"
911
)
1012

1113
var (
@@ -69,3 +71,24 @@ func Infof(format string, args ...interface{}) {
6971
func Errorf(format string, args ...interface{}) {
7072
color.Red(format+"\n", args...)
7173
}
74+
75+
//二倍均值算法,count剩余个数,amount剩余时间
76+
func DoubleAverage(count, total int) int {
77+
if count == 1 {
78+
//返回剩余时间
79+
return total
80+
}
81+
// 提交间隔至少 1h
82+
min := 60 * 60
83+
//计算最大可用时间,min最小是1分钱,减去的min,下面会加上,避免出现0分钱
84+
max := total - min*count
85+
//计算最大可用平均值
86+
avg := max / count
87+
//二倍均值基础加上最小时间,防止0出现,作为上限
88+
avg2 := 2*avg + min
89+
//随机commit时间序列元素,把二倍均值作为随机的最大数
90+
rand.Seed(time.Now().UnixNano())
91+
//加min是为了避免出现0值,上面也减去了min
92+
x := rand.Intn(avg2) + min
93+
return x
94+
}

0 commit comments

Comments
 (0)