Skip to content

Commit

Permalink
Merge pull request #16 from autarch/autarch/ignore-repos
Browse files Browse the repository at this point in the history
Add a feature to ignore git repos, specified by root dir
  • Loading branch information
justjanne authored Aug 25, 2017
2 parents 0cee48f + 6135c94 commit bae2b36
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 39 deletions.
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type args struct {
Modules *string
Priority *string
MaxWidthPercentage *int
IgnoreRepos *string
PrevError *int
}

Expand Down Expand Up @@ -127,6 +128,10 @@ func main() {
" "),
PrevError: flag.Int("error", 0,
"Exit code of previously executed command"),
IgnoreRepos: flag.String("ignore-repos",
"",
"A list of git repos to ignore. Separate with ','\n"+
"Repos are identified by their root directory."),
}
flag.Parse()
priorities := map[string]int{}
Expand Down
7 changes: 7 additions & 0 deletions powerline.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"bytes"
"fmt"
"strings"

"golang.org/x/text/width"
"math"
"os"
Expand All @@ -25,6 +27,7 @@ type powerline struct {
reset string
symbolTemplates Symbols
priorities map[string]int
ignoreRepos map[string]bool
Segments []segment
}

Expand All @@ -37,6 +40,10 @@ func NewPowerline(args args, cwd string, priorities map[string]int) *powerline {
p.reset = fmt.Sprintf(p.shellInfo.colorTemplate, "[0m")
p.symbolTemplates = symbolTemplates[*args.Mode]
p.priorities = priorities
p.ignoreRepos = make(map[string]bool)
for _, r := range strings.Split(*args.IgnoreRepos, ",") {
p.ignoreRepos[r] = true
}
p.Segments = make([]segment, 0)
return p
}
Expand Down
91 changes: 52 additions & 39 deletions segment-git.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,28 @@ func gitProcessEnv() []string {
return result
}

func runGitCommand(cmd string, args ...string) (string, error) {
command := exec.Command(cmd, args...)
command.Env = gitProcessEnv()
out, err := command.Output()
return string(out), err
}

func parseGitBranchInfo(status []string) map[string]string {
return groupDict(branchRegex, status[0])
}

func getGitDetachedBranch(p *powerline) string {
command := exec.Command("git", "rev-parse", "--short", "HEAD")
command.Env = gitProcessEnv()
out, err := command.Output()
out, err := runGitCommand("git", "rev-parse", "--short", "HEAD")
if err != nil {
command2 := exec.Command("git", "symbolic-ref", "--short", "HEAD")
command2.Env = gitProcessEnv()
out, err := command2.Output()
out, err := runGitCommand("git", "symbolic-ref", "--short", "HEAD")
if err != nil {
return "Error"
} else {
return strings.SplitN(string(out), "\n", 2)[0]
return strings.SplitN(out, "\n", 2)[0]
}
} else {
detachedRef := strings.SplitN(string(out), "\n", 2)
detachedRef := strings.SplitN(out, "\n", 2)
return fmt.Sprintf("%s %s", p.symbolTemplates.RepoDetached, detachedRef[0])
}
}
Expand Down Expand Up @@ -121,42 +124,52 @@ func parseGitStats(status []string) repoStats {
}

func segmentGit(p *powerline) {
command := exec.Command("git", "status", "--porcelain", "-b")
command.Env = gitProcessEnv()
out, err := command.Output()
if len(p.ignoreRepos) > 0 {
out, err := runGitCommand("git", "rev-parse", "--show-toplevel")
if err != nil {
return
}
out = strings.TrimSpace(out)
if p.ignoreRepos[out] {
return
}
}

out, err := runGitCommand("git", "status", "--porcelain", "-b")
if err != nil {
} else {
status := strings.Split(string(out), "\n")
stats := parseGitStats(status)
branchInfo := parseGitBranchInfo(status)
var branch string
return
}

if branchInfo["local"] != "" {
ahead, _ := strconv.ParseInt(branchInfo["ahead"], 10, 32)
stats.ahead = int(ahead)
status := strings.Split(out, "\n")
stats := parseGitStats(status)
branchInfo := parseGitBranchInfo(status)
var branch string

behind, _ := strconv.ParseInt(branchInfo["behind"], 10, 32)
stats.behind = int(behind)
if branchInfo["local"] != "" {
ahead, _ := strconv.ParseInt(branchInfo["ahead"], 10, 32)
stats.ahead = int(ahead)

branch = branchInfo["local"]
} else {
branch = getGitDetachedBranch(p)
}
behind, _ := strconv.ParseInt(branchInfo["behind"], 10, 32)
stats.behind = int(behind)

var foreground, background uint8
if stats.dirty() {
foreground = p.theme.RepoDirtyFg
background = p.theme.RepoDirtyBg
} else {
foreground = p.theme.RepoCleanFg
background = p.theme.RepoCleanBg
}
branch = branchInfo["local"]
} else {
branch = getGitDetachedBranch(p)
}

p.appendSegment("git-branch", segment{
content: fmt.Sprintf(" %s ", branch),
foreground: foreground,
background: background,
})
stats.addToPowerline(p)
var foreground, background uint8
if stats.dirty() {
foreground = p.theme.RepoDirtyFg
background = p.theme.RepoDirtyBg
} else {
foreground = p.theme.RepoCleanFg
background = p.theme.RepoCleanBg
}

p.appendSegment("git-branch", segment{
content: fmt.Sprintf(" %s ", branch),
foreground: foreground,
background: background,
})
stats.addToPowerline(p)
}

0 comments on commit bae2b36

Please sign in to comment.