/** * AltGit - altgit * * This file is licensed under the Affero General Public License version 3 or * later. See the COPYING file. * * @author Paolo Lulli * @copyright Paolo Lulli 2026 */ package handler import ( "altgit/internal/config" "altgit/internal/gitrepo" "altgit/internal/rendering" "fmt" "html/template" "net/http" "net/url" "strings" "github.com/gin-gonic/gin" ) // HandleDiff renders a unified diff between two commits or branches. // // Query parameters: // - from – base ref (commit hash, branch, or tag) // - to – target ref (commit hash, branch, or tag); defaults to HEAD when omitted func HandleDiff(cfg config.AltGitConfiguration) gin.HandlerFunc { return func(c *gin.Context) { repoName, rp, ok := rendering.ResolveRepo(cfg, c.Param("repo")) if !ok { return } from := c.Query("from") to := c.Query("to") if from == "" { c.Redirect(http.StatusFound, "/"+repoName+"/") return } if to == "" { to = "HEAD" } shortFrom := from if len(shortFrom) > 8 { shortFrom = shortFrom[:8] } shortTo := to if len(shortTo) > 8 { shortTo = shortTo[:8] } pd := rendering.BasePageData(c, repoName, rp, "diff", template.HTML(fmt.Sprintf( `repos / %s`+ ` / diff %s → %s`, url.PathEscape(repoName), template.HTMLEscapeString(repoName), template.HTMLEscapeString(shortFrom), template.HTMLEscapeString(shortTo), ))) diff, cmd, err := gitrepo.DiffCommits(rp, from, to) var body strings.Builder if err != nil { body.WriteString(fmt.Sprintf( `
⚠ Could not compute diff %s → %s: %s
`, template.HTMLEscapeString(shortFrom), template.HTMLEscapeString(shortTo), template.HTMLEscapeString(err.Error()), )) } else if strings.TrimSpace(diff) == "" { body.WriteString(`
No differences between the two refs.
`) } else { body.WriteString(fmt.Sprintf( `
`+ `%s`+ `→`+ `%s`+ ``+ `
`, template.HTMLEscapeString(shortFrom), template.HTMLEscapeString(shortTo), )) body.WriteString(`
`) body.WriteString(renderDiff(diff)) body.WriteString(`
`) } pd.Title = fmt.Sprintf("diff %s → %s — %s", shortFrom, shortTo, repoName) pd.GitCmd = cmd pd.Body = template.HTML(body.String()) rendering.RenderPage(cfg, c, pd) } } // renderDiff converts a unified diff string into an HTML table with per-line // addition/deletion/context styling. Each file section gets its own block. func renderDiff(diff string) string { var out strings.Builder lines := strings.Split(diff, "\n") inFile := false for _, line := range lines { switch { case strings.HasPrefix(line, "diff --git"): if inFile { out.WriteString("") } inFile = true // Extract a tidy "a/foo → b/foo" label from the diff header. parts := strings.Fields(line) label := line if len(parts) >= 4 { label = strings.TrimPrefix(parts[2], "a/") + " → " + strings.TrimPrefix(parts[3], "b/") } out.WriteString(fmt.Sprintf( `
`+ `
%s
`+ ``, template.HTMLEscapeString(label), )) case strings.HasPrefix(line, "---") || strings.HasPrefix(line, "+++"): // Skip the file path lines; the header already shows the name. case strings.HasPrefix(line, "@@"): // Hunk header – show it spanning both columns. out.WriteString(fmt.Sprintf( ``, template.HTMLEscapeString(line), )) case strings.HasPrefix(line, "+"): out.WriteString(fmt.Sprintf( ``, template.HTMLEscapeString(line[1:]), )) case strings.HasPrefix(line, "-"): out.WriteString(fmt.Sprintf( ``, template.HTMLEscapeString(line[1:]), )) case line == "" || line == "\\ No newline at end of file": // Skip blank separator lines and git's "no newline" notice. case len(line) > 0 && line[0] == ' ': // True context line — strip the leading space sigil. out.WriteString(fmt.Sprintf( ``, template.HTMLEscapeString(line[1:]), )) default: // Git meta-lines (index, old/new mode, binary notice, etc.) — show verbatim. out.WriteString(fmt.Sprintf( ``, template.HTMLEscapeString(line), )) } } if inFile { out.WriteString("
%s
+%s
−%s
%s
%s
") } return out.String() }