From 1df3884b55dc3b8d045e9322225c102b551a2a51 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Tue, 11 Feb 2025 23:35:53 +0800 Subject: [PATCH] repo_commit: Don't crash on null from/to files --- go.mod | 2 +- go.sum | 2 ++ handle_repo_commit.go | 28 ++++++++++++++++++++++++++++ diff --git a/go.mod b/go.mod index 558d57ea54f50800afc73e98bde7918f2dd02d01..e428ed482f5443e2c6107b4bf83ae76c5260652a 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ github.com/alecthomas/chroma/v2 v2.15.0 github.com/go-git/go-git/v5 v5.13.2 github.com/microcosm-cc/bluemonday v1.0.27 github.com/yuin/goldmark v1.7.8 - go.lindenii.runxiyu.org/lindenii-common v0.0.0-20250211092902-f64ead6a659e + go.lindenii.runxiyu.org/lindenii-common v0.0.0-20250211153243-8946fae17bd0 ) require ( diff --git a/go.sum b/go.sum index 33449a944cb0c049a5771fd4d2b8da829354a8f7..21235e3eb81a577e76a2b3a9e824fd6bdec50da6 100644 --- a/go.sum +++ b/go.sum @@ -87,6 +87,8 @@ github.com/yuin/goldmark v1.7.8 h1:iERMLn0/QJeHFhxSt3p6PeN9mGnvIKSpG9YYorDMnic= github.com/yuin/goldmark v1.7.8/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E= go.lindenii.runxiyu.org/lindenii-common v0.0.0-20250211092902-f64ead6a659e h1:Gb58k5z3NjOWdYMBvZaTLG4IWY6HcCVkwPz/J0lFKT0= go.lindenii.runxiyu.org/lindenii-common v0.0.0-20250211092902-f64ead6a659e/go.mod h1:bOxuuGXA3UpbLb1lKohr2j2MVcGGLcqfAprGx9VCkMA= +go.lindenii.runxiyu.org/lindenii-common v0.0.0-20250211153243-8946fae17bd0 h1:NxXd9AOAMNKm2+WThVWqd41IqAKC3bKBvZMIUqAdT40= +go.lindenii.runxiyu.org/lindenii-common v0.0.0-20250211153243-8946fae17bd0/go.mod h1:bOxuuGXA3UpbLb1lKohr2j2MVcGGLcqfAprGx9VCkMA= golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.33.0 h1:IOBPskki6Lysi0lo9qQvbxiQ+FvsCC/YWOecCHAixus= golang.org/x/crypto v0.33.0/go.mod h1:bVdXmD7IV/4GdElGPozy6U7lWdRXA4qyRVGJV57uQ5M= diff --git a/handle_repo_commit.go b/handle_repo_commit.go index 56103e7e3b8654720a5a8f35064e4b4909b204e8..d45fd1d5981cdb716fa00c9eae2da6beaa7c1aa4 100644 --- a/handle_repo_commit.go +++ b/handle_repo_commit.go @@ -5,7 +5,9 @@ "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" + "go.lindenii.runxiyu.org/lindenii-common/misc" ) type usable_file_patch struct { @@ -67,6 +69,12 @@ // TODO: Remove unnecessary context usable_file_patches := make([]usable_file_patch, 0) for _, file_patch := range patch.FilePatches() { from, to := file_patch.Files() + if from == nil { + from = fake_diff_file_null + } + if to == nil { + to = fake_diff_file_null + } usable_file_patch := usable_file_patch{ Chunks: file_patch.Chunks(), From: from, @@ -82,3 +90,23 @@ _, _ = w.Write([]byte("Error rendering template: " + err.Error())) return } } + +type fake_diff_file struct { + hash plumbing.Hash + mode filemode.FileMode + path string +} +func (f fake_diff_file) Hash() plumbing.Hash { + return f.hash +} +func (f fake_diff_file) Mode() filemode.FileMode { + return f.mode +} +func (f fake_diff_file) Path() string { + return f.path +} +var fake_diff_file_null = fake_diff_file{ + hash: plumbing.NewHash("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"), + mode: misc.First_or_panic(filemode.New("100644")), + path: "NULL", +} -- 2.48.1