all repos — legit @ 60298a69538ccdda417613a09e5acbd917bfc53a

web frontend for git, written in go

git: rework diff to fetch initial commit
Anirudh Oppiliappan x@icyphox.sh
Sun, 18 Dec 2022 10:42:29 +0530
commit

60298a69538ccdda417613a09e5acbd917bfc53a

parent

e4d12fc667c9830dd5f08f827b2a70adff58a09b

3 files changed, 70 insertions(+), 17 deletions(-)

jump to
M git/diff.gogit/diff.go

@@ -44,19 +44,28 @@ if err != nil {

return nil, fmt.Errorf("commit object: %w", err) } - var parent *object.Commit - if len(c.ParentHashes) > 0 { - parent, err = c.Parent(0) - if err != nil { - return nil, fmt.Errorf("getting parent: %w", err) + patch := &object.Patch{} + commitTree, err := c.Tree() + parent := &object.Commit{} + if err == nil { + parentTree := &object.Tree{} + if c.NumParents() != 0 { + parent, err = c.Parents().Next() + if err == nil { + parentTree, err = parent.Tree() + if err == nil { + patch, err = parentTree.Patch(commitTree) + if err != nil { + return nil, fmt.Errorf("patch: %w", err) + } + } + } + } else { + patch, err = parentTree.Patch(commitTree) + if err != nil { + return nil, fmt.Errorf("patch: %w", err) + } } - } else { - parent = c - } - - patch, err := parent.Patch(c) - if err != nil { - return nil, fmt.Errorf("patch: %w", err) } diffs, _, err := gitdiff.Parse(strings.NewReader(patch.String()))

@@ -66,7 +75,12 @@ }

nd := NiceDiff{} nd.Commit.This = c.Hash.String() - nd.Commit.Parent = parent.Hash.String() + + if parent.Hash.IsZero() { + nd.Commit.Parent = "" + } else { + nd.Commit.Parent = parent.Hash.String() + } nd.Commit.Author = c.Author nd.Commit.Message = c.Message
M routes/routes.goroutes/routes.go

@@ -106,8 +106,8 @@

tpath := filepath.Join(d.c.Dirs.Templates, "*") t := template.Must(template.ParseGlob(tpath)) - if len(commits) >= 5 { - commits = commits[:5] + if len(commits) >= 3 { + commits = commits[:3] } data := make(map[string]any)

@@ -149,6 +149,7 @@ data := make(map[string]any)

data["name"] = name data["ref"] = ref data["parent"] = treePath + data["desc"] = getDescription(path) d.listFiles(files, data, w) return

@@ -171,6 +172,7 @@ contents, err := gr.FileContent(treePath)

data := make(map[string]any) data["name"] = name data["ref"] = ref + data["desc"] = getDescription(path) d.showFile(contents, data, w) return

@@ -188,7 +190,6 @@ return

} commits, err := gr.Commits() - log.Println(len(commits)) if err != nil { d.Write500(w) log.Println(err)

@@ -203,6 +204,7 @@ data["commits"] = commits

data["meta"] = d.c.Meta data["name"] = name data["ref"] = ref + data["desc"] = getDescription(path) if err := t.ExecuteTemplate(w, "log", data); err != nil { log.Println(err)

@@ -239,6 +241,7 @@ data["diff"] = diff.Diff

data["meta"] = d.c.Meta data["name"] = name data["ref"] = ref + data["desc"] = getDescription(path) if err := t.ExecuteTemplate(w, "commit", data); err != nil { log.Println(err)

@@ -278,6 +281,7 @@ data["meta"] = d.c.Meta

data["name"] = name data["branches"] = branches data["tags"] = tags + data["desc"] = getDescription(path) if err := t.ExecuteTemplate(w, "refs", data); err != nil { log.Println(err)
M static/style.cssstatic/style.css

@@ -147,7 +147,9 @@ white-space: pre-wrap;

} .diff { - padding-top: 1rem; + margin: 1rem 0 1rem 0; + padding: 1rem 0 1rem 0; + border-bottom: 1.5px solid var(--medium-gray); } .diff pre {

@@ -162,6 +164,14 @@ .commit-email {

color: var(--gray); } +.commit-email:before { + content: '<'; +} + +.commit-email:after { + content: '>'; +} + .commit pre { padding-bottom: 1rem; white-space: pre-wrap;

@@ -182,6 +192,31 @@ }

.diff-noop { color: var(--gray); +} + +.ref { + font-family: var(--display-font); + font-size: 14px; + color: var(--gray); + display: inline-block; + padding-top: 0.7em; +} + +.refs { + display: grid; + grid-template-columns: 1fr 1fr; + align-items: center; +} + +.line-numbers { + white-space: pre-line; +} + +.file-wrapper { + display: flex; + flex-direction: row; + grid-template-columns: 1rem minmax(0, 1fr); + gap: 1rem; } @media (max-width: 600px) {