def test_make_commit_should_set_last_commit(self): branch = Branch('master') branch.init_config() self.assertEquals(self.index.last_commit, 'None') self.index.add_new_file('TESTING.txt') self.index.make_commit("new commit", 'master') self.assertIsNotNone(self.index.last_commit)
def init(self): """Initializes repository with master branch""" branch = Branch('master') branch.init_config() self.__current_branch_name = 'master' self.head.init_config() self.head.current_branch = branch self.init_config()
def test_reset_should_reset_to_head_commit(self): head = Head() head.init_config() branch = Branch('master') branch.init_config() head.current_branch = branch commit = Commit('test') commit.branch_name = 'master' commit.init_config() head.current_branch.set_current_commit(commit) self.index.reset(head) self.assertEqual(self.index.last_commit.commit_number, commit.commit_number)
def set_current_branch_name(self, name): self.load_config() self.config['info']['current_branch'] = name branch = Branch.make_branch_from_config(name) commit = branch.get_current_commit() if commit is None: self.config['info']['last_commit'] = '' else: self.config['info']['last_commit'] = commit.commit_number self.save_config()
def test_reset_should_move_head_to_previous_commit(self): di = DirectoryInfo() di.init(os.getcwd()) head = Head() head.init_config() branch = Branch('master') branch.init_config() head.current_branch = branch commit = Commit('first') commit.branch_name = 'master' commit.init_config() prev_commit = Commit('second') prev_commit.branch_name = 'master' prev_commit.init_config() commit.set_previous_commit_number(prev_commit.commit_number) previous_commit_number = commit.get_previous_commit_number() head.current_branch.set_current_commit(commit) head.reset() current_commit = head.current_branch.get_current_commit() self.assertEqual(current_commit.commit_number, previous_commit_number)
def add_commit(self, commit): """Adds new commit to last branch, copying files""" di = DirectoryInfo() self.load_config() for file in commit.files_with_copying_paths: path = commit.files_with_copying_paths[file] commits_path = di.get_commits_path(self.__current_branch_name) commit_path = os.path.join(commits_path, commit.commit_number) if not os.path.exists(commit_path): os.makedirs(commit_path) copy_path = os.path.join(commit_path, file) copyfile(path, copy_path) file_hash = commit.files_hashes[file] print(f'File {file} saved - {file_hash}') commit.set_previous_commit_number(self.last_commit_number) self.last_commit_number = commit.commit_number self.config['info']['last_commit'] = commit.commit_number branch = Branch.make_branch_from_config(self.__current_branch_name) branch.set_current_commit(commit) self.save_config()
def reset(self) -> bool: """Moves head to previous commit""" self.load_config() branch = Branch(self.__current_branch_name) current_commit = branch.get_current_commit() if current_commit is None: print('Cannot reset: No commits found!') return False previous = current_commit.get_previous_commit() current_commit.delete_commit() if previous is None: branch.set_current_commit(None) print(f'Commits fully reset: no commits anymore') return False branch.set_current_commit(previous) commit_number = previous.commit_number commit_message = previous.commit_message print(f'New head commit is {commit_number} {commit_message}') return True
def make_commit(self, commit_message, branch_name) -> Commit: """ Makes commit, freezing current files state :returns Commit """ self.load_config() commit = Commit(commit_message) commit.branch_name = branch_name commit.init_config() branch = Branch.make_branch_from_config(branch_name) prev_commit = branch.get_current_commit() if prev_commit is not None: commit_number = prev_commit.commit_number commit.set_previous_commit_number(commit_number) commit.freeze_files(self.__indexed_files, self.__directory) self.__last_commit = commit self.config['info']['files'] = '' self.config['info']['last_commit'] = commit.commit_number self.config['info']['last_commit_branch'] = commit.branch_name self.save_config() return commit
def diff(self, filename, first_version, second_version): branch_name = self.repository.current_branch.name branch = Branch.make_branch_from_config(branch_name) branch.diff(filename, first_version, second_version)
def update(self, filename, version): """Updates file with a specific version from repository""" branch_name = self.repository.current_branch.name branch = Branch.make_branch_from_config(branch_name) branch.update(filename, version)
def test_make_commit_should_return_new_commit(self): branch = Branch('master') branch.init_config() self.index.add_new_file('TESTING.txt') commit = self.index.make_commit("new commit", 'master') self.assertIsNotNone(commit)
def current_branch(self): self.load_config() branch = Branch.make_branch_from_config(self.__current_branch_name) return branch