-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-foreach.py
executable file
·68 lines (57 loc) · 1.87 KB
/
git-foreach.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python3
import argparse
import glob
import multiprocessing
import subprocess
import os
import sys
def build_command(command, git_dir, work_tree=None):
result = ['git', '--git-dir=' + git_dir]
if work_tree:
result.append('--work-tree=' + work_tree)
result.extend(command)
return result
def build_jobs(pattern, bare_repos, command):
jobs = []
for match in glob.glob(pattern):
if bare_repos:
command_args = build_command(command, match),
else:
command_args = build_command(command, match + '/.git', match)
jobs.append({
'dir': match,
'command': command_args,
})
return jobs
def get_executor(jobs):
if jobs != 0:
return multiprocessing.Pool(jobs).map
else:
return map
def build_argparser():
parser = argparse.ArgumentParser()
parser.add_argument('-b', '--bare-repos', action='store_true')
parser.add_argument('-q', '--quiet', action='store_true')
parser.add_argument('-j', '--jobs', type=int,
default=multiprocessing.cpu_count())
parser.add_argument('pattern', metavar='PATTERN', type=str)
parser.add_argument('command', metavar='COMMAND', type=str,
nargs=argparse.REMAINDER)
return parser
def runner(command):
try:
return subprocess.check_output(command)
except:
return ""
def main():
args = build_argparser().parse_args()
jobs = build_jobs(args.pattern, args.bare_repos, args.command)
executor = get_executor(args.jobs)
results = executor(runner, (job['command'] for job in jobs))
for job, result in zip(jobs, results):
if not args.quiet:
print(job['dir'])
if result != '':
print(result.decode('ascii'), end='')
if __name__ == '__main__':
main()