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

Function handlePush

pkg/cmd/pr/create/create.go:1243–1362  ·  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 baseRefs: baseRefs{
1258 baseRepo: forkableRefs.baseRepo,
1259 baseBranchName: forkableRefs.baseBranchName,
1260 },
1261 }
1262 }
1263
1264 // We may have upcast to pushableRefs on fork, or we may have been passed an instance
1265 // already. But if we haven't, then there's nothing more to do.
1266 pushableRefs, ok := refs.(pushableRefs)
1267 if !ok {
1268 return nil
1269 }
1270
1271 // There are two cases when an existing remote for the head repo will be
1272 // missing (and an error will be returned):
1273 // 1. the head repo was just created by auto-forking;
1274 // 2. an existing fork was discovered by querying the API.
1275 // In either case, we want to add the head repo as a new git remote so we
1276 // can push to it. We will try to add the head repo as the "origin" remote
1277 // and fallback to the "fork" remote if it is unavailable. Also, if the
1278 // base repo is the "origin" remote we will rename it "upstream".
1279 headRemote, _ := ctx.ResolvedRemotes.RemoteForRepo(pushableRefs.HeadRepo())
1280 if headRemote == nil {
1281 cfg, err := opts.Config()
1282 if err != nil {
1283 return err
1284 }
1285
1286 remotes, err := opts.Remotes()
1287 if err != nil {
1288 return err
1289 }
1290
1291 cloneProtocol := cfg.GitProtocol(pushableRefs.HeadRepo().RepoHost()).Value
1292 headRepoURL := ghrepo.FormatRemoteURL(pushableRefs.HeadRepo(), cloneProtocol)
1293 gitClient := ctx.GitClient
1294 origin, _ := remotes.FindByName("origin")
1295 upstreamName := "upstream"
1296 upstream, _ := remotes.FindByName(upstreamName)
1297 remoteName := "origin"
1298
1299 if origin != nil {
1300 remoteName = "fork"

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