diff options
-rw-r--r-- | git_format_patch.go | 59 | ||||
-rw-r--r-- | handle_repo_commit.go | 18 | ||||
-rw-r--r-- | main.go | 2 | ||||
-rw-r--r-- | templates/repo_commit.html.tmpl | 4 |
4 files changed, 80 insertions, 3 deletions
diff --git a/git_format_patch.go b/git_format_patch.go new file mode 100644 index 0000000..c899ed5 --- /dev/null +++ b/git_format_patch.go @@ -0,0 +1,59 @@ +package main + +import ( + "bytes" + "errors" + "fmt" + "strings" + "time" + + "github.com/go-git/go-git/v5/plumbing/object" + "go.lindenii.runxiyu.org/lindenii-common/misc" +) + +var err_get_patch = errors.New("Failed to get patch from commit") + +func format_patch_from_commit(commit *object.Commit) (string, error) { + parent, err := commit.Parent(0) + if err != nil { + return "", err + } + + var patch *object.Patch + patch, err = parent.Patch(commit) + if err != nil { + return "", misc.Wrap_one_error(err_get_patch, err) + } + + var buf bytes.Buffer + + author := commit.Author + date := author.When.Format(time.RFC1123Z) + + commit_msg_title, commit_msg_details, _ := strings.Cut(commit.Message, "\n") + + fmt.Fprintf(&buf, "From %s Mon Sep 17 00:00:00 2001\n", commit.Hash) + fmt.Fprintf(&buf, "From: %s <%s>\n", author.Name, author.Email) + fmt.Fprintf(&buf, "Date: %s\n", date) + fmt.Fprintf(&buf, "Subject: [PATCH] %s\n\n", commit_msg_title) + + if commit_msg_details != "" { + fmt.Println("fdsafsad") + commit_msg_details_first_line, commit_msg_details_rest, _ := strings.Cut(commit_msg_details, "\n") + if strings.TrimSpace(commit_msg_details_first_line) == "" { + commit_msg_details = commit_msg_details_rest + } + buf.WriteString(commit_msg_details) + buf.WriteString("\n") + } + buf.WriteString("---\n") + fmt.Fprint(&buf, patch.Stats().String()) + fmt.Fprintln(&buf) + + buf.WriteString(patch.String()) + + fmt.Fprintf(&buf, "\n-- \n2.48.1\n") + + return buf.String(), nil +} + diff --git a/handle_repo_commit.go b/handle_repo_commit.go index 4660ff3..9e10a9f 100644 --- a/handle_repo_commit.go +++ b/handle_repo_commit.go @@ -2,6 +2,7 @@ package main import ( "net/http" + "strings" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/format/diff" @@ -17,19 +18,32 @@ func handle_repo_commit(w http.ResponseWriter, r *http.Request) { data := make(map[string]any) // TODO: Sanitize path values group_name, repo_name, commit_id_string := r.PathValue("group_name"), r.PathValue("repo_name"), r.PathValue("commit_id") - data["group_name"], data["repo_name"], data["commit_id"] = group_name, repo_name, commit_id_string + data["group_name"], data["repo_name"] = group_name, repo_name repo, err := open_git_repo(group_name, repo_name) if err != nil { _, _ = w.Write([]byte("Error opening repo: " + err.Error())) return } - commit_id := plumbing.NewHash(commit_id_string) + commit_id_string_real := strings.TrimSuffix(commit_id_string, ".patch") + commit_id := plumbing.NewHash(commit_id_string_real) commit_object, err := repo.CommitObject(commit_id) if err != nil { _, _ = w.Write([]byte("Error getting commit object: " + err.Error())) return } + + if commit_id_string_real != commit_id_string { + patch, err := format_patch_from_commit(commit_object) + if err != nil { + _, _ = w.Write([]byte("Error formatting patch: " + err.Error())) + return + } + _, _ = w.Write([]byte(patch)) + return + } + data["commit_object"] = commit_object + data["commit_id"] = commit_object.Hash.String() parent_commit_object, err := commit_object.Parent(0) if err != nil { @@ -39,7 +39,7 @@ func main() { http.HandleFunc("/g/{group_name}/repos/{repo_name}/tree/{ref}/{rest...}", handle_repo_tree) http.HandleFunc("/g/{group_name}/repos/{repo_name}/raw/{ref}/{rest...}", handle_repo_raw) http.HandleFunc("/g/{group_name}/repos/{repo_name}/log/{ref}/", handle_repo_log) - http.HandleFunc("/g/{group_name}/repos/{repo_name}/commit/{commit_id}/", handle_repo_commit) + http.HandleFunc("/g/{group_name}/repos/{repo_name}/commit/{commit_id}", handle_repo_commit) listener, err := net.Listen(config.HTTP.Net, config.HTTP.Addr) if err != nil { diff --git a/templates/repo_commit.html.tmpl b/templates/repo_commit.html.tmpl index 5b6cb84..a288949 100644 --- a/templates/repo_commit.html.tmpl +++ b/templates/repo_commit.html.tmpl @@ -38,6 +38,10 @@ <th scope="row">Message</th> <td><pre>{{ .commit_object.Message }}</pre></td> </tr> + <tr> + <th scope="row">Actions</th> + <td><pre><a href="{{ .commit_object.Hash }}.patch">Get as Patch</a></pre></td> + </tr> </tbody> </table> </div> |