-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Improve the git status speed.
By using the native git client its possible to improve the speed significantly. Signed-off-by: Matthias Glastra <[email protected]>
- Loading branch information
Showing
2 changed files
with
78 additions
and
4 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,56 @@ | ||
package git | ||
|
||
import ( | ||
"os/exec" | ||
"strings" | ||
|
||
"github.com/go-git/go-git/v5" | ||
) | ||
|
||
// GitExists checks if the git binary is available. | ||
// This can be used to fall back to go-git implementation. | ||
func GitExists() bool { | ||
|
||
_, err := exec.LookPath("git") | ||
if err != nil { | ||
return false | ||
} else { | ||
return true | ||
} | ||
} | ||
|
||
func GitGetStatus(workDir string) (map[string]Status, error) { | ||
|
||
// Execute the git status --porcelain command | ||
cmd := exec.Command("git", "-C", workDir, "status", "--porcelain", ) | ||
outputBytes, err := cmd.Output() | ||
if err != nil { | ||
return map[string]Status{}, err | ||
} | ||
|
||
// Convert the output to a string and split into lines | ||
output := string(outputBytes) | ||
lines := strings.Split(output, "\n") | ||
|
||
// Iterate over the lines and parse the status | ||
var gitStatuses map[string]Status = make(map[string]Status) | ||
for _, line := range lines { | ||
// Skip empty lines | ||
if len(line) == 0 { | ||
continue | ||
} | ||
|
||
// The first two characters are the status codes | ||
repoStatus := statusCodeString(git.StatusCode(line[0])) | ||
worktreeStatus := statusCodeString(git.StatusCode(line[1])) | ||
filePath := strings.TrimSpace(line[2:]) | ||
|
||
// Append the parsed status to the list | ||
gitStatuses[filePath] = Status{ | ||
Staging: repoStatus, | ||
Worktree: worktreeStatus, | ||
} | ||
} | ||
|
||
return gitStatuses, nil | ||
} |