mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 16:40:24 +08:00 
			
		
		
		
	Do not assume all 40 char strings are SHA1s (#14624)
GetCommit() assumes that all 40 char strings are SHA1s. This leads to an error if you try to do a PR on a branch which is 40 characters long. This PR attempts the SHA first - and if it fails will switch to using rev-parse. Fix #14470 Signed-off-by: Andrew Thornton <art27@cantab.net>
This commit is contained in:
		@@ -31,8 +31,13 @@ func (repo *Repository) GetTagCommitID(name string) (string, error) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// ConvertToSHA1 returns a Hash object from a potential ID string
 | 
					// ConvertToSHA1 returns a Hash object from a potential ID string
 | 
				
			||||||
func (repo *Repository) ConvertToSHA1(commitID string) (SHA1, error) {
 | 
					func (repo *Repository) ConvertToSHA1(commitID string) (SHA1, error) {
 | 
				
			||||||
	if len(commitID) != 40 {
 | 
						if len(commitID) == 40 {
 | 
				
			||||||
		var err error
 | 
							sha1, err := NewIDFromString(commitID)
 | 
				
			||||||
 | 
							if err == nil {
 | 
				
			||||||
 | 
								return sha1, nil
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	actualCommitID, err := NewCommand("rev-parse", "--verify", commitID).RunInDir(repo.Path)
 | 
						actualCommitID, err := NewCommand("rev-parse", "--verify", commitID).RunInDir(repo.Path)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		if strings.Contains(err.Error(), "unknown revision or path") ||
 | 
							if strings.Contains(err.Error(), "unknown revision or path") ||
 | 
				
			||||||
@@ -41,9 +46,8 @@ func (repo *Repository) ConvertToSHA1(commitID string) (SHA1, error) {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
		return SHA1{}, err
 | 
							return SHA1{}, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
		commitID = actualCommitID
 | 
					
 | 
				
			||||||
	}
 | 
						return NewIDFromString(actualCommitID)
 | 
				
			||||||
	return NewIDFromString(commitID)
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// GetCommit returns commit object of by ID string.
 | 
					// GetCommit returns commit object of by ID string.
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user