From 80aa4ce20708872dbb12b8fada84e3d6de62dfed Mon Sep 17 00:00:00 2001 From: shy Date: Sat, 30 Dec 2023 20:18:48 +0800 Subject: [PATCH] opt git --- misc/git/counts.go | 135 ++++++++++++++++++++++++++++++++++++++++++++ misc/git/spides.go | 22 ++++++++ misc/git/totals.go | 137 +++++++++++++++++++++++++++++++++++++++++++++ misc/git/trends.go | 36 ++++++++++++ 4 files changed, 330 insertions(+) create mode 100644 misc/git/counts.go create mode 100644 misc/git/spides.go create mode 100644 misc/git/totals.go create mode 100644 misc/git/trends.go diff --git a/misc/git/counts.go b/misc/git/counts.go new file mode 100644 index 00000000..d7fb0b56 --- /dev/null +++ b/misc/git/counts.go @@ -0,0 +1,135 @@ +package git + +import ( + "path" + "strings" + + ice "shylinux.com/x/icebergs" + "shylinux.com/x/icebergs/base/cli" + "shylinux.com/x/icebergs/base/lex" + "shylinux.com/x/icebergs/base/mdb" + "shylinux.com/x/icebergs/base/nfs" + "shylinux.com/x/icebergs/core/code" + kit "shylinux.com/x/toolkits" +) + +func _counts_count(m *ice.Message, arg []string, cb func(string)) { + if m.Warn(len(arg) == 0 || arg[0] == nfs.USR, ice.ErrNotValid, nfs.DIR, "to many files, please select sub dir") { + return + } + nfs.DirDeepAll(m, "", arg[0], func(value ice.Maps) { + if file := value[nfs.PATH]; kit.Contains(file, nfs.BIN, nfs.VAR, "node_modules/") { + return + } else if kit.IsIn(kit.Ext(file), "tags", "sum", "log") { + return + } else { + cb(file) + } + }, nfs.PATH) +} + +const COUNTS = "counts" + +func init() { + const ( + FILES = "files" + LINES = "lines" + ) + Index.MergeCommands(ice.Commands{ + COUNTS: {Name: "counts path@key auto order count package tags", Help: "代码行", Actions: ice.Actions{ + mdb.INPUTS: {Hand: func(m *ice.Message, arg ...string) { + m.Cmdy(nfs.DIR, path.Dir(kit.Select(nfs.PWD, arg[1]))).CutTo(nfs.PATH, arg[0]) + }}, + cli.ORDER: {Help: "排行", Hand: func(m *ice.Message, arg ...string) { + files := map[string]int{} + _counts_count(m, arg, func(file string) { + m.Cmdy(nfs.CAT, file, func(text string) { files[strings.TrimPrefix(file, arg[0])]++ }) + }) + kit.For(files, func(k string, v int) { m.Push(FILES, k).Push(LINES, v) }) + m.SortIntR(LINES) + }}, + mdb.COUNT: {Help: "计数", Hand: func(m *ice.Message, arg ...string) { + files, lines := map[string]int{}, map[string]int{} + _counts_count(m, arg, func(file string) { + files[mdb.TOTAL]++ + files[kit.Ext(file)]++ + m.Cmdy(nfs.CAT, file, func(text string) { + if kit.Ext(file) == code.GO { + switch { + case strings.HasPrefix(text, "type "): + lines["_type"]++ + case strings.HasPrefix(text, "func "): + lines["_func"]++ + } + } + lines[kit.Ext(file)]++ + lines[mdb.TOTAL]++ + }) + }) + kit.For(lines, func(k string, v int) { m.Push(mdb.TYPE, k).Push(FILES, files[k]).Push(LINES, lines[k]) }) + m.SortIntR(LINES) + }}, + code.PACKAGE: {Help: "依赖", Hand: func(m *ice.Message, arg ...string) { + list := map[string]map[string]int{} + ls := map[string]int{} + pkg, block := "", false + add := func(mod string) { + if _, ok := list[pkg]; !ok { + list[pkg] = map[string]int{} + } + kit.If(mod, func() { list[pkg][mod]++; ls[mod]++ }) + } + _counts_count(m, arg, func(file string) { + m.Cmdy(nfs.CAT, file, func(text string) { + if kit.Ext(file) == code.GO { + switch { + case strings.HasPrefix(text, "package "): + pkg = kit.Split(text)[1] + case strings.HasPrefix(text, "import ("): + block = true + case strings.HasPrefix(text, "import "): + add(kit.Select("", kit.Split(text), -1)) + case strings.HasPrefix(text, ")"): + block = false + default: + kit.If(block, func() { add(kit.Select("", kit.Split(text), -1)) }) + } + } + }) + }) + m.Appendv(ice.MSG_APPEND, []string{code.PACKAGE, mdb.COUNT}) + kit.For(ls, func(key string, value int) { + if !strings.Contains(key, "shylinux.com") { + return + } + count := 0 + m.Push(code.PACKAGE, key) + kit.For(kit.SortedKey(list), func(k string) { + if n := list[k][key]; n == 0 { + m.Push(k, "") + } else { + m.Push(k, n) + count++ + } + }) + m.Push(mdb.COUNT, count) + }) + m.SortIntR(mdb.COUNT) + }}, + nfs.TAGS: {Help: "索引", Hand: func(m *ice.Message, arg ...string) { + count := map[string]int{} + m.Cmd(nfs.CAT, path.Join(arg[0], nfs.TAGS), func(line string) { + if ls := strings.SplitN(line, lex.TB, 3); len(ls) < 3 { + return + } else if ls = strings.SplitN(ls[2], ";\"", 2); len(ls) < 2 { + return + } else { + count[kit.Split(ls[1])[0]]++ + } + }) + kit.For(count, func(k string, v int) { m.Push(mdb.TYPE, k).Push(mdb.COUNT, v) }) + m.SortIntR(mdb.COUNT) + }}, + }, Hand: func(m *ice.Message, arg ...string) { m.Cmdy(nfs.DIR, arg) }}, + }) +} diff --git a/misc/git/spides.go b/misc/git/spides.go new file mode 100644 index 00000000..0b2e3ac8 --- /dev/null +++ b/misc/git/spides.go @@ -0,0 +1,22 @@ +package git + +import ( + ice "shylinux.com/x/icebergs" + "shylinux.com/x/icebergs/base/ctx" + "shylinux.com/x/icebergs/base/nfs" +) + +const SPIDES = "spides" + +func init() { + Index.MergeCommands(ice.Commands{ + SPIDES: {Name: "spides repos auto", Help: "构架图", Hand: func(m *ice.Message, arg ...string) { + if len(arg) == 0 { + m.Cmdy(REPOS) + } else if p := _repos_path(m, arg[0]); len(arg) == 1 { + nfs.DirDeepAll(m, p, "", nil, nfs.PATH).Options(nfs.DIR_ROOT, p+nfs.PS) + ctx.DisplayStory(m, "") + } + }}, + }) +} diff --git a/misc/git/totals.go b/misc/git/totals.go new file mode 100644 index 00000000..70b903d7 --- /dev/null +++ b/misc/git/totals.go @@ -0,0 +1,137 @@ +package git + +import ( + "path" + "strings" + "sync" + "time" + + ice "shylinux.com/x/icebergs" + "shylinux.com/x/icebergs/base/cli" + "shylinux.com/x/icebergs/base/ctx" + "shylinux.com/x/icebergs/base/lex" + "shylinux.com/x/icebergs/base/mdb" + "shylinux.com/x/icebergs/base/nfs" + "shylinux.com/x/icebergs/base/web" + kit "shylinux.com/x/toolkits" + "shylinux.com/x/toolkits/logs" + "shylinux.com/x/toolkits/task" +) + +const TOTAL = "total" + +func init() { + const ( + FROM = "from" + DAYS = "days" + COMMIT = "commit" + ADDS = "adds" + DELS = "dels" + REST = "rest" + ) + Index.MergeCommands(ice.Commands{ + TOTAL: {Name: "total repos auto pie", Help: "统计量", Actions: ice.Actions{ + "pie": {Help: "饼图", Hand: func(m *ice.Message, arg ...string) { + defer ctx.DisplayStory(m, "pie.js") + m.Cmd("", func(value ice.Maps) { + kit.If(value[REPOS] != mdb.TOTAL, func() { + m.Push(REPOS, value[REPOS]).Push(mdb.VALUE, value[REST]).Push("", value, []string{FROM, DAYS, COMMIT, ADDS, DELS}) + }) + }) + }}, + }, Hand: func(m *ice.Message, arg ...string) { + if len(arg) > 0 { + ReposList(m.Spawn()).Table(func(value ice.Maps) { + kit.If(value[REPOS] == arg[0], func() { m.Cmdy("_sum", value[nfs.PATH], arg[1:]) }) + }) + m.StatusTimeCount(m.AppendSimple(FROM)) + return + } + defer web.ToastProcess(m)() + from, days, commit, adds, dels, rest := "", 0, 0, 0, 0, 0 + TableGo(ReposList(m.Spawn()), func(value ice.Maps, lock *task.Lock) { + if kit.IsIn(value[REPOS], "websocket", "go-qrcode", "go-git", "icons", "geoarea", "program", "node_modules") { + return + } + msg := m.Cmd("_sum", value[nfs.PATH], mdb.TOTAL, "10000") + defer lock.Lock()() + msg.Table(func(value ice.Maps) { + kit.If(kit.Int(value[DAYS]) > days, func() { from, days = value[FROM], kit.Int(value[DAYS]) }) + commit += kit.Int(value[COMMIT]) + adds += kit.Int(value[ADDS]) + dels += kit.Int(value[DELS]) + rest += kit.Int(value[REST]) + }) + m.Push(REPOS, value[REPOS]).Copy(msg) + }) + m.Push(REPOS, mdb.TOTAL).Push(TAGS, "v5.0.0").Push(FROM, from).Push(DAYS, days).Push(COMMIT, commit).Push(ADDS, adds).Push(DELS, dels).Push(REST, rest).SortIntR(REST) + }}, + "_sum": {Name: "_sum [path] [total] [count|date] args...", Help: "统计量", Hand: func(m *ice.Message, arg ...string) { + m.Options(nfs.DIR_ROOT, "", nfs.CAT_CONTENT, "") + if len(arg) > 0 { + if nfs.Exists(m, path.Join(arg[0], _GIT)) || nfs.Exists(m, path.Join(arg[0], "refs/heads/")) { + m.Option(cli.CMD_DIR, arg[0]) + arg = arg[1:] + } + } + total := false + kit.If(len(arg) > 0 && arg[0] == mdb.TOTAL, func() { total, arg = true, arg[1:] }) + args := []string{LOG, "--shortstat", "--pretty=commit: %H %ad %n%s", "--date=iso", "--reverse"} + if len(arg) > 0 { + arg[0] += kit.Select("", " 00:00:00", strings.Contains(arg[0], "-") && !strings.Contains(arg[0], nfs.DF)) + args = append(args, kit.Select("-n", "--since", strings.Contains(arg[0], "-"))) + args = append(args, arg...) + } else { + args = append(args, "-n", "30") + } + from, days, commit, adds, dels := "", 0, 0, 0, 0 + kit.SplitKV(lex.NL, "commit:", _git_cmds(m, args...), func(text string, ls []string) { + add, del := "0", "0" + for _, v := range kit.Split(strings.TrimSpace(kit.Select("", ls, -1)), mdb.FS) { + switch { + case strings.Contains(v, "inser"): + add = kit.Split(v)[0] + case strings.Contains(v, "delet"): + del = kit.Split(v)[0] + } + } + hs := strings.Split(ls[0], lex.SP) + if total { + if commit++; from == "" { + if t, e := time.Parse("2006-01-02", hs[1]); e == nil { + from, days = hs[1], int(time.Now().Sub(t).Hours())/24 + } + } + adds += kit.Int(add) + dels += kit.Int(del) + return + } + m.Push(FROM, hs[1]).Push(ADDS, add).Push(DELS, del).Push(REST, kit.Int(add)-kit.Int(del)).Push(COMMIT, ls[1]).Push(mdb.HASH, hs[0]) + }) + if total { + m.Push(TAGS, _git_cmds(m, "describe", "--tags")) + m.Push(FROM, from).Push(DAYS, days).Push(COMMIT, commit) + m.Push(ADDS, adds).Push(DELS, dels).Push(REST, adds-dels) + } + }}, + }) +} +func TableGo(m *ice.Message, cb ice.Any) *ice.Message { + wg, lock := sync.WaitGroup{}, &task.Lock{} + defer wg.Wait() + m.Table(func(value ice.Maps) { + wg.Add(1) + task.Put(m.FormatTaskMeta(), logs.FileLine(cb), func(*task.Task) { + defer wg.Done() + switch cb := cb.(type) { + case func(ice.Maps, *task.Lock): + cb(value, lock) + case func(ice.Maps): + cb(value) + default: + m.ErrorNotImplement(cb) + } + }) + }) + return m +} diff --git a/misc/git/trends.go b/misc/git/trends.go new file mode 100644 index 00000000..8678a436 --- /dev/null +++ b/misc/git/trends.go @@ -0,0 +1,36 @@ +package git + +import ( + ice "shylinux.com/x/icebergs" + "shylinux.com/x/icebergs/base/ctx" + "shylinux.com/x/icebergs/base/mdb" + "shylinux.com/x/icebergs/base/nfs" + "shylinux.com/x/icebergs/base/web/html" + "shylinux.com/x/icebergs/core/code" + kit "shylinux.com/x/toolkits" +) + +const TRENDS = "trends" + +func init() { + Index.MergeCommands(ice.Commands{ + TRENDS: {Name: "trends repos begin_time@date auto", Help: "趋势图", Meta: kit.Dict( + ice.CTX_TRANS, kit.Dict(html.INPUT, kit.Dict( + "from", "起始", "date", "日期", "max", "最多", "add", "添加", "del", "删除", + )), + ), Actions: ice.Actions{ + mdb.DETAIL: {Hand: func(m *ice.Message, arg ...string) { + m.Cmdy("", code.INNER, m.Option(REPOS), MASTER, m.Option(mdb.HASH), m.Cmdv(REPOS, m.Option(REPOS), MASTER, m.Option(mdb.HASH), nfs.FILE)) + }}, + code.INNER: {Hand: func(m *ice.Message, arg ...string) { + ctx.DisplayLocalInner(m.Cmdy(REPOS, code.INNER, arg), ctx.STYLE, html.FLOAT) + }}, + }, Hand: func(m *ice.Message, arg ...string) { + if len(arg) == 0 { + m.Cmdy(REPOS) + } else { + ctx.DisplayStory(m.Cmdy(TOTAL, kit.Slice(arg, 0, 2)), "") + } + }}, + }) +}