Skip to content

Commit 91fae3e

Browse files
committed
add ResetHard
1 parent e6102a9 commit 91fae3e

3 files changed

Lines changed: 15 additions & 3 deletions

File tree

internal/gitsemver/gitter.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ type Gitter interface {
3939
GetBuild(repo string) (string, error)
4040
// GetHead returns the current HEAD commit hash if skip is false.
4141
GetHead(repo string, skip bool) (head string, err error)
42+
// ResetHard hard-resets the repository to the given commit. Does nothing if commit is empty.
43+
ResetHard(repo, commit string) (err error)
4244
// FetchTags calls "git fetch --tags". Uses the "--unshallow" option if needed.
4345
FetchTags(repo string) error
4446
// CreateTag creates a new lightweight tag. Does nothing if tag is empty.
@@ -353,6 +355,13 @@ func (dg DefaultGitter) GetHead(repo string, skip bool) (head string, err error)
353355
return
354356
}
355357

358+
func (dg DefaultGitter) ResetHard(repo, commit string) (err error) {
359+
if commit != "" {
360+
_, err = dg.Exec("-C", repo, "reset", "--hard", commit)
361+
}
362+
return
363+
}
364+
356365
func (dg DefaultGitter) FetchTags(repo string) (err error) {
357366
var b []byte
358367
if b, err = dg.Exec("-C", repo, "rev-parse", "--is-shallow-repository"); err == nil {

internal/gitsemver/mockgitter_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ func (mg *MockGitter) GetHead(repo string, skip bool) (head string, err error) {
164164
return
165165
}
166166

167+
func (mg *MockGitter) ResetHard(repo, commit string) (err error) {
168+
return nil
169+
}
170+
167171
func (mg *MockGitter) FetchTags(repo string) error {
168172
return nil
169173
}

main.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,8 @@ func mainfn() int {
173173
}
174174
}
175175
// revert the commit
176-
if preRunHead != afterHead {
177-
_, resetErr := vs.Git.Exec("-C", repoDir, "reset", "--hard", preRunHead)
178-
err = errors.Join(err, resetErr)
176+
if outpath != "" && preRunHead != afterHead {
177+
err = errors.Join(err, vs.Git.ResetHard(repoDir, preRunHead))
179178
}
180179
}
181180
}

0 commit comments

Comments
 (0)