Exemplo n.º 1
0
    def test_merge_conflicts(self):
        """ Test the pygit2_utils.GitRepo().merge method when it conflicts
        on a merge
        """
        # Create second repo
        second_path = os.path.join(self.gitroot, 'second_test_repo')
        second_repo_obj = pygit2.init_repository(second_path)
        second_repo = pygit2_utils.GitRepo(second_path)

        # Create a commit in our second repo that conflicts with those on
        # the first
        with open(os.path.join(second_path, 'sources'), 'w') as stream:
            stream.write('new sources <...>')
        second_repo.commit('new sources', 'sources')

        self.setup_git_repo()

        repo_path = os.path.join(self.gitroot, 'test_repo')
        repo = pygit2_utils.GitRepo(repo_path)
        repo_obj = pygit2.Repository(repo_path)
        # Add commit to the original repo
        self.add_commits()

        # Add original repo to second repo
        remote = second_repo.add_remote('upstream', repo_path)
        remote.fetch()

        commit = repo_obj.lookup_reference('HEAD').get_object()
        # Merge original repo into second repo
        self.assertRaises(pygit2_utils.exceptions.MergeConflictsError,
                          second_repo.merge,
                          commit.oid.hex,
                          message='test merge')
Exemplo n.º 2
0
    def test_merge(self):
        """ Test the pygit2_utils.GitRepo().merge method used to merge a
        branch from a repo to another
        """

        # Create second repo
        second_path = os.path.join(self.gitroot, 'second_test_repo')
        second_repo_obj = pygit2.init_repository(second_path)
        second_repo = pygit2_utils.GitRepo(second_path)

        # Create a commit in our second repo to create the `master` branch
        open(os.path.join(second_path, 'tmp'), 'w').close()
        second_repo.commit('first commit', 'tmp')

        self.setup_git_repo()

        repo_path = os.path.join(self.gitroot, 'test_repo')
        repo = pygit2_utils.GitRepo(repo_path)
        repo_obj = pygit2.Repository(repo_path)
        # Add commit to the original repo
        self.add_commits()

        # Add original repo to second repo
        remote = second_repo.add_remote('upstream', repo_path)
        remote.fetch()

        commit = repo_obj.lookup_reference('HEAD').get_object()
        # Merge original repo into second repo
        sha = second_repo.merge(commit.oid.hex, message='test merge')

        self.assertNotEqual(commit.oid.hex, sha.hex)

        commit = second_repo_obj.lookup_reference('HEAD').get_object()
        self.assertEqual(commit.message, 'test merge')

        # Second merge

        with open(os.path.join(repo_path, 'sources'), 'w') as stream:
            stream.write('new sources <...>')
        repo.commit('new sources', 'sources')

        with open(os.path.join(second_path, 'tmp'), 'w') as stream:
            stream.write('new tmp <...>')
        second_repo.commit('another blank commit', 'tmp')

        remote.fetch()

        commit = repo_obj.lookup_reference('HEAD').get_object()
        # Merge original repo into second repo
        sha = second_repo.merge(commit.oid.hex)

        self.assertNotEqual(commit.oid.hex, sha.hex)

        commitid = commit.oid.hex
        commit = second_repo_obj.lookup_reference('HEAD').get_object()
        self.assertEqual(commit.message, 'Merge %s into master' % commitid)
Exemplo n.º 3
0
    def test_remote_current_branch(self):
        """ Test the pygit2_utils.GitRepo().remote_current_branch returning
        the remote of the current branch
        """
        self.setup_git_repo()

        repo_path = os.path.join(self.gitroot, 'test_repo')
        repo = pygit2_utils.GitRepo(repo_path)

        self.assertEqual(repo.remote_current_branch, 'origin/master')

        repo_path = os.path.join(self.gitroot, 'test_repo.git')
        repo = pygit2_utils.GitRepo(repo_path)

        self.assertEqual(repo.remote_current_branch, None)
