Skip to content

Commit fe17fee

Browse files
committed
fix: run fetch before checkout
Sometimes the local clone is out of date and needs to be updated with fetch before doing any other operations, e.g. checkout, pull, etc. Repos can get in this state if the default branch was changed after being cloned
1 parent 0171c3d commit fe17fee

1 file changed

Lines changed: 28 additions & 5 deletions

File tree

cmd/github-org-sync.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ func (gh *GhOrgSync) fetchRepos(ctx context.Context) []*github.Repository {
6363
return repos
6464
}
6565

66-
func repoExistLocal(repo *github.Repository) bool {
67-
repoGitDir := fmt.Sprintf("%s/%s/.git", destPath, repo.GetName())
66+
func repoExistLocal(destPath string, repo *github.Repository) bool {
67+
repoGitDir := fmt.Sprintf("%s%s/.git", destPath, repo.GetName())
6868
if _, err := os.Stat(repoGitDir); err != nil {
6969
return false
7070
}
@@ -104,6 +104,13 @@ func gitDirtyBranch(repoDir string) bool {
104104
return err != nil
105105
}
106106

107+
func gitRepoEmpty(repoDir string) bool {
108+
gitRevParseCmd := strings.Fields(fmt.Sprintf("git -C %s rev-parse --verify HEAD", repoDir))
109+
cmd := exec.Command(gitRevParseCmd[0], gitRevParseCmd[1:]...)
110+
err := cmd.Run()
111+
return err != nil
112+
}
113+
107114
func (gh *GhOrgSync) updateLocalRepo(sem chan struct{}, repo *github.Repository) {
108115
defer gh.wg.Done()
109116

@@ -113,6 +120,10 @@ func (gh *GhOrgSync) updateLocalRepo(sem chan struct{}, repo *github.Repository)
113120
}()
114121

115122
repoPath := fmt.Sprintf("%s%s", gh.destPath, repo.GetName())
123+
if gitRepoEmpty(repoPath) {
124+
fmt.Printf("INFO: %s is empty, skipping update\n", repoPath)
125+
return
126+
}
116127
if gitDirtyBranch(fmt.Sprintf("%s%s", gh.destPath, repo.GetName())) {
117128
fmt.Printf("INFO: %s is dirty, so stashing first\n", repoPath)
118129
gitStashCmd := strings.Fields(fmt.Sprintf("git -C %s stash push", repoPath))
@@ -124,10 +135,22 @@ func (gh *GhOrgSync) updateLocalRepo(sem chan struct{}, repo *github.Repository)
124135
}
125136
}
126137

138+
gitFetchCmd := strings.Fields(fmt.Sprintf("git -C %s fetch origin", repoPath))
139+
cmd := exec.Command(gitFetchCmd[0], gitFetchCmd[1:]...)
140+
err := cmd.Run()
141+
if err != nil {
142+
fmt.Printf("ERROR: Failed to fetch origin for repo %s: %s\n", repo.GetName(), err)
143+
return
144+
}
145+
146+
if repo.DefaultBranch == nil {
147+
fmt.Printf("INFO: Repo %s has no default branch, skipping update\n", repo.GetName())
148+
return
149+
}
127150
defaultBranch := repo.DefaultBranch
128151
gitCheckoutCmd := strings.Fields(fmt.Sprintf("git -C %s checkout %s", repoPath, *defaultBranch))
129-
cmd := exec.Command(gitCheckoutCmd[0], gitCheckoutCmd[1:]...)
130-
err := cmd.Run()
152+
cmd = exec.Command(gitCheckoutCmd[0], gitCheckoutCmd[1:]...)
153+
err = cmd.Run()
131154
if err != nil {
132155
fmt.Printf("ERROR: Failed to checkout default branch %s for repo %s: %s\n", *defaultBranch, repo.GetName(), err)
133156
return
@@ -169,7 +192,7 @@ func main(args []string) {
169192
continue
170193
} else {
171194
gh.wg.Add(1)
172-
if repoExistLocal(repo) {
195+
if repoExistLocal(gh.destPath, repo) {
173196
go gh.updateLocalRepo(sem, repo)
174197
} else {
175198
go gh.cloneRepo(sem, repo)

0 commit comments

Comments
 (0)