fix git command taking too long and crashing
This commit is contained in:
parent
5bff961095
commit
eedbc09193
1 changed files with 36 additions and 19 deletions
|
@ -7,6 +7,7 @@ import pwd
|
||||||
import socket
|
import socket
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
import time
|
||||||
|
|
||||||
COLOR_LOAD_IDLE = 10
|
COLOR_LOAD_IDLE = 10
|
||||||
COLOR_LOAD_OK = 2
|
COLOR_LOAD_OK = 2
|
||||||
|
@ -321,14 +322,19 @@ class PathPart(Part):
|
||||||
|
|
||||||
def command(cmd, timeout=0.5):
|
def command(cmd, timeout=0.5):
|
||||||
"""
|
"""
|
||||||
Executes a command, returns stdout, suppresses stderr."
|
Executes a command, returns stdout, suppresses stderr.
|
||||||
|
|
||||||
|
Kills the process on timeout.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if type(cmd) == str:
|
if type(cmd) == str:
|
||||||
cmd = cmd.split()
|
cmd = cmd.split()
|
||||||
|
try:
|
||||||
s = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
s = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||||
s.wait(timeout)
|
s.wait(timeout)
|
||||||
|
except subprocess.TimeoutExpired:
|
||||||
|
s.kill()
|
||||||
|
raise
|
||||||
|
|
||||||
return s.stdout.read().decode("utf-8")
|
return s.stdout.read().decode("utf-8")
|
||||||
|
|
||||||
|
@ -410,6 +416,15 @@ class WineprefixPart(Part):
|
||||||
else:
|
else:
|
||||||
self.fragments.append(":64")
|
self.fragments.append(":64")
|
||||||
|
|
||||||
|
class TimeBudget:
|
||||||
|
def __init__(self, budget, min=0.1):
|
||||||
|
self.end = time.monotonic() + budget
|
||||||
|
self.min = min
|
||||||
|
|
||||||
|
@property
|
||||||
|
def trem(self):
|
||||||
|
return max(self.min, self.end - time.monotonic())
|
||||||
|
|
||||||
class GitPart(Part):
|
class GitPart(Part):
|
||||||
"""
|
"""
|
||||||
↘2 ↗4 ⚠M master ?11 ✎6 ✉10
|
↘2 ↗4 ⚠M master ?11 ✎6 ✉10
|
||||||
|
@ -427,20 +442,21 @@ class GitPart(Part):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
Part.__init__(self)
|
Part.__init__(self)
|
||||||
|
tb = TimeBudget(1) # don't spend more than a second here
|
||||||
|
|
||||||
branch_name = command("git name-rev --name-only --no-undefined --always HEAD").strip()
|
try:
|
||||||
|
branch_name = command("git name-rev --name-only --no-undefined --always HEAD", tb.trem).strip()
|
||||||
|
|
||||||
if branch_name:
|
if branch_name:
|
||||||
try:
|
|
||||||
self.fragments.append("↱ ")
|
self.fragments.append("↱ ")
|
||||||
|
|
||||||
count_to_pull = command("git log --oneline ..@{u}").count("\n")
|
count_to_pull = command("git log --oneline ..@{u}", tb.trem).count("\n")
|
||||||
count_to_push = command("git log --oneline @{u}..").count("\n")
|
count_to_push = command("git log --oneline @{u}..", tb.trem).count("\n")
|
||||||
git_dir = command("git rev-parse --git-dir").strip()
|
git_dir = command("git rev-parse --git-dir", tb.trem).strip()
|
||||||
merging = os.path.exists(os.path.join(git_dir, "MERGE_HEAD"))
|
merging = os.path.exists(os.path.join(git_dir, "MERGE_HEAD"))
|
||||||
untracked = command("git ls-files --other --exclude-standard", timeout=0.2).count("\n")
|
untracked = command("git ls-files --other --exclude-standard", tb.trem).count("\n")
|
||||||
modified = command("git diff --name-only").count("\n")
|
modified = command("git diff --name-only", tb.trem).count("\n")
|
||||||
staged = command("git diff --name-only --staged").count("\n")
|
staged = command("git diff --name-only --staged", tb.trem).count("\n")
|
||||||
|
|
||||||
if count_to_pull:
|
if count_to_pull:
|
||||||
self.fragments.append("↘%d " %(count_to_pull))
|
self.fragments.append("↘%d " %(count_to_pull))
|
||||||
|
@ -473,6 +489,7 @@ class GitPart(Part):
|
||||||
self.fragments.append(" ✉%d" %(staged))
|
self.fragments.append(" ✉%d" %(staged))
|
||||||
|
|
||||||
self.fragments.append(style_reset())
|
self.fragments.append(style_reset())
|
||||||
|
|
||||||
except subprocess.TimeoutExpired:
|
except subprocess.TimeoutExpired:
|
||||||
self.fragments = []
|
self.fragments = []
|
||||||
self.fragments.append(style_color(COLOR_GIT_DIRTY))
|
self.fragments.append(style_color(COLOR_GIT_DIRTY))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue