summaryrefslogtreecommitdiffstats
path: root/modules/git
diff options
context:
space:
mode:
authorJason Song <i@wolfogre.com>2022-12-28 18:03:21 +0800
committerGitHub <noreply@github.com>2022-12-28 11:03:21 +0100
commite9bc2c77c35d078c5ba5e6107c3551f31410c936 (patch)
tree8c0b771a9efe60101fd5520db7403ed9c59e2f31 /modules/git
parent9b4da56963692819d236b07504e565d4b4fb4021 (diff)
downloadgitea-e9bc2c77c35d078c5ba5e6107c3551f31410c936.tar.gz
gitea-e9bc2c77c35d078c5ba5e6107c3551f31410c936.zip
Use complete SHA to create and query commit status (#22244) (#22257)
Backport #22244. Fix #13485. Co-authored-by: delvh <dev.lh@web.de> Co-authored-by: Lauris BH <lauris@nix.lv> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: delvh <dev.lh@web.de> Co-authored-by: Lauris BH <lauris@nix.lv> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Diffstat (limited to 'modules/git')
-rw-r--r--modules/git/repo_commit_gogit.go2
-rw-r--r--modules/git/repo_commit_nogogit.go2
-rw-r--r--modules/git/repo_index.go2
-rw-r--r--modules/git/repo_tree_gogit.go2
-rw-r--r--modules/git/repo_tree_nogogit.go2
-rw-r--r--modules/git/sha1.go5
6 files changed, 9 insertions, 6 deletions
diff --git a/modules/git/repo_commit_gogit.go b/modules/git/repo_commit_gogit.go
index 14fec3f9c6..7a869b38b6 100644
--- a/modules/git/repo_commit_gogit.go
+++ b/modules/git/repo_commit_gogit.go
@@ -42,7 +42,7 @@ func (repo *Repository) RemoveReference(name string) error {
// ConvertToSHA1 returns a Hash object from a potential ID string
func (repo *Repository) ConvertToSHA1(commitID string) (SHA1, error) {
- if len(commitID) == 40 {
+ if len(commitID) == SHAFullLength {
sha1, err := NewIDFromString(commitID)
if err == nil {
return sha1, nil
diff --git a/modules/git/repo_commit_nogogit.go b/modules/git/repo_commit_nogogit.go
index 13a7be778f..2d91ee0955 100644
--- a/modules/git/repo_commit_nogogit.go
+++ b/modules/git/repo_commit_nogogit.go
@@ -138,7 +138,7 @@ func (repo *Repository) getCommitFromBatchReader(rd *bufio.Reader, id SHA1) (*Co
// ConvertToSHA1 returns a Hash object from a potential ID string
func (repo *Repository) ConvertToSHA1(commitID string) (SHA1, error) {
- if len(commitID) == 40 && IsValidSHAPattern(commitID) {
+ if len(commitID) == SHAFullLength && IsValidSHAPattern(commitID) {
sha1, err := NewIDFromString(commitID)
if err == nil {
return sha1, nil
diff --git a/modules/git/repo_index.go b/modules/git/repo_index.go
index 5542883288..3ff761d930 100644
--- a/modules/git/repo_index.go
+++ b/modules/git/repo_index.go
@@ -17,7 +17,7 @@ import (
// ReadTreeToIndex reads a treeish to the index
func (repo *Repository) ReadTreeToIndex(treeish string, indexFilename ...string) error {
- if len(treeish) != 40 {
+ if len(treeish) != SHAFullLength {
res, _, err := NewCommand(repo.Ctx, "rev-parse", "--verify").AddDynamicArguments(treeish).RunStdString(&RunOpts{Dir: repo.Path})
if err != nil {
return err
diff --git a/modules/git/repo_tree_gogit.go b/modules/git/repo_tree_gogit.go
index e720164936..9676bceebe 100644
--- a/modules/git/repo_tree_gogit.go
+++ b/modules/git/repo_tree_gogit.go
@@ -20,7 +20,7 @@ func (repo *Repository) getTree(id SHA1) (*Tree, error) {
// GetTree find the tree object in the repository.
func (repo *Repository) GetTree(idStr string) (*Tree, error) {
- if len(idStr) != 40 {
+ if len(idStr) != SHAFullLength {
res, _, err := NewCommand(repo.Ctx, "rev-parse", "--verify").AddDynamicArguments(idStr).RunStdString(&RunOpts{Dir: repo.Path})
if err != nil {
return nil, err
diff --git a/modules/git/repo_tree_nogogit.go b/modules/git/repo_tree_nogogit.go
index dc4a5becb9..6dea6cf026 100644
--- a/modules/git/repo_tree_nogogit.go
+++ b/modules/git/repo_tree_nogogit.go
@@ -67,7 +67,7 @@ func (repo *Repository) getTree(id SHA1) (*Tree, error) {
// GetTree find the tree object in the repository.
func (repo *Repository) GetTree(idStr string) (*Tree, error) {
- if len(idStr) != 40 {
+ if len(idStr) != SHAFullLength {
res, err := repo.GetRefCommitID(idStr)
if err != nil {
return nil, err
diff --git a/modules/git/sha1.go b/modules/git/sha1.go
index 15f282c6e4..7c777c5e47 100644
--- a/modules/git/sha1.go
+++ b/modules/git/sha1.go
@@ -18,6 +18,9 @@ const EmptySHA = "0000000000000000000000000000000000000000"
// EmptyTreeSHA is the SHA of an empty tree
const EmptyTreeSHA = "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
+// SHAFullLength is the full length of a git SHA
+const SHAFullLength = 40
+
// SHAPattern can be used to determine if a string is an valid sha
var shaPattern = regexp.MustCompile(`^[0-9a-f]{4,40}$`)
@@ -51,7 +54,7 @@ func MustIDFromString(s string) SHA1 {
func NewIDFromString(s string) (SHA1, error) {
var id SHA1
s = strings.TrimSpace(s)
- if len(s) != 40 {
+ if len(s) != SHAFullLength {
return id, fmt.Errorf("Length must be 40: %s", s)
}
b, err := hex.DecodeString(s)