Skip to content

Commit 1c8e680

Browse files
committed
Update git config repo name collection
1 parent 8d7f964 commit 1c8e680

File tree

1 file changed

+25
-33
lines changed

1 file changed

+25
-33
lines changed

util/file.go

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package util
22

33
import (
44
"bufio"
5+
"bytes"
56
"fmt"
67
"os"
8+
"os/exec"
79
"os/user"
8-
"path/filepath"
910
"strconv"
1011
"strings"
1112

@@ -126,45 +127,36 @@ func AppendToFile(filePath, content string) error {
126127
return nil
127128
}
128129

129-
// GetRepoNameFromConfig reads the .git/config file and extracts the repository name
130+
// GetRepoNameFromConfig checks if the current directory (or any of its parent directories) is inside a Git repository.
131+
// If inside a Git repo, it retrieves and returns the repository name.
130132
func GetRepoNameFromConfig(path string) (string, error) {
131-
132-
gitPath := filepath.Join(path, ".git")
133-
info, err := os.Stat(gitPath)
134-
if err != nil && info != nil && !info.IsDir() {
135-
return "", fmt.Errorf("could not find .git directory: %w", err)
136-
}
137-
138-
configPath := filepath.Join(gitPath, "config")
139-
file, err := Fs.Open(configPath)
133+
// Check if we're inside a Git worktree
134+
cmd := exec.Command("git", "-C", path, "rev-parse", "--is-inside-work-tree")
135+
var out bytes.Buffer
136+
cmd.Stdout = &out
137+
err := cmd.Run()
138+
if err != nil || strings.TrimSpace(out.String()) != "true" {
139+
return "", fmt.Errorf("not inside a Git repository: %w", err)
140+
}
141+
142+
// Retrieve the remote origin URL
143+
cmd = exec.Command("git", "-C", path, "config", "--get", "remote.origin.url")
144+
out.Reset()
145+
cmd.Stdout = &out
146+
err = cmd.Run()
140147
if err != nil {
141-
return "", fmt.Errorf("could not open .git/config: %w", err)
142-
}
143-
defer file.Close()
144-
145-
var url string
146-
scanner := bufio.NewScanner(file)
147-
for scanner.Scan() {
148-
line := strings.TrimSpace(scanner.Text())
149-
150-
// Look for the URL in the origin section
151-
if strings.HasPrefix(line, "url =") {
152-
url = strings.TrimSpace(strings.TrimPrefix(line, "url ="))
153-
break
154-
}
155-
}
156-
157-
if err := scanner.Err(); err != nil {
158-
return "", fmt.Errorf("error reading .git/config: %w", err)
148+
return "", fmt.Errorf("could not get remote origin URL: %w", err)
159149
}
160150

151+
url := strings.TrimSpace(out.String())
161152
if url == "" {
162-
return "", fmt.Errorf("no origin URL found in .git/config")
153+
return "", fmt.Errorf("no origin URL found in Git configuration")
163154
}
164155

165-
url = strings.TrimSuffix(url, ".git") // Remove .git suffix if present
156+
// Remove .git suffix if present and extract repo name from the URL
157+
url = strings.TrimSuffix(url, ".git")
166158
parts := strings.Split(url, "/")
159+
repoName := parts[len(parts)-1]
167160

168-
// Extract repo name from URL
169-
return parts[len(parts)-1], nil
161+
return repoName, nil
170162
}

0 commit comments

Comments
 (0)