8356
Wx
www-xonecas-io / files
www-xonecas-io / internal / site / tree.go
3.0 KB
1package site 2 3import ( 4 "net/url" 5 "path" 6 "slices" 7 "strconv" 8 "strings" 9 10 "xonecas.io/site/internal/repo" 11) 12 13// Tree is the files and folders on a project's default branch, with each file's size, 14// and the last commit that touched each path ("" is the root). 15type Tree struct { 16 Files map[string]int64 17 Dirs map[string]bool 18 Last map[string][2]string // subject, date 19} 20 21// ReadTree lists every path on HEAD of the bare repo dir, and finds its last commit in one pass over the log. 22func ReadTree(dir string) (Tree, error) { 23 t := Tree{Files: map[string]int64{}, Dirs: map[string]bool{}, Last: map[string][2]string{}} 24 out, err := repo.Git(dir, "ls-tree", "-r", "-z", "-l", "HEAD") 25 if err != nil { 26 return t, err 27 } 28 for _, rec := range strings.Split(out, "\x00") { 29 meta, f, ok := strings.Cut(rec, "\t") 30 if fields := strings.Fields(meta); ok && len(fields) == 4 && fields[1] == "blob" { 31 t.Files[f], _ = strconv.ParseInt(fields[3], 10, 64) 32 for d := path.Dir(f); d != "."; d = path.Dir(d) { 33 t.Dirs[d] = true 34 } 35 } 36 } 37 out, err = repo.Git(dir, "-c", "core.quotePath=false", "log", "--format=%x00%s%x09%as", "--name-only", "HEAD") 38 for _, rec := range strings.Split(out, "\x00")[1:] { 39 head, names, _ := strings.Cut(rec, "\n") 40 msg, date, _ := strings.Cut(head, "\t") 41 for _, f := range strings.Split(names, "\n") { 42 for ; f != "" && f != "."; f = path.Dir(f) { 43 if _, ok := t.Last[f]; !ok { 44 t.Last[f] = [2]string{msg, date} 45 } 46 } 47 } 48 if _, ok := t.Last[""]; !ok { 49 t.Last[""] = [2]string{msg, date} 50 } 51 } 52 return t, err 53} 54 55// href is where a path of project name lives on the site: kind is tree, blob or raw. 56func href(name, kind, p string) string { 57 u := (&url.URL{Path: "/projects/" + name + "/" + kind + "/" + p}).String() 58 if kind != "raw" { 59 u += "/" 60 } 61 return u 62} 63 64// headCommit is the last commit on HEAD of project name, linked to its commit page. 65func headCommit(name, dir string) (Commit, error) { 66 out, err := repo.Git(dir, "log", "-1", "--format=%H%x09%h%x09%an%x09%as%x09%s", "HEAD") 67 f := strings.SplitN(out, "\t", 5) 68 if err != nil || len(f) < 5 { 69 return Commit{}, err 70 } 71 return Commit{Hash: f[1], Author: f[2], Date: f[3], Msg: f[4], Href: "/projects/" + name + "/commit/" + f[0] + "/"}, nil 72} 73 74// entries is the folder dir ("" for the root) of project name, folders first, each with its last commit. 75func entries(name, dir string, t Tree) []TreeEntry { 76 var list []TreeEntry 77 add := func(p string, isDir bool) { 78 if path.Dir(p) == dir || (dir == "" && path.Dir(p) == ".") { 79 kind := "blob" 80 if isDir { 81 kind = "tree" 82 } 83 last := t.Last[p] 84 list = append(list, TreeEntry{Dir: isDir, Name: path.Base(p), Msg: last[0], Date: last[1], Href: href(name, kind, p)}) 85 } 86 } 87 for f := range t.Files { 88 add(f, false) 89 } 90 for d := range t.Dirs { 91 add(d, true) 92 } 93 slices.SortFunc(list, func(a, b TreeEntry) int { 94 if a.Dir != b.Dir { 95 if a.Dir { 96 return -1 97 } 98 return 1 99 } 100 return strings.Compare(strings.ToLower(a.Name), strings.ToLower(b.Name)) 101 }) 102 return list 103}