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_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_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 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
 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_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"])
Пример #7
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_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_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_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_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_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_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_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_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"

        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_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_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_tag(self):
        gitrepo = Repository(self.main_repo)
        gitrepo.tag("new-tag", message="fake tag")

        git = GitCmd(self.main_repo)
        self.assertNotEquals(git('show-ref', 'refs/tags/new-tag'), '')
    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_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_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'))