def test_push_all_with_reference_and_revision(self): git1 = GitCmd(self.main_repo_bare) git2 = GitCmd(self.cloned_from_repo) repo2 = Repository(self.cloned_from_repo) repo2.commit('A commit', allow_empty=True) cs = repo2.commit('A second commit', allow_empty=True) repo2.tag('unqualified', revision=cs.hash) notes_ref = repo2.append_note('some note dude', cs.hash) repo2.push(self.main_repo, self.main_repo_bare, rev=cs.hash, ref_name='master') notes_ref_repo1, commit_ref_repo1 = git1('notes', 'list').split() notes_ref_repo2, commit_ref_repo2 = git2('notes', 'list').split() self.assertEqual(commit_ref_repo1, commit_ref_repo2) self.assertEqual(notes_ref_repo1, notes_ref_repo2) self.assertEqual(notes_ref, notes_ref_repo1) changesets1 = list( git1('log', 'unqualified', '--', pretty='oneline', _iter=True)) changesets2 = list( git2('log', 'unqualified', '--', pretty='oneline', _iter=True)) self.assertEquals(changesets1, changesets2)
def test_commit_custom_parent(self): gitrepo = Repository(self.main_repo) gitrepo.update('master') c1 = gitrepo.commit('A commit', allow_empty=True) c2 = gitrepo.commit('Other commit', allow_empty=True) gitrepo.commit('Commit with custom parent', allow_empty=True, custom_parent=c1.hash) self.assertEquals([p.hash for p in gitrepo.parents()], [c2.hash, c1.hash])
def test_commit_custom_parent(self): gitrepo = Repository(self.main_repo) gitrepo.update('master') c1 = gitrepo.commit('A commit', allow_empty=True) c2 = gitrepo.commit('Other commit', allow_empty=True) gitrepo.commit('Commit with custom parent', allow_empty=True, custom_parent=c1.hash) self.assertEquals( [p.hash for p in gitrepo.parents()], [c2.hash, c1.hash])
def test_commit_commits_but_with_removed_files(self): file_name = "test1.txt" file_path = os.path.join(self.main_repo, file_name) commit_msg = "Test message" gitrepo = Repository(self.main_repo) gitrepo.update('master') os.remove(file_path) git = GitCmd(self.main_repo) gitrepo.commit(commit_msg) git('reset', hard=True) self.assertTrue(os.path.exists(file_path))
def test_commit_commits_but_with_removed_files(self): file_name = "test1.txt" file_path = os.path.join(self.main_repo, file_name) commit_msg = "Test message" gitrepo = Repository(self.main_repo) gitrepo.update('master') os.remove(file_path) gitrepo.commit(commit_msg) repo = pygit2.Repository(self.main_repo) repo.reset(repo.head.target.hex, pygit2.GIT_RESET_HARD) repo.checkout_head(strategy=(pygit2.GIT_CHECKOUT_FORCE | pygit2.GIT_CHECKOUT_REMOVE_UNTRACKED)) self.assertTrue(os.path.exists(file_path))
def test_no_notes_go_fine(self): gitrepo = Repository(self.cloned_from_repo) gitrepo.update('master') changeset = gitrepo.commit('A new commit!', allow_empty=True) notes = gitrepo.get_changeset_notes(changeset.hash) self.assertEqual([], notes) notes = gitrepo.get_changeset_notes() self.assertEqual([], notes)
def test_commit(self): file_name = "test_file" file_path = os.path.join(self.main_repo, file_name) with open(file_path, "a") as file: file.write('test content') commit_msg = "Test message" git = GitCmd(self.main_repo) initial_len = len(list(git('log', 'HEAD', pretty='oneline', _iter=True))) gitrepo = Repository(self.main_repo) gitrepo.add(file_name) commit = gitrepo.commit(commit_msg) final_len = len(list(git('log', 'HEAD', pretty='oneline', _iter=True))) self.assertEquals(final_len, initial_len + 1) self.assertEquals(git('log', '-1', pretty='%B'), commit_msg) self.assertEquals(commit.desc, commit_msg) self.assertIsNone(gitrepo.commit(commit_msg))
def test_commit_commits_all(self): file_name = "test1.txt" file_path = os.path.join(self.main_repo, file_name) expected_content = "changed content" commit_msg = "Test message" with open(file_path, "w+") as file: file.write(expected_content) gitrepo = Repository(self.main_repo) gitrepo.commit(commit_msg) with open(file_path, "w+") as fd: fd.write('content changed again') git = GitCmd(self.main_repo) git('reset', hard=True) self.assertTrue(os.path.exists(file_path)) with open(file_path) as fd: self.assertEquals(expected_content, fd.read())
def test_commit(self): file_name = "test_file" file_path = os.path.join(self.main_repo, file_name) with open(file_path, "a") as file: file.write('test content') commit_msg = "Test message" repo = pygit2.Repository(self.main_repo) initial_len = len(list(repo.walk(repo.head.target, pygit2.GIT_SORT_TOPOLOGICAL))) gitrepo = Repository(self.main_repo) gitrepo.add(file_name) commit = gitrepo.commit(commit_msg) final_len = len(list(repo.walk(repo.head.target, pygit2.GIT_SORT_TOPOLOGICAL))) self.assertEquals(final_len, initial_len + 1) self.assertEquals(repo.head.get_object().message, commit_msg) self.assertEquals(commit.desc, commit_msg) self.assertIsNone(gitrepo.commit(commit_msg))
def test_commit(self): file_name = "test_file" file_path = os.path.join(self.main_repo, file_name) with open(file_path, "a") as file: file.write('test content') commit_msg = "Test message" git = GitCmd(self.main_repo) initial_len = len( list(git('log', 'HEAD', pretty='oneline', _iter=True))) gitrepo = Repository(self.main_repo) gitrepo.add(file_name) commit = gitrepo.commit(commit_msg) final_len = len(list(git('log', 'HEAD', pretty='oneline', _iter=True))) self.assertEquals(final_len, initial_len + 1) self.assertEquals(git('log', '-1', pretty='%B'), commit_msg) self.assertEquals(commit.desc, commit_msg) self.assertIsNone(gitrepo.commit(commit_msg))
def test_commit_commits_all(self): file_name = "test1.txt" file_path = os.path.join(self.main_repo, file_name) expected_content = "changed content" commit_msg = "Test message" with open(file_path, "w+") as file: file.write(expected_content) gitrepo = Repository(self.main_repo) gitrepo.commit(commit_msg) with open(file_path, "w+") as fd: fd.write('content changed again') repo = pygit2.Repository(self.main_repo) repo.reset(repo.head.target.hex, pygit2.GIT_RESET_HARD) repo.checkout_head(strategy=(pygit2.GIT_CHECKOUT_FORCE | pygit2.GIT_CHECKOUT_REMOVE_UNTRACKED)) self.assertTrue(os.path.exists(file_path)) with open(file_path) as fd: self.assertEquals(expected_content, fd.read())
def test_push_all_with_reference_and_revision(self): git1 = GitCmd(self.main_repo_bare) git2 = GitCmd(self.cloned_from_repo) repo2 = Repository(self.cloned_from_repo) repo2.commit('A commit', allow_empty=True) cs = repo2.commit('A second commit', allow_empty=True) repo2.tag('unqualified', revision=cs.hash) notes_ref = repo2.append_note('some note dude', cs.hash) repo2.push(self.main_repo, self.main_repo_bare, rev=cs.hash, ref_name='master') notes_ref_repo1, commit_ref_repo1 = git1('notes', 'list').split() notes_ref_repo2, commit_ref_repo2 = git2('notes', 'list').split() self.assertEqual(commit_ref_repo1, commit_ref_repo2) self.assertEqual(notes_ref_repo1, notes_ref_repo2) self.assertEqual(notes_ref, notes_ref_repo1) changesets1 = list(git1('log', 'unqualified', '--', pretty='oneline', _iter=True)) changesets2 = list(git2('log', 'unqualified', '--', pretty='oneline', _iter=True)) self.assertEquals(changesets1, changesets2)
def test_push_to_unqualified_destination(self): git1 = GitCmd(self.main_repo_bare) git2 = GitCmd(self.cloned_from_repo) repo2 = Repository(self.cloned_from_repo) cs = repo2.commit('A commit', allow_empty=True) # Pushing a revision to a reference name that doesn't exist is # considered a push to an unqualified destination repo2.push(self.main_repo, self.main_repo_bare, rev=cs.hash, ref_name='unqualified') changesets1 = list(git1('log', 'unqualified', pretty='oneline', _iter=True)) changesets2 = list(git2('log', cs.hash, pretty='oneline', _iter=True)) self.assertEquals(changesets1, changesets2)
def test_append_get_and_has_notes(self): gitrepo = Repository(self.cloned_from_repo) gitrepo.update('master') changeset = gitrepo.commit('A new commit!', allow_empty=True) gitrepo.append_note('Hello note 1', revision=changeset.hash) gitrepo.append_note('Goodbye note 2') notes = gitrepo.get_changeset_notes(changeset.hash) self.assertEqual(['Hello note 1', 'Goodbye note 2'], notes) self.assertTrue(gitrepo.has_note('Hello note 1')) self.assertTrue(gitrepo.has_note('Goodbye note 2', changeset.hash)) self.assertFalse(gitrepo.has_note('')) self.assertFalse(gitrepo.has_note('\n'))
def test_push_to_unqualified_destination(self): git1 = GitCmd(self.main_repo_bare) git2 = GitCmd(self.cloned_from_repo) repo2 = Repository(self.cloned_from_repo) cs = repo2.commit('A commit', allow_empty=True) # Pushing a revision to a reference name that doesn't exist is # considered a push to an unqualified destination repo2.push(self.main_repo, self.main_repo_bare, rev=cs.hash, ref_name='unqualified') changesets1 = list( git1('log', 'unqualified', pretty='oneline', _iter=True)) changesets2 = list(git2('log', cs.hash, pretty='oneline', _iter=True)) self.assertEquals(changesets1, changesets2)
def test_push_only_notes(self): git1 = GitCmd(self.main_repo_bare) git2 = GitCmd(self.cloned_from_repo) repo2 = Repository(self.cloned_from_repo) cs = repo2.commit('A commit', allow_empty=True) repo2.push(self.main_repo, self.main_repo_bare, rev=cs.hash, ref_name='master') notes_ref = repo2.append_note('some note dude', cs.hash) repo2.push(self.main_repo, self.main_repo_bare, ref_name='refs/notes/*') notes_ref_repo1, commit_ref_repo1 = git1('notes', 'list').split() notes_ref_repo2, commit_ref_repo2 = git2('notes', 'list').split() self.assertEqual(commit_ref_repo1, commit_ref_repo2) self.assertEqual(notes_ref_repo1, notes_ref_repo2) self.assertEqual(notes_ref, notes_ref_repo1)
def test_merge_with_conflict(self): gitrepo = Repository(self.cloned_from_repo) # Checkout gitrepo.update('newbranch') file_to_conflict_name = 'test1.txt' file_to_conflict = os.path.join(self.cloned_from_repo, file_to_conflict_name) with open(file_to_conflict, "w") as file: file_content = "Absurd content" file.write(file_content) gitrepo.add(file_to_conflict_name) conflict_cs = gitrepo.commit("Provoking conflict") gitrepo.update('master') try: gitrepo.merge(other_rev=conflict_cs) self.fail() except MergeConflictError as exp: self.assertTrue('Conflicts found: merging test1.txt failed' in exp)
def test_merge_fastforward_no_ff(self): git = GitCmd(self.cloned_from_repo) gitrepo = Repository(self.cloned_from_repo) gitrepo.update('master') gitrepo.branch('ff-branch') ff_file_name = 'ff-file.txt' ff_file = os.path.join(self.cloned_from_repo, ff_file_name) with open(ff_file, "w") as file: file_content = "Absurd content" file.write(file_content) gitrepo.add(ff_file_name) ff_head = gitrepo.commit(message="commit ff file") gitrepo.update('master') cs = gitrepo.merge(other_rev=ff_head, other_branch_name='test') self.assertEquals(len(git('log', '-1', pretty='%P').split()), 2) self.assertEquals(git('rev-parse', 'HEAD'), cs.hash) # We want a commit in fastforward merges, hashes must be different self.assertNotEquals(ff_head.hash, cs.hash) self.assertTrue(os.path.isfile(ff_file))
def test_merge_fastforward_no_ff(self): repo = pygit2.Repository(self.cloned_from_repo) gitrepo = Repository(self.cloned_from_repo) gitrepo.update('master') gitrepo.branch('ff-branch') ff_file_name = 'ff-file.txt' ff_file = os.path.join(self.cloned_from_repo, ff_file_name) with open(ff_file, "w") as file: file_content = "Absurd content" file.write(file_content) gitrepo.add(ff_file_name) ff_head = gitrepo.commit(message="commit ff file") gitrepo.update('master') cs = gitrepo.merge(other_rev=ff_head, other_branch_name='test') self.assertEquals(len(repo.head.get_object().parents), 2) self.assertEquals(repo.head.get_object().hex, cs.hash) # We want a commit in fastforward merges, hashes must be different self.assertNotEquals(ff_head.hash, cs.hash) self.assertTrue(os.path.isfile(ff_file))
def test_merge_fastforward(self): repo = pygit2.Repository(self.cloned_from_repo) gitrepo = Repository(self.cloned_from_repo) gitrepo.update('master') gitrepo.branch('ff-branch') ff_file_name = 'ff-file.txt' ff_file = os.path.join(self.cloned_from_repo, ff_file_name) with open(ff_file, "w") as file: file_content = "Absurd content" file.write(file_content) gitrepo.add(ff_file_name) signature = Signature(user="******") ff_head = gitrepo.commit(message="commit ff file", signature=signature) gitrepo.update('master') cs = gitrepo.merge_fastforward( signature, other_rev=ff_head, other_branch_name='test') self.assertEquals(len(repo.head.get_object().parents), 1) self.assertEquals(repo.head.get_object().hex, cs.hash) self.assertEquals(ff_head.hash, cs.hash) self.assertTrue(os.path.isfile(ff_file))
def test_merge_with_conflict(self): gitrepo = Repository(self.cloned_from_repo) # Checkout gitrepo.update('newbranch') file_to_conflict_name = 'test1.txt' file_to_conflict = os.path.join(self.cloned_from_repo, file_to_conflict_name) with open(file_to_conflict, "w") as file: file_content = "Absurd content" file.write(file_content) gitrepo.add(file_to_conflict_name) conflict_cs = gitrepo.commit("Provoking conflict") gitrepo.update('master') try: gitrepo.merge(other_rev=conflict_cs) self.fail('Merge with conflict should have failed') except MergeConflictError as exp: print exp self.assertTrue('Conflicts found: merging test1.txt failed' in exp)