def test_add_files(self):
        file_name = "absurd_file"
        file_path = os.path.join(self.main_repo, file_name)
        file_name2 = "absurd_file2"
        file_path2 = os.path.join(self.main_repo, file_name2)
        with open(file_path, "w") as file:
            file_content = "Absurd content"
            file.write(file_content)
        with open(file_path2, "w") as file:
            file_content = "Absurd content2"
            file.write(file_content)

        repo = pygit2.Repository(self.main_repo)
        status = repo.status()
        self.assertTrue(file_name in status)
        self.assertTrue(file_name2 in status)
        self.assertEquals(status[file_name], pygit2.GIT_STATUS_WT_NEW)
        self.assertEquals(status[file_name2], pygit2.GIT_STATUS_WT_NEW)
        gitrepo = Repository(self.main_repo)
        gitrepo.add([file_name, file_name2])
        status = repo.status()
        self.assertEquals(status[file_name], pygit2.GIT_STATUS_INDEX_NEW)
        self.assertEquals(status[file_name2], pygit2.GIT_STATUS_INDEX_NEW)
        with self.assertRaises(RepositoryError):
            gitrepo.add("nonexistentfile")
 def test_merge_isuptodate(self):
     gitrepo = Repository(self.cloned_from_repo)
     gitrepo.update('master')
     uptodate_hash = '52109e71fd7f16cb366acfcbb140d6d7f2fc50c9'
     cs = gitrepo[uptodate_hash]
     should_be_none = gitrepo.merge(other_rev=cs)
     self.assertIsNone(should_be_none)
    def test_add_files(self):
        file_name = "absurd_file"
        file_path = os.path.join(self.main_repo, file_name)
        file_name2 = "absurd_file2"
        file_path2 = os.path.join(self.main_repo, file_name2)
        with open(file_path, "w") as file:
            file_content = "Absurd content"
            file.write(file_content)
        with open(file_path2, "w") as file:
            file_content = "Absurd content2"
            file.write(file_content)

        def get_status():
            git = GitCmd(self.main_repo)
            status = {}
            for f in git('status', porcelain=True, _iter=True):
                s, path = f.split()
                status[path] = s
            return status

        status = get_status()
        self.assertTrue(file_name in status)
        self.assertTrue(file_name2 in status)
        self.assertEquals(status[file_name], '??')
        self.assertEquals(status[file_name2], '??')

        gitrepo = Repository(self.main_repo)
        gitrepo.add([file_name, file_name2])

        status = get_status()
        self.assertEquals(status[file_name], 'A')
        self.assertEquals(status[file_name2], 'A')
        with self.assertRaises(RepositoryError):
            gitrepo.add("nonexistentfile")
    def test_update(self):
        repo_name = 'fixture-3'
        path = os.path.join(self.environment_path, repo_name)
        self.add_content_to_repo(
            os.path.join(FIXTURE_PATH, 'fixture-3.git.bundle'), path)
        git = GitCmd(path)
        gitrepo = Repository(path)

        gitrepo.update("master")
        self.assertFalse(os.path.isfile(os.path.join(path, 'file2.txt')))
        self.assertTrue(os.path.isfile(os.path.join(path, 'file1.txt')))
        self.assertFalse(os.path.isfile(os.path.join(path, 'file3.txt')))
        self.assertNotEquals(git('rev-parse', '--abbrev-ref', 'HEAD'), 'HEAD')

        gitrepo.update("branch-1")
        self.assertTrue(os.path.isfile(os.path.join(path, 'file2.txt')))
        self.assertTrue(os.path.isfile(os.path.join(path, 'file1.txt')))
        self.assertFalse(os.path.isfile(os.path.join(path, 'file3.txt')))
        self.assertNotEquals(git('rev-parse', '--abbrev-ref', 'HEAD'), 'HEAD')

        gitrepo.update("branch-2")
        self.assertTrue(os.path.isfile(os.path.join(path, 'file3.txt')))
        self.assertFalse(os.path.isfile(os.path.join(path, 'file2.txt')))
        self.assertTrue(os.path.isfile(os.path.join(path, 'file1.txt')))
        self.assertNotEquals(git('rev-parse', '--abbrev-ref', 'HEAD'), 'HEAD')

        gitrepo.update("08b952ae66e59b216b1171c0c57082353bc80863")
        self.assertFalse(os.path.isfile(os.path.join(path, 'file3.txt')))
        self.assertFalse(os.path.isfile(os.path.join(path, 'file2.txt')))
        self.assertTrue(os.path.isfile(os.path.join(path, 'file1.txt')))
        self.assertEquals(git('rev-parse', '--abbrev-ref', 'HEAD'), 'HEAD')
    def test_push(self):
        gitrepo1 = pygit2.Repository(self.main_repo_bare)
        gitrepo2 = pygit2.Repository(self.cloned_from_repo)
        print "Main repo %s " % gitrepo1.path
        print "Cloned from repo %s " % gitrepo2.path

        walk_topological = lambda repo: repo.walk(
            repo.head.target, pygit2.GIT_SORT_TOPOLOGICAL)

        changesets1 = list(walk_topological(gitrepo1))
        changesets2 = list(walk_topological(gitrepo2))
        self.assertNotEqual(len(changesets1), len(changesets2))

        repo2 = Repository(self.cloned_from_repo)
        with self.assertRaises(RepositoryError):
            repo2.push(
                self.main_repo_bare,
                "inexistent_destination",
                ref_name='master')

        repo2.push(self.main_repo, self.main_repo_bare, ref_name='master')

        changesets1 = list(walk_topological(gitrepo1))
        changesets2 = list(walk_topological(gitrepo2))
        self.assertEquals(len(changesets1), len(changesets2))
    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_pull(self):
        gitrepo1 = GitCmd(self.main_repo)
        gitrepo2 = GitCmd(self.cloned_from_repo)

        self.assertNotEqual(
            gitrepo1('rev-list', all=True).split(),
            gitrepo2('rev-list', all=True).split())

        repo = Repository(self.main_repo)

        # Pulling a branch
        self.assertNotIn('newbranch', [b.name for b in repo.get_branches()])
        repo.pull(remote=self.cloned_from_repo, branch='newbranch')
        self.assertIn('newbranch', [b.name for b in repo.get_branches()])

        # Pulling everything
        repo.pull(remote=self.cloned_from_repo)

        self.assertEqual(
            gitrepo1('rev-list', all=True).split().sort(),
            gitrepo2('rev-list', all=True).split().sort())

        gitrepo1_refs = list(gitrepo1('show-ref', _iter=True))
        gitrepo2_refs = list(gitrepo2('show-ref', _iter=True))

        # Check that all remote refs have been fetched
        for ref in gitrepo2_refs:
            self.assertIn(ref, gitrepo1_refs)

        # Pulling from a non existing remote
        with self.assertRaises(RepositoryError):
            repo.pull(remote='wrong repo')
 def test_merge_isuptodate(self):
     gitrepo = Repository(self.cloned_from_repo)
     gitrepo.update('master')
     uptodate_hash = '52109e71fd7f16cb366acfcbb140d6d7f2fc50c9'
     cs = gitrepo[uptodate_hash]
     should_be_none = gitrepo.merge(other_rev=cs)
     self.assertIsNone(should_be_none)
