def test_sync_repository(self, mock_labbook_lfs_disabled): username = '******' lb = mock_labbook_lfs_disabled[2] bm = BranchManager(lb, username) assert bm.branches_remote == [] assert bm.branches_local == ['master'] jobs.publish_repository(lb, username=username, access_token='fake', id_token='fake-too') assert os.path.exists(lb.remote) # Assert that publish only pushes up the master branch. assert bm.branches_local == ['master'] assert bm.branches_remote == ['master'] assert bm.get_commits_ahead('master') == 0 assert bm.get_commits_behind('master') == 0 lb.write_readme("do a commit") assert bm.get_commits_ahead('master') == 1 assert bm.get_commits_behind('master') == 0 jobs.sync_repository(lb, username=username, override=MergeOverride.OURS, access_token='fake', id_token='fake-too') assert bm.get_commits_ahead('master') == 0 assert bm.get_commits_behind('master') == 0
def resolve_commits_behind(self, info): logged_in_user = get_logged_in_username() lb = InventoryManager().load_labbook(logged_in_user, self.owner, self.name) self._configure_git(lb, info) bm = BranchManager(lb) if self._fetch_loader: return self._fetch_loader.load( f"labbook&{logged_in_user}&{self.owner}&{self.name}").then( lambda _: bm.get_commits_behind(branch_name=self. branch_name)) else: return bm.get_commits_behind(branch_name=self.branch_name)
def sync_branch(repository: Repository, username: Optional[str], override: str, pull_only: bool, feedback_callback: Callable) -> int: """""" if not repository.has_remote: return 0 repository.sweep_uncommitted_changes() repository.git.fetch() bm = BranchManager(repository) branch_name = bm.active_branch if pull_only and branch_name not in bm.branches_remote: # Cannot pull when remote branch doesn't exist. feedback_callback("Pull complete - nothing to pull") return 0 if branch_name not in bm.branches_remote: # Branch does not exist, so push it to remote. _set_upstream_branch(repository, bm.active_branch, feedback_callback) return 0 else: pulled_updates_count = bm.get_commits_behind() _pull(repository, branch_name, override, feedback_callback) should_push = not pull_only if should_push: # Skip pushing back up if set to pull_only push_tokens = f'git push origin {branch_name}'.split() if branch_name not in bm.branches_remote: push_tokens.insert(2, "--set-upstream") call_subprocess(push_tokens, cwd=repository.root_dir) feedback_callback("Sync complete") else: feedback_callback("Pull complete") return pulled_updates_count
def resolve_commits_behind(self, info): lb = InventoryManager().load_labbook(get_logged_in_username(), self.owner, self.name) self._configure_git(lb, info) bm = BranchManager(lb) return bm.get_commits_behind(branch_name=self.branch_name)
def test_count_commits_behind_remote_when_no_change(self, mock_config_file, remote_labbook_repo, mock_labbook_lfs_disabled): # When the branch is up to date, ensure it doesn't report being behind. lb = mock_labbook_lfs_disabled[2] bm = BranchManager(lb, username='******') lb.add_remote("origin", remote_labbook_repo) bm.workon_branch('testing-branch') bm.fetch() behind = bm.get_commits_behind() ahead = bm.get_commits_ahead() assert ahead == 0 assert behind == 0
def test_count_commits_behind_for_local_branch(self, mock_config_file, remote_labbook_repo, mock_labbook_lfs_disabled): # When we're using a local branch, by definition it is never behind. lb = mock_labbook_lfs_disabled[2] bm = BranchManager(lb, username='******') lb.add_remote("origin", remote_labbook_repo) bm.create_branch("super-local-branch") bm.fetch() behind = bm.get_commits_behind() ahead = bm.get_commits_ahead() # Should be up-to-date. assert ahead == 0 assert behind == 0
def test_get_commits_with_local_changes(self, mock_config_file, remote_labbook_repo, mock_labbook_lfs_disabled): # When the branch is up to date, ensure it doesn't report being behind. lb = mock_labbook_lfs_disabled[2] lb.add_remote("origin", remote_labbook_repo) bm = BranchManager(lb, username='******') bm.workon_branch("testing-branch") # Do some stuff to make commits locally FileOperations.makedir(lb, 'code/rand_dir', create_activity_record=True) FileOperations.delete_files(lb, 'code', ['rand_dir']) behind = bm.get_commits_behind() ahead = bm.get_commits_ahead() assert ahead == 4 assert behind == 0
def test_get_commits_with_remote_changes(self, mock_config_file, remote_labbook_repo, mock_labbook_lfs_disabled): # When the branch is up to date, ensure it doesn't report being behind. lb = mock_labbook_lfs_disabled[2] lb.add_remote("origin", remote_labbook_repo) bm = BranchManager(lb, username='******') bm.workon_branch("testing-branch") from gtmcore.inventory.inventory import InventoryManager remote_lb = InventoryManager(mock_config_file[0]).load_labbook_from_directory(remote_labbook_repo) remote_bm = BranchManager(remote_lb, 'test') remote_bm.workon_branch("testing-branch") FileOperations.makedir(remote_lb, 'code/xyzdir', create_activity_record=True) bm.fetch() behind = bm.get_commits_behind() ahead = bm.get_commits_ahead() assert ahead == 0 assert behind == 2
def helper_resolve_commits_behind(self, dataset): """Helper to get the commits behind for a dataset. Used for linked datasets to see if they are out of date""" bm = BranchManager(dataset) bm.fetch() return bm.get_commits_behind(branch_name='master')