Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions pkg/controller/git/bare_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,11 @@ func CloneBare(
}
b := &bareRepo{
baseRepo: &baseRepo{
creds: clientOpts.Credentials,
dir: filepath.Join(homeDir, "repo"),
homeDir: homeDir,
url: repoURL,
creds: clientOpts.Credentials,
dir: filepath.Join(homeDir, "repo"),
homeDir: homeDir,
internalURL: repoURL,
externalURL: repoURL,
},
}
if err = b.setupClient(homeDir, clientOpts); err != nil {
Expand All @@ -117,10 +118,10 @@ func CloneBare(
}

func (b *bareRepo) clone() error {
cmd := b.buildGitCommand("clone", "--bare", b.url, b.dir)
cmd := b.buildGitCommand("clone", "--bare", b.internalURL, b.dir)
cmd.Dir = b.homeDir // Override the cmd.Dir that's set by r.buildGitCommand()
if _, err := libExec.Exec(cmd); err != nil {
return fmt.Errorf("error cloning repo %q into %q: %w", b.url, b.dir, err)
return fmt.Errorf("error cloning repo %q into %q: %w", b.externalURL, b.dir, err)
}
return nil
}
Expand Down Expand Up @@ -192,10 +193,11 @@ func (b *bareRepo) AddWorkTree(path string, opts *AddWorkTreeOptions) (WorkTree,
}
return &workTree{
baseRepo: &baseRepo{
creds: b.creds,
dir: path,
homeDir: b.homeDir,
url: b.url,
creds: b.creds,
dir: path,
homeDir: b.homeDir,
internalURL: b.internalURL,
externalURL: b.externalURL,
},
bareRepo: b,
}, nil
Expand Down Expand Up @@ -242,10 +244,11 @@ func (b *bareRepo) WorkTrees() ([]WorkTree, error) {
for i, workTreePath := range workTreePaths {
workTrees[i] = &workTree{
baseRepo: &baseRepo{
creds: b.creds,
dir: workTreePath,
homeDir: b.homeDir,
url: b.url,
creds: b.creds,
dir: workTreePath,
homeDir: b.homeDir,
internalURL: b.internalURL,
externalURL: b.externalURL,
},
bareRepo: b,
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/git/bare_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestBareRepo(t *testing.T) {

t.Run("can clone", func(t *testing.T) {
var repoURL *url.URL
repoURL, err = url.Parse(r.url)
repoURL, err = url.Parse(r.internalURL)
require.NoError(t, err)
repoURL.User = nil
require.Equal(t, testRepoURL, repoURL.String())
Expand All @@ -96,7 +96,7 @@ func TestBareRepo(t *testing.T) {
})

t.Run("can get the repo url", func(t *testing.T) {
require.Equal(t, r.url, r.URL())
require.Equal(t, r.externalURL, r.URL())
})

t.Run("can get the home dir", func(t *testing.T) {
Expand Down
36 changes: 27 additions & 9 deletions pkg/controller/git/base_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ type baseRepo struct {
creds *RepoCredentials
dir string
homeDir string
url string
// Store the URL two ways. One is for internal use and may contain username@,
// while the other lacks that and is suitable for use in error messages and
// consumption by external callers of the URL() method.
internalURL string
externalURL string
Comment on lines +32 to +33
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find the internalURL and externalURL naming quite confusing. Would something like url (or operationalURL) and canonicalURL maybe be better?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that you've pointed it out, I agree the names aren't intuitive. You'd need to read the comments to understand. "Canonical" doesn't work because it's too similar to "normalized," and there's no real giant this is normalized. I'll noodle on this a bit. I agree there's a better naming scheme somewhere here that I've overlooked.

}

// ClientOptions represents options for a repository-specific Git client.
Expand Down Expand Up @@ -218,14 +222,15 @@ func (b *baseRepo) setupAuth(homeDir string) error {
return nil
}

lowerURL := strings.ToLower(b.url)
// Add username@ to the URL that's used internally...
lowerURL := strings.ToLower(b.internalURL)
if strings.HasPrefix(lowerURL, "http://") || strings.HasPrefix(lowerURL, "https://") {
u, err := url.Parse(b.url)
u, err := url.Parse(b.internalURL)
if err != nil {
return fmt.Errorf("error parsing URL %q: %w", b.url, err)
return fmt.Errorf("error parsing URL %q: %w", b.internalURL, err)
}
u.User = url.User(b.creds.Username)
b.url = u.String()
b.internalURL = u.String()
}

return nil
Expand Down Expand Up @@ -273,7 +278,20 @@ func (b *baseRepo) loadURL() error {
if err != nil {
return fmt.Errorf(`error getting URL of remote "origin": %w`, err)
}
b.url = strings.TrimSpace(string(res))
b.internalURL = strings.TrimSpace(string(res))
b.externalURL = b.internalURL
lowerURL := strings.ToLower(b.internalURL)
if strings.HasPrefix(lowerURL, "http://") || strings.HasPrefix(lowerURL, "https://") {
// This URL very likely contains username@ and we'd like to hang on to a
// representation of the URL that lacks that and is suitable for use in
// error messages and consumption by external callers of the URL() method.
u, err := url.Parse(b.externalURL)
if err != nil {
return fmt.Errorf("error parsing URL %q: %w", b.externalURL, err)
}
u.User = nil
b.externalURL = u.String()
}
return nil
}

Expand Down Expand Up @@ -324,7 +342,7 @@ func (b *baseRepo) RemoteBranchExists(branch string) (bool, error) {
"ls-remote",
"--heads",
"--exit-code", // Return 2 if not found
b.url,
b.internalURL,
branch,
))
var exitErr *libExec.ExitError
Expand All @@ -336,15 +354,15 @@ func (b *baseRepo) RemoteBranchExists(branch string) (bool, error) {
return false, fmt.Errorf(
"error checking for existence of branch %q in remote repo %q: %w",
branch,
b.url,
b.externalURL,
err,
)
}
return true, nil
}

func (b *baseRepo) URL() string {
return b.url
return b.externalURL
}

func (b *baseRepo) setCmdHome(cmd *exec.Cmd, homeDir string) {
Expand Down
13 changes: 7 additions & 6 deletions pkg/controller/git/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,11 @@ func Clone(
fmt.Errorf("error resolving symlinks in path %s: %w", homeDir, err)
}
baseRepo := &baseRepo{
creds: clientOpts.Credentials,
dir: filepath.Join(homeDir, "repo"),
homeDir: homeDir,
url: repoURL,
creds: clientOpts.Credentials,
dir: filepath.Join(homeDir, "repo"),
homeDir: homeDir,
internalURL: repoURL,
externalURL: repoURL,
}
r := &repo{
baseRepo: baseRepo,
Expand Down Expand Up @@ -123,11 +124,11 @@ func (r *repo) clone(opts *CloneOptions) error {
if opts.Depth > 0 {
args = append(args, "--depth", fmt.Sprint(opts.Depth))
}
args = append(args, r.url, r.dir)
args = append(args, r.internalURL, r.dir)
cmd := r.buildGitCommand(args...)
cmd.Dir = r.homeDir // Override the cmd.Dir that's set by r.buildGitCommand()
if _, err := libExec.Exec(cmd); err != nil {
return fmt.Errorf("error cloning repo %q into %q: %w", r.url, r.dir, err)
return fmt.Errorf("error cloning repo %q into %q: %w", r.externalURL, r.dir, err)
}
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/git/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestRepo(t *testing.T) {

t.Run("can clone", func(t *testing.T) {
var repoURL *url.URL
repoURL, err = url.Parse(r.url)
repoURL, err = url.Parse(r.internalURL)
require.NoError(t, err)
repoURL.User = nil
require.Equal(t, testRepoURL, repoURL.String())
Expand All @@ -75,7 +75,7 @@ func TestRepo(t *testing.T) {
})

t.Run("can get the repo url", func(t *testing.T) {
require.Equal(t, r.url, r.URL())
require.Equal(t, r.externalURL, r.URL())
})

t.Run("can get the home dir", func(t *testing.T) {
Expand Down
37 changes: 29 additions & 8 deletions pkg/controller/git/work_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,10 @@ func (w *workTree) Checkout(branch string) error {
// paths within the repo.
"--",
)); err != nil {
return fmt.Errorf("error checking out branch %q from repo %q: %w", branch, w.url, err)
return fmt.Errorf(
"error checking out branch %q from repo %q: %w",
branch, w.externalURL, err,
)
}
return nil
}
Expand Down Expand Up @@ -268,7 +271,10 @@ func (w *workTree) CreateChildBranch(branch string) error {
// paths within the repo.
"--",
)); err != nil {
return fmt.Errorf("error creating new branch %q for repo %q: %w", branch, w.url, err)
return fmt.Errorf(
"error creating new branch %q for repo %q: %w",
branch, w.externalURL, err,
)
}
return nil
}
Expand All @@ -280,15 +286,21 @@ func (w *workTree) CreateOrphanedBranch(branch string) error {
branch,
"--discard-changes",
)); err != nil {
return fmt.Errorf("error creating orphaned branch %q for repo %q: %w", branch, w.url, err)
return fmt.Errorf(
"error creating orphaned branch %q for repo %q: %w",
branch, w.externalURL, err,
)
}
return w.Clean()
}

func (w *workTree) CurrentBranch() (string, error) {
res, err := libExec.Exec(w.buildGitCommand("branch", "--show-current"))
if err != nil {
return "", fmt.Errorf("error checking current branch for repo %q: %w", w.url, err)
return "", fmt.Errorf(
"error checking current branch for repo %q: %w",
w.externalURL, err,
)
}
return strings.TrimSpace(string(res)), nil
}
Expand All @@ -300,7 +312,7 @@ func (w *workTree) DeleteBranch(branch string) error {
"--force",
branch,
)); err != nil {
return fmt.Errorf("error deleting branch %q for repo %q: %w", branch, w.url, err)
return fmt.Errorf("error deleting branch %q for repo %q: %w", branch, w.internalURL, err)
}
return nil
}
Expand Down Expand Up @@ -413,7 +425,10 @@ func (w *workTree) ListCommits(limit, skip uint) ([]CommitMetadata, error) {

commitsBytes, err := libExec.Exec(w.buildGitCommand(args...))
if err != nil {
return nil, fmt.Errorf("error listing commits for repo %q: %w", w.url, err)
return nil, fmt.Errorf(
"error listing commits for repo %q: %w",
w.externalURL, err,
)
}

var commits []CommitMetadata
Expand Down Expand Up @@ -497,7 +512,10 @@ func parseTagMetadataLine(line []byte) (TagMetadata, error) {

func (w *workTree) ListTags() ([]TagMetadata, error) {
if _, err := libExec.Exec(w.buildGitCommand("fetch", "origin", "--tags")); err != nil {
return nil, fmt.Errorf("error fetching tags from repo %q: %w", w.url, err)
return nil, fmt.Errorf(
"error fetching tags from repo %q: %w",
w.externalURL, err,
)
}

// These formats are quite complex, so we break them down into smaller
Expand Down Expand Up @@ -533,7 +551,10 @@ func (w *workTree) ListTags() ([]TagMetadata, error) {
"refs/tags",
))
if err != nil {
return nil, fmt.Errorf("error listing tags for repo %q: %w", w.url, err)
return nil, fmt.Errorf(
"error listing tags for repo %q: %w",
w.externalURL, err,
)
}

var tags []TagMetadata
Expand Down