Skip to content

Commit

Permalink
Double check if files still exist before git-add
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
goerz committed Oct 20, 2019
1 parent 0f19ff7 commit 6fdf9b9
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions doctr/travis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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", "<unknown>")
TRAVIS_BRANCH = os.environ.get("TRAVIS_BRANCH", "<unknown>")
TRAVIS_COMMIT = os.environ.get("TRAVIS_COMMIT", "<unknown>")
Expand All @@ -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
Expand Down

0 comments on commit 6fdf9b9

Please sign in to comment.