git: rework diff to fetch initial commit

This commit is contained in:
Anirudh Oppiliappan 2022-12-18 10:42:29 +05:30
parent e4d12fc667
commit 60298a6953
No known key found for this signature in database
GPG key ID: 8A93F96F78C5D4C4
3 changed files with 70 additions and 17 deletions

View file

@ -44,20 +44,29 @@ func (g *GitRepo) Diff() (*NiceDiff, error) {
return nil, fmt.Errorf("commit object: %w", err) return nil, fmt.Errorf("commit object: %w", err)
} }
var parent *object.Commit patch := &object.Patch{}
if len(c.ParentHashes) > 0 { commitTree, err := c.Tree()
parent, err = c.Parent(0) parent := &object.Commit{}
if err != nil { if err == nil {
return nil, fmt.Errorf("getting parent: %w", err) parentTree := &object.Tree{}
} if c.NumParents() != 0 {
} else { parent, err = c.Parents().Next()
parent = c if err == nil {
} parentTree, err = parent.Tree()
if err == nil {
patch, err := parent.Patch(c) patch, err = parentTree.Patch(commitTree)
if err != nil { if err != nil {
return nil, fmt.Errorf("patch: %w", err) return nil, fmt.Errorf("patch: %w", err)
} }
}
}
} else {
patch, err = parentTree.Patch(commitTree)
if err != nil {
return nil, fmt.Errorf("patch: %w", err)
}
}
}
diffs, _, err := gitdiff.Parse(strings.NewReader(patch.String())) diffs, _, err := gitdiff.Parse(strings.NewReader(patch.String()))
if err != nil { if err != nil {
@ -66,7 +75,12 @@ func (g *GitRepo) Diff() (*NiceDiff, error) {
nd := NiceDiff{} nd := NiceDiff{}
nd.Commit.This = c.Hash.String() nd.Commit.This = c.Hash.String()
if parent.Hash.IsZero() {
nd.Commit.Parent = ""
} else {
nd.Commit.Parent = parent.Hash.String() nd.Commit.Parent = parent.Hash.String()
}
nd.Commit.Author = c.Author nd.Commit.Author = c.Author
nd.Commit.Message = c.Message nd.Commit.Message = c.Message

View file

@ -106,8 +106,8 @@ func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) {
tpath := filepath.Join(d.c.Dirs.Templates, "*") tpath := filepath.Join(d.c.Dirs.Templates, "*")
t := template.Must(template.ParseGlob(tpath)) t := template.Must(template.ParseGlob(tpath))
if len(commits) >= 5 { if len(commits) >= 3 {
commits = commits[:5] commits = commits[:3]
} }
data := make(map[string]any) data := make(map[string]any)
@ -149,6 +149,7 @@ func (d *deps) RepoTree(w http.ResponseWriter, r *http.Request) {
data["name"] = name data["name"] = name
data["ref"] = ref data["ref"] = ref
data["parent"] = treePath data["parent"] = treePath
data["desc"] = getDescription(path)
d.listFiles(files, data, w) d.listFiles(files, data, w)
return return
@ -171,6 +172,7 @@ func (d *deps) FileContent(w http.ResponseWriter, r *http.Request) {
data := make(map[string]any) data := make(map[string]any)
data["name"] = name data["name"] = name
data["ref"] = ref data["ref"] = ref
data["desc"] = getDescription(path)
d.showFile(contents, data, w) d.showFile(contents, data, w)
return return
@ -188,7 +190,6 @@ func (d *deps) Log(w http.ResponseWriter, r *http.Request) {
} }
commits, err := gr.Commits() commits, err := gr.Commits()
log.Println(len(commits))
if err != nil { if err != nil {
d.Write500(w) d.Write500(w)
log.Println(err) log.Println(err)
@ -203,6 +204,7 @@ func (d *deps) Log(w http.ResponseWriter, r *http.Request) {
data["meta"] = d.c.Meta data["meta"] = d.c.Meta
data["name"] = name data["name"] = name
data["ref"] = ref data["ref"] = ref
data["desc"] = getDescription(path)
if err := t.ExecuteTemplate(w, "log", data); err != nil { if err := t.ExecuteTemplate(w, "log", data); err != nil {
log.Println(err) log.Println(err)
@ -239,6 +241,7 @@ func (d *deps) Diff(w http.ResponseWriter, r *http.Request) {
data["meta"] = d.c.Meta data["meta"] = d.c.Meta
data["name"] = name data["name"] = name
data["ref"] = ref data["ref"] = ref
data["desc"] = getDescription(path)
if err := t.ExecuteTemplate(w, "commit", data); err != nil { if err := t.ExecuteTemplate(w, "commit", data); err != nil {
log.Println(err) log.Println(err)
@ -278,6 +281,7 @@ func (d *deps) Refs(w http.ResponseWriter, r *http.Request) {
data["name"] = name data["name"] = name
data["branches"] = branches data["branches"] = branches
data["tags"] = tags data["tags"] = tags
data["desc"] = getDescription(path)
if err := t.ExecuteTemplate(w, "refs", data); err != nil { if err := t.ExecuteTemplate(w, "refs", data); err != nil {
log.Println(err) log.Println(err)

View file

@ -147,7 +147,9 @@ a:hover {
} }
.diff { .diff {
padding-top: 1rem; margin: 1rem 0 1rem 0;
padding: 1rem 0 1rem 0;
border-bottom: 1.5px solid var(--medium-gray);
} }
.diff pre { .diff pre {
@ -162,6 +164,14 @@ a:hover {
color: var(--gray); color: var(--gray);
} }
.commit-email:before {
content: '<';
}
.commit-email:after {
content: '>';
}
.commit pre { .commit pre {
padding-bottom: 1rem; padding-bottom: 1rem;
white-space: pre-wrap; white-space: pre-wrap;
@ -184,6 +194,31 @@ a:hover {
color: var(--gray); 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) { @media (max-width: 600px) {
.index { .index {
grid-row-gap: 0.8em; grid-row-gap: 0.8em;