def test_branch_exists_locally_and_upstream(self): # There's an ourbranch upstream self.origin_repo.create_head("ourbranch") self.origin_repo.heads.ourbranch.checkout() self.add_file(self.origin_repo) upstream_commit = self.origin_repo.heads.ourbranch.commit self.origin_repo.heads.otherbranch.checkout() # Switch to otherbranch # We use the local branch, but update to the upstream tip self.local_repo.remotes.origin.fetch() self.local_repo.create_head("ourbranch") upstream_branch = get_branch( self.local_repo.remotes.origin, "ourbranch" ) self.local_repo.heads.ourbranch.set_tracking_branch(upstream_branch) self.local_repo.heads.ourbranch.checkout() self.add_file(self.local_repo) old_local_repo_commit = self.local_repo.heads.ourbranch.commit self.local_repo.heads.otherbranch.checkout() # Switch to otherbranch setup_local_branch(self.local_repo, "ourbranch") our_branch = self.local_repo.heads.ourbranch self.assertEqual(upstream_commit, our_branch.commit) self.assertNotEqual(old_local_repo_commit, our_branch.commit)
def test_branch_exists_nowhere_but_parent_does(self): # No "ourbranch" locally or upstream, so we branch from origin/develop setup_local_branch(self.local_repo, "ourbranch") our_branch = self.local_repo.heads.ourbranch self.assertEqual(self.origin_repo.heads.develop.commit, our_branch.commit) self.assertNotEqual(self.origin_repo.heads.master.commit, our_branch.commit)
def handle_updated_translation_branch(self, repo, legalcodes): # legalcodes whose translations have been updated and all belong to the same translation branch # if we update the branch, we'll also publish updates to its static files. if not legalcodes: return branch_name = legalcodes[0].branch_name() language_code = legalcodes[0].language_code version = legalcodes[0].license.version self.say(2, f"Updating branch {branch_name}") setup_local_branch(repo, branch_name) # Track the translation update using a TranslationBranch object # First-party/Local from licenses.models import TranslationBranch branch_object, _ = TranslationBranch.objects.get_or_create( branch_name=branch_name, language_code=language_code, version=version, complete=False, ) for legalcode in legalcodes: self.update_branch_for_legalcode(repo, legalcode, branch_object) self.say(2, "Publishing static files") call_command("publish", branch_name=branch_name) repo.index.add([ os.path.relpath(settings.DISTILL_DIR, settings.TRANSLATION_REPOSITORY_DIRECTORY) ]) # Commit and push this branch self.say(2, "Committing and pushing") commit_and_push_changes(repo, "Translation changes from Transifex.", "", push=True) self.say( 2, f"Updated branch {branch_name} with updated translations and pushed" ) # Don't need local branch anymore kill_branch(repo, branch_name) # Now that we know the new changes are upstream, save the LegalCode # objects with their new translation_last_updates, and the branch object. # First-party/Local from licenses.models import LegalCode LegalCode.objects.bulk_update( legalcodes, fields=["translation_last_update"], ) branch_object.save()
def test_branch_exists_nowhere_but_parent_does(self): # No "ourbranch" locally or upstream, so we branch from origin/main # # setup_local_branch uses settings.OFFICIAL_GIT_BRANCH. This function # will fail if that value is not "main". setup_local_branch(self.local_repo, "ourbranch") our_branch = self.local_repo.heads.ourbranch self.assertEqual(self.origin_repo.heads.main.commit, our_branch.commit) self.assertNotEqual( self.origin_repo.heads.otherbranch.commit, our_branch.commit )
def test_branch_exists_upstream(self): # There's an ourbranch upstream and we branch from that self.origin_repo.create_head("ourbranch") self.origin_repo.heads.ourbranch.checkout() self.add_file(self.origin_repo) self.origin_repo.heads.master.checkout() assert branch_exists(self.origin_repo, "ourbranch") setup_local_branch(self.local_repo, "ourbranch") our_branch = self.local_repo.heads.ourbranch self.assertEqual(self.origin_repo.heads.ourbranch.commit, our_branch.commit) self.assertNotEqual(self.origin_repo.heads.develop.commit, our_branch.commit) self.assertNotEqual(self.origin_repo.heads.master.commit, our_branch.commit)
def publish_branch(self, branch: str): """Workflow for publishing a single branch""" print(f"Publishing branch {branch}") with git.Repo(settings.TRANSLATION_REPOSITORY_DIRECTORY) as repo: setup_local_branch(repo, branch) self.run_django_distill() if repo.is_dirty(untracked_files=True): # Add any changes and new files commit_and_push_changes(repo, "Updated built HTML files", self.relpath, push=self.push) if repo.is_dirty(untracked_files=True): raise Exception( "Something went wrong, the repo is still dirty") else: print(f"\n{branch} build dir is up to date.\n")