Skip to content

Commit 521446d

Browse files
committed
refactor(git): replace go-git library with os/exec for staged diff retrieval
1 parent 2588009 commit 521446d

1 file changed

Lines changed: 13 additions & 56 deletions

File tree

internal/git/git.go

Lines changed: 13 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,72 +4,29 @@ import (
44
"bytes"
55
"fmt"
66

7-
"github.com/go-git/go-git/v5"
7+
"os/exec"
88
)
99

1010
// GetStagedDiff returns the diff of the staged files.
1111
func GetStagedDiff() (string, error) {
12-
repo, err := git.PlainOpen(".")
12+
cmd := exec.Command("git", "diff", "--cached")
13+
var out bytes.Buffer
14+
cmd.Stdout = &out
15+
err := cmd.Run()
1316
if err != nil {
14-
return "", fmt.Errorf("error opening repository: %w", err)
17+
return "", fmt.Errorf("error running git diff --cached: %w", err)
1518
}
16-
17-
// Get the worktree to access the staging area
18-
w, err := repo.Worktree()
19-
if err != nil {
20-
return "", fmt.Errorf("error getting worktree: %w", err)
21-
}
22-
23-
// Get the status to see what's staged
24-
status, err := w.Status()
25-
if err != nil {
26-
return "", fmt.Errorf("error getting status: %w", err)
27-
}
28-
29-
// Create a buffer to store the diff
30-
var diff bytes.Buffer
31-
32-
// For each staged file, get its diff
33-
for path, change := range status {
34-
if change.Staging != git.Unmodified {
35-
// For a more complete implementation, we would compare the staged version
36-
// with the HEAD version, but for now we'll just note that files are staged
37-
diff.WriteString(fmt.Sprintf("Staged file: %s\n", path))
38-
}
39-
}
40-
41-
return diff.String(), nil
19+
return out.String(), nil
4220
}
4321

4422
// GetWorkingTreeDiff returns the diff of the working tree.
4523
func GetWorkingTreeDiff() (string, error) {
46-
repo, err := git.PlainOpen(".")
24+
cmd := exec.Command("git", "diff")
25+
var out bytes.Buffer
26+
cmd.Stdout = &out
27+
err := cmd.Run()
4728
if err != nil {
48-
return "", fmt.Errorf("error opening repository: %w", err)
29+
return "", fmt.Errorf("error running git diff: %w", err)
4930
}
50-
51-
// Get the worktree
52-
w, err := repo.Worktree()
53-
if err != nil {
54-
return "", fmt.Errorf("error getting worktree: %w", err)
55-
}
56-
57-
// Get the status
58-
status, err := w.Status()
59-
if err != nil {
60-
return "", fmt.Errorf("error getting status: %w", err)
61-
}
62-
63-
// Create a buffer for the diff
64-
var diff bytes.Buffer
65-
66-
// For each modified file, get its diff
67-
for path, change := range status {
68-
if change.Worktree != git.Unmodified {
69-
// Note that files are modified
70-
diff.WriteString(fmt.Sprintf("Modified file: %s\n", path))
71-
}
72-
}
73-
74-
return diff.String(), nil
31+
return out.String(), nil
7532
}

0 commit comments

Comments
 (0)