Skip to content

Commit

Permalink
use direct Git commands with -Cpath in subprocess calls
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed Jun 4, 2019
1 parent 60225e8 commit d28d0cc
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 29 deletions.
2 changes: 1 addition & 1 deletion gitutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def find_dir_missing_file(fn: str, path: Path, copyfile: Path = None) -> List[Pa

dlist = (x for x in path.iterdir() if x.is_dir())

missing = []
missing: List[Path] = []
for d in dlist:
if not (d / fn).is_file():
if isinstance(copyfile, Path):
Expand Down
4 changes: 1 addition & 3 deletions gitutils/branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

from .git import baddir, GITEXE

CMD = [GITEXE, 'rev-parse', '--abbrev-ref', 'HEAD']


async def findbranch(mainbranch: str, rdir: Path) -> List[Tuple[Path, str]]:
"""
Expand Down Expand Up @@ -63,7 +61,7 @@ async def _arbiter(mainbranch: str, path: Path) -> Tuple[Path, str]:

async def _worker(mainbranch: str, path: Path) -> Tuple[Path, str]:

proc = await asyncio.create_subprocess_exec(*CMD, cwd=path,
proc = await asyncio.create_subprocess_exec(*[GITEXE, '-C', str(path), 'rev-parse', '--abbrev-ref', 'HEAD'],
stdout=asyncio.subprocess.PIPE)
stdout, _ = await proc.communicate()

Expand Down
4 changes: 1 addition & 3 deletions gitutils/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

from .git import gitdirs, GITEXE, TIMEOUT

CMD = [GITEXE, 'log', '--pretty="%ce"']


def gitemail(path: Path,
exclude: str = None) -> Iterator[Tuple[Path, List[Tuple[str, int]]]]:
Expand All @@ -36,7 +34,7 @@ def gitemail(path: Path,
for d in gitdirs(path):

try:
ret = subprocess.check_output(CMD, cwd=d, universal_newlines=True,
ret = subprocess.check_output([GITEXE, '-C', str(d), 'log', '--pretty="%ce"'], universal_newlines=True,
timeout=TIMEOUT)
except subprocess.CalledProcessError as e:
logging.error(f'{path} {e}')
Expand Down
5 changes: 2 additions & 3 deletions gitutils/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,8 @@ def listchanged(path: Path) -> List[str]:
changes : list of str
filenames changed in this Git repo
"""
ret = subprocess.check_output([GITEXE, 'ls-files', '--modified'],
universal_newlines=True,
cwd=path)
ret = subprocess.check_output([GITEXE, '-C', str(path), 'ls-files', '--modified'],
universal_newlines=True)

changes = ret.split('\n')

Expand Down
20 changes: 8 additions & 12 deletions gitutils/pull.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,11 @@ def fetchpull(mode: str, rdir: Path) -> Iterator[Tuple[Path, str]]:
# Lmax = len(max(map(attrgetter('name'), dlist), key=len))

for d in gitdirs(rdir):
try:
# don't use timeout as it doesn't work right when waiting for user input (password)
ret = subprocess.run([GITEXE] + mode.split(), cwd=d,
universal_newlines=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.PIPE,
timeout=TIMEOUT)

if ret.stderr:
yield d, ret.stderr
except subprocess.CalledProcessError as e:
yield d, e.output
ret = subprocess.run([GITEXE, '-C', str(d), mode],
universal_newlines=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.PIPE,
timeout=TIMEOUT)

if ret.returncode:
yield d, ret.stderr
12 changes: 6 additions & 6 deletions gitutils/push.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import subprocess
from .git import gitdirs, GITEXE, TIMEOUT

C0 = [GITEXE, 'rev-parse', '--abbrev-ref', 'HEAD'] # get branch name
C1 = [GITEXE, 'status', '--porcelain'] # uncommitted or changed files
C0 = ['rev-parse', '--abbrev-ref', 'HEAD'] # get branch name
C1 = ['status', '--porcelain'] # uncommitted or changed files


def gitpushall(rdir: Path) -> Iterator[Tuple[Path, str]]:
Expand All @@ -27,17 +27,17 @@ def gitpushall(rdir: Path) -> Iterator[Tuple[Path, str]]:
for d in gitdirs(rdir):
try:
# %% detect uncommitted changes
ret = subprocess.check_output(C1, cwd=d, universal_newlines=True,
ret = subprocess.check_output([GITEXE, '-C', str(d)] + C1, universal_newlines=True,
timeout=TIMEOUT)
if ret:
yield d, ret
continue
# %% detect committed, but not pushed
branch = subprocess.check_output(C0, cwd=d, universal_newlines=True,
branch = subprocess.check_output([GITEXE, '-C', str(d)] + C0, universal_newlines=True,
timeout=TIMEOUT)[:-1]

C2 = [GITEXE, 'diff', '--stat', f'origin/{branch}..']
ret = subprocess.check_output(C2, cwd=d, universal_newlines=True,
C2 = [GITEXE, '-C', str(d), 'diff', '--stat', f'origin/{branch}..']
ret = subprocess.check_output(C2, universal_newlines=True,
timeout=TIMEOUT)
if ret:
yield d, ret
Expand Down
6 changes: 5 additions & 1 deletion pull.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
for a root directory, assumes all subdirectories are Git repos
and "git pull" each of them.
"""
from gitutils import fetchpull
from gitutils.pull import fetchpull
from argparse import ArgumentParser
from gitutils.git import MAGENTA, BLACK

Expand All @@ -14,9 +14,13 @@ def main():
p.add_argument('-v', '--verbose', action='store_true')
P = p.parse_args()

failed = []
for d, v in fetchpull('pull', P.codepath):
print(MAGENTA + str(d))
print(BLACK + v)
failed.append(d)

print('\n'.join(map(str, failed))) # map(str,) needed for Windows


if __name__ == '__main__':
Expand Down

0 comments on commit d28d0cc

Please sign in to comment.