From db6fd2574104e53737cc0fbbb24a5a47b45e830b Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Thu, 21 Dec 2023 15:54:55 +0100 Subject: [PATCH] feat: add command line argument to irgnore new git commits MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - add new argument `--ignore-git-status` to continue even if there are no new commits - update `update_git_repo` function to accept new argument and skip git status check if argument is passed - update README.md with new Parameters section 🤖 --- README.md | 4 ++++ dc-ops | 9 ++++++++- lib/helper.py | 28 +++++++++++++++------------- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index a3df4e3..c0c121f 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,10 @@ See `requirements.txt` 2. Adapt `config.yml.example` to your needs and save it as `config.yml` 3. Run `dc-ops` from the shell or from a cron job +## Parameters + +See `dc-ops --help` + ## Configuration Options - `loglevel`: (optional) define loglevel. Defaults to "INFO" if not set. - `compose-opts`: (optional) define additional parameters to `docker compose up -d`. diff --git a/dc-ops b/dc-ops index 92805cd..6c183f2 100755 --- a/dc-ops +++ b/dc-ops @@ -5,6 +5,7 @@ # CC-BY-SA (https://creativecommons.org/licenses/by-sa/4.0/deed.de) # for civil use only +import argparse import logging as log from pathlib import Path @@ -17,6 +18,12 @@ configfile = Path(__file__).with_name("config.yml") with configfile.open("r") as f: cfg = yaml.safe_load(f.read()) +# fmt: off +parser = argparse.ArgumentParser() +parser.add_argument("--ignore-git-status", action="store_true", help="continue even if there are no new commits") # noqa +args = parser.parse_args() +# fmt: on + # init logging loglevel = cfg.get("loglevel", "INFO").upper() log.basicConfig(format="%(message)s", level=loglevel) @@ -35,7 +42,7 @@ for stack in cfg["stacks"]: log.info(f"* processing {stackdir}") # update repo and check for new commits - if not update_git_repo(stackdir): + if not update_git_repo(stackdir, args.ignore_git_status): continue # run pre-command diff --git a/lib/helper.py b/lib/helper.py index 54970a8..2efa89e 100644 --- a/lib/helper.py +++ b/lib/helper.py @@ -22,33 +22,35 @@ def run_subprocess(command: str, workdir: str) -> bool: return True -def update_git_repo(repo: str) -> bool: +def update_git_repo(repo_path: str, ignore_git_status: bool) -> bool: """update repo and check for new commits Args: - repo (str): path to git repo + repo_path (str): path to git repo + ignore_git_status (bool): continue even if no changes Returns: bool: False if any step fails """ # create repo instance if it exists try: - repo = git.Repo(repo) + repo = git.Repo(repo_path) except git.exc.NoSuchPathError: log.error("directory not found") return False - # try to fetch latest changes - try: - repo.git.fetch() - except git.exc.GitCommandError as e: - log.error(str(e)) - return False + if not ignore_git_status: + # try to fetch latest changes + try: + repo.git.fetch() + except git.exc.GitCommandError as e: + log.error(str(e)) + return False - # check for new commits - if repo.rev_parse("HEAD") == repo.rev_parse(f"origin/{repo.active_branch}"): - log.info("no changes - skipping") - return False + # check for new commits + if repo.rev_parse("HEAD") == repo.rev_parse(f"origin/{repo.active_branch}"): + log.info("no changes - skipping") + return False # pull remote changes to local branch try: