diff options
author | Runxi Yu <me@runxiyu.org> | 2025-03-05 09:32:40 +0800 |
---|---|---|
committer | Runxi Yu <me@runxiyu.org> | 2025-03-05 09:47:01 +0800 |
commit | 134b84f2672a9fe3e2e8a92b712261b47c4bd022 (patch) | |
tree | 5d666a509384f210a78b0e691d8f531b71b04875 /http_handle_repo_commit.go | |
parent | *: Replace some := with var (diff) | |
download | forge-134b84f2672a9fe3e2e8a92b712261b47c4bd022.tar.gz forge-134b84f2672a9fe3e2e8a92b712261b47c4bd022.tar.zst forge-134b84f2672a9fe3e2e8a92b712261b47c4bd022.zip |
repo/*: Use var instead of :=
Diffstat (limited to 'http_handle_repo_commit.go')
-rw-r--r-- | http_handle_repo_commit.go | 48 |
1 files changed, 31 insertions, 17 deletions
diff --git a/http_handle_repo_commit.go b/http_handle_repo_commit.go index ba09d25..094a862 100644 --- a/http_handle_repo_commit.go +++ b/http_handle_repo_commit.go @@ -12,12 +12,13 @@ import ( "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/filemode" "github.com/go-git/go-git/v5/plumbing/format/diff" + "github.com/go-git/go-git/v5/plumbing/object" "go.lindenii.runxiyu.org/lindenii-common/misc" ) // The file patch type from go-git isn't really usable in HTML templates // either. -type usable_file_patch struct { +type usable_file_patch_t struct { From diff.File To diff.File Chunks []usable_chunk @@ -29,25 +30,33 @@ type usable_chunk struct { } func handle_repo_commit(w http.ResponseWriter, r *http.Request, params map[string]any) { - repo, commit_id_specified_string := params["repo"].(*git.Repository), params["commit_id"].(string) - - commit_id_specified_string_without_suffix := strings.TrimSuffix(commit_id_specified_string, ".patch") - commit_id := plumbing.NewHash(commit_id_specified_string_without_suffix) - commit_object, err := repo.CommitObject(commit_id) - if err != nil { + var repo *git.Repository + var commit_id_specified_string, commit_id_specified_string_without_suffix string + var commit_id plumbing.Hash + var parent_commit_hash plumbing.Hash + var commit_object *object.Commit + var commit_id_string string + var err error + var patch *object.Patch + + repo, commit_id_specified_string = params["repo"].(*git.Repository), params["commit_id"].(string) + + commit_id_specified_string_without_suffix = strings.TrimSuffix(commit_id_specified_string, ".patch") + commit_id = plumbing.NewHash(commit_id_specified_string_without_suffix) + if commit_object, err = repo.CommitObject(commit_id); err != nil { http.Error(w, "Error getting commit object: "+err.Error(), http.StatusInternalServerError) return } if commit_id_specified_string_without_suffix != commit_id_specified_string { - patch, err := format_patch_from_commit(commit_object) - if err != nil { + var formatted_patch string + if formatted_patch, err = format_patch_from_commit(commit_object); err != nil { http.Error(w, "Error formatting patch: "+err.Error(), http.StatusInternalServerError) return } - fmt.Fprintln(w, patch) + fmt.Fprintln(w, formatted_patch) return } - commit_id_string := commit_object.Hash.String() + commit_id_string = commit_object.Hash.String() if commit_id_string != commit_id_specified_string { http.Redirect(w, r, commit_id_string, http.StatusSeeOther) @@ -57,7 +66,7 @@ func handle_repo_commit(w http.ResponseWriter, r *http.Request, params map[strin params["commit_object"] = commit_object params["commit_id"] = commit_id_string - parent_commit_hash, patch, err := get_patch_from_commit(commit_object) + parent_commit_hash, patch, err = get_patch_from_commit(commit_object) if err != nil { http.Error(w, "Error getting patch from commit: "+err.Error(), http.StatusInternalServerError) return @@ -97,18 +106,23 @@ var fake_diff_file_null = fake_diff_file{ func make_usable_file_patches(patch diff.Patch) (usable_file_patches []usable_file_patch) { // TODO: Remove unnecessary context // TODO: Prepend "+"/"-"/" " instead of solely distinguishing based on color - usable_file_patches = make([]usable_file_patch, 0) + for _, file_patch := range patch.FilePatches() { - from, to := file_patch.Files() + var from, to diff.File + var usable_file_patch usable_file_patch_t + chunks := []usable_chunk{} + + from, to = file_patch.Files() if from == nil { from = fake_diff_file_null } if to == nil { to = fake_diff_file_null } - chunks := []usable_chunk{} for _, chunk := range file_patch.Chunks() { - content := chunk.Content() + var content string + + content = chunk.Content() if len(content) > 0 && content[0] == '\n' { content = "\n" + content } // Horrible hack to fix how browsers newlines that immediately proceed <pre> @@ -117,7 +131,7 @@ func make_usable_file_patches(patch diff.Patch) (usable_file_patches []usable_fi Content: content, }) } - usable_file_patch := usable_file_patch{ + usable_file_patch = usable_file_patch_t{ Chunks: chunks, From: from, To: to, |