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

Function handlePush

pkg/cmd/pr/create/create.go:1243–1360  ·  view source on GitHub ↗
(opts CreateOptions, ctx CreateContext)

Source from the content-addressed store, hash-verified

1241}
1242
1243func handlePush(opts CreateOptions, ctx CreateContext) error {
1244 refs := ctx.PRRefs
1245 forkableRefs, requiresFork := refs.(forkableRefs)
1246 if requiresFork {
1247 opts.IO.StartProgressIndicator()
1248 forkedRepo, err := api.ForkRepo(ctx.Client, forkableRefs.BaseRepo(), "", "", true)
1249 opts.IO.StopProgressIndicator()
1250 if err != nil {
1251 return fmt.Errorf("error forking repo: %w", err)
1252 }
1253
1254 refs = pushableRefs{
1255 headRepo: forkedRepo,
1256 headBranchName: forkableRefs.qualifiedHeadRef.BranchName(),
1257 baseRepo: forkableRefs.baseRepo,
1258 baseBranchName: forkableRefs.baseBranchName,
1259 }
1260 }
1261
1262 // We may have upcast to pushableRefs on fork, or we may have been passed an instance
1263 // already. But if we haven't, then there's nothing more to do.
1264 pushableRefs, ok := refs.(pushableRefs)
1265 if !ok {
1266 return nil
1267 }
1268
1269 // There are two cases when an existing remote for the head repo will be
1270 // missing (and an error will be returned):
1271 // 1. the head repo was just created by auto-forking;
1272 // 2. an existing fork was discovered by querying the API.
1273 // In either case, we want to add the head repo as a new git remote so we
1274 // can push to it. We will try to add the head repo as the "origin" remote
1275 // and fallback to the "fork" remote if it is unavailable. Also, if the
1276 // base repo is the "origin" remote we will rename it "upstream".
1277 headRemote, _ := ctx.ResolvedRemotes.RemoteForRepo(pushableRefs.HeadRepo())
1278 if headRemote == nil {
1279 cfg, err := opts.Config()
1280 if err != nil {
1281 return err
1282 }
1283
1284 remotes, err := opts.Remotes()
1285 if err != nil {
1286 return err
1287 }
1288
1289 cloneProtocol := cfg.GitProtocol(pushableRefs.HeadRepo().RepoHost()).Value
1290 headRepoURL := ghrepo.FormatRemoteURL(pushableRefs.HeadRepo(), cloneProtocol)
1291 gitClient := ctx.GitClient
1292 origin, _ := remotes.FindByName("origin")
1293 upstreamName := "upstream"
1294 upstream, _ := remotes.FindByName(upstreamName)
1295 remoteName := "origin"
1296
1297 if origin != nil {
1298 remoteName = "fork"
1299 }
1300

Callers 1

createRunFunction · 0.85

Calls 15

FlushMethod · 0.95
ForkRepoFunction · 0.92
FormatRemoteURLFunction · 0.92
IsSameFunction · 0.92
FullNameFunction · 0.92
WithStderrFunction · 0.92
NewRegexpWriterFunction · 0.85
BranchNameMethod · 0.80
RemoteForRepoMethod · 0.80
HeadRepoMethod · 0.80
FindByNameMethod · 0.80
CommandMethod · 0.80

Tested by

no test coverage detected