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

Function handlePush

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

Source from the content-addressed store, hash-verified

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

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