MCPcopy Create free account
hub / github.com/cli/cli / executeLocalRepoSync

Function executeLocalRepoSync

pkg/cmd/repo/sync/sync.go:221–282  ·  view source on GitHub ↗
(srcRepo ghrepo.Interface, remote string, opts *SyncOptions)

Source from the content-addressed store, hash-verified

219var divergingError = errors.New("diverging changes")
220
221func executeLocalRepoSync(srcRepo ghrepo.Interface, remote string, opts *SyncOptions) error {
222 git := opts.Git
223 branch := opts.Branch
224 useForce := opts.Force
225
226 hasLocalBranch := git.HasLocalBranch(branch)
227 if hasLocalBranch {
228 fastForward, err := git.IsAncestor(branch, "FETCH_HEAD")
229 if err != nil {
230 return err
231 }
232 if !fastForward && !useForce {
233 return divergingError
234 }
235 if fastForward && useForce {
236 useForce = false
237 }
238 }
239
240 currentBranch, err := git.CurrentBranch()
241 if err != nil && !errors.Is(err, gitpkg.ErrNotOnAnyBranch) {
242 return err
243 }
244 if currentBranch == branch {
245 if isDirty, err := git.IsDirty(); err == nil && isDirty {
246 return fmt.Errorf("refusing to sync due to uncommitted/untracked local changes\ntip: use `git stash --all` before retrying the sync and run `git stash pop` afterwards")
247 } else if err != nil {
248 return err
249 }
250 if useForce {
251 if err := git.ResetHard("FETCH_HEAD"); err != nil {
252 return err
253 }
254 } else {
255 if err := git.MergeFastForward("FETCH_HEAD"); err != nil {
256 return err
257 }
258 }
259 } else {
260 if hasLocalBranch {
261 if err := git.UpdateBranch(branch, "FETCH_HEAD"); err != nil {
262 worktrees, worktreeErr := git.Worktrees()
263 if worktreeErr != nil {
264 return fmt.Errorf("cannot update branch %q: %w; also cannot retrieve worktrees: %w", branch, err, worktreeErr)
265 }
266 if worktree := gitpkg.WorktreeForBranch(worktrees, branch); worktree != nil {
267 if worktree.Prunable {
268 return fmt.Errorf("can't sync %q because stale worktree metadata still associates it with %s\ntip: run `git worktree prune` and retry", branch, worktree.Path)
269 }
270 return fmt.Errorf("can't sync %q because it's checked out in another worktree at %s\ntip: run `gh repo sync` from that worktree instead", branch, worktree.Path)
271 }
272 return err
273 }
274 } else {
275 if err := git.CreateBranch(branch, "FETCH_HEAD", fmt.Sprintf("%s/%s", remote, branch)); err != nil {
276 return err
277 }
278 }

Callers 2

syncLocalRepoFunction · 0.85

Calls 10

HasLocalBranchMethod · 0.65
IsAncestorMethod · 0.65
CurrentBranchMethod · 0.65
IsDirtyMethod · 0.65
ErrorfMethod · 0.65
ResetHardMethod · 0.65
MergeFastForwardMethod · 0.65
UpdateBranchMethod · 0.65
WorktreesMethod · 0.65
CreateBranchMethod · 0.65

Tested by 1