Make API "compare" accept commit IDs (#32801)
This commit is contained in:
parent
01b1896bf5
commit
22bf2ca6ba
10 changed files with 154 additions and 124 deletions
|
@ -474,3 +474,17 @@ func (c *Commit) GetRepositoryDefaultPublicGPGKey(forceUpdate bool) (*GPGSetting
|
|||
}
|
||||
return c.repo.GetDefaultPublicGPGKey(forceUpdate)
|
||||
}
|
||||
|
||||
func IsStringLikelyCommitID(objFmt ObjectFormat, s string, minLength ...int) bool {
|
||||
minLen := util.OptionalArg(minLength, objFmt.FullLength())
|
||||
if len(s) < minLen || len(s) > objFmt.FullLength() {
|
||||
return false
|
||||
}
|
||||
for _, c := range s {
|
||||
isHex := (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')
|
||||
if !isHex {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
|
|
@ -142,7 +142,6 @@ func (ref RefName) RemoteName() string {
|
|||
|
||||
// ShortName returns the short name of the reference name
|
||||
func (ref RefName) ShortName() string {
|
||||
refName := string(ref)
|
||||
if ref.IsBranch() {
|
||||
return ref.BranchName()
|
||||
}
|
||||
|
@ -158,8 +157,7 @@ func (ref RefName) ShortName() string {
|
|||
if ref.IsFor() {
|
||||
return ref.ForBranchName()
|
||||
}
|
||||
|
||||
return refName
|
||||
return string(ref) // usually it is a commit ID
|
||||
}
|
||||
|
||||
// RefGroup returns the group type of the reference
|
||||
|
|
|
@ -61,3 +61,31 @@ func parseTags(refs []string) []string {
|
|||
}
|
||||
return results
|
||||
}
|
||||
|
||||
// UnstableGuessRefByShortName does the best guess to see whether a "short name" provided by user is a branch, tag or commit.
|
||||
// It could guess wrongly if the input is already ambiguous. For example:
|
||||
// * "refs/heads/the-name" vs "refs/heads/refs/heads/the-name"
|
||||
// * "refs/tags/1234567890" vs commit "1234567890"
|
||||
// In most cases, it SHOULD AVOID using this function, unless there is an irresistible reason (eg: make API friendly to end users)
|
||||
// If the function is used, the caller SHOULD CHECK the ref type carefully.
|
||||
func (repo *Repository) UnstableGuessRefByShortName(shortName string) RefName {
|
||||
if repo.IsBranchExist(shortName) {
|
||||
return RefNameFromBranch(shortName)
|
||||
}
|
||||
if repo.IsTagExist(shortName) {
|
||||
return RefNameFromTag(shortName)
|
||||
}
|
||||
if strings.HasPrefix(shortName, "refs/") {
|
||||
if repo.IsReferenceExist(shortName) {
|
||||
return RefName(shortName)
|
||||
}
|
||||
}
|
||||
commit, err := repo.GetCommit(shortName)
|
||||
if err == nil {
|
||||
commitIDString := commit.ID.String()
|
||||
if strings.HasPrefix(commitIDString, shortName) {
|
||||
return RefName(commitIDString)
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue