From fd722b5cc3a53f7eec8a112adbd3f1b5ff963f80 Mon Sep 17 00:00:00 2001 From: danielballan Date: Mon, 22 May 2017 11:48:38 -0400 Subject: [PATCH 1/4] ENH: Support pushing to *.github.io from other repo. --- doctr/travis.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/doctr/travis.py b/doctr/travis.py index 86c46ad4..50b521c8 100644 --- a/doctr/travis.py +++ b/doctr/travis.py @@ -15,6 +15,8 @@ from cryptography.fernet import Fernet +DOCTR_WORKING_BRANCH = '__doctr_working_branch' + def decrypt_file(file, key): """ Decrypts the file ``file``. @@ -242,13 +244,9 @@ def checkout_deploy_branch(deploy_branch, canpush=True): Checkout the deploy branch, creating it if it doesn't exist. """ #create empty branch with .nojekyll if it doesn't already exist - new_deploy_branch = create_deploy_branch(deploy_branch, push=canpush) - print("Checking out {}".format(deploy_branch)) - local_deploy_branch_exists = deploy_branch in subprocess.check_output(['git', 'branch']).decode('utf-8').split() - if new_deploy_branch or local_deploy_branch_exists: - run(['git', 'checkout', deploy_branch]) - else: - run(['git', 'checkout', '-b', deploy_branch, '--track', 'doctr_remote/{}'.format(deploy_branch)]) + create_deploy_branch(deploy_branch, push=canpush) + print("Checking out doctr working branch tracking doctr_remote/{}".format(deploy_branch)) + run(['git', 'checkout', '-B', DOCTR_WORKING_BRANCH, '--track', 'doctr_remote/{}'.format(deploy_branch)]) print("Done") def deploy_branch_exists(deploy_branch): @@ -448,7 +446,7 @@ def push_docs(deploy_branch='gh-pages', retries=3): print("Pulling") code = run(['git', 'pull', 'doctr_remote', deploy_branch]) print("Pushing commit") - code = run(['git', 'push', '-q', 'doctr_remote', deploy_branch], exit=False) + code = run(['git', 'push', '-q', 'doctr_remote', '{}:{}'.format(DOCTR_WORKING_BRANCH, deploy_branch)]) if code: retries -= 1 print("Push failed, retrying") From f559abb1ba72a03e3b8c33ec2cad644418a29121 Mon Sep 17 00:00:00 2001 From: danielballan Date: Tue, 23 May 2017 08:35:28 -0400 Subject: [PATCH 2/4] API: deploy_branch default should depend on deploy_repo not deploy_dir If we are deploying *to* a github.io repo we must deploy to 'master' (GH does not even let you configure this -- it's always 'master') regardless of whether we are publishing content *from* a *.github.io repo or some other repo. Thus, deploy_repo, not deploy_dir. --- doctr/__main__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doctr/__main__.py b/doctr/__main__.py index e720b96e..4d9781a9 100644 --- a/doctr/__main__.py +++ b/doctr/__main__.py @@ -247,7 +247,7 @@ def deploy(args, parser): if args.deploy_branch_name: deploy_branch = args.deploy_branch_name else: - deploy_branch = 'master' if deploy_dir.endswith(('.github.io', '.github.com')) else 'gh-pages' + deploy_branch = 'master' if deploy_repo.endswith(('.github.io', '.github.com')) else 'gh-pages' current_commit = subprocess.check_output(['git', 'rev-parse', 'HEAD']).decode('utf-8').strip() try: From 33695381ac66b07b52d35d904ec98d166ca10287 Mon Sep 17 00:00:00 2001 From: danielballan Date: Tue, 23 May 2017 08:37:24 -0400 Subject: [PATCH 3/4] TST: Add a test deploying to drdoctr.github.io repo. --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index e2c11f93..dca7adaa 100644 --- a/.travis.yml +++ b/.travis.yml @@ -39,6 +39,7 @@ script: python -m doctr deploy --no-require-master --command "echo test; ls; touch docs/_build/html/test" docs; # Test syncing a tracked file with a change python -m doctr deploy --sync . --built-docs test; + python -m doctr deploy --deploy-repo drdoctr/drdoctr.github.io docs; fi - if [[ "${TESTS}" == "true" ]]; then pyflakes doctr; From 7408e1e845219ae0765c1045d8ebaa3fe6939995 Mon Sep 17 00:00:00 2001 From: danielballan Date: Tue, 23 May 2017 08:52:55 -0400 Subject: [PATCH 4/4] FIX: Properly create new master branch on deploy_remote if needed. Use the working branch approach in branch creation as well. Former output (from Travis failure): ``` Creating master branch git checkout --orphan master fatal: A branch named 'master' already exists. ``` --- doctr/travis.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/doctr/travis.py b/doctr/travis.py index 50b521c8..a5e59cdc 100644 --- a/doctr/travis.py +++ b/doctr/travis.py @@ -246,9 +246,17 @@ def checkout_deploy_branch(deploy_branch, canpush=True): #create empty branch with .nojekyll if it doesn't already exist create_deploy_branch(deploy_branch, push=canpush) print("Checking out doctr working branch tracking doctr_remote/{}".format(deploy_branch)) - run(['git', 'checkout', '-B', DOCTR_WORKING_BRANCH, '--track', 'doctr_remote/{}'.format(deploy_branch)]) + clear_working_branch() + run(['git', 'checkout', '-b', DOCTR_WORKING_BRANCH, '--track', 'doctr_remote/{}'.format(deploy_branch)]) print("Done") + return canpush + +def clear_working_branch(): + local_branch_names = subprocess.check_output(['git', 'branch']).decode('utf-8').split() + if DOCTR_WORKING_BRANCH in local_branch_names: + run(['git', 'branch', '-D', DOCTR_WORKING_BRANCH]) + def deploy_branch_exists(deploy_branch): """ Check if there is a remote branch with name specified in ``deploy_branch``. @@ -275,20 +283,24 @@ def create_deploy_branch(deploy_branch, push=True): Return True if ``deploy_branch`` was created, False if not. """ if not deploy_branch_exists(deploy_branch): - print("Creating {} branch".format(deploy_branch)) - run(['git', 'checkout', '--orphan', deploy_branch]) + print("Creating {} branch on doctr_remote".format(deploy_branch)) + clear_working_branch() + run(['git', 'checkout', '--orphan', DOCTR_WORKING_BRANCH]) # delete everything in the new ref. this is non-destructive to existing # refs/branches, etc... run(['git', 'rm', '-rf', '.']) - print("Adding .nojekyll file to {} branch".format(deploy_branch)) + print("Adding .nojekyll file to working branch") run(['touch', '.nojekyll']) run(['git', 'add', '.nojekyll']) run(['git', 'commit', '-m', 'Create new {} branch with .nojekyll'.format(deploy_branch)]) if push: - print("Pushing {} branch to remote".format(deploy_branch)) - run(['git', 'push', '-u', 'doctr_remote', deploy_branch]) - # return to master branch - run(['git', 'checkout', '-']) + print("Pushing working branch to remote {} branch".format(deploy_branch)) + run(['git', 'push', '-u', 'doctr_remote', '{}:{}'.format(DOCTR_WORKING_BRANCH, deploy_branch)]) + # return to master branch and clear the working branch + run(['git', 'checkout', 'master']) + run(['git', 'branch', '-D', DOCTR_WORKING_BRANCH]) + # fetch the remote so that doctr_remote/{deploy_branch} is resolved + run(['git', 'fetch', 'doctr_remote']) return True return False