Beispiel #9
0
 def test_merge_no_conflicts(self):
     git = GitCmd(self.cloned_from_repo)
     headnewbranch = git('rev-parse', 'refs/heads/newbranch')
     gitrepo = Repository(self.cloned_from_repo)
     # Checkout to master
     gitrepo.update('master')
     cs = gitrepo.merge(other_rev=gitrepo[headnewbranch])
     self.assertEquals(len(git('log', '-1', pretty='%P').split()), 2)
     self.assertEquals(git('rev-parse', 'HEAD'), cs.hash)
    def test_update_failures(self):
        repo_name = 'fixture-3'
        self.add_content_to_repo(
            os.path.join(FIXTURE_PATH, 'fixture-3.git.bundle'),
            os.path.join(self.environment_path, repo_name))
        gitrepo = Repository(os.path.join(self.environment_path, repo_name))

        with self.assertRaises(RepositoryError):
            gitrepo.update("doesntexist")
 def test_strip(self):
     repo = pygit2.Repository(self.cloned_from_repo)
     gitrepo = Repository(self.cloned_from_repo)
     old_head = repo.head.get_object()
     parent_old_head = old_head.parents[0]
     gitrepo.strip(gitrepo[old_head.hex])
     new_head = repo.head.get_object()
     self.assertNotEquals(old_head.hex, new_head.hex)
     self.assertEquals(new_head.hex, parent_old_head.hex)
    def test_update_failures(self):
        repo_name = 'fixture-3'
        self.add_content_to_repo(
            os.path.join(FIXTURE_PATH, 'fixture-3.git.bundle'),
            os.path.join(self.environment_path, repo_name))
        gitrepo = Repository(os.path.join(self.environment_path, repo_name))

        with self.assertRaises(RepositoryError):
            gitrepo.update("doesntexist")
 def test_strip(self):
     git = GitCmd(self.cloned_from_repo)
     gitrepo = Repository(self.cloned_from_repo)
     old_head = git('rev-parse', 'HEAD')
     parent_old_head = git('log', '-1', pretty='%P').split()[0]
     gitrepo.strip(gitrepo[old_head])
     new_head = git('rev-parse', 'HEAD')
     self.assertNotEquals(old_head, new_head)
     self.assertEquals(new_head, parent_old_head)
 def test_strip(self):
     git = GitCmd(self.cloned_from_repo)
     gitrepo = Repository(self.cloned_from_repo)
     old_head = git('rev-parse', 'HEAD')
     parent_old_head = git('log', '-1', pretty='%P').split()[0]
     gitrepo.strip(gitrepo[old_head])
     new_head = git('rev-parse', 'HEAD')
     self.assertNotEquals(old_head, new_head)
     self.assertEquals(new_head, parent_old_head)
 def test_merge_no_conflicts(self):
     repo = pygit2.Repository(self.cloned_from_repo)
     headnewbranch = repo.lookup_reference(
         'refs/remotes/origin/newbranch').get_object().hex
     gitrepo = Repository(self.cloned_from_repo)
     # Checkout to master
     gitrepo.update('master')
     cs = gitrepo.merge(other_rev=gitrepo[headnewbranch])
     self.assertEquals(len(repo.head.get_object().parents), 2)
     self.assertEquals(repo.head.get_object().hex, cs.hash)
    def test_merge_no_conflicts(self):
        git = GitCmd(self.cloned_from_repo)

        headnewbranch = git('rev-parse', 'refs/heads/newbranch')
        gitrepo = Repository(self.cloned_from_repo)
        # Checkout to master
        gitrepo.update('master')
        cs = gitrepo.merge(other_rev=gitrepo[headnewbranch])
        self.assertEquals(len(git('log', '-1', pretty='%P').split()), 2)
        self.assertEquals(git('rev-parse', 'HEAD'), cs.hash)
    def test_update(self):
        repo_name = 'fixture-3'
        path = os.path.join(self.environment_path, repo_name)
        self.add_content_to_repo(
            os.path.join(FIXTURE_PATH, 'fixture-3.git.bundle'),
            path)
        git = GitCmd(path)
        gitrepo = Repository(path)

        gitrepo.update("master")
        self.assertFalse(os.path.isfile(os.path.join(path, 'file2.txt')))
        self.assertTrue(os.path.isfile(os.path.join(path, 'file1.txt')))
        self.assertFalse(os.path.isfile(os.path.join(path, 'file3.txt')))
        self.assertNotEquals(git('rev-parse', '--abbrev-ref', 'HEAD'), 'HEAD')

        gitrepo.update("branch-1")
        self.assertTrue(os.path.isfile(os.path.join(path, 'file2.txt')))
        self.assertTrue(os.path.isfile(os.path.join(path, 'file1.txt')))
        self.assertFalse(os.path.isfile(os.path.join(path, 'file3.txt')))
        self.assertNotEquals(git('rev-parse', '--abbrev-ref', 'HEAD'), 'HEAD')

        gitrepo.update("branch-2")
        self.assertTrue(os.path.isfile(os.path.join(path, 'file3.txt')))
        self.assertFalse(os.path.isfile(os.path.join(path, 'file2.txt')))
        self.assertTrue(os.path.isfile(os.path.join(path, 'file1.txt')))
        self.assertNotEquals(git('rev-parse', '--abbrev-ref', 'HEAD'), 'HEAD')

        gitrepo.update("08b952ae66e59b216b1171c0c57082353bc80863")
        self.assertFalse(os.path.isfile(os.path.join(path, 'file3.txt')))
        self.assertFalse(os.path.isfile(os.path.join(path, 'file2.txt')))
        self.assertTrue(os.path.isfile(os.path.join(path, 'file1.txt')))
        self.assertEquals(git('rev-parse', '--abbrev-ref', 'HEAD'), 'HEAD')
    def test_pull(self):
        gitrepo1 = GitCmd(self.main_repo)
        gitrepo2 = GitCmd(self.cloned_from_repo)

        self.assertNotEqual(
            gitrepo1('rev-list', all=True).split(),
            gitrepo2('rev-list', all=True).split())

        repo = Repository(self.main_repo)

        # Pulling a branch
        self.assertNotIn('newbranch', [b.name for b in repo.get_branches()])
        repo.pull(remote=self.cloned_from_repo, branch='newbranch')
        self.assertIn('newbranch', [b.name for b in repo.get_branches()])

        # Pulling everything
        repo.pull(remote=self.cloned_from_repo)

        self.assertEqual(
            gitrepo1('rev-list', all=True).split().sort(),
            gitrepo2('rev-list', all=True).split().sort())

        gitrepo1_refs = list(gitrepo1('show-ref', _iter=True))
        gitrepo2_refs = list(gitrepo2('show-ref', _iter=True))

        # Check that all remote refs have been fetched
        for ref in gitrepo2_refs:
            self.assertIn(ref, gitrepo1_refs)

        # Pulling from a non existing remote
        with self.assertRaises(RepositoryError):
            repo.pull(remote='wrong repo')
 def test_compare_branches(self):
     gitrepo = Repository(self.cloned_from_repo)
     masterhead_hash = 'b7fa61d5faf434642e35744b55d8d8f367afc343'
     newbranch_hash = 'a277468c9cc0088ba69e0a4b085822d067e360ff'
     firstway = gitrepo.compare_branches(masterhead_hash, 'newbranch')
     self.assertEquals([
         gitrepo['b7fa61d5faf434642e35744b55d8d8f367afc343'],
         gitrepo['2a9e1b9be3fb95ed0841aacc1f20972430dc1a5c']
     ], firstway)
     secondway = gitrepo.compare_branches(newbranch_hash, 'master')
     self.assertEquals(
         [gitrepo['a277468c9cc0088ba69e0a4b085822d067e360ff']], secondway)
 def test_init(self):
     git = GitCmd(os.path.join(self.environment_path, 'remote'))
     gitrepo = Repository(os.path.join(self.environment_path, 'remote'))
     gitcs = gitrepo[git('rev-parse', 'HEAD')]
     self.assertEquals(gitcs.author, "Jose Plana")
     self.assertEquals(gitcs.hash,
                       "52109e71fd7f16cb366acfcbb140d6d7f2fc50c9")
     self.assertEquals(gitcs.desc.rstrip('\n'),
                       "Second changeset".rstrip('\n'))
     self.assertFalse(gitcs.merge)
     print gitrepo.get_parents("52109e71fd7f16cb366acfcbb140d6d7f2fc50c9")
     self.assertEquals(gitcs.parents[0].hash,
                       "e3b1fc907ea8b3482e29eb91520c0e2eee2b4cdb")
 def test_init(self):
     git = GitCmd(os.path.join(self.environment_path, 'remote'))
     gitrepo = Repository(os.path.join(self.environment_path, 'remote'))
     gitcs = gitrepo[git('rev-parse', 'HEAD')]
     self.assertEquals(gitcs.author, "Jose Plana")
     self.assertEquals(
         gitcs.hash, "52109e71fd7f16cb366acfcbb140d6d7f2fc50c9")
     self.assertEquals(
         gitcs.desc.rstrip('\n'), "Second changeset".rstrip('\n'))
     self.assertFalse(gitcs.merge)
     print(gitrepo.get_parents("52109e71fd7f16cb366acfcbb140d6d7f2fc50c9"))
     self.assertEquals(
         gitcs.parents[0].hash, "e3b1fc907ea8b3482e29eb91520c0e2eee2b4cdb")
 def test_compare_branches(self):
     gitrepo = Repository(self.cloned_from_repo)
     masterhead_hash = 'b7fa61d5faf434642e35744b55d8d8f367afc343'
     newbranch_hash = 'a277468c9cc0088ba69e0a4b085822d067e360ff'
     firstway = gitrepo.compare_branches(masterhead_hash, 'newbranch')
     self.assertEquals([
         gitrepo['b7fa61d5faf434642e35744b55d8d8f367afc343'],
         gitrepo['2a9e1b9be3fb95ed0841aacc1f20972430dc1a5c']],
         firstway)
     secondway = gitrepo.compare_branches(newbranch_hash, 'master')
     self.assertEquals([
         gitrepo['a277468c9cc0088ba69e0a4b085822d067e360ff']],
         secondway)
 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_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_create_branch(self):
     non_bare_repo_path = os.path.join(
         self.environment_path, 'remote-non-bare')
     sh.git("clone",
         os.path.join(self.environment_path, 'remote'),
         non_bare_repo_path,
     )
     git = GitCmd(non_bare_repo_path)
     gitrepo = Repository(non_bare_repo_path)
     gitcs = gitrepo[git('rev-parse', 'HEAD')]
     branch = gitcs.create_branch('fakebranch')
     self.assertEquals(branch.get_changeset(), gitrepo.tip())
     self.assertEquals(
         'fakebranch', git('rev-parse', '--abbrev-ref', 'fakebranch'))
 def test_create_branch(self):
     non_bare_repo_path = os.path.join(
         self.environment_path, 'remote-non-bare')
     pygit2.clone_repository(
         os.path.join(self.environment_path, 'remote'),
         non_bare_repo_path,
     )
     clone = pygit2.Repository(non_bare_repo_path)
     gitrepo = Repository(non_bare_repo_path)
     gitcs = gitrepo[clone.head.get_object().hex]
     branch = gitcs.create_branch('fakebranch')
     self.assertEquals(branch.get_changeset(), gitrepo.tip())
     self.assertIsNotNone(clone.lookup_branch('fakebranch'))
     self.assertEquals(
         'fakebranch', clone.lookup_branch('fakebranch').branch_name)
    def test_branch(self):
        gitrepo = Repository(self.cloned_from_repo)
        gitrepo.branch("test_branch")
        git = GitCmd(self.cloned_from_repo)
        # Checking we are in the branch
        self.assertEquals(
                git('rev-parse', 'test_branch'),
                git('rev-parse', 'HEAD'))
        self.assertEquals(git('rev-parse', '--abbrev-ref', 'HEAD'), 'test_branch')

        gitrepo.branch('newbranch')
        self.assertEquals(
                git('rev-parse', 'newbranch'),
                git('rev-parse', 'HEAD'))
        self.assertEquals(git('rev-parse', '--abbrev-ref', 'HEAD'), 'newbranch')
 def test_create_branch(self):
     non_bare_repo_path = os.path.join(self.environment_path,
                                       'remote-non-bare')
     sh.git(
         "clone",
         os.path.join(self.environment_path, 'remote'),
         non_bare_repo_path,
     )
     git = GitCmd(non_bare_repo_path)
     gitrepo = Repository(non_bare_repo_path)
     gitcs = gitrepo[git('rev-parse', 'HEAD')]
     branch = gitcs.create_branch('fakebranch')
     self.assertEquals(branch.get_changeset(), gitrepo.tip())
     self.assertEquals('fakebranch',
                       git('rev-parse', '--abbrev-ref', 'fakebranch'))
    def test_get_ancestor(self):
        # According to the bundle
        ancestor_hash = "52109e71fd7f16cb366acfcbb140d6d7f2fc50c9"
        repo = pygit2.Repository(self.cloned_from_repo)
        gitrepo = Repository(self.cloned_from_repo)
        headmaster = repo.lookup_reference(
            'refs/remotes/origin/master').get_object().hex
        headnewbranch = repo.lookup_reference(
            'refs/remotes/origin/newbranch').get_object().hex
        ancestor = gitrepo.get_ancestor(gitrepo[headmaster],
                                        gitrepo[headnewbranch])

        self.assertEquals(ancestor.hash, ancestor_hash)

        with self.assertRaises(RepositoryError):
            gitrepo.get_ancestor(None, ancestor_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)
        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_get_ancestor(self):
        # According to the bundle
        ancestor_hash = "52109e71fd7f16cb366acfcbb140d6d7f2fc50c9"

        git = GitCmd(self.cloned_from_repo)
        headmaster = git('rev-parse', 'refs/heads/master')
        headnewbranch = git('rev-parse', 'refs/heads/newbranch')

        gitrepo = Repository(self.cloned_from_repo)
        ancestor = gitrepo.get_ancestor(gitrepo[headmaster],
                                        gitrepo[headnewbranch])

        self.assertEquals(ancestor.hash, ancestor_hash)

        with self.assertRaises(RepositoryError):
            gitrepo.get_ancestor(None, ancestor_hash)
 def test___str__(self):
     git = GitCmd(os.path.join(self.environment_path, 'remote'))
     gitrepo = Repository(os.path.join(self.environment_path, 'remote'))
     gitcs = gitrepo[git('rev-parse', 'HEAD')]
     self.assertEquals(
         gitcs.__str__(),
         git('rev-parse', 'HEAD')[:Changeset.SHORT_HASH_COUNT])
    def test_get_ancestor(self):
        # According to the bundle
        ancestor_hash = "52109e71fd7f16cb366acfcbb140d6d7f2fc50c9"

        git = GitCmd(self.cloned_from_repo)
        headmaster = git('rev-parse', 'refs/heads/master')
        headnewbranch = git('rev-parse', 'refs/heads/newbranch')

        gitrepo = Repository(self.cloned_from_repo)
        ancestor = gitrepo.get_ancestor(gitrepo[headmaster],
                                        gitrepo[headnewbranch])

        self.assertEquals(ancestor.hash, ancestor_hash)

        with self.assertRaises(RepositoryError):
            gitrepo.get_ancestor(None, ancestor_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_branch(self):
     gitrepo = Repository(self.cloned_from_repo)
     gitrepo.branch("test_branch")
     repo = pygit2.Repository(self.cloned_from_repo)
     # Checking branch exists
     self.assertIsNotNone(repo.lookup_reference('refs/heads/test_branch'))
     # Checking were in the branch
     head_hash = repo.head.get_object().hex
     self.assertEquals(repo.lookup_branch('test_branch').get_object().hex,
                       head_hash)
     self.assertEquals(repo.head.name, "refs/heads/test_branch")
     # this does not throw exception, even though the branch already exists,
     # but this must switch to the  branch
     gitrepo.branch("newbranch")
     head_hash = repo.head.get_object().hex
     self.assertEquals(repo.lookup_branch('newbranch').get_object().hex,
                       head_hash)
     self.assertEquals(repo.head.name, "refs/heads/newbranch")
    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_get_changeset_tags(self):
     git = GitCmd(self.main_repo)
     gitrepo = Repository(self.main_repo)
     rev = gitrepo[git('rev-parse', 'HEAD')]
     gitrepo.tag("test_tag", revision=rev.hash)
     gitrepo.tag("test_tag2", revision=rev.hash)
     tags = gitrepo.get_changeset_tags(rev.hash)
     self.assertListEqual(tags, ["test_tag", "test_tag2"])
    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_get_branch(self):
     repo = Repository(self.cloned_from_repo)
     branch = repo.get_branch('newbranch')
     self.assertEquals(branch.name, 'newbranch')
     repo.update('newbranch')
     branch = repo.get_branch()
     self.assertEquals(branch.name, 'newbranch')
     with self.assertRaises(RepositoryError):
         branch = repo.get_branch('does_not_exist')
 def test_is_merge(self):
     git = GitCmd(self.cloned_from_repo)
     headnewbranch = git('show-ref', '-s', 'refs/heads/newbranch')
     gitrepo = Repository(self.cloned_from_repo)
     self.assertFalse(gitrepo.is_merge(git('rev-parse', 'HEAD')))
     # Do a merge
     gitrepo.update('master')
     merge_rev = gitrepo.merge(other_rev=gitrepo[headnewbranch])
     self.assertTrue(gitrepo.is_merge(merge_rev.hash))
    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_push(self):
        git1 = GitCmd(self.main_repo_bare)
        git2 = GitCmd(self.cloned_from_repo)

        changesets1 = list(git1('log', pretty='oneline', _iter=True))
        changesets2 = list(git2('log', pretty='oneline', _iter=True))
        self.assertNotEqual(len(changesets1), len(changesets2))

        repo2 = Repository(self.cloned_from_repo)
        with self.assertRaises(RepositoryError):
            repo2.push(
                self.main_repo_bare,
                "inexistent_destination",
                ref_name='master')

        repo2.push(self.main_repo, self.main_repo_bare, ref_name='master')

        changesets1 = list(git1('log', pretty='oneline', _iter=True))
        changesets2 = list(git2('log', pretty='oneline', _iter=True))
        self.assertEquals(len(changesets1), len(changesets2))
    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"

        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_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"
        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_add_files(self):
        file_name = "absurd_file"
        file_path = os.path.join(self.main_repo, file_name)
        file_name2 = "absurd_file2"
        file_path2 = os.path.join(self.main_repo, file_name2)
        with open(file_path, "w") as file:
            file_content = "Absurd content"
            file.write(file_content)
        with open(file_path2, "w") as file:
            file_content = "Absurd content2"
            file.write(file_content)

        def get_status():
            git = GitCmd(self.main_repo)
            status = {}
            for f in git('status', porcelain=True, _iter=True):
                s, path = f.strip().split(maxsplit=1)
                status[path] = s
            return status

        status = get_status()
        self.assertTrue(file_name in status)
        self.assertTrue(file_name2 in status)
        self.assertEquals(status[file_name], '??')
        self.assertEquals(status[file_name2], '??')

        gitrepo = Repository(self.main_repo)
        gitrepo.add([file_name, file_name2])

        status = get_status()
        self.assertEquals(status[file_name], 'A')
        self.assertEquals(status[file_name2], 'A')
        with self.assertRaises(RepositoryError):
            gitrepo.add("nonexistentfile")
    def test_pull(self):
        gitrepo1 = pygit2.Repository(self.main_repo)
        gitrepo2 = pygit2.Repository(self.cloned_from_repo)

        self.assertNotEqual(
            len(list(gitrepo1.walk(gitrepo1.head.target,
                pygit2.GIT_SORT_TOPOLOGICAL))),
            len(list(gitrepo2.walk(gitrepo2.head.target,
                pygit2.GIT_SORT_TOPOLOGICAL))))

        repo = Repository(self.main_repo)

        repo.pull(remote=self.cloned_from_repo)

        self.assertEqual(
            len(list(gitrepo1.walk(gitrepo1.head.target,
                pygit2.GIT_SORT_TOPOLOGICAL))),
            len(list(gitrepo2.walk(gitrepo2.head.target,
                pygit2.GIT_SORT_TOPOLOGICAL))))

        with self.assertRaises(RepositoryError):
            repo.pull(remote='wrong repo')
    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_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_branch(self):
        gitrepo = Repository(self.cloned_from_repo)
        gitrepo.branch("test_branch")
        git = GitCmd(self.cloned_from_repo)
        # Checking we are in the branch
        self.assertEquals(git('rev-parse', 'test_branch'),
                          git('rev-parse', 'HEAD'))
        self.assertEquals(git('rev-parse', '--abbrev-ref', 'HEAD'),
                          'test_branch')

        gitrepo.branch('newbranch')
        self.assertEquals(git('rev-parse', 'newbranch'),
                          git('rev-parse', 'HEAD'))
        self.assertEquals(git('rev-parse', '--abbrev-ref', 'HEAD'),
                          'newbranch')
    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_push(self):
        git1 = GitCmd(self.main_repo_bare)
        git2 = GitCmd(self.cloned_from_repo)

        changesets1 = list(git1('log', pretty='oneline', _iter=True))
        changesets2 = list(git2('log', pretty='oneline', _iter=True))
        self.assertNotEqual(len(changesets1), len(changesets2))

        repo2 = Repository(self.cloned_from_repo)
        with self.assertRaises(RepositoryError):
            repo2.push(self.main_repo_bare,
                       "inexistent_destination",
                       ref_name='master')

        repo2.push(self.main_repo, self.main_repo_bare, ref_name='master')

        changesets1 = list(git1('log', pretty='oneline', _iter=True))
        changesets2 = list(git2('log', pretty='oneline', _iter=True))
        self.assertEquals(len(changesets1), len(changesets2))
    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_get_branch_tip(self):
     git = GitCmd(self.cloned_from_repo)
     gitrepo = Repository(self.cloned_from_repo)
     self.assertEquals(
         gitrepo.get_branch_tip('master').hash, git('rev-parse', 'master'))
 def test_parents(self):
     git = GitCmd(self.cloned_from_repo)
     gitrepo = Repository(self.cloned_from_repo)
     self.assertEquals([x.hash for x in gitrepo.parents()],
                       git('log', '-1', pretty='%P').split())
    def test_get_revset(self):
        git = GitCmd(self.cloned_from_repo)
        gitrepo = Repository(self.cloned_from_repo)

        # Just cs_from
        just_from_second = gitrepo.get_revset(
            cs_from="52109e71fd7f16cb366acfcbb140d6d7f2fc50c9")
        self.assertEquals(len(list(just_from_second)), 3)

        # No params
        no_params = gitrepo.get_revset()
        self.assertEquals(len(list(no_params)), 4)

        # From first commit to head
        first_to_head = gitrepo.get_revset(
            cs_from="e3b1fc907ea8b3482e29eb91520c0e2eee2b4cdb",
            cs_to=git('rev-parse', 'HEAD'))
        self.assertEquals(len(list(first_to_head)), 4)
        second_to_head = gitrepo.get_revset(
            cs_from="52109e71fd7f16cb366acfcbb140d6d7f2fc50c9",
            cs_to=git('rev-parse', 'HEAD'))
        self.assertEquals(len(list(second_to_head)), 3)
        second_to_third = gitrepo.get_revset(
            cs_from="52109e71fd7f16cb366acfcbb140d6d7f2fc50c9",
            cs_to="2a9e1b9be3fb95ed0841aacc1f20972430dc1a5c")
        self.assertEquals(len(list(second_to_third)), 2)

        # Just by branch
        by_branch = gitrepo.get_revset(branch='newbranch')
        self.assertEquals(len(list(by_branch)), 3)

        # Just by branch being in another
        gitrepo.update('master')
        by_branch = gitrepo.get_revset(branch='newbranch')
        self.assertEquals(len(list(by_branch)), 3)
        self.assertEquals(git('rev-parse', '--abbrev-ref', 'HEAD'), 'master')

        # Only common ancestor belong to newbranch
        common_ancestor = gitrepo.get_revset(
            cs_to="b7fa61d5faf434642e35744b55d8d8f367afc343",
            cs_from="52109e71fd7f16cb366acfcbb140d6d7f2fc50c9",
            branch='newbranch')
        self.assertEquals(len(list(common_ancestor)), 1)

        # Zero changesets belong to newbranch
        none = gitrepo.get_revset(
            cs_to="b7fa61d5faf434642e35744b55d8d8f367afc343",
            cs_from="2a9e1b9be3fb95ed0841aacc1f20972430dc1a5c",
            branch='newbranch')
        self.assertEquals(len(list(none)), 0)

        # From the beginning to master tip so only common changesets in both
        # branches
        common_changesets = gitrepo.get_revset(
            cs_to="b7fa61d5faf434642e35744b55d8d8f367afc343",
            branch='newbranch')
        self.assertEquals(len(list(common_changesets)), 2)

        # From the beginning to common ancestor, that belongs to both branches
        toboth = gitrepo.get_revset(
            cs_to="52109e71fd7f16cb366acfcbb140d6d7f2fc50c9",
            branch='newbranch')
        self.assertEquals(len(list(toboth)), 2)

        # From newbranch origin to newbranch tip
        ignore_branch3 = gitrepo.get_revset(
            cs_from="e3b1fc907ea8b3482e29eb91520c0e2eee2b4cdb",
            branch='newbranch')
        self.assertEquals(len(list(ignore_branch3)), 3)
    def test_exterminate_branch(self):
        branch_name = 'newbranch'
        gitrepo = Repository(self.cloned_from_repo)
        gitrepo_main = Repository(self.main_repo)
        gitrepo.update(branch_name)
        # Pushing the branch to the remote repo so we can check it's removed
        # remotely too
        gitrepo.push(None, self.main_repo, ref_name=branch_name)

        self.assertEquals(len(list(gitrepo.get_branches())), 2)
        self.assertEquals(len(list(gitrepo_main.get_branches())), 4)

        gitrepo.exterminate_branch(branch_name, None, self.main_repo)

        self.assertEquals(len(list(gitrepo.get_branches())), 1)
        self.assertEquals(len(list(gitrepo_main.get_branches())), 3)

        # Terminating a branch already terminated
        # it shouldn't do anything but warning with a message
        gitrepo.exterminate_branch(branch_name, None, self.main_repo)
    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'))