Repository: cli/cli
File: pkg/cmd/pr/create/create.go (Go)

Code before:
	gitClient := ctx.GitClient

	commits, err := gitClient.Commits(context.Background(), baseRef, headRef)
	if err != nil {
		return err
	}

	if useFirstCommit {
		state.Title = commits[len(commits)-1].Title
		state.Body = commits[len(commits)-1].Body
	} else {
		state.Title = humanize(headRef)
		var body strings.Builder
		for i := len(commits) - 1; i >= 0; i-- {
			fmt.Fprintf(&body, "- **%s**\n", commits[i].Title)
			if addBody {
				chunks := chunkString(commits[i].Body, 72)
				for idx, chunk := range chunks {
					if chunk[0] == ' ' {
						chunk = chunk[1:]
					}
					fmt.Fprintf(&body, "  %s", chunk)
					if idx < len(chunks)-1 {
						fmt.Fprintln(&body)
					}
				}

				if i > 0 {
					fmt.Fprintln(&body)
					fmt.Fprintln(&body)
				}
			}
		}
		state.Body = body.String()
	}

	return nil
}

func determineTrackingBranch(gitClient *git.Client, remotes ghContext.Remotes, headBranch string) *git.TrackingRef {
	refsForLookup := []string{"HEAD"}
	var trackingRefs []git.TrackingRef

	headBranchConfig := gitClient.ReadBranchConfig(context.Background(), headBranch)
	if headBranchConfig.RemoteName != "" {
		tr := git.TrackingRef{
			RemoteName: headBranchConfig.RemoteName,
			BranchName: strings.TrimPrefix(headBranchConfig.MergeRef, "refs/heads/"),
		}
		trackingRefs = append(trackingRefs, tr)
		refsForLookup = append(refsForLookup, tr.String())

Review comment (suggestion):
**What would happen if body was not chunked on 72 characters?**

Being the logic that adds body information to the PR body, this is indenting the commit body to ensure the output follows markdown formatting.  This chunking is effectively word wrapping the content before it is indented, correct?

Whether to word wrap this or not is a funny situation because this ideally should use the body as-is.  Instead of chunking, this could be simplified with something like:

```go
    var regexPattern = regexp.MustCompile(`(?m)^`)
    ...
    fmt.Fprintln(regexPattern.ReplaceAllString(commits[i].Body, "  "))
```

Code after:
		}
		trackingRefs = append(trackingRefs, tr)
		refsForLookup = append(refsForLookup, tr.String())
	}

	for _, remote := range remotes {
		tr := git.TrackingRef{
			RemoteName: remote.Name,
			BranchName: headBranch,
		}
		trackingRefs = append(trackingRefs, tr)
		refsForLookup = append(refsForLookup, tr.String())
	}

	resolvedRefs, _ := gitClient.ShowRefs(context.Background(), refsForLookup)
	if len(resolvedRefs) > 1 {
		for _, r := range resolvedRefs[1:] {
			if r.Hash != resolvedRefs[0].Hash {
				continue
			}
			for _, tr := range trackingRefs {
				if tr.String() != r.Name {
					continue
				}
				return &tr
			}
		}
	}

	return nil
}

func NewIssueState(ctx CreateContext, opts CreateOptions) (*shared.IssueMetadataState, error) {
	var milestoneTitles []string
	if opts.Milestone != "" {
		milestoneTitles = []string{opts.Milestone}
	}

	meReplacer := shared.NewMeReplacer(ctx.Client, ctx.BaseRepo.RepoHost())
	assignees, err := meReplacer.ReplaceSlice(opts.Assignees)
	if err != nil {
		return nil, err
	}

	state := &shared.IssueMetadataState{
		Type:       shared.PRMetadata,
		Reviewers:  opts.Reviewers,
		Assignees:  assignees,
		Labels:     opts.Labels,
		Projects:   opts.Projects,
		Milestones: milestoneTitles,