Skip to content

Commit

Permalink
Clean untracked files while updating index (kubernetes-sigs#367)
Browse files Browse the repository at this point in the history
* Clean untracked while updating index

* Separate clean from update for clarity

* Merge update and clean
  • Loading branch information
ferhatelmas authored and k8s-ci-robot committed Nov 12, 2019
1 parent 21220fb commit 560be99
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions pkg/gitutil/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,28 @@ func IsGitCloned(gitPath string) (bool, error) {
return err == nil && f.IsDir(), err
}

// update will fetch origin and set HEAD to origin/HEAD.
func update(destinationPath string) error {
// update will fetch origin and set HEAD to origin/HEAD
// and also will create a pristine working directory by removing
// untracked files and directories.
func updateAndCleanUntracked(destinationPath string) error {
if err := exec(destinationPath, "fetch", "-v"); err != nil {
return errors.Wrapf(err, "fetch index at %q failed", destinationPath)
}

err := exec(destinationPath, "reset", "--hard", "@{upstream}")
return errors.Wrapf(err, "reset index at %q failed", destinationPath)
if err := exec(destinationPath, "reset", "--hard", "@{upstream}"); err != nil {
return errors.Wrapf(err, "reset index at %q failed", destinationPath)
}

err := exec(destinationPath, "clean", "-xfd")
return errors.Wrapf(err, "clean index at %q failed", destinationPath)
}

// EnsureUpdated will ensure the destination path exists and is up to date.
func EnsureUpdated(uri, destinationPath string) error {
if err := EnsureCloned(uri, destinationPath); err != nil {
return err
}
return update(destinationPath)
return updateAndCleanUntracked(destinationPath)
}

func exec(pwd string, args ...string) error {
Expand Down

0 comments on commit 560be99

Please sign in to comment.