-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
default repoURL using git remote origin url config
- Loading branch information
1 parent
96575dc
commit 5cd7f86
Showing
3 changed files
with
56 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package git | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os/exec" | ||
"strings" | ||
|
||
giturls "github.com/whilp/git-urls" | ||
"golang.org/x/exp/slog" | ||
) | ||
|
||
func CurrentRepoURL(ctx context.Context) (string, error) { | ||
cmd := exec.CommandContext(ctx, "git", "config", "--get", "remote.origin.url") | ||
stdout, err := cmd.Output() | ||
if err != nil { | ||
return "", fmt.Errorf("failed to get remote origin url: %w", err) | ||
} | ||
|
||
result := strings.TrimSpace(string(stdout)) | ||
url, err := giturls.Parse(result) | ||
if err != nil { | ||
slog.Error("failed to parse git url.", "error", err) | ||
return "", fmt.Errorf("failed to parse git url: %w", err) | ||
} | ||
|
||
host := url.Host | ||
user := strings.Split(url.Path, "/")[0] | ||
repoName := strings.TrimSuffix(strings.Split(url.Path, "/")[1], ".git") | ||
|
||
return fmt.Sprintf("https://%s/%s/%s", host, user, repoName), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters