Add --run argument to gitcheckup.py.

This commit is contained in:
voussoir 2021-04-04 17:30:54 -07:00
parent 1404b7e7e7
commit 68a45ab344
No known key found for this signature in database
GPG key ID: 5F7554F8C26DACCB

View file

@ -28,6 +28,11 @@ flags:
--push: --push:
Run `git push` in each directory. Run `git push` in each directory.
--run <command>:
Run `git <command>` in each directory. You can use \- to escape - in your
git arguments, since they would confuse this program's argparse.
If this is used, any --fetch, --pull, --push is ignored.
--add path: --add path:
Add path to the gitcheckup.txt file. Add path to the gitcheckup.txt file.
@ -38,9 +43,11 @@ Examples:
> gitcheckup > gitcheckup
> gitcheckup --fetch > gitcheckup --fetch
> gitcheckup D:\\Git\\cmd D:\\Git\\YCDL --pull > gitcheckup D:\\Git\\cmd D:\\Git\\YCDL --pull
> gitcheckup --run add README.md
''' '''
import argparse import argparse
import os import os
import re
import subprocess import subprocess
import sys import sys
@ -246,9 +253,19 @@ def checkup_pushed():
details.pushed = all_pushed details.pushed = all_pushed
return details return details
def gitcheckup(directory, do_fetch=False, do_pull=False, do_push=False): def gitcheckup(
directory,
do_fetch=False,
do_pull=False,
do_push=False,
run_command=None,
):
os.chdir(directory.absolute_path) os.chdir(directory.absolute_path)
if run_command:
command = [GIT, *run_command]
check_output(command)
else:
if do_fetch: if do_fetch:
git_fetch() git_fetch()
@ -298,9 +315,18 @@ def gitcheckup_argparse(args):
else: else:
directories = read_directories_file() directories = read_directories_file()
if args.run_command:
args.run_command = [re.sub(r'^\\-', '-', arg) for arg in args.run_command]
try: try:
for directory in directories: for directory in directories:
gitcheckup(directory, do_fetch=args.do_fetch, do_pull=args.do_pull, do_push=args.do_push) gitcheckup(
directory,
do_fetch=args.do_fetch,
do_pull=args.do_pull,
do_push=args.do_push,
run_command=args.run_command,
)
except subprocess.CalledProcessError as exc: except subprocess.CalledProcessError as exc:
sys.stdout.write(f'{exc.cmd} exited with status {exc.returncode}\n') sys.stdout.write(f'{exc.cmd} exited with status {exc.returncode}\n')
sys.stdout.write(exc.output.decode()) sys.stdout.write(exc.output.decode())
@ -314,6 +340,7 @@ def main(argv):
parser.add_argument('--pull', dest='do_pull', action='store_true') parser.add_argument('--pull', dest='do_pull', action='store_true')
parser.add_argument('--push', dest='do_push', action='store_true') parser.add_argument('--push', dest='do_push', action='store_true')
parser.add_argument('--add', dest='add_directory') parser.add_argument('--add', dest='add_directory')
parser.add_argument('--run', dest='run_command', nargs='+')
parser.add_argument('--remove', dest='remove_directory') parser.add_argument('--remove', dest='remove_directory')
parser.set_defaults(func=gitcheckup_argparse) parser.set_defaults(func=gitcheckup_argparse)