Exemplo n.º 4
0
    def test_checkout(self):
        """ Test the pygit2_utils.GitRepo().checkout method used to change
        branch
        """
        self.setup_git_repo()

        repo_path = os.path.join(self.gitroot, 'test_repo')
        repo = pygit2_utils.GitRepo(repo_path)

        branches = repo.list_branches('local')
        self.assertEqual(branches, ['master'])
        self.assertEqual(repo.current_branch, 'master')

        self.assertRaises(pygit2_utils.exceptions.NoSuchBranchError,
                          repo.checkout, 'foo0')

        self.add_branches()
        branches = repo.list_branches('local')
        self.assertEqual(branches, ['foo0', 'foo1', 'master'])
        self.assertEqual(repo.current_branch, 'master')

        repo.checkout('foo0')
        branches = repo.list_branches('local')
        self.assertEqual(branches, ['foo0', 'foo1', 'master'])
        self.assertEqual(repo.current_branch, 'foo0')
Exemplo n.º 5
0
    def test_add_remote(self):
        """ Test the pygit2_utils.GitRepo().add_remote method used to add a
        remote to a git repository.
        """
        self.setup_git_repo()

        repo_path = os.path.join(self.gitroot, 'test_repo')
        repo = pygit2_utils.GitRepo(repo_path)

        remote = repo.add_remote('origin_master', repo_path)

        remote.fetch()

        branches = repo.list_branches()
        self.assertEqual(branches,
                         ['master', 'origin/master', 'origin_master/master'])
        self.assertEqual(repo.current_branch, 'master')

        commit_local = repo.head_of_branch('master')
        self.assertEqual(repo.current_branch, 'master')
        self.assertTrue(isinstance(commit_local, pygit2.Commit))

        # Check that both commits are the same since `master` and `foo0`
        # are at the same level
        commit_remote = repo.head_of_branch('origin_master/master')
        self.assertEqual(repo.current_branch, 'master')
        self.assertTrue(commit_local, commit_remote)
Exemplo n.º 6
0
    def test_list_branches(self):
        """ Test the pygit2_utils.GitRepo().list_branches method returning
        the list of branches.
        """
        self.setup_git_repo()

        repo_path = os.path.join(self.gitroot, 'test_repo')
        repo = pygit2_utils.GitRepo(repo_path)

        # Fails: status invalid
        self.assertRaises(
            ValueError,
            repo.list_branches,
            'foo',
        )

        branches = repo.list_branches()
        self.assertEqual(branches, ['master', 'origin/master'])

        self.add_branches()

        branches = repo.list_branches()
        self.assertEqual(sorted(branches),
                         ['foo0', 'foo1', 'master', 'origin/master'])

        branches = repo.list_branches('local')
        self.assertEqual(sorted(branches), ['foo0', 'foo1', 'master'])

        branches = repo.list_branches('remote')
        self.assertEqual(sorted(branches), ['origin/master'])
Exemplo n.º 7
0
    def test_merge_nothing_to_merge(self):
        """ Test the pygit2_utils.GitRepo().merge method when it raises a
        NothingToMergeError exception
        """
        self.setup_git_repo()

        repo_path = os.path.join(self.gitroot, 'test_repo')
        repo = pygit2_utils.GitRepo(repo_path)
        repo_obj = pygit2.Repository(repo_path)
        # Add commit to the original repo
        self.add_commits()

        # Create second repo
        second_path = os.path.join(self.gitroot, 'second_test_repo')
        second_repo = pygit2_utils.GitRepo.clone_repo(repo_path, second_path)
        second_repo_obj = pygit2.init_repository(second_path)

        # Add original repo to second repo
        remote = second_repo.add_remote('upstream', repo_path)
        remote.fetch()

        commit = repo_obj.lookup_reference('HEAD').get_object()
        # Merge original repo into second repo
        self.assertRaises(pygit2_utils.exceptions.NothingToMergeError,
                          second_repo.merge,
                          commit.oid.hex,
                          message='test merge')
Exemplo n.º 8
0
    def test_commit(self):
        """ Test the pygit2_utils.GitRepo().commit returning the commit
        """
        self.setup_git_repo()

        repo_path = os.path.join(self.gitroot, 'test_repo')
        repo = pygit2_utils.GitRepo(repo_path)

        repo_obj = pygit2.Repository(repo_path)

        # Check before commit that the commit we do, does not already exist
        commit = repo_obj.get(repo_obj.revparse_single('HEAD').oid.hex)
        self.assertNotEqual(commit.message, 'Commit from the tests')
        self.assertNotEqual(commit.author.name, 'foo')
        self.assertNotEqual(commit.author.email, '*****@*****.**')

        with open(os.path.join(repo_path, 'sources'), 'w') as stream:
            stream.write('\nBoo!!2')

        # Commit with a list of (one) file
        commitid = repo.commit('Commit from the tests', ['sources'])

        # Check that the commitid returned has an .hex attribute that is a
        # string
        self.assertTrue(isinstance(commitid.hex, str))

        # Check the latest commit has the same hash as the commitid returned
        self.assertEqual(commitid.hex,
                         repo_obj.revparse_single('HEAD').oid.hex)

        # Check the information of the latest commit
        commit = repo_obj.get(repo_obj.revparse_single('HEAD').oid.hex)

        self.assertEqual(commit.message, 'Commit from the tests')
        self.assertEqual(commit.author.name, 'foo')
        self.assertEqual(commit.author.email, '*****@*****.**')

        with open(os.path.join(repo_path, 'sources'), 'w') as stream:
            stream.write('\nBoo!!2')

        # Commit with a single file
        commitid = repo.commit('Commit from the tests',
                               'sources',
                               username='******',
                               useremail='*****@*****.**')

        # Check that the commitid returned has an .hex attribute that is a
        # string
        self.assertTrue(isinstance(commitid.hex, str))

        # Check the latest commit has the same hash as the commitid returned
        self.assertEqual(commitid.hex,
                         repo_obj.revparse_single('HEAD').oid.hex)

        # Check the information of the latest commit
        commit = repo_obj.get(repo_obj.revparse_single('HEAD').oid.hex)

        self.assertEqual(commit.message, 'Commit from the tests')
        self.assertEqual(commit.author.name, 'bar')
        self.assertEqual(commit.author.email, '*****@*****.**')
Exemplo n.º 9
0
    def test_head_of_branch(self):
        """ Test the pygit2_utils.GitRepo().head_of_branch method used to
        retrieve the HEAD commit of a specified branch
        """
        self.setup_git_repo()

        repo_path = os.path.join(self.gitroot, 'test_repo')
        repo = pygit2_utils.GitRepo(repo_path)

        branches = repo.list_branches('local')
        self.assertEqual(branches, ['master'])
        self.assertEqual(repo.current_branch, 'master')

        self.assertRaises(pygit2_utils.exceptions.NoSuchBranchError,
                          repo.head_of_branch, 'foo0')

        self.add_branches()
        branches = repo.list_branches('local')
        self.assertEqual(branches, ['foo0', 'foo1', 'master'])
        self.assertEqual(repo.current_branch, 'master')

        commit = repo.head_of_branch('foo0')
        self.assertEqual(repo.current_branch, 'master')
        self.assertTrue(isinstance(commit, pygit2.Commit))

        # Check that both commits are the same since `master` and `foo0`
        # are at the same level
        commit_master = repo.head_of_branch('master')
        self.assertEqual(repo.current_branch, 'master')
        self.assertTrue(commit, commit_master)
Exemplo n.º 10
0
    def test_files_untracked(self):
        """ Test the pygit2_utils.GitRepo().files_untracked returning the
        list of files not tracked but present locally
        """
        self.setup_git_repo()

        repo_path = os.path.join(self.gitroot, 'test_repo')
        repo = pygit2_utils.GitRepo(repo_path)

        with open(os.path.join(repo_path, 'sources'), 'w') as stream:
            stream.write('\nBoo!!2')
        with open(os.path.join(repo_path, 'bar'), 'w') as stream:
            stream.write('blah')

        self.assertEqual(repo.files_untracked, ['bar'])
