Skip to content

Commit 05bcab5

Browse files
committed
Update
1 parent ebb349a commit 05bcab5

2 files changed

Lines changed: 32 additions & 35 deletions

File tree

‎entity/change/git/change_id.go‎

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,38 +32,35 @@ const scheme = "git"
3232
const refPrefix = "refs/"
3333

3434
// changeIDFormat is the expected format for change IDs, included in error messages.
35-
const changeIDFormat = "git://{host}/{repo-path}/{ref}/{commit_sha}"
35+
const changeIDFormat = "git://{remote}/{repo}/{ref}/{commit_sha}"
3636

3737
// shaLength is the length of a git commit SHA.
3838
const shaLength = 40
3939

4040
// ChangeID represents a parsed git:// change identifier.
41-
// Format: git://{host}/{repo-path}/{ref}/{commit_sha}
41+
// Format: git://{remote}/{repo}/{ref}/{commit_sha}
4242
//
4343
// Ref is a fully-qualified, percent-encoded git ref so that branches, tags, and
4444
// ref names containing slashes all fit a single path segment unambiguously.
4545
type ChangeID struct {
4646
// Scheme captures the URI scheme (always "git" in current implementation).
4747
Scheme string
48-
// Host is the git remote authority, "host" or "host:port" (e.g.
49-
// "git.example.com" or "git.example.com:9418"). Unlike a forge "org", git
50-
// addresses a repository by the remote it lives on.
51-
Host string
52-
// RepoPath is the path to the repository on the remote and may contain
53-
// slashes (e.g. "uber/monorepo" or "team/group/repo.git").
54-
RepoPath string
48+
// Remote is the host (or host:port) of the git remote the repository lives
49+
// on (e.g. "git.example.com" or "git.example.com:9418").
50+
Remote string
51+
// Repo is the path to the repository on the remote and may contain slashes
52+
// (e.g. "uber/monorepo" or "team/group/repo.git").
53+
Repo string
5554
// Ref is the fully-qualified git ref the change landed on, decoded from the
5655
// URI (e.g. "refs/heads/main", "refs/tags/v1.0").
5756
Ref string
58-
// CommitSHA is the commit Ref pointed at, captured at a point in time. It is
59-
// not necessarily a branch head; it records the commit Ref resolved to when
60-
// the change entered the pipeline.
57+
// CommitSHA is a commit that ref has pointed to at some point in time.
6158
CommitSHA string
6259
}
6360

