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

Function handlePush

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

Source from the content-addressed store, hash-verified

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