Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Allow changing refs for remote #363

Merged
merged 3 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## master (unreleased)

- fix: Allow changing refs for remote ([PR #363](https://github.com/evilmartians/lefthook/pull/363) by @mrexox)

## 1.2.0 (2022-11-7)

- fix: Full support for interactive commands and scripts ([PR #352](https://github.com/evilmartians/lefthook/pull/352) by @mrexox)
Expand Down
1 change: 1 addition & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ remote:
ref: v1.0.0
```

> :warning: Please, note that if you initially had `ref` option, ran `lefthook install`, and then removed it, lefthook won't decide which branch/tag to use as a ref. So, if you added it once, please, use it always to avoid issues in local setups.

### `config`

Expand Down
24 changes: 17 additions & 7 deletions internal/git/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,24 @@ func (r *Repository) SyncRemote(url, ref string) error {
func (r *Repository) updateRemote(path, ref string) error {
log.Debugf("Updating remote config repository: %s", path)

cmdFetch := []string{"git", "-C", path, "pull", "--quiet"}
if len(ref) == 0 {
cmdFetch = append(cmdFetch, "origin", ref)
}
if len(ref) != 0 {
cmdFetch := []string{"git", "-C", path, "fetch", "--quiet", "--depth", "1", "origin", ref}
_, err := execGit(strings.Join(cmdFetch, " "))
if err != nil {
return err
}

_, err := execGit(strings.Join(cmdFetch, " "))
if err != nil {
return err
cmdFetch = []string{"git", "-C", path, "checkout", "FETCH_HEAD"}
_, err = execGit(strings.Join(cmdFetch, " "))
if err != nil {
return err
}
} else {
cmdFetch := []string{"git", "-C", path, "pull", "--quiet"}
_, err := execGit(strings.Join(cmdFetch, " "))
if err != nil {
return err
}
}

return nil
Expand Down