diff --git a/main.go b/main.go index 74ea0871..edbd902c 100644 --- a/main.go +++ b/main.go @@ -30,34 +30,35 @@ const ( ) type args struct { - CwdMode *string - CwdMaxDepth *int - CwdMaxDirSize *int - ColorizeHostname *bool - HostnameOnlyIfSSH *bool - SshAlternateIcon *bool - EastAsianWidth *bool - PromptOnNewLine *bool - StaticPromptIndicator *bool - Mode *string - Theme *string - Shell *string - Modules *string - ModulesRight *string - Priority *string - MaxWidthPercentage *int - TruncateSegmentWidth *int - PrevError *int - NumericExitCodes *bool - IgnoreRepos *string - ShortenGKENames *bool - ShortenEKSNames *bool - ShellVar *string - PathAliases *string - Duration *string - DurationMin *string - Eval *bool - Condensed *bool + CwdMode *string + CwdMaxDepth *int + CwdMaxDirSize *int + ColorizeHostname *bool + HostnameOnlyIfSSH *bool + SshAlternateIcon *bool + EastAsianWidth *bool + PromptOnNewLine *bool + StaticPromptIndicator *bool + GitAssumeUnchangedSize *int64 + Mode *string + Theme *string + Shell *string + Modules *string + ModulesRight *string + Priority *string + MaxWidthPercentage *int + TruncateSegmentWidth *int + PrevError *int + NumericExitCodes *bool + IgnoreRepos *string + ShortenGKENames *bool + ShortenEKSNames *bool + ShellVar *string + PathAliases *string + Duration *string + DurationMin *string + Eval *bool + Condensed *bool } func warn(msg string) { @@ -174,6 +175,10 @@ func main() { "static-prompt-indicator", false, comments("Always show the prompt indicator with the default color, never with the error color")), + GitAssumeUnchangedSize: flag.Int64( + "git-assume-unchanged-size", + 2048, + comments("Disable checking for changed/edited files in git repositories where the index is larger than this size (in KB), improves performance")), Mode: flag.String( "mode", "patched", diff --git a/segment-git.go b/segment-git.go index 5961bc4b..b2cdc291 100644 --- a/segment-git.go +++ b/segment-git.go @@ -5,6 +5,7 @@ import ( pwl "github.com/justjanne/powerline-go/powerline" "os" "os/exec" + "path" "regexp" "strconv" "strings" @@ -127,19 +128,43 @@ func parseGitStats(status []string) repoStats { return stats } +func repoRoot(path string) (string, error) { + out, err := runGitCommand("git", "rev-parse", "--show-toplevel") + if err != nil { + return "", err + } + return strings.TrimSpace(out), nil +} + +func indexSize(root string) (int64, error) { + fileInfo, err := os.Stat(path.Join(root, ".git", "index")) + if err != nil { + return 0, err + } + + return fileInfo.Size(), nil +} + func segmentGit(p *powerline) []pwl.Segment { - if len(p.ignoreRepos) > 0 { - out, err := runGitCommand("git", "rev-parse", "--show-toplevel") - if err != nil { - return []pwl.Segment{} - } - out = strings.TrimSpace(out) - if p.ignoreRepos[out] { - return []pwl.Segment{} - } + repoRoot, err := repoRoot(p.cwd) + if err != nil { + return []pwl.Segment{} + } + + if len(p.ignoreRepos) > 0 && p.ignoreRepos[repoRoot] { + return []pwl.Segment{} + } + + indexSize, err := indexSize(p.cwd) + args := []string{ + "status", "--porcelain", "-b", "--ignore-submodules", + } + + if *p.args.GitAssumeUnchangedSize > 0 && indexSize > (*p.args.GitAssumeUnchangedSize*1024) { + args = append(args, "-uno") } - out, err := runGitCommand("git", "status", "--porcelain", "-b", "--ignore-submodules") + out, err := runGitCommand("git", args...) if err != nil { return []pwl.Segment{} }