Commit a37b278

Anton Medvedev <anton@medv.io>
2025-12-01 20:32:00
Associate commits with branch information and display branch in commit views
1 parent 236d6df
Changed files (4)
pkg/git/types.go
@@ -23,6 +23,7 @@ type Commit struct {
 	Email     string
 	Date      time.Time
 	Parents   []string
+	Branch    Ref
 	RefNames  []RefName
 	Href      string
 }
pkg/templates/commit.gohtml
@@ -224,21 +224,27 @@
         </div>
         <div class="commit-message">
             <div class="commit-subject">
-              {{ .Commit.Subject }}
-              {{ if .Commit.RefNames }}
-              <span class="ref-badges">
-                {{ range .Commit.RefNames }}
-                  {{ if or (eq .Kind "Branch") (eq .Kind "Tag") }}
-                    <span class="badge">{{ if eq .Kind "Tag" }}tag: {{ end }}{{ .Name }}</span>
-                  {{ end }}
+                {{ .Commit.Subject }}
+                {{ if .Commit.RefNames }}
+                    <span class="ref-badges">
+                        {{ range .Commit.RefNames }}
+                            {{ if or (eq .Kind "Branch") (eq .Kind "Tag") }}
+                                <span class="badge">{{ if eq .Kind "Tag" }}tag: {{ end }}{{ .Name }}</span>
+                            {{ end }}
+                        {{ end }}
+                    </span>
                 {{ end }}
-              </span>
-              {{ end }}
             </div>
             {{ if .Commit.Body }}
-                <div class="commit-body">{{ .Commit.Body }}</div>{{ end }}
+                <div class="commit-body">{{ .Commit.Body }}</div>
+            {{ end }}
         </div>
         <div class="commit-subinfo">
+            <div class="commit-branch">
+                {{ if .Commit.Branch }}
+                    <a href="../commits/{{ .Commit.Branch }}/index.html" class="badge">{{ .Commit.Branch }}</a>
+                {{ end }}
+            </div>
             {{ if .Commit.Parents }}
                 <div class="commit-parents">
                     {{ if eq (len .Commit.Parents)  1 }}
commit.go
@@ -229,13 +229,18 @@ func generateCommitPage(commit git.Commit, params Params) error {
 		return filesViews[i].Path < filesViews[j].Path
 	})
 
+	currentRef := params.DefaultRef
+	if commit.Branch != "" {
+		currentRef = commit.Branch
+	}
+
 	err = templates.CommitTemplate.ExecuteTemplate(f, "layout.gohtml", templates.CommitParams{
 		LayoutParams: templates.LayoutParams{
 			Title:      fmt.Sprintf("%s %s %s@%s", commit.Subject, dot, params.Name, commit.ShortHash),
 			Name:       params.Name,
 			Dark:       params.Dark,
 			RootHref:   rootHref,
-			CurrentRef: params.Ref,
+			CurrentRef: currentRef,
 			Selected:   "commits",
 		},
 		Commit:    commit,
main.go
@@ -165,6 +165,10 @@ func main() {
 		}
 
 		for _, commit := range commitsFor[branch] {
+			if alreadyExisting, ok := commits[commit.Hash]; ok && alreadyExisting.Branch == params.DefaultRef {
+				continue
+			}
+			commit.Branch = branch
 			commits[commit.Hash] = commit
 		}
 	}
@@ -175,10 +179,15 @@ func main() {
 			panic(err)
 		}
 		for _, commit := range commitsForTag {
+			if alreadyExisting, ok := commits[commit.Hash]; ok && alreadyExisting.Branch != "" {
+				continue
+			}
 			commits[commit.Hash] = commit
 		}
 	}
 
+	echo(fmt.Sprintf("> %s: %d branches, %d tags, %d commits", params.Name, len(branches), len(tags), len(commits)))
+
 	if err := generateBranches(branches, flagDefaultBranch, params); err != nil {
 		panic(err)
 	}
@@ -218,7 +227,7 @@ func main() {
 		}
 	}
 
-	// Pack to the default branch
+	// Back to the default branch
 	params.Ref = git.Ref(flagDefaultBranch)
 
 	// Commits pages generation