integrate branch parsing into single system call
The `git status --porcelain` command can take an additional argument `-b` which causes the porcelain output to also contain branch information in a stable and supposedly nonchanging way. This change adds that argument to the initial `git status` call, and parses the branch/ahead/behind information from that. The end result is the entire call to `git branch -v` can be removed, resulting in one less subshell command and hopefully a more reliable target across future versions of git.
This commit is contained in:
@@ -22,14 +22,15 @@
|
|||||||
|
|
||||||
@project_root = File.exist?(".git") ? Dir.pwd : `\git rev-parse --show-toplevel 2> /dev/null`.strip
|
@project_root = File.exist?(".git") ? Dir.pwd : `\git rev-parse --show-toplevel 2> /dev/null`.strip
|
||||||
|
|
||||||
@git_status = `\git status --porcelain 2> /dev/null`
|
@git_status = `\git status --porcelain -b 2> /dev/null`
|
||||||
|
|
||||||
git_branch = `\git branch -v 2> /dev/null`
|
git_status_lines = @git_status.split("\n")
|
||||||
@branch = git_branch[/^\* (\(no branch\)|[^ ]*)/, 1]
|
git_branch = git_status_lines[0]
|
||||||
@ahead = git_branch[/^\* [^ ]* *[^ ]* *\[ahead ?(\d+).*\]/, 1]
|
@branch = git_branch[/^## (?:Initial commit on )?([^ \.]+)/, 1]
|
||||||
@behind = git_branch[/^\* [^ ]* *[^ ]* *\[.*behind ?(\d+)\]/, 1]
|
@ahead = git_branch[/\[ahead ?(\d+).*\]/, 1]
|
||||||
|
@behind = git_branch[/\[.*behind ?(\d+)\]/, 1]
|
||||||
|
|
||||||
@changes = @git_status.split("\n")
|
@changes = git_status_lines[1..-1]
|
||||||
# Exit if too many changes
|
# Exit if too many changes
|
||||||
exit if @changes.size > ENV["gs_max_changes"].to_i
|
exit if @changes.size > ENV["gs_max_changes"].to_i
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user