diff options
Diffstat (limited to '')
-rw-r--r-- | git_format_patch.go | 13 | ||||
-rw-r--r-- | git_misc.go | 30 | ||||
-rw-r--r-- | handle_repo_commit.go | 29 |
3 files changed, 34 insertions, 38 deletions
diff --git a/git_format_patch.go b/git_format_patch.go index 1f282fa..4f37e92 100644 --- a/git_format_patch.go +++ b/git_format_patch.go @@ -2,7 +2,6 @@ package main import ( "bytes" - "errors" "fmt" "strings" "time" @@ -11,18 +10,10 @@ import ( "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) + _, patch, err := get_patch_from_commit(commit) if err != nil { - return "", misc.Wrap_one_error(err_get_patch, err) + return "", misc.Wrap_one_error(err_getting_patch_of_commit, err) } var buf bytes.Buffer diff --git a/git_misc.go b/git_misc.go index 851fb68..c6ec9f9 100644 --- a/git_misc.go +++ b/git_misc.go @@ -13,6 +13,9 @@ import ( ) var err_unsafe_path = errors.New("Unsafe path") +var err_getting_commit_tree = errors.New("Error getting commit tree") +var err_getting_patch_of_commit = errors.New("Error getting patch of commit") +var err_getting_parent_commit_object = errors.New("Error getting parent commit object") func open_git_repo(group_name, repo_name string) (*git.Repository, error) { group_name, group_name_ok := misc.Sanitize_path(group_name) @@ -82,3 +85,30 @@ func get_recent_commits(repo *git.Repository, head_hash plumbing.Hash, number_of } return recent_commits, err } + +func get_patch_from_commit(commit_object *object.Commit) (parent_commit_hash plumbing.Hash, patch *object.Patch, ret_err error) { + parent_commit_object, err := commit_object.Parent(0) + if errors.Is(err, object.ErrParentNotFound) { + commit_tree, err := commit_object.Tree() + if err != nil { + ret_err = misc.Wrap_one_error(err_getting_commit_tree, err) + return + } + patch, err = (&object.Tree{}).Patch(commit_tree) + if err != nil { + ret_err = misc.Wrap_one_error(err_getting_patch_of_commit, err) + return + } + } else if err != nil { + ret_err = misc.Wrap_one_error(err_getting_parent_commit_object, err) + return + } else { + parent_commit_hash = parent_commit_object.Hash + patch, err = parent_commit_object.Patch(commit_object) + if err != nil { + ret_err = misc.Wrap_one_error(err_getting_patch_of_commit, err) + return + } + } + return +} diff --git a/handle_repo_commit.go b/handle_repo_commit.go index 03a7cd4..aeea380 100644 --- a/handle_repo_commit.go +++ b/handle_repo_commit.go @@ -1,14 +1,12 @@ package main import ( - "errors" "net/http" "strings" "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" ) @@ -53,31 +51,8 @@ func handle_repo_commit(w http.ResponseWriter, r *http.Request) { data["commit_object"] = commit_object data["commit_id"] = commit_id_string - var patch *object.Patch - parent_commit_object, err := commit_object.Parent(0) - if errors.Is(err, object.ErrParentNotFound) { - commit_tree, err := commit_object.Tree() - if err != nil { - _, _ = w.Write([]byte("Error getting commit tree (for comparing against an empty tree): " + err.Error())) - return - } - patch, err = (&object.Tree{}).Patch(commit_tree) - if err != nil { - _, _ = w.Write([]byte("Error getting patch of commit: " + err.Error())) - return - } - } else if err != nil { - _, _ = w.Write([]byte("Error getting parent commit object: " + err.Error())) - return - } else { - data["parent_commit_hash"] = parent_commit_object.Hash.String() - - patch, err = parent_commit_object.Patch(commit_object) - if err != nil { - _, _ = w.Write([]byte("Error getting patch of commit: " + err.Error())) - return - } - } + parent_commit_hash, patch, err := get_patch_from_commit(commit_object) + data["parent_commit_hash"] = parent_commit_hash.String() data["patch"] = patch // TODO: Remove unnecessary context |