コード例 #1
0
ファイル: test_index.py プロジェクト: klausstocking/GitPython
    def test_index_file_base(self):
        # read from file
        index = IndexFile(self.rorepo, fixture_path("index"))
        assert index.entries
        assert index.version > 0

        # test entry
        entry = next(iter(index.entries.values()))
        for attr in ("path", "ctime", "mtime", "dev", "inode", "mode", "uid",
                     "gid", "size", "binsha", "hexsha", "stage"):
            getattr(entry, attr)
        # END for each method

        # test update
        entries = index.entries
        assert isinstance(index.update(), IndexFile)
        assert entries is not index.entries

        # test stage
        index_merge = IndexFile(self.rorepo, fixture_path("index_merge"))
        self.assertEqual(len(index_merge.entries), 106)
        assert len([e for e in index_merge.entries.values() if e.stage != 0])

        # write the data - it must match the original
        tmpfile = tempfile.mktemp()
        index_merge.write(tmpfile)
        with open(tmpfile, 'rb') as fp:
            self.assertEqual(fp.read(), fixture("index_merge"))
        os.remove(tmpfile)
コード例 #2
0
 def _git_rel_path(path):
     try:
         repo = Repo(path, search_parent_directories=True)
         index = IndexFile(repo)
         path = Path(index._to_relative_path(path))
         return path.as_posix()
     except Exception:
         return ''
コード例 #3
0
ファイル: gitreformat.py プロジェクト: battyone/gitreformat
    def time_warp(self, c_o):
        """
        History rewriting occurs here.  We read everything from the original
        commit, reformat the python, and checkin, mirroring the original
        commit history.
        :param c_o: Commit object representing "before"
        :return: None
        """

        log.info('warping: {} | {} | {:f} MB | {}s'.format(
            time_convert(c_o.authored_date, c_o.author_tz_offset), c_o.summary,
            resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1000,
            time.clock()))

        items = self.handle_commit(c_o)

        parent_commits = tuple(self.converted[v] for v in c_o.parents)

        # for the singular case of init / root / the genesis
        if len(parent_commits) == 0:
            parent_commits = {c_o}

        self.repo.head.reference = c_o
        self.repo.head.reset(index=True, working_tree=True)

        idx = IndexFile(self.repo)

        idx.add(items)

        idx.write_tree()

        com_msg = [c_o.message]

        com_msg.extend('\n'.join(self.convert_errors))
        self.convert_errors = []  # todo: rearchitect - too easy to forget

        com_msg.append('\n[gitreformat yapf-ify (github/ghtdak) on {}]'.format(
            time.strftime('%c')))
        com_msg.append('\n[from commit: {}]'.format(c_o.hexsha))

        c_n = idx.commit(''.join(com_msg),
                         parent_commits=parent_commits,
                         author=c_o.author,
                         author_date=time_convert(c_o.authored_date,
                                                  c_o.author_tz_offset),
                         committer=c_o.committer,
                         commit_date=time_convert(c_o.committed_date,
                                                  c_o.committer_tz_offset))

        self.repo.head.reference = c_n
        self.repo.head.reset(index=True, working_tree=True)

        self.verify_paths(c_o.tree, c_n.tree)

        self.converted[c_o] = c_n

        return c_n
コード例 #4
0
ファイル: test_index.py プロジェクト: klausstocking/GitPython
    def test__to_relative_path_at_root(self):
        root = osp.abspath(os.sep)

        class Mocked(object):
            bare = False
            git_dir = root
            working_tree_dir = root

        repo = Mocked()
        path = os.path.join(root, 'file')
        index = IndexFile(repo)

        rel = index._to_relative_path(path)
        self.assertEqual(rel, os.path.relpath(path, root))
