From f4cf6be5d8a70d0031df4a2094c412f786dd212e Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Mon, 2 Dec 2019 16:50:58 -0500 Subject: [PATCH] git-repack: make it python3-only --- .bin/git-repack | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/.bin/git-repack b/.bin/git-repack index eb0061b..82f8f17 100755 --- a/.bin/git-repack +++ b/.bin/git-repack @@ -1,4 +1,7 @@ -#!/usr/bin/python +#!/usr/bin/python3 + +# pylint: disable=fixme,invalid-name +# pylint: disable=too-many-branches,too-many-locals,too-many-statements """Repack git repos fully the way I like them.""" @@ -67,7 +70,8 @@ def find_temp_dir(): def readfile(path): """Read |path| and return its data""" if os.path.isfile(path): - return open(path).read() + with open(path) as fp: + return fp.read() return '' @@ -97,7 +101,7 @@ def is_packed(path): """See if the git repo is already packed""" obj_path = os.path.join(path, 'objects') paths = set(os.listdir(obj_path)) - if {'info', 'pack'} != paths and {'pack'} != paths: + if paths not in ({'info', 'pack'}, {'pack'}): return False packs = os.listdir(os.path.join(obj_path, 'pack')) if len(packs) != 2: @@ -147,7 +151,7 @@ def repack(path): packed_refs = readfile(os.path.join(path, 'packed-refs')) if os.path.exists(origin_path) or 'refs/remotes/origin/' in packed_refs: cmd = ['git', '--git-dir', path, 'remote', 'prune', 'origin'] - subprocess.check_call(cmd, cwd='/') + subprocess.run(cmd, cwd='/', check=True) clean_packs(path) @@ -164,36 +168,37 @@ def repack(path): cmd = ['git', '--git-dir', rundir, 'reflog', 'expire', '--all', '--stale-fix'] print('Cleaning reflog: %s' % ' '.join(cmd)) - subprocess.check_call(cmd, cwd='/') + subprocess.run(cmd, cwd='/', check=True) # This also packs refs/tags for us. cmd = ['git', '--git-dir', rundir, 'gc', '--aggressive', '--prune=all'] print('Repacking git repo: %s' % ' '.join(cmd)) - subprocess.check_call(cmd, cwd='/') + subprocess.run(cmd, cwd='/', check=True) # Clean empty dirs. cmd = ['find', rundir, '-depth', '-type', 'd', '-exec', 'rmdir', '{}', '+'] - subprocess.call(cmd, stderr=open('/dev/null', 'w')) + subprocess.call(cmd, stderr=subprocess.DEVNULL) # There's a few dirs we need to exist even if they're empty. refdir = os.path.join(rundir, 'refs') - if not os.path.isdir(refdir): - os.mkdir(refdir) + os.makedirs(refdir, exist_ok=True) if tmpdir: cmd = ['rsync', '-a', '--delete', tmpdir + '/', path + '/'] print('Syncing back git repo: %s' % ' '.join(cmd)) - subprocess.check_call(cmd, cwd='/') + subprocess.run(cmd, cwd='/', check=True) cmd = ['find', path + '/', '-exec', 'chmod', 'u+rw', '{}', '+'] - subprocess.check_call(cmd, cwd='/') + subprocess.run(cmd, cwd='/', check=True) finally: if grafts: - open(graft_file, 'w').write(grafts) + with open(graft_file, 'w') as fp: + fp.write(grafts) if alts: - open(alt_file, 'w').write(alts) - if tmpdir and os.path.exists(tmpdir): - shutil.rmtree(tmpdir) + with open(alt_file, 'w') as fp: + fp.write(alts) + if tmpdir: + shutil.rmtree(tmpdir, ignore_errors=True) def get_parser(): -- 2.39.2