Skip to content

Commit baf2cdd

Browse files
committed
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 synching, 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 befor calling "git add", respectively "removed" files are still deleted. If there are any changes to the status after the sync, a warning seems appropriate.
1 parent 0f19ff7 commit baf2cdd

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

doctr/travis.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,8 @@ def commit_docs(*, added, removed):
486486
Returns True if changes were committed and False if no changes were
487487
committed.
488488
"""
489+
from os.path import exists
490+
489491
TRAVIS_BUILD_NUMBER = os.environ.get("TRAVIS_BUILD_NUMBER", "<unknown>")
490492
TRAVIS_BRANCH = os.environ.get("TRAVIS_BRANCH", "<unknown>")
491493
TRAVIS_COMMIT = os.environ.get("TRAVIS_COMMIT", "<unknown>")
@@ -496,11 +498,25 @@ def commit_docs(*, added, removed):
496498

497499
DOCTR_COMMAND = ' '.join(map(shlex.quote, sys.argv))
498500

501+
# It may be that a custom command removed some of the synced
502+
# files, so we should double check whether files still exist
503+
added_confirmed = []
504+
for file in added:
505+
if exists(file):
506+
added_confirmed.append(file)
507+
else:
508+
print("Warning: File %s doesn't exist and won't be added." % file, file=sys.stderr)
509+
removed_confirmed = []
510+
for file in removed:
511+
if exists(file):
512+
print("Warning: File %s exists and won't be removed." % file, file=sys.stderr)
513+
else:
514+
removed_confirmed.append(file)
499515

500-
if added:
501-
run(['git', 'add', *added])
502-
if removed:
503-
run(['git', 'rm', *removed])
516+
if added_confirmed:
517+
run(['git', 'add', *added_confirmed])
518+
if removed_confirmed:
519+
run(['git', 'rm', *removed_confirmed])
504520

505521
commit_message = """\
506522
Update docs after building Travis build {TRAVIS_BUILD_NUMBER} of

0 commit comments

Comments
 (0)