Exemplo n.º 11
0
    def test_diff_commit(self):
        """ Test the pygit2_utils.GitRepo().diff returning the diff of a
        specified commit with its parents.
        """
        self.setup_git_repo()

        repo_path = os.path.join(self.gitroot, 'test_repo')
        repo = pygit2_utils.GitRepo(repo_path)
        repo_obj = pygit2.Repository(repo_path)

        # Fails: hash invalid
        self.assertRaises(ValueError, repo.diff, 'foo')

        self.assertRaises(pygit2_utils.exceptions.NoSuchRefError, repo.diff,
                          'f05c03a2054f2d203bb')

        commitid = repo_obj.revparse_single('HEAD').oid.hex

        exp = '''diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/.gitignore
diff --git a/sources b/sources
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/sources
'''

        diff = repo.diff(commitid)
        self.assertEqual(diff.patch, exp)

        self.add_commits()

        commitid = repo_obj.revparse_single('HEAD').oid.hex

        exp = '''diff --git a/sources b/sources
index 94921de..fa457ba 100644
--- a/sources
+++ b/sources
@@ -1 +1 @@
-0/2
+1/2
'''

        diff = repo.diff(commitid)
        self.assertEqual(diff.patch, exp)
Exemplo n.º 12
0
    def test_list_tags(self):
        """ Test the pygit2_utils.GitRepo().list_tags method returning the
        list of tags. present in the repo
        """
        self.setup_git_repo()

        repo_path = os.path.join(self.gitroot, 'test_repo')
        repo = pygit2_utils.GitRepo(repo_path)

        tags = repo.list_tags()
        self.assertEqual(tags, [])

        self.add_tags()

        tags = repo.list_tags()
        self.assertEqual(tags, ['v0', 'v1'])
Exemplo n.º 13
0
    def test_files_changed(self):
        """ Test the pygit2_utils.GitRepo().files_changed returning the
        list of files tracked that have changed locally
        """
        self.setup_git_repo()

        repo_path = os.path.join(self.gitroot, 'test_repo')
        repo = pygit2_utils.GitRepo(repo_path)

        with open(os.path.join(repo_path, 'sources'), 'w') as stream:
            stream.write('\nBoo!!2')
        with open(os.path.join(repo_path, '.gitignore'), 'w') as stream:
            stream.write('boo!')
        with open(os.path.join(repo_path, 'bar'), 'w') as stream:
            stream.write('blah')

        self.assertEqual(sorted(repo.files_changed), ['.gitignore', 'sources'])
Exemplo n.º 14
0
    def test_get_patch(self):
        """ Test the pygit2_utils.GitRepo().get_patch returning the patch of
        one commit
        """
        self.setup_git_repo()

        repo_path = os.path.join(self.gitroot, 'test_repo')
        repo = pygit2_utils.GitRepo(repo_path)
        repo_obj = pygit2.Repository(repo_path)

        # Fails: hash invalid
        self.assertRaises(KeyError, repo.get_patch, 'foo')

        # Retrieve one commit to work with
        commitid = repo_obj.revparse_single('HEAD').oid.hex

        patch = repo.get_patch(commitid)

        exp = """From <id> Mon Sep 17 00:00:00 2001
From: Alice Author <*****@*****.**>
Date:
Subject: Add basic file required


---

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/.gitignore
diff --git a/sources b/sources
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/sources

"""
        patch = patch.split('\n')
        # We can't predict the git hash
        patch[0] = 'From <id> Mon Sep 17 00:00:00 2001'
        # nor the exact date & time we create the commit
        patch[2] = 'Date:'
        patch = '\n'.join(patch)

        self.assertEqual(patch, exp)
Exemplo n.º 15
0
    def test_tag(self):
        """ Test the pygit2_utils.GitRepo().tag method used to tag a
        specific commit
        """
        self.setup_git_repo()

        repo_path = os.path.join(self.gitroot, 'test_repo')
        repo = pygit2_utils.GitRepo(repo_path)
        repo_obj = pygit2.Repository(repo_path)

        tags = repo.list_tags()
        self.assertEqual(tags, [])

        self.add_commits(n=4)

        commitid = repo_obj.revparse_single('HEAD~3').oid.hex
        tagid = repo.tag('test1', commitid, 'test commit')
        tagobj = repo_obj.get(tagid)
        self.assertEqual(tagobj.name, 'test1')
        self.assertEqual(tagobj.target.hex, commitid)
        self.assertEqual(tagobj.message, 'test commit')

        tags = repo.list_tags()
        self.assertEqual(tags, ['test1'])

        commitid = repo_obj.revparse_single('HEAD~2').oid.hex
        tagid = repo.tag('test2', commitid)
        tagobj = repo_obj.get(tagid)
        self.assertEqual(tagobj.name, 'test2')
        self.assertEqual(tagobj.target.hex, commitid)
        self.assertEqual(tagobj.message, '')

        tags = repo.list_tags()
        self.assertEqual(tags, ['test1', 'test2'])

        tagid = repo.tag('test3')
        tagobj = repo_obj.get(tagid)
        self.assertEqual(tagobj.name, 'test3')
        self.assertEqual(tagobj.target.hex,
                         repo_obj.revparse_single('HEAD').oid.hex)
        self.assertEqual(tagobj.message, '')

        tags = repo.list_tags()
        self.assertEqual(tags, ['test1', 'test2', 'test3'])
Exemplo n.º 16
0
    def test_diff_head(self):
        """ Test the pygit2_utils.GitRepo().diff returning the diff against
        HEAD
        """
        self.setup_git_repo()

        repo_path = os.path.join(self.gitroot, 'test_repo')
        repo = pygit2_utils.GitRepo(repo_path)

        with open(os.path.join(repo_path, 'sources'), 'w') as stream:
            stream.write('\nBoo!!2')

        exp = '''diff --git a/sources b/sources
index e69de29..d7d49f8 100644
--- a/sources
+++ b/sources
@@ -0,0 +1,2 @@
+
+Boo!!2
\ No newline at end of file
'''

        diff = repo.diff()
        self.assertEqual(diff.patch, exp)
Exemplo n.º 17
0
    def test_merge_fastforward(self):
        """ Test the pygit2_utils.GitRepo().merge method when it merges
        fast-forward
        """
        self.setup_git_repo()

        repo_path = os.path.join(self.gitroot, 'test_repo')
        repo = pygit2_utils.GitRepo(repo_path)
        repo_obj = pygit2.Repository(repo_path)
        # Add commit to the original repo
        self.add_commits()

        # Create second repo
        second_path = os.path.join(self.gitroot, 'second_test_repo')
        second_repo = pygit2_utils.GitRepo.clone_repo(repo_path, second_path)
        second_repo_obj = pygit2.Repository(second_path)

        # Create a new commit in our original repo
        with open(os.path.join(repo_path, 'sources'), 'w') as stream:
            stream.write('new sources')
        commit = repo.commit('new sources', 'sources')

        # Add original repo to second repo
        remote = second_repo.add_remote('upstream', repo_path)
        remote.fetch()

        commit = repo_obj.lookup_reference('HEAD').get_object()
        # Merge original repo into second repo
        sha = second_repo.merge(commit.oid.hex, message='test merge')

        self.assertEqual(commit.oid.hex, sha.hex)

        head_commit = second_repo_obj.lookup_reference('HEAD').get_object()
        self.assertEqual(head_commit.message, commit.message)
        self.assertEqual(commit.oid.hex, sha.hex)
        self.assertEqual(head_commit.oid.hex, commit.hex)
