From f3921d4d63f97d1f1fb49a69ea85744bb7ef184b Mon Sep 17 00:00:00 2001 From: Liz Prucka Date: Mon, 5 Jun 2023 10:23:32 -0500 Subject: pkg/vcs: force git branch checkout We are currently seeing errors that there are "untracked working tree files which would be overrwritten by checkout". This error occurs when files of the same path differ between branches, regardless of whether the repository is clean or not. Forcing FETCH_HEAD checkout, then moving repair() to after checkout to clean repository to the current checkout. Additionally, added a second "force" to git clean to clean directories with '.git' subdirectories. --- pkg/vcs/git.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'pkg/vcs') diff --git a/pkg/vcs/git.go b/pkg/vcs/git.go index 4ed33972c..3d5b3ca2f 100644 --- a/pkg/vcs/git.go +++ b/pkg/vcs/git.go @@ -114,12 +114,18 @@ func (git *git) CheckoutBranch(repo, branch string) (*Commit, error) { if err != nil { return nil, err } - if _, err := git.git("checkout", "FETCH_HEAD"); err != nil { + if _, err := git.git("checkout", "FETCH_HEAD", "--force"); err != nil { return nil, err } if _, err := git.git("submodule", "update", "--init"); err != nil { return nil, err } + // If the branch checkout had to be "forced" the directory may + // contain remaining untracked files. + // Clean again to ensure the new branch is in a clean state. + if err := git.repair(); err != nil { + return nil, err + } return git.HeadCommit() } @@ -177,8 +183,8 @@ func (git *git) reset() error { return nil } git.git("reset", "--hard", "--recurse-submodules") - git.git("clean", "-fdx") - git.git("submodule", "foreach", "--recursive", "git", "clean", "-fdx") + git.git("clean", "-xfdf") + git.git("submodule", "foreach", "--recursive", "git", "clean", "-xfdf") git.git("bisect", "reset") _, err := git.git("reset", "--hard", "--recurse-submodules") return err -- cgit mrf-deployment