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,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

View File

@ -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)

View File

@ -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;