| 1 |
// Copyright 2020 Google LLC |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package git |
| 16 |
|
| 17 |
import ( |
| 18 |
"fmt" |
| 19 |
"log" |
| 20 |
"os" |
| 21 |
"strings" |
| 22 |
|
| 23 |
"cloud.google.com/go/internal/gapicgen/execv" |
| 24 |
"github.com/go-git/go-git/v5" |
| 25 |
) |
| 26 |
|
| 27 |
const ( |
| 28 |
maxTruncatedTitleLen = 22 |
| 29 |
) |
| 30 |
|
| 31 |
// ChangeInfo represents a change and its associated metadata. |
| 32 |
type ChangeInfo struct { |
| 33 |
Body string |
| 34 |
Title string |
| 35 |
GoogleapisHash string |
| 36 |
} |
| 37 |
|
| 38 |
// FormatChanges turns a slice of changes into formatted string that will match |
| 39 |
// the conventional commit footer pattern. This will allow these changes to be |
| 40 |
// parsed into the changelog. |
| 41 |
func FormatChanges(changes []*ChangeInfo, onlyGapicChanges bool) string { |
| 42 |
formatted := truncateAndFormatChanges(changes, onlyGapicChanges, false) |
| 43 |
if len(formatted) > maxChangesLen { |
| 44 |
// Retry formatting by truncating |
| 45 |
return truncateAndFormatChanges(changes, onlyGapicChanges, true) |
| 46 |
} |
| 47 |
return formatted |
| 48 |
} |
| 49 |
|
| 50 |
func truncateAndFormatChanges(changes []*ChangeInfo, onlyGapicChanges, truncate bool) string { |
| 51 |
if len(changes) == 0 { |
| 52 |
return "" |
| 53 |
} |
| 54 |
var sb strings.Builder |
| 55 |
sb.WriteString("\nChanges:\n\n") |
| 56 |
for _, c := range changes { |
| 57 |
if onlyGapicChanges { |
| 58 |
continue |
| 59 |
} |
| 60 |
title := c.Title |
| 61 |
if truncate && len(title) > maxTruncatedTitleLen { |
| 62 |
title = fmt.Sprintf("%v...", title[:maxTruncatedTitleLen]) |
| 63 |
} |
| 64 |
sb.WriteString(fmt.Sprintf("%s\n", title)) |
| 65 |
|
| 66 |
// Format the commit body to conventional commit footer standards. |
| 67 |
splitBody := strings.Split(c.Body, "\n") |
| 68 |
for i := range splitBody { |
| 69 |
splitBody[i] = fmt.Sprintf(" %s", splitBody[i]) |
| 70 |
} |
| 71 |
body := strings.Join(splitBody, "\n") |
| 72 |
|
| 73 |
if truncate { |
| 74 |
startBody := strings.Index(body, "PiperOrigin-RevId") |
| 75 |
if startBody != -1 { |
| 76 |
body = fmt.Sprintf(" %s", body[startBody:]) |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
sb.WriteString(fmt.Sprintf("%s\n\n", body)) |
| 81 |
} |
| 82 |
// If the buffer is empty except for the "Changes:" text return an empty |
| 83 |
// string. |
| 84 |
if sb.Len() == 11 { |
| 85 |
return "" |
| 86 |
} |
| 87 |
return sb.String() |
| 88 |
} |
| 89 |
|
| 90 |
// ParseChangeInfo gets the ChangeInfo for a given googleapis hash. |
| 91 |
func ParseChangeInfo(googleapisDir string, hashes []string) ([]*ChangeInfo, error) { |
| 92 |
var changes []*ChangeInfo |
| 93 |
for _, hash := range hashes { |
| 94 |
// Get commit title and body |
| 95 |
c := execv.Command("git", "show", "--pretty=format:%s~~%b", "-s", hash) |
| 96 |
c.Dir = googleapisDir |
| 97 |
b, err := c.Output() |
| 98 |
if err != nil { |
| 99 |
return nil, err |
| 100 |
} |
| 101 |
|
| 102 |
ss := strings.Split(string(b), "~~") |
| 103 |
if len(ss) != 2 { |
| 104 |
return nil, fmt.Errorf("expected two segments for commit, got %d: %s", len(ss), b) |
| 105 |
} |
| 106 |
title, body := strings.TrimSpace(ss[0]), strings.TrimSpace(ss[1]) |
| 107 |
|
| 108 |
// Add link so corresponding googleapis commit. |
| 109 |
body = fmt.Sprintf("%s\nSource-Link: https://github.com/googleapis/googleapis/commit/%s", body, hash) |
| 110 |
|
| 111 |
changes = append(changes, &ChangeInfo{ |
| 112 |
Title: title, |
| 113 |
Body: body, |
| 114 |
GoogleapisHash: hash, |
| 115 |
}) |
| 116 |
} |
| 117 |
return changes, nil |
| 118 |
} |
| 119 |
|
| 120 |
// parseConventionalCommitPkg parses the package context for conventional commit |
| 121 |
// messages. |
| 122 |
func parseConventionalCommitPkg(importPath string) string { |
| 123 |
s := strings.TrimPrefix(importPath, "cloud.google.com/go/") |
| 124 |
ss := strings.Split(s, "/") |
| 125 |
// remove the version, i.e /apiv1 |
| 126 |
return strings.Join(ss[:len(ss)-1], "/") |
| 127 |
} |
| 128 |
|
| 129 |
// CommitsSinceHash gathers all of the commits since the provided hash for the |
| 130 |
// given gitDir. The inclusive parameter tells if the provided hash should also |
| 131 |
// be returned in the slice. |
| 132 |
func CommitsSinceHash(gitDir, hash string, inclusive bool) ([]string, error) { |
| 133 |
var commitRange string |
| 134 |
if inclusive { |
| 135 |
commitRange = fmt.Sprintf("%s^..", hash) |
| 136 |
} else { |
| 137 |
commitRange = fmt.Sprintf("%s..", hash) |
| 138 |
} |
| 139 |
|
| 140 |
c := execv.Command("git", "rev-list", commitRange) |
| 141 |
c.Dir = gitDir |
| 142 |
b, err := c.Output() |
| 143 |
if err != nil { |
| 144 |
return nil, err |
| 145 |
} |
| 146 |
return strings.Split(strings.TrimSpace(string(b)), "\n"), nil |
| 147 |
} |
| 148 |
|
| 149 |
// UpdateFilesSinceHash returns a listed of files updated since the provided |
| 150 |
// hash for the given gitDir. |
| 151 |
func UpdateFilesSinceHash(gitDir, hash string) ([]string, error) { |
| 152 |
// The provided diff-filter flags restricts to files that have been: |
| 153 |
// - (A) Added |
| 154 |
// - (C) Copied |
| 155 |
// - (M) Modified |
| 156 |
// - (R) Renamed |
| 157 |
c := execv.Command("git", "diff-tree", "--no-commit-id", "--name-only", "--diff-filter=ACMR", "-r", fmt.Sprintf("%s..HEAD", hash)) |
| 158 |
c.Dir = gitDir |
| 159 |
b, err := c.Output() |
| 160 |
if err != nil { |
| 161 |
return nil, err |
| 162 |
} |
| 163 |
return strings.Split(string(b), "\n"), nil |
| 164 |
} |
| 165 |
|
| 166 |
// HasChanges reports whether the given directory has uncommitted git changes. |
| 167 |
func HasChanges(dir string) (bool, error) { |
| 168 |
c := execv.Command("bash", "-c", "git status --short") |
| 169 |
c.Dir = dir |
| 170 |
b, err := c.Output() |
| 171 |
return len(b) > 0, err |
| 172 |
} |
| 173 |
|
| 174 |
// DeepClone clones a repository in the given directory. |
| 175 |
func DeepClone(repo, dir string) error { |
| 176 |
log.Printf("cloning %s\n", repo) |
| 177 |
|
| 178 |
_, err := git.PlainClone(dir, false, &git.CloneOptions{ |
| 179 |
URL: repo, |
| 180 |
Progress: os.Stdout, |
| 181 |
}) |
| 182 |
return err |
| 183 |
} |
| 184 |
|
| 185 |
// filesChanged returns a list of files changed in a commit for the provdied |
| 186 |
// hash in the given gitDir. |
| 187 |
func filesChanged(gitDir, hash string) ([]string, error) { |
| 188 |
c := execv.Command("git", "show", "--pretty=format:", "--name-only", hash) |
| 189 |
c.Dir = gitDir |
| 190 |
b, err := c.Output() |
| 191 |
if err != nil { |
| 192 |
return nil, err |
| 193 |
} |
| 194 |
return strings.Split(string(b), "\n"), nil |
| 195 |
} |