Skip to content

Commit

Permalink
Automatically disable checking for untracked files in git repositorie…
Browse files Browse the repository at this point in the history
…s above a certain size
  • Loading branch information
justjanne committed Apr 24, 2020
1 parent 0968ccd commit fe41414
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 38 deletions.
61 changes: 33 additions & 28 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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",
Expand Down
45 changes: 35 additions & 10 deletions segment-git.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
pwl "github.com/justjanne/powerline-go/powerline"
"os"
"os/exec"
"path"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -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{}
}
Expand Down

0 comments on commit fe41414

Please sign in to comment.