func fixCommitMessage()

in git-codereview/hook.go [186:245]


func fixCommitMessage(msg []byte) []byte {
	data := append([]byte{}, msg...)
	data = stripComments(data)

	// Empty message not allowed.
	if len(bytes.TrimSpace(data)) == 0 {
		dief("empty commit message")
	}

	// Insert a blank line between first line and subsequent lines if not present.
	eol := bytes.IndexByte(data, '\n')
	if eol != -1 && len(data) > eol+1 && data[eol+1] != '\n' {
		data = append(data, 0)
		copy(data[eol+1:], data[eol:])
		data[eol+1] = '\n'
	}

	issueRepo := config()["issuerepo"]
	// Update issue references to point to issue repo, if set.
	if issueRepo != "" {
		data = issueRefRE.ReplaceAll(data, []byte("${space}"+issueRepo+"${ref}"))
	}
	// TestHookCommitMsgIssueRepoRewrite makes sure the regex is valid
	oldFixesRE := regexp.MustCompile(fmt.Sprintf(oldFixesRETemplate, regexp.QuoteMeta(issueRepo)))
	data = oldFixesRE.ReplaceAll(data, []byte("Fixes "+issueRepo+"#${issueNum}"))

	if haveGerrit() {
		// Complain if two Change-Ids are present.
		// This can happen during an interactive rebase;
		// it is easy to forget to remove one of them.
		nChangeId := bytes.Count(data, []byte("\nChange-Id: "))
		if nChangeId > 1 {
			dief("multiple Change-Id lines")
		}

		// Add Change-Id to commit message if not present.
		if nChangeId == 0 {
			data = bytes.TrimRight(data, "\n")
			sep := "\n\n"
			if endsWithMetadataLine(data) {
				sep = "\n"
			}
			data = append(data, fmt.Sprintf("%sChange-Id: I%x\n", sep, randomBytes())...)
		}

		// Add branch prefix to commit message if not present and on a
		// dev or release branch and not a special Git fixup! or
		// squash! commit message.
		b := CurrentBranch()
		branch := strings.TrimPrefix(b.OriginBranch(), "origin/")
		if strings.HasPrefix(branch, "dev.") || strings.HasPrefix(branch, "release-branch.") {
			prefix := "[" + branch + "] "
			if !bytes.HasPrefix(data, []byte(prefix)) && !isFixup(data) {
				data = []byte(prefix + string(data))
			}
		}
	}

	return data
}