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

Function executeRemoteRepoSync

pkg/cmd/repo/sync/sync.go:291–340  ·  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 if baseBranch, err := triggerUpstreamMerge(client, destRepo, branchName); err == nil {
302 return baseBranch, nil
303 } else if _, ok := errors.AsType[upstreamMergeErr](err); !ok {
304 return "", err
305 }
306
307 if srcRepo == nil {
308 var err error
309 srcRepo, err = api.RepoParent(client, destRepo)
310 if err != nil {
311 return "", err
312 }
313 if srcRepo == nil {
314 return "", fmt.Errorf("can't determine source repository for %s because repository is not fork", ghrepo.FullName(destRepo))
315 }
316 }
317
318 commit, err := latestCommit(client, srcRepo, branchName)
319 if err != nil {
320 return "", err
321 }
322
323 // Using string comparison is a brittle way to determine the error returned by the API
324 // endpoint but unfortunately the API returns 422 for many reasons so we must
325 // interpret the message provide better error messaging for our users.
326 err = syncFork(client, destRepo, branchName, commit.Object.SHA, opts.Force)
327 if err != nil {
328 if httpErr, ok := errors.AsType[api.HTTPError](err); ok {
329 switch httpErr.Message {
330 case notFastForwardErrorMessage:
331 return "", divergingError
332 case branchDoesNotExistErrorMessage:
333 return "", fmt.Errorf("%s branch does not exist on %s repository", branchName, ghrepo.FullName(destRepo))
334 }
335 }
336 return "", err
337 }
338
339 return fmt.Sprintf("%s:%s", srcRepo.RepoOwner(), branchName), nil
340}

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