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

Function executeRemoteRepoSync

pkg/cmd/repo/sync/sync.go:291–342  ·  view source on GitHub ↗

ExecuteRemoteRepoSync will take several steps to sync the source and destination repositories. First it will try to use the merge-upstream API endpoint. If this fails due to merge conflicts or unknown merge issues then it will fallback to using the low level git references API endpoint. The reason t

(client *api.Client, destRepo, srcRepo ghrepo.Interface, opts *SyncOptions)

Source from the content-addressed store, hash-verified

289// the git references API endpoint gives more detailed error responses as to why the sync failed.
290// Unless the --force flag is specified we will not perform non-fast-forward merges.
291func executeRemoteRepoSync(client *api.Client, destRepo, srcRepo ghrepo.Interface, opts *SyncOptions) (string, error) {
292 branchName := opts.Branch
293 if branchName == "" {
294 var err error
295 branchName, err = api.RepoDefaultBranch(client, destRepo)
296 if err != nil {
297 return "", err
298 }
299 }
300
301 var apiErr upstreamMergeErr
302 if baseBranch, err := triggerUpstreamMerge(client, destRepo, branchName); err == nil {
303 return baseBranch, nil
304 } else if !errors.As(err, &apiErr) {
305 return "", err
306 }
307
308 if srcRepo == nil {
309 var err error
310 srcRepo, err = api.RepoParent(client, destRepo)
311 if err != nil {
312 return "", err
313 }
314 if srcRepo == nil {
315 return "", fmt.Errorf("can't determine source repository for %s because repository is not fork", ghrepo.FullName(destRepo))
316 }
317 }
318
319 commit, err := latestCommit(client, srcRepo, branchName)
320 if err != nil {
321 return "", err
322 }
323
324 // Using string comparison is a brittle way to determine the error returned by the API
325 // endpoint but unfortunately the API returns 422 for many reasons so we must
326 // interpret the message provide better error messaging for our users.
327 err = syncFork(client, destRepo, branchName, commit.Object.SHA, opts.Force)
328 var httpErr api.HTTPError
329 if err != nil {
330 if errors.As(err, &httpErr) {
331 switch httpErr.Message {
332 case notFastForwardErrorMessage:
333 return "", divergingError
334 case branchDoesNotExistErrorMessage:
335 return "", fmt.Errorf("%s branch does not exist on %s repository", branchName, ghrepo.FullName(destRepo))
336 }
337 }
338 return "", err
339 }
340
341 return fmt.Sprintf("%s:%s", srcRepo.RepoOwner(), branchName), nil
342}

Callers 1

syncRemoteRepoFunction · 0.85

Calls 8

RepoDefaultBranchFunction · 0.92
RepoParentFunction · 0.92
FullNameFunction · 0.92
triggerUpstreamMergeFunction · 0.85
latestCommitFunction · 0.85
syncForkFunction · 0.85
ErrorfMethod · 0.65
RepoOwnerMethod · 0.65

Tested by

no test coverage detected