From 60298a69538ccdda417613a09e5acbd917bfc53a Mon Sep 17 00:00:00 2001 From: Anirudh Oppiliappan Date: Sun, 18 Dec 2022 10:42:29 +0530 Subject: git: rework diff to fetch initial commit --- git/diff.go | 40 +++++++++++++++++++++++++++------------- routes/routes.go | 10 +++++++--- static/style.css | 37 ++++++++++++++++++++++++++++++++++++- 3 files changed, 70 insertions(+), 17 deletions(-) diff --git a/git/diff.go b/git/diff.go index 4b42b9a..38c00f2 100644 --- a/git/diff.go +++ b/git/diff.go @@ -44,19 +44,28 @@ func (g *GitRepo) Diff() (*NiceDiff, error) { 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 @@ func (g *GitRepo) Diff() (*NiceDiff, error) { 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 diff --git a/routes/routes.go b/routes/routes.go index 7e2e897..920e66c 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -106,8 +106,8 @@ func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) { 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 @@ func (d *deps) RepoTree(w http.ResponseWriter, r *http.Request) { data["name"] = name data["ref"] = ref data["parent"] = treePath + data["desc"] = getDescription(path) d.listFiles(files, data, w) return @@ -171,6 +172,7 @@ func (d *deps) FileContent(w http.ResponseWriter, r *http.Request) { 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 @@ func (d *deps) Log(w http.ResponseWriter, r *http.Request) { } commits, err := gr.Commits() - log.Println(len(commits)) if err != nil { d.Write500(w) log.Println(err) @@ -203,6 +204,7 @@ func (d *deps) Log(w http.ResponseWriter, r *http.Request) { 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 @@ func (d *deps) Diff(w http.ResponseWriter, r *http.Request) { 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 @@ func (d *deps) Refs(w http.ResponseWriter, r *http.Request) { 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) diff --git a/static/style.css b/static/style.css index 0b0664f..340635e 100644 --- a/static/style.css +++ b/static/style.css @@ -147,7 +147,9 @@ a:hover { } .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 @@ a:hover { color: var(--gray); } +.commit-email:before { + content: '<'; +} + +.commit-email:after { + content: '>'; +} + .commit pre { padding-bottom: 1rem; white-space: pre-wrap; @@ -184,6 +194,31 @@ a:hover { 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) { .index { grid-row-gap: 0.8em; -- cgit