Skip to content

Commit 44e6741

Browse files
committed
Fix go-git making
Signed-off-by: Andrew Thornton <[email protected]>
1 parent 729b735 commit 44e6741

File tree

3 files changed

+47
-26
lines changed

3 files changed

+47
-26
lines changed

modules/git/repo_commit.go

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,6 @@ func (repo *Repository) GetTagCommitID(name string) (string, error) {
2424
return repo.GetRefCommitID(TagPrefix + name)
2525
}
2626

27-
// ConvertToSHA1 returns a Hash object from a potential ID string
28-
func (repo *Repository) ConvertToSHA1(commitID string) (SHA1, error) {
29-
if len(commitID) == 40 {
30-
sha1, err := NewIDFromString(commitID)
31-
if err == nil {
32-
return sha1, nil
33-
}
34-
}
35-
36-
wr, rd, cancel := repo.CatFileBatchCheck()
37-
defer cancel()
38-
_, err := wr.Write([]byte(commitID + "\n"))
39-
if err != nil {
40-
return SHA1{}, err
41-
}
42-
sha, _, _, err := ReadBatchLine(rd)
43-
if err != nil {
44-
if IsErrNotExist(err) {
45-
return SHA1{}, ErrNotExist{commitID, ""}
46-
}
47-
return SHA1{}, err
48-
}
49-
50-
return MustID(sha), nil
51-
}
52-
5327
// GetCommit returns commit object of by ID string.
5428
func (repo *Repository) GetCommit(commitID string) (*Commit, error) {
5529
id, err := repo.ConvertToSHA1(commitID)

modules/git/repo_commit_gogit.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,27 @@ func (repo *Repository) GetRefCommitID(name string) (string, error) {
3030
return ref.Hash().String(), nil
3131
}
3232

33+
// ConvertToSHA1 returns a Hash object from a potential ID string
34+
func (repo *Repository) ConvertToSHA1(commitID string) (SHA1, error) {
35+
if len(commitID) == 40 {
36+
sha1, err := NewIDFromString(commitID)
37+
if err == nil {
38+
return sha1, nil
39+
}
40+
}
41+
42+
actualCommitID, err := NewCommand("rev-parse", "--verify", commitID).RunInDir(repo.Path)
43+
if err != nil {
44+
if strings.Contains(err.Error(), "unknown revision or path") ||
45+
strings.Contains(err.Error(), "fatal: Needed a single revision") {
46+
return SHA1{}, ErrNotExist{commitID, ""}
47+
}
48+
return SHA1{}, err
49+
}
50+
51+
return NewIDFromString(actualCommitID)
52+
}
53+
3354
// IsCommitExist returns true if given commit exists in current repository.
3455
func (repo *Repository) IsCommitExist(name string) bool {
3556
hash := plumbing.NewHash(name)

modules/git/repo_commit_nogogit.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,29 @@ func (repo *Repository) getCommit(id SHA1) (*Commit, error) {
115115
}
116116
}
117117
}
118+
119+
// ConvertToSHA1 returns a Hash object from a potential ID string
120+
func (repo *Repository) ConvertToSHA1(commitID string) (SHA1, error) {
121+
if len(commitID) == 40 {
122+
sha1, err := NewIDFromString(commitID)
123+
if err == nil {
124+
return sha1, nil
125+
}
126+
}
127+
128+
wr, rd, cancel := repo.CatFileBatchCheck()
129+
defer cancel()
130+
_, err := wr.Write([]byte(commitID + "\n"))
131+
if err != nil {
132+
return SHA1{}, err
133+
}
134+
sha, _, _, err := ReadBatchLine(rd)
135+
if err != nil {
136+
if IsErrNotExist(err) {
137+
return SHA1{}, ErrNotExist{commitID, ""}
138+
}
139+
return SHA1{}, err
140+
}
141+
142+
return MustID(sha), nil
143+
}

0 commit comments

Comments
 (0)