Skip to content

Commit ad55484

Browse files
committed
git: add 'GetRemoteUrl()'
Add a 'GetRemoteUrl()' function to 'git.GitHelper', which calls 'git remote get-url origin' on a given repository path and returns the whitespace- trimmed remote URL. Neither stdout nor stderr are printed to the console, but the stderr text is included in the error message if Git exits with an error status. Although it is not used in this patch, 'GetRemoteUrl()' will be used in later commits to display information about the configuration of the bundle server. Signed-off-by: Victoria Dye <[email protected]>
1 parent 7067bfa commit ad55484

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

internal/git/git.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type GitHelper interface {
1717
CreateIncrementalBundle(ctx context.Context, repoDir string, filename string, prereqs []string) (bool, error)
1818
CloneBareRepo(ctx context.Context, url string, destination string) error
1919
UpdateBareRepo(ctx context.Context, repoDir string) error
20+
GetRemoteUrl(ctx context.Context, repoDir string) (string, error)
2021
}
2122

2223
type gitHelper struct {
@@ -47,6 +48,25 @@ func (g *gitHelper) gitCommand(ctx context.Context, args ...string) error {
4748
return nil
4849
}
4950

51+
func (g *gitHelper) gitCommandQuiet(ctx context.Context, args ...string) (*bytes.Buffer, *bytes.Buffer, error) {
52+
stdout := &bytes.Buffer{}
53+
stderr := &bytes.Buffer{}
54+
55+
exitCode, err := g.cmdExec.Run(ctx, "git", args,
56+
cmd.Stdout(stdout),
57+
cmd.Stderr(stderr),
58+
cmd.Env([]string{"LC_CTYPE=C"}),
59+
)
60+
61+
if err != nil {
62+
return nil, nil, g.logger.Error(ctx, err)
63+
} else if exitCode != 0 {
64+
return stdout, stderr, g.logger.Errorf(ctx, "'git' exited with status %d\n%s", exitCode, stderr.String())
65+
}
66+
67+
return stdout, stderr, nil
68+
}
69+
5070
func (g *gitHelper) gitCommandWithStdin(ctx context.Context, stdinLines []string, args ...string) error {
5171
buffer := bytes.Buffer{}
5272
for line := range stdinLines {
@@ -149,3 +169,11 @@ func (g *gitHelper) UpdateBareRepo(ctx context.Context, repoDir string) error {
149169

150170
return nil
151171
}
172+
173+
func (g *gitHelper) GetRemoteUrl(ctx context.Context, repoDir string) (string, error) {
174+
stdout, _, gitErr := g.gitCommandQuiet(ctx, "-C", repoDir, "remote", "get-url", "origin")
175+
if gitErr != nil {
176+
return "", g.logger.Errorf(ctx, "failed to get remote URL: %w", gitErr)
177+
}
178+
return strings.TrimSpace(stdout.String()), nil
179+
}

internal/testhelpers/mocks.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,8 @@ func (m *MockGitHelper) UpdateBareRepo(ctx context.Context, repoDir string) erro
229229
fnArgs := m.Called(ctx, repoDir)
230230
return fnArgs.Error(0)
231231
}
232+
233+
func (m *MockGitHelper) GetRemoteUrl(ctx context.Context, repoDir string) (string, error) {
234+
fnArgs := m.Called(ctx, repoDir)
235+
return fnArgs.String(0), fnArgs.Error(1)
236+
}

0 commit comments

Comments
 (0)