From cb899efa7f64fc0f2986f2cdf6160a6c13f161ce Mon Sep 17 00:00:00 2001 From: Bauke Date: Sat, 1 Oct 2022 12:51:53 +0200 Subject: [PATCH] Add simple-git-push script. --- .aliases.zsh | 1 + .local/bin/simple-git-push | 43 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100755 .local/bin/simple-git-push diff --git a/.aliases.zsh b/.aliases.zsh index f95259f..23f49ea 100644 --- a/.aliases.zsh +++ b/.aliases.zsh @@ -14,6 +14,7 @@ alias gd="git diff" alias gdc="git diff --cached" alias gl="git log" alias gls="git log --all --decorate --oneline --graph" +alias gp="simple-git-push" alias gs="git status" alias clipboard-to-file="xclip -sel clip -o > $1" diff --git a/.local/bin/simple-git-push b/.local/bin/simple-git-push new file mode 100755 index 0000000..f2f72e0 --- /dev/null +++ b/.local/bin/simple-git-push @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 + +import argparse +import subprocess +import sys +import typing + + +def main() -> None: + available_remotes = git_remote() + + if len(available_remotes) == 0: + print("No remotes found") + sys.exit(0) + + # Parse any extra arguments passed to use them in git_push, this way + # simple-git-push acts like an aliased command. + extra_args = argparse.ArgumentParser(add_help=False).parse_known_args()[1] + remotes_to_push = ["origin", "github"] + + for remote in available_remotes: + if remote in remotes_to_push: + git_push(remote, extra_args) + + +def git_remote() -> typing.List[str]: + """Run `git remote` and return found remote names.""" + + command = ["git", "remote"] + output = subprocess.run(command, capture_output=True, encoding="utf8") + return output.stdout.splitlines() + + +def git_push(remote: str, extra_args: typing.List[str]) -> None: + """Run `git push` for the given remote.""" + + command = ["git", "push", "--follow-tags", remote, *extra_args] + print(f"""Pushing: {" ".join(command)}""") + subprocess.run(command) + + +if __name__ == "__main__": + main()