def test_commit_no_encode_decode(self):
        r = self._repo
        repo_path_bytes = r.path.encode(sys.getfilesystemencoding())
        encodings = ('utf8', 'latin1')
        names = [u'À'.encode(encoding) for encoding in encodings]
        for name, encoding in zip(names, encodings):
            full_path = os.path.join(repo_path_bytes, name)
            with open(full_path, 'wb') as f:
                f.write(encoding.encode('ascii'))
            # These files are break tear_down_repo, so cleanup these files
            # ourselves.
            self.addCleanup(os.remove, full_path)

        r.stage(names)
        commit_sha = r.do_commit(
            b'Files with different encodings',
            committer=b'Test Committer <*****@*****.**>',
            author=b'Test Author <*****@*****.**>',
            commit_timestamp=12395,
            commit_timezone=0,
            author_timestamp=12395,
            author_timezone=0,
            ref=None,
            merge_heads=[self._root_commit])

        for name, encoding in zip(names, encodings):
            mode, id = tree_lookup_path(r.get_object, r[commit_sha].tree, name)
            self.assertEqual(stat.S_IFREG | 0o644, mode)
            self.assertEqual(encoding.encode('ascii'), r[id].data)
 def get_blob_or_tree(self, commit, path):
     """Return the Git tree or blob object for `path` at `commit`."""
     if isinstance(path, str):
         path = path.encode()
     try:
         (mode, oid) = tree_lookup_path(self.__getitem__, commit.tree, path)
     except NotTreeError:
         # Some part of the path was a file where a folder was expected.
         # Example: path="/path/to/foo.txt" but "to" is a file in "/path".
         raise KeyError
     return self[oid]
 def test_commit_symlink(self):
     r = self._repo
     os.symlink('a', os.path.join(r.path, 'b'))
     r.stage(['a', 'b'])
     commit_sha = r.do_commit(
         b'Symlink b',
         committer=b'Test Committer <*****@*****.**>',
         author=b'Test Author <*****@*****.**>',
         commit_timestamp=12395,
         commit_timezone=0,
         author_timestamp=12395,
         author_timezone=0)
     self.assertEqual([self._root_commit], r[commit_sha].parents)
     b_mode, b_id = tree_lookup_path(r.get_object, r[commit_sha].tree, b'b')
     self.assertTrue(stat.S_ISLNK(b_mode))
     self.assertEqual(b'a', r[b_id].data)
 def test_commit_modified(self):
     r = self._repo
     with open(os.path.join(r.path, 'a'), 'wb') as f:
         f.write(b'new contents')
     r.stage(['a'])
     commit_sha = r.do_commit(
         b'modified a',
         committer=b'Test Committer <*****@*****.**>',
         author=b'Test Author <*****@*****.**>',
         commit_timestamp=12395,
         commit_timezone=0,
         author_timestamp=12395,
         author_timezone=0)
     self.assertEqual([self._root_commit], r[commit_sha].parents)
     a_mode, a_id = tree_lookup_path(r.get_object, r[commit_sha].tree, b'a')
     self.assertEqual(stat.S_IFREG | 0o644, a_mode)
     self.assertEqual(b'new contents', r[a_id].data)
Пример #5
0
def remove(repo=".", paths=None, cached=False):
    """Remove files from the staging area.

    :param repo: Repository for the files
    :param paths: Paths to remove
    """
    with open_repo_closing(repo) as r:
        index = r.open_index()
        for p in paths:
            full_path = os.path.abspath(p).encode(sys.getfilesystemencoding())
            tree_path = path_to_tree_path(r.path, p)
            try:
                index_sha = index[tree_path].sha
            except KeyError:
                raise Exception('%s did not match any files' % p)

            if not cached:
                try:
                    st = os.lstat(full_path)
                except OSError:
                    pass
                else:
                    try:
                        blob = blob_from_path_and_stat(full_path, st)
                    except IOError:
                        pass
                    else:
                        try:
                            committed_sha = tree_lookup_path(
                                r.__getitem__, r[r.head()].tree, tree_path)[1]
                        except KeyError:
                            committed_sha = None

                        if blob.id != index_sha and index_sha != committed_sha:
                            raise Exception(
                                'file has staged content differing '
                                'from both the file and head: %s' % p)

                        if index_sha != committed_sha:
                            raise Exception('file has staged changes: %s' % p)
                        os.remove(full_path)
            del index[tree_path]
        index.write()