Exemplo n.º 18
0
    def test_diff_commits(self):
        """ Test the pygit2_utils.GitRepo().diff returning the diff of two
        specific commits.
        """
        self.setup_git_repo()
        self.add_commits()

        repo_path = os.path.join(self.gitroot, 'test_repo')
        repo = pygit2_utils.GitRepo(repo_path)
        repo_obj = pygit2.Repository(repo_path)

        # Fails: hash invalid (too short)
        self.assertRaises(KeyError, repo.diff, 'foo', 'bar')
        self.assertRaises(KeyError, repo.diff, 'foo', 'f05c03a2054f2d203bb')
        self.assertRaises(KeyError, repo.diff, 'f05c03a2054f2d203bb', 'bar')

        # Fails: hash invalid (does not exist)
        self.assertRaises(KeyError, repo.diff, '8f167d70462b088e00b',
                          'f05c03a2054f2d203bb')

        # Retrieve some commits to work with
        commitid = repo_obj.revparse_single('HEAD').oid.hex
        commitid1 = repo_obj.revparse_single('HEAD^').oid.hex
        commitid2 = repo_obj.revparse_single('%s^' % commitid1).oid.hex

        exp = '''diff --git a/sources b/sources
index fa457ba..94921de 100644
--- a/sources
+++ b/sources
@@ -1 +1 @@
-1/2
+0/2
'''

        diff = repo.diff(commitid, commitid1)
        self.assertEqual(diff.patch, exp)

        exp = '''diff --git a/sources b/sources
index 94921de..e69de29 100644
--- a/sources
+++ b/sources
@@ -1 +0,0 @@
-0/2
'''

        diff = repo.diff(commitid1, commitid2)
        self.assertEqual(diff.patch, exp)

        exp = '''diff --git a/sources b/sources
index fa457ba..e69de29 100644
--- a/sources
+++ b/sources
@@ -1 +0,0 @@
-1/2
'''

        diff = repo.diff(commitid, commitid2)
        self.assertEqual(diff.patch, exp)

        exp = '''diff --git a/sources b/sources
index e69de29..fa457ba 100644
--- a/sources
+++ b/sources
@@ -0,0 +1 @@
+1/2
'''

        diff = repo.diff(commitid2, commitid)
        self.assertEqual(diff.patch, exp)
Exemplo n.º 19
0
    def test_get_patch_multi(self):
        """ Test the pygit2_utils.GitRepo().get_patch returning a single patch
        for multiple commits
        """
        self.setup_git_repo()

        repo_path = os.path.join(self.gitroot, 'test_repo')
        repo = pygit2_utils.GitRepo(repo_path)
        repo_obj = pygit2.Repository(repo_path)

        # Fails: hash invalid
        self.assertRaises(KeyError, repo.get_patch, 'foo')

        self.assertRaises(KeyError, repo.get_patch, '8f167d70462b088e00b')

        self.add_commits()

        commitids = [repo_obj.revparse_single('HEAD').oid.hex]
        commitids.append(repo_obj.revparse_single('HEAD^').oid.hex)

        patch = repo.get_patch(commitids)

        exp = """From <id> Mon Sep 17 00:00:00 2001
From: Alice Author <*****@*****.**>
Date:
Subject: [PATCH 1/2] Add commit 1 out of 2


 foo
---

diff --git a/sources b/sources
index 94921de..fa457ba 100644
--- a/sources
+++ b/sources
@@ -1 +1 @@
-0/2
+1/2

From <id> Mon Sep 17 00:00:00 2001
From: Alice Author <*****@*****.**>
Date:
Subject: [PATCH 2/2] Add commit 0 out of 2


 foo
---

diff --git a/sources b/sources
index e69de29..94921de 100644
--- a/sources
+++ b/sources
@@ -0,0 +1 @@
+0/2

"""
        patch = patch.split('\n')
        # We can't predict the git hash
        patch[0] = 'From <id> Mon Sep 17 00:00:00 2001'
        patch[17] = 'From <id> Mon Sep 17 00:00:00 2001'
        # nor the exact date & time we create the commit
        patch[2] = 'Date:'
        patch[19] = 'Date:'
        patch = '\n'.join(patch)

        self.assertEqual(patch, exp)