feat: add command line argument to irgnore new git commits

- 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

🤖
This commit is contained in:
Sebastian Mark 2023-12-21 15:54:55 +01:00
parent 16bc0974e1
commit db6fd25741
3 changed files with 27 additions and 14 deletions

View file

@ -18,6 +18,10 @@ See `requirements.txt`
2. Adapt `config.yml.example` to your needs and save it as `config.yml` 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 3. Run `dc-ops` from the shell or from a cron job
## Parameters
See `dc-ops --help`
## Configuration Options ## Configuration Options
- `loglevel`: (optional) define loglevel. Defaults to "INFO" if not set. - `loglevel`: (optional) define loglevel. Defaults to "INFO" if not set.
- `compose-opts`: (optional) define additional parameters to `docker compose up -d`. - `compose-opts`: (optional) define additional parameters to `docker compose up -d`.

9
dc-ops
View file

@ -5,6 +5,7 @@
# CC-BY-SA (https://creativecommons.org/licenses/by-sa/4.0/deed.de) # CC-BY-SA (https://creativecommons.org/licenses/by-sa/4.0/deed.de)
# for civil use only # for civil use only
import argparse
import logging as log import logging as log
from pathlib import Path from pathlib import Path
@ -17,6 +18,12 @@ configfile = Path(__file__).with_name("config.yml")
with configfile.open("r") as f: with configfile.open("r") as f:
cfg = yaml.safe_load(f.read()) 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 # init logging
loglevel = cfg.get("loglevel", "INFO").upper() loglevel = cfg.get("loglevel", "INFO").upper()
log.basicConfig(format="%(message)s", level=loglevel) log.basicConfig(format="%(message)s", level=loglevel)
@ -35,7 +42,7 @@ for stack in cfg["stacks"]:
log.info(f"* processing {stackdir}") log.info(f"* processing {stackdir}")
# update repo and check for new commits # update repo and check for new commits
if not update_git_repo(stackdir): if not update_git_repo(stackdir, args.ignore_git_status):
continue continue
# run pre-command # run pre-command

View file

@ -22,33 +22,35 @@ def run_subprocess(command: str, workdir: str) -> bool:
return True 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 """update repo and check for new commits
Args: Args:
repo (str): path to git repo repo_path (str): path to git repo
ignore_git_status (bool): continue even if no changes
Returns: Returns:
bool: False if any step fails bool: False if any step fails
""" """
# create repo instance if it exists # create repo instance if it exists
try: try:
repo = git.Repo(repo) repo = git.Repo(repo_path)
except git.exc.NoSuchPathError: except git.exc.NoSuchPathError:
log.error("directory not found") log.error("directory not found")
return False return False
# try to fetch latest changes if not ignore_git_status:
try: # try to fetch latest changes
repo.git.fetch() try:
except git.exc.GitCommandError as e: repo.git.fetch()
log.error(str(e)) except git.exc.GitCommandError as e:
return False log.error(str(e))
return False
# check for new commits # check for new commits
if repo.rev_parse("HEAD") == repo.rev_parse(f"origin/{repo.active_branch}"): if repo.rev_parse("HEAD") == repo.rev_parse(f"origin/{repo.active_branch}"):
log.info("no changes - skipping") log.info("no changes - skipping")
return False return False
# pull remote changes to local branch # pull remote changes to local branch
try: try: