def test_push_clone_three_way_merge(server_instance, repo_2_br_no_conf, managed_tmpdir): from hangar import Repository repo_2_br_no_conf.remote.add('origin', server_instance) push1 = repo_2_br_no_conf.remote.push('origin', 'master') assert push1 == 'master' push2 = repo_2_br_no_conf.remote.push('origin', 'testbranch') assert push2 == 'testbranch' test_head = repo_2_br_no_conf.log(branch='testbranch', return_contents=True)['head'] master_head = repo_2_br_no_conf.log(branch='master', return_contents=True)['head'] merge_cmt = repo_2_br_no_conf.merge('merge commit', 'master', 'testbranch') merge_head = repo_2_br_no_conf.log(branch='master', return_contents=True)['head'] merge_order = repo_2_br_no_conf.log(branch='master', return_contents=True)['order'] merge_push = repo_2_br_no_conf.remote.push('origin', 'master') assert merge_push == 'master' assert merge_head != master_head assert merge_head != test_head new_tmpdir = pjoin(managed_tmpdir, 'new') mkdir(new_tmpdir) newRepo = Repository(path=new_tmpdir, exists=False) newRepo.clone('Test User', '*****@*****.**', server_instance, remove_old=True) clone_head = newRepo.log(branch='master', return_contents=True)['head'] clone_order = newRepo.log(branch='master', return_contents=True)['order'] assert clone_head == merge_head == merge_cmt assert merge_order == clone_order newRepo._env._close_environments()
def test_cannot_operate_without_repo_init(managed_tmpdir): repo = Repository(path=managed_tmpdir, exists=False) with pytest.raises(RuntimeError): repo.writer_lock_held() with pytest.raises(RuntimeError): repo.checkout() with pytest.raises(RuntimeError): repo.writer_lock_held() with pytest.raises(RuntimeError): repo.log() with pytest.raises(RuntimeError): repo.summary() with pytest.raises(RuntimeError): repo.merge('fail', 'master', 'nonexistant') with pytest.raises(RuntimeError): repo.create_branch('test') with pytest.raises(RuntimeError): repo.list_branches() with pytest.raises(RuntimeError): repo.force_release_writer_lock() with pytest.raises(RuntimeError): repo.remote.add('origin', 'foo') with pytest.raises(RuntimeError): repo.remote.remove('origin') with pytest.raises(RuntimeError): repo.remote.fetch('origin', 'master') with pytest.raises(RuntimeError): repo.remote.fetch_data('origin', branch='master') with pytest.raises(RuntimeError): repo.remote.list_all() with pytest.raises(RuntimeError): repo.remote.ping('origin') with pytest.raises(RuntimeError): repo.remote.push('origin', 'master') with pytest.raises(RuntimeError): repo.remove_branch('master') with pytest.raises(RuntimeError): repo.path with pytest.raises(RuntimeError): repo.version with pytest.raises(RuntimeError): repo.writer_lock_held with pytest.raises(RuntimeError): repo.size_human with pytest.raises(RuntimeError): repo.size_nbytes assert repo._env.repo_is_initialized is False
def log(repo: Repository, startpoint): """Display commit graph starting at STARTPOINT (short-digest or name) If no argument is passed in, the staging area branch HEAD will be used as the starting point. """ from hangar.records.commiting import expand_short_commit_digest if startpoint is None: click.echo(repo.log()) elif startpoint in repo.list_branches(): click.echo(repo.log(branch=startpoint)) else: base_commit = expand_short_commit_digest(repo._env.refenv, startpoint) click.echo(repo.log(commit=base_commit))
def log(ctx, startpoint): """Display commit graph starting at STARTPOINT (short-digest or name) If no argument is passed in, the staging area branch HEAD will be used as the starting point. """ P = os.getcwd() repo = Repository(path=P) if startpoint is None: click.echo(repo.log()) elif startpoint in repo.list_branches(): click.echo(repo.log(branch=startpoint)) else: base_commit = expand_short_commit_digest(repo._env.refenv, startpoint) click.echo(repo.log(commit=base_commit))
def init_repo(name=None, email=None, overwrite=False): """ init hangar repo, create stock file and add details to .gitignore """ if not Path.cwd().joinpath('.git').exists(): warnings.warn( "initializing stock repository in a directory which is not a " "git repository. Some features won't work", UserWarning) repo = Repository(Path.cwd(), exists=False) if not overwrite and repo.initialized: commit_hash = repo.log(return_contents=True)['head'] print(f'Hangar Repo already exists at {repo.path}. ' f'Initializing it as stock repository') else: if name is None or email is None: raise ValueError("Both ``name`` and ``email`` cannot be None") commit_hash = '' repo.init(user_name=name, user_email=email, remove_old=overwrite) # closing the environment for avoiding issues in windows repo._env._close_environments() stock_file = Path.cwd() / 'head.stock' if stock_file.exists(): warnings.warn( "Trying to initialize an already initialized stock repository. " "No action taken", UserWarning) else: with open(str(stock_file), 'w+') as f: f.write(commit_hash) print("Stock file created") gitignore = Path.cwd() / '.gitignore' with open(str(gitignore), 'a+') as f: f.seek(0) if '.hangar' not in f.read(): f.write('\n# hangar artifacts\n.hangar\n')
def test_log(written_two_cmt_server_repo, capsys): server, base_repo = written_two_cmt_server_repo runner = CliRunner() with runner.isolated_filesystem(): with capsys.disabled(): P = getcwd() new_repo = Repository(P, exists=False) res = runner.invoke( cli.clone, ['--name', 'Foo Tester', '--email', '*****@*****.**', f'{server}'], obj=new_repo) assert res.exit_code == 0 assert new_repo.log() == base_repo.log() new_repo.log() with capsys.disabled(): res = runner.invoke(cli.log, ['master'], obj=new_repo) assert res.stdout == f"{capsys.readouterr().out}\n"
def test_clone(written_two_cmt_server_repo): server, base_repo = written_two_cmt_server_repo runner = CliRunner() with runner.isolated_filesystem(): P = getcwd() new_repo = Repository(P, exists=False) res = runner.invoke( cli.clone, ['--name', 'Foo Tester', '--email', '*****@*****.**', f'{server}'], obj=new_repo) assert res.exit_code == 0 newLog = new_repo.log(return_contents=True) baseLog = base_repo.log(return_contents=True) assert newLog == baseLog assert new_repo.summary() == base_repo.summary()
('mnist_test_images', 'mnist_test_labels')] for imgs, labels in arraysets_list: print(co.arraysets[imgs], co.arraysets[labels]) # In[17]: for i, (imgs, labels) in enumerate(arraysets_list): print(i) img_aset, label_aset = co.arraysets[imgs], co.arraysets[labels] with img_aset, label_aset: for idx, image in enumerate(data[i][0]): img_aset.add(data=image, name=idx) label_aset.add(data=np.array([data[i][1][idx]]), name=idx) # In[18]: co.arraysets['mnist_training_images'] # In[19]: co.commit('added all the mnist datasets') # In[20]: repo.log() # In[21]: co.close()
def log(b): P = os.getcwd() repo = Repository(path=P) click.echo(repo.log(branch_name=b))
class Hinterface(object): """ Interface class to interact with hangar repositories. It enables the APIs to ignore the internals of hangar and can utilize the high level functions """ def __init__(self, path, branch='master', arrayset_name=None, sample_name=None): if not path.exists(): raise FileNotFoundError("Repository does not exist") self.repo = Repository(path) # TODO: fix hangar's version compatibility check if not self.repo.initialized: raise RuntimeError("Repository not initialized") self.branch = branch self.arrayset_name = arrayset_name self.sample_name = sample_name self.rcheckout = self.repo.checkout(branch=self.branch) @classmethod def create_repo(cls, path, username, email, desc=None, create_path=True): # TODO: Remove if it is not necessary if not path.exists() and create_path: path.mkdir() repo = Repository(path) repo.init(username, email) @property def repo_details(self): cmt_details = self.repo.log(return_contents=True) # TODO: make sure pop returns the latest try: top = cmt_details['order'].pop() except IndexError: cmt_time = None else: cmt_time = cmt_details['specs'][top]['commit_time'] return { "last_commit_time": cmt_time, "total_commit_count": len(cmt_details["order"]), "branch_count": len(self.repo.list_branches()), "hangar_version": self.repo.version } @property def arraysets(self): if self.arrayset_name: yield self.rcheckout.arraysets[self.arrayset_name] else: for val in self.rcheckout.arraysets.values(): yield val @property def sample_names(self): if self.arrayset_name: aset = self.rcheckout[self.arrayset_name] yield aset.name, list(aset.keys()) else: for aset in self.rcheckout.arraysets.values(): yield aset.name, list(aset.keys()) def get_samples(self, plugin_name=None): pass