6461
// ParseChangeID parses a raw change ID string into a ChangeID.
65-
// Expected format: git://{host}/{repo-path}/{commit_sha} with a fully-qualified,
66-
// percent-encoded ref as the second-to-last path segment.
62+
// Expected format: git://{remote}/{repo}/{ref}/{commit_sha}, where {ref} is a
63+
// fully-qualified, percent-encoded git ref (e.g. "refs%2Fheads%2Fmain").
6764
func ParseChangeID(raw string) (ChangeID, error) {
6865
u, err := url.Parse(raw)
6966
if err != nil {
@@ -73,20 +70,20 @@ func ParseChangeID(raw string) (ChangeID, error) {
7370
return ChangeID{}, fmt.Errorf("invalid change ID %q: scheme must be %q, got %q (expected format: %s)", raw, scheme, u.Scheme, changeIDFormat)
7471
}
7572
if u.Host == "" {
76-
return ChangeID{}, fmt.Errorf("invalid change ID %q: missing remote host (expected format: %s)", raw, changeIDFormat)
73+
return ChangeID{}, fmt.Errorf("invalid change ID %q: missing remote (expected format: %s)", raw, changeIDFormat)
7774
}
7875

7976
// Split on the escaped path so the percent-encoded ref stays a single
8077
// segment (url.URL.Path decodes %2F to "/", which would split it apart).
8178
segments := strings.Split(strings.TrimPrefix(u.EscapedPath(), "/"), "/")
82-
// Need at least 3 segments: {repo-path}/{ref}/{commit_sha}.
79+
// Need at least 3 segments: {repo}/{ref}/{commit_sha}.
8380
if len(segments) < 3 {
84-
return ChangeID{}, fmt.Errorf("invalid change ID %q: need at least repo-path/ref/sha, got %d path segments (expected format: %s)", raw, len(segments), changeIDFormat)
81+
return ChangeID{}, fmt.Errorf("invalid change ID %q: need at least repo/ref/sha, got %d path segments (expected format: %s)", raw, len(segments), changeIDFormat)
8582
}
8683

8784
sha := segments[len(segments)-1]
8885
encodedRef := segments[len(segments)-2]
89-
repoPath := strings.Join(segments[:len(segments)-2], "/")
86+
repo := strings.Join(segments[:len(segments)-2], "/")
9087

9188
if sha == "" {
9289
return ChangeID{}, fmt.Errorf("invalid change ID %q: empty commit SHA (expected format: %s)", raw, changeIDFormat)
@@ -103,20 +100,20 @@ func ParseChangeID(raw string) (ChangeID, error) {
103100
return ChangeID{}, fmt.Errorf("invalid change ID %q: ref %q must be a fully-qualified git ref (e.g. refs/heads/main, refs/tags/v1.0) (expected format: %s)", raw, ref, changeIDFormat)
104101
}
105102

106-
if repoPath == "" {
107-
return ChangeID{}, fmt.Errorf("invalid change ID %q: empty repo path (expected format: %s)", raw, changeIDFormat)
103+
if repo == "" {
104+
return ChangeID{}, fmt.Errorf("invalid change ID %q: empty repo (expected format: %s)", raw, changeIDFormat)
108105
}
109106

110107
return ChangeID{
111108
Scheme: u.Scheme,
112-
Host: u.Host,
113-
RepoPath: repoPath,
109+
Remote: u.Host,
110+
Repo: repo,
114111
Ref: ref,
115112
CommitSHA: sha,
116113
}, nil
117114
}
118115

119116
// String returns the string representation of the change ID.
120117
func (c ChangeID) String() string {
121-
return fmt.Sprintf("%s://%s/%s/%s/%s", c.Scheme, c.Host, c.RepoPath, url.PathEscape(c.Ref), c.CommitSHA)
118+
return fmt.Sprintf("%s://%s/%s/%s/%s", c.Scheme, c.Remote, c.Repo, url.PathEscape(c.Ref), c.CommitSHA)
122119
}

‎entity/change/git/change_id_test.go‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ func TestParseChangeID(t *testing.T) {
3535
raw: "git://git.example.com/uber/monorepo/refs%2Fheads%2Fmain/" + sha,
3636
want: ChangeID{
3737
Scheme: "git",
38-
Host: "git.example.com",
39-
RepoPath: "uber/monorepo",
38+
Remote: "git.example.com",
39+
Repo: "uber/monorepo",
4040
Ref: "refs/heads/main",
4141
CommitSHA: sha,
4242
},
@@ -46,8 +46,8 @@ func TestParseChangeID(t *testing.T) {
4646
raw: "git://git.example.com:9418/uber/monorepo/refs%2Fheads%2Fmain/" + sha,
4747
want: ChangeID{
4848
Scheme: "git",
49-
Host: "git.example.com:9418",
50-
RepoPath: "uber/monorepo",
49+
Remote: "git.example.com:9418",
50+
Repo: "uber/monorepo",
5151
Ref: "refs/heads/main",
5252
CommitSHA: sha,
5353
},
@@ -57,8 +57,8 @@ func TestParseChangeID(t *testing.T) {
5757
raw: "git://git.example.com/monorepo/refs%2Fheads%2Fmain/" + sha,
5858
want: ChangeID{
5959
Scheme: "git",
60-
Host: "git.example.com",
61-
RepoPath: "monorepo",
60+
Remote: "git.example.com",
61+
Repo: "monorepo",
6262
Ref: "refs/heads/main",
6363
CommitSHA: sha,
6464
},
@@ -68,8 +68,8 @@ func TestParseChangeID(t *testing.T) {
6868
raw: "git://git.example.com/uber/monorepo/refs%2Fheads%2Ffeature%2Fx/" + sha,
6969
want: ChangeID{
7070
Scheme: "git",
71-
Host: "git.example.com",
72-
RepoPath: "uber/monorepo",
71+
Remote: "git.example.com",
72+
Repo: "uber/monorepo",
7373
Ref: "refs/heads/feature/x",
7474
CommitSHA: sha,
7575
},
@@ -79,8 +79,8 @@ func TestParseChangeID(t *testing.T) {
7979
raw: "git://git.example.com/uber/monorepo/refs%2Ftags%2Fv1.0/" + sha,
8080
want: ChangeID{
8181
Scheme: "git",
82-
Host: "git.example.com",
83-
RepoPath: "uber/monorepo",
82+
Remote: "git.example.com",
83+
Repo: "uber/monorepo",
8484
Ref: "refs/tags/v1.0",
8585
CommitSHA: sha,
8686
},
@@ -90,8 +90,8 @@ func TestParseChangeID(t *testing.T) {
9090
raw: "git://git.example.com/uber/deepteam/monorepo/refs%2Fheads%2Fmain/" + sha,
9191
want: ChangeID{
9292
Scheme: "git",
93-
Host: "git.example.com",
94-
RepoPath: "uber/deepteam/monorepo",
93+
Remote: "git.example.com",
94+
Repo: "uber/deepteam/monorepo",
9595
Ref: "refs/heads/main",
9696
CommitSHA: sha,
9797
},

0 commit comments

Comments
 (0)