From 96725c337f825e97335541ab7f2780f5cb3ed871 Mon Sep 17 00:00:00 2001 From: shaoying Date: Sat, 28 Dec 2019 18:20:08 +0800 Subject: [PATCH] add git --- misc/git/git.go | 99 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 misc/git/git.go diff --git a/misc/git/git.go b/misc/git/git.go new file mode 100644 index 00000000..a75d8e7a --- /dev/null +++ b/misc/git/git.go @@ -0,0 +1,99 @@ +package git + +import ( + "github.com/shylinux/icebergs" + "github.com/shylinux/icebergs/base/cli" + "github.com/shylinux/toolkits" + "os" + "strings" + "time" +) + +var Index = &ice.Context{Name: "git", Help: "代码管理", + Caches: map[string]*ice.Cache{}, + Configs: map[string]*ice.Config{ + "buffer": {Name: "buffer", Help: "缓存", Value: kit.Data()}, + }, + Commands: map[string]*ice.Command{ + ice.ICE_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + }}, + ice.ICE_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + }}, + "sum": {Name: "sum", Help: "统计", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + total := false + if len(arg) > 0 && arg[0] == "total" { + total, arg = true, arg[1:] + } + + args := []string{} + if len(arg) > 0 { + if s, e := os.Stat(arg[0] + "/.git"); e == nil && s.IsDir() { + args, arg = append(args, "-C", arg[0]), arg[1:] + } + } + + args = append(args, "log", "--shortstat", "--pretty=commit: %ad %n%s", "--date=iso", "--reverse") + if len(arg) > 0 { + args = append(args, kit.Select("-n", "--since", strings.Contains(arg[0], "-"))) + if strings.Contains(arg[0], "-") && !strings.Contains(arg[0], ":") { + arg[1] = arg[1] + " 00:00:00" + } + args = append(args, arg[0:]...) + } else { + args = append(args, "-n", "30") + } + + var total_day time.Duration + count, count_add, count_del := 0, 0, 0 + for i, v := range strings.Split(m.Cmdx(ice.CLI_SYSTEM, "git", args), "commit: ") { + if i > 0 { + l := strings.Split(v, "\n") + hs := strings.Split(l[0], " ") + + add, del := "0", "0" + if len(l) > 3 { + fs := strings.Split(strings.TrimSpace(l[3]), ", ") + if adds := strings.Split(fs[1], " "); len(fs) > 2 { + dels := strings.Split(fs[2], " ") + add = adds[0] + del = dels[0] + } else if adds[1] == "insertions(+)" { + add = adds[0] + } else { + del = adds[0] + } + } + + if total { + if count++; i == 1 { + if t, e := time.Parse("2006-01-02", hs[0]); e == nil { + total_day = time.Now().Sub(t) + m.Append("from", hs[0]) + } + } + count_add += kit.Int(add) + count_del += kit.Int(del) + continue + } + + m.Push("date", hs[0]) + m.Push("adds", add) + m.Push("dels", del) + m.Push("rest", kit.Int(add)-kit.Int(del)) + m.Push("note", l[1]) + m.Push("hour", strings.Split(hs[1], ":")[0]) + m.Push("time", hs[1]) + } + } + if total { + m.Push("days", int(total_day.Hours())/24) + m.Push("commit", count) + m.Push("adds", count_add) + m.Push("dels", count_del) + m.Push("rest", count_add-count_del) + } + }}, + }, +} + +func init() { cli.Index.Register(Index, nil) }