From 6fdf9b9a8eb6c2c141e7c19bd0aebb09c92c4745 Mon Sep 17 00:00:00 2001 From: Michael Goerz Date: Sun, 20 Oct 2019 02:09:39 -0400 Subject: [PATCH] Double check if files still exist before git-add If the --command flag is used, the given command runs after syncing, but before committing. If the command removes any files that were "added" by the syncing, or re-creates files that were "removed" during the syncing, then the "git add" and "git rm" commands will fail. To get around this, we can just double check if "added" files are still there before calling "git add", respectively "removed" files are still deleted. If there are any changes to the status after the sync, a warning seems appropriate. --- doctr/travis.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/doctr/travis.py b/doctr/travis.py index 0f15eca5..6f2ccdd8 100644 --- a/doctr/travis.py +++ b/doctr/travis.py @@ -486,6 +486,8 @@ def commit_docs(*, added, removed): Returns True if changes were committed and False if no changes were committed. """ + from os.path import exists + TRAVIS_BUILD_NUMBER = os.environ.get("TRAVIS_BUILD_NUMBER", "") TRAVIS_BRANCH = os.environ.get("TRAVIS_BRANCH", "") TRAVIS_COMMIT = os.environ.get("TRAVIS_COMMIT", "") @@ -496,11 +498,25 @@ def commit_docs(*, added, removed): DOCTR_COMMAND = ' '.join(map(shlex.quote, sys.argv)) + # It may be that a custom command removed some of the synced + # files, so we should double check whether files still exist + added_confirmed = [] + for file in added: + if exists(file): + added_confirmed.append(file) + else: + print("Warning: File %s doesn't exist and won't be added." % file, file=sys.stderr) + removed_confirmed = [] + for file in removed: + if exists(file): + print("Warning: File %s exists and won't be removed." % file, file=sys.stderr) + else: + removed_confirmed.append(file) - if added: - run(['git', 'add', *added]) - if removed: - run(['git', 'rm', *removed]) + if added_confirmed: + run(['git', 'add', *added_confirmed]) + if removed_confirmed: + run(['git', 'rm', *removed_confirmed]) commit_message = """\ Update docs after building Travis build {TRAVIS_BUILD_NUMBER} of