コード例 #5
0
ファイル: test_index.py プロジェクト: klausstocking/GitPython
    def test_index_file_diffing(self, rw_repo):
        # default Index instance points to our index
        index = IndexFile(rw_repo)
        assert index.path is not None
        assert len(index.entries)

        # write the file back
        index.write()

        # could sha it, or check stats

        # test diff
        # resetting the head will leave the index in a different state, and the
        # diff will yield a few changes
        cur_head_commit = rw_repo.head.reference.commit
        rw_repo.head.reset('HEAD~6', index=True, working_tree=False)

        # diff against same index is 0
        diff = index.diff()
        self.assertEqual(len(diff), 0)

        # against HEAD as string, must be the same as it matches index
        diff = index.diff('HEAD')
        self.assertEqual(len(diff), 0)

        # against previous head, there must be a difference
        diff = index.diff(cur_head_commit)
        assert len(diff)

        # we reverse the result
        adiff = index.diff(str(cur_head_commit), R=True)
        odiff = index.diff(cur_head_commit,
                           R=False)  # now its not reversed anymore
        assert adiff != odiff
        self.assertEqual(odiff, diff)  # both unreversed diffs against HEAD

        # against working copy - its still at cur_commit
        wdiff = index.diff(None)
        assert wdiff != adiff
        assert wdiff != odiff

        # against something unusual
        self.failUnlessRaises(ValueError, index.diff, int)

        # adjust the index to match an old revision
        cur_branch = rw_repo.active_branch
        cur_commit = cur_branch.commit
        rev_head_parent = 'HEAD~1'
        assert index.reset(rev_head_parent) is index

        self.assertEqual(cur_branch, rw_repo.active_branch)
        self.assertEqual(cur_commit, rw_repo.head.commit)

        # there must be differences towards the working tree which is in the 'future'
        assert index.diff(None)

        # reset the working copy as well to current head,to pull 'back' as well
        new_data = b"will be reverted"
        file_path = osp.join(rw_repo.working_tree_dir, "CHANGES")
        with open(file_path, "wb") as fp:
            fp.write(new_data)
        index.reset(rev_head_parent, working_tree=True)
        assert not index.diff(None)
        self.assertEqual(cur_branch, rw_repo.active_branch)
        self.assertEqual(cur_commit, rw_repo.head.commit)
        with open(file_path, 'rb') as fp:
            assert fp.read() != new_data

        # test full checkout
        test_file = osp.join(rw_repo.working_tree_dir, "CHANGES")
        with open(test_file, 'ab') as fd:
            fd.write(b"some data")
        rval = index.checkout(None, force=True, fprogress=self._fprogress)
        assert 'CHANGES' in list(rval)
        self._assert_fprogress([None])
        assert osp.isfile(test_file)

        os.remove(test_file)
        rval = index.checkout(None, force=False, fprogress=self._fprogress)
        assert 'CHANGES' in list(rval)
        self._assert_fprogress([None])
        assert osp.isfile(test_file)

        # individual file
        os.remove(test_file)
        rval = index.checkout(test_file, fprogress=self._fprogress)
        self.assertEqual(list(rval)[0], 'CHANGES')
        self._assert_fprogress([test_file])
        assert osp.exists(test_file)

        # checking out non-existing file throws
        self.failUnlessRaises(CheckoutError, index.checkout,
                              "doesnt_exist_ever.txt.that")
        self.failUnlessRaises(CheckoutError,
                              index.checkout,
                              paths=["doesnt/exist"])

        # checkout file with modifications
        append_data = b"hello"
        with open(test_file, "ab") as fp:
            fp.write(append_data)
        try:
            index.checkout(test_file)
        except CheckoutError as e:
            self.assertEqual(len(e.failed_files), 1)
            self.assertEqual(e.failed_files[0], osp.basename(test_file))
            self.assertEqual(len(e.failed_files), len(e.failed_reasons))
            self.assertIsInstance(e.failed_reasons[0], str)
            self.assertEqual(len(e.valid_files), 0)
            with open(test_file, 'rb') as fd:
                s = fd.read()
            self.assertTrue(s.endswith(append_data), s)
        else:
            raise AssertionError("Exception CheckoutError not thrown")

        # if we force it it should work
        index.checkout(test_file, force=True)
        assert not open(test_file, 'rb').read().endswith(append_data)

        # checkout directory
        rmtree(osp.join(rw_repo.working_tree_dir, "lib"))
        rval = index.checkout('lib')
        assert len(list(rval)) > 1