Beispiel #1
0
def write_pack_index_v2(filename, entries, pack_checksum):
    """Write a new pack index file.

    :param filename: The filename of the new pack index file.
    :param entries: List of tuples with object name (sha), offset_in_pack,  and
            crc32_checksum.
    :param pack_checksum: Checksum of the pack file.
    """
    f = GitFile(filename, 'wb')
    f = SHA1Writer(f)
    f.write('\377tOc') # Magic!
    f.write(struct.pack(">L", 2))
    fan_out_table = defaultdict(lambda: 0)
    for (name, offset, entry_checksum) in entries:
        fan_out_table[ord(name[0])] += 1
    # Fan-out table
    for i in range(0x100):
        f.write(struct.pack(">L", fan_out_table[i]))
        fan_out_table[i+1] += fan_out_table[i]
    for (name, offset, entry_checksum) in entries:
        f.write(name)
    for (name, offset, entry_checksum) in entries:
        f.write(struct.pack(">L", entry_checksum))
    for (name, offset, entry_checksum) in entries:
        # FIXME: handle if MSBit is set in offset
        f.write(struct.pack(">L", offset))
    # FIXME: handle table for pack files > 8 Gb
    assert len(pack_checksum) == 20
    f.write(pack_checksum)
    f.close()
Beispiel #2
0
    def get_packed_refs(self):
        """Get contents of the packed-refs file.

        :return: Dictionary mapping ref names to SHA1s

        :note: Will return an empty dictionary when no packed-refs file is
            present.
        """
        # TODO: invalidate the cache on repacking
        if self._packed_refs is None:
            # set both to empty because we want _peeled_refs to be
            # None if and only if _packed_refs is also None.
            self._packed_refs = {}
            self._peeled_refs = {}
            path = os.path.join(self.path, 'packed-refs')
            try:
                f = GitFile(path, 'rb')
            except IOError, e:
                if e.errno == errno.ENOENT:
                    return {}
                raise
            try:
                first_line = iter(f).next().rstrip()
                if (first_line.startswith("# pack-refs") and " peeled" in
                        first_line):
                    for sha, name, peeled in read_packed_refs_with_peeled(f):
                        self._packed_refs[name] = sha
                        if peeled:
                            self._peeled_refs[name] = peeled
                else:
                    f.seek(0)
                    for sha, name in read_packed_refs(f):
                        self._packed_refs[name] = sha
            finally:
                f.close()
Beispiel #3
0
    def get_packed_refs(self):
        """Get contents of the packed-refs file.

        :return: Dictionary mapping ref names to SHA1s

        :note: Will return an empty dictionary when no packed-refs file is
            present.
        """
        # TODO: invalidate the cache on repacking
        if self._packed_refs is None:
            # set both to empty because we want _peeled_refs to be
            # None if and only if _packed_refs is also None.
            self._packed_refs = {}
            self._peeled_refs = {}
            path = os.path.join(self.path, 'packed-refs')
            try:
                f = GitFile(path, 'rb')
            except IOError, e:
                if e.errno == errno.ENOENT:
                    return {}
                raise
            try:
                first_line = iter(f).next().rstrip()
                if (first_line.startswith("# pack-refs") and " peeled" in
                        first_line):
                    for sha, name, peeled in read_packed_refs_with_peeled(f):
                        self._packed_refs[name] = sha
                        if peeled:
                            self._peeled_refs[name] = peeled
                else:
                    f.seek(0)
                    for sha, name in read_packed_refs(f):
                        self._packed_refs[name] = sha
            finally:
                f.close()
Beispiel #4
0
    def add_alternate_path(self, path):
        """Add an alternate path to this object store.
        """
        try:
            os.mkdir(os.path.join(self.path, "info"))
        except OSError as e:
            if e.errno != errno.EEXIST:
                raise
        alternates_path = os.path.join(self.path, "info/alternates")
        f = GitFile(alternates_path, 'wb')
        try:
            try:
                orig_f = open(alternates_path, 'rb')
            except (OSError, IOError) as e:
                if e.errno != errno.ENOENT:
                    raise
            else:
                try:
                    f.write(orig_f.read())
                finally:
                    orig_f.close()
            f.write("%s\n" % path)
        finally:
            f.close()

        if not os.path.isabs(path):
            path = os.path.join(self.path, path)
        self.alternates.append(DiskObjectStore(path))
 def writeIndex(self, filename, entries, pack_checksum):
     # FIXME: Write to BytesIO instead rather than hitting disk ?
     f = GitFile(filename, "wb")
     try:
         self._write_fn(f, entries, pack_checksum)
     finally:
         f.close()
Beispiel #6
0
    def read_loose_ref(self, name):
        """Read a reference file and return its contents.

        If the reference file a symbolic reference, only read the first line of
        the file. Otherwise, only read the first 40 bytes.

        :param name: the refname to read, relative to refpath
        :return: The contents of the ref file, or None if the file does not
            exist.
        :raises IOError: if any other error occurs
        """
        filename = self.refpath(name)
        try:
            f = GitFile(filename, 'rb')
            try:
                header = f.read(len(SYMREF))
                if header == SYMREF:
                    # Read only the first line
                    return header + iter(f).next().rstrip("\r\n")
                else:
                    # Read only the first 40 bytes
                    return header + f.read(40-len(SYMREF))
            finally:
                f.close()
        except IOError, e:
            if e.errno == errno.ENOENT:
                return None
            raise
Beispiel #7
0
    def add_if_new(self, name, ref):
        """Add a new reference only if it does not already exist.

        This method follows symrefs, and only ensures that the last ref in the
        chain does not exist.

        :param name: The refname to set.
        :param ref: The new sha the refname will refer to.
        :return: True if the add was successful, False otherwise.
        """
        try:
            realname, contents = self._follow(name)
            if contents is not None:
                return False
        except KeyError:
            realname = name
        self._check_refname(realname)
        filename = self.refpath(realname)
        ensure_dir_exists(os.path.dirname(filename))
        f = GitFile(filename, 'wb')
        try:
            if os.path.exists(filename) or name in self.get_packed_refs():
                f.abort()
                return False
            try:
                f.write(ref+"\n")
            except (OSError, IOError):
                f.abort()
                raise
        finally:
            f.close()
        return True
Beispiel #8
0
    def read_loose_ref(self, name):
        """Read a reference file and return its contents.

        If the reference file a symbolic reference, only read the first line of
        the file. Otherwise, only read the first 40 bytes.

        :param name: the refname to read, relative to refpath
        :return: The contents of the ref file, or None if the file does not
            exist.
        :raises IOError: if any other error occurs
        """
        filename = self.refpath(name)
        try:
            f = GitFile(filename, 'rb')
            try:
                header = f.read(len(SYMREF))
                if header == SYMREF:
                    # Read only the first line
                    return header + iter(f).next().rstrip("\r\n")
                else:
                    # Read only the first 40 bytes
                    return header + f.read(40 - len(SYMREF))
            finally:
                f.close()
        except IOError as e:
            if e.errno == errno.ENOENT:
                return None
            raise
Beispiel #9
0
    def add_if_new(self, name, ref):
        """Add a new reference only if it does not already exist.

        This method follows symrefs, and only ensures that the last ref in the
        chain does not exist.

        :param name: The refname to set.
        :param ref: The new sha the refname will refer to.
        :return: True if the add was successful, False otherwise.
        """
        try:
            realname, contents = self._follow(name)
            if contents is not None:
                return False
        except KeyError:
            realname = name
        self._check_refname(realname)
        filename = self.refpath(realname)
        ensure_dir_exists(os.path.dirname(filename))
        f = GitFile(filename, 'wb')
        try:
            if os.path.exists(filename) or name in self.get_packed_refs():
                f.abort()
                return False
            try:
                f.write(ref + "\n")
            except (OSError, IOError):
                f.abort()
                raise
        finally:
            f.close()
        return True
Beispiel #10
0
    def move_in_pack(self, path):
        """Move a specific file containing a pack into the pack directory.

        :note: The file should be on the same file system as the
            packs directory.

        :param path: Path to the pack file.
        """
        p = PackData(path)
        try:
            entries = p.sorted_entries()
            basename = os.path.join(
                self.pack_dir,
                "pack-%s" % iter_sha1(entry[0] for entry in entries))
            f = GitFile(basename + ".idx", "wb")
            try:
                write_pack_index_v2(f, entries, p.get_stored_checksum())
            finally:
                f.close()
        finally:
            p.close()
        os.rename(path, basename + ".pack")
        final_pack = Pack(basename)
        self._add_known_pack(final_pack)
        return final_pack
Beispiel #11
0
    def set_symbolic_ref(self,
                         name,
                         other,
                         committer=None,
                         timestamp=None,
                         timezone=None,
                         message=None):
        """Make a ref point at another ref.

        :param name: Name of the ref to set
        :param other: Name of the ref to point at
        :param message: Optional message to describe the change
        """
        self._check_refname(name)
        self._check_refname(other)
        filename = self.refpath(name)
        f = GitFile(filename, 'wb')
        try:
            f.write(SYMREF + other + b'\n')
            sha = self.follow(name)[-1]
            self._log(name,
                      sha,
                      sha,
                      committer=committer,
                      timestamp=timestamp,
                      timezone=timezone,
                      message=message)
        except BaseException:
            f.abort()
            raise
        else:
            f.close()
Beispiel #12
0
 def writeIndex(self, filename, entries, pack_checksum):
     # FIXME: Write to StringIO instead rather than hitting disk ?
     f = GitFile(filename, "wb")
     try:
         self._write_fn(f, entries, pack_checksum)
     finally:
         f.close()
    def _complete_thin_pack(self, f, path, copier, indexer):
        """Move a specific file containing a pack into the pack directory.

        :note: The file should be on the same file system as the
            packs directory.

        :param f: Open file object for the pack.
        :param path: Path to the pack file.
        :param copier: A PackStreamCopier to use for writing pack data.
        :param indexer: A PackIndexer for indexing the pack.
        """
        entries = list(indexer)

        # Update the header with the new number of objects.
        f.seek(0)
        write_pack_header(f, len(entries) + len(indexer.ext_refs()))

        # Must flush before reading (http://bugs.python.org/issue3207)
        f.flush()

        # Rescan the rest of the pack, computing the SHA with the new header.
        new_sha = compute_file_sha(f, end_ofs=-20)

        # Must reposition before writing (http://bugs.python.org/issue3207)
        f.seek(0, os.SEEK_CUR)

        # Complete the pack.
        for ext_sha in indexer.ext_refs():
            assert len(ext_sha) == 20
            type_num, data = self.get_raw(ext_sha)
            offset = f.tell()
            crc32 = write_pack_object(f, type_num, data, sha=new_sha)
            entries.append((ext_sha, offset, crc32))
        pack_sha = new_sha.digest()
        f.write(pack_sha)
        f.close()

        # Move the pack in.
        entries.sort()
        pack_base_name = self._get_pack_basepath(entries)
        try:
            os.rename(path, pack_base_name + '.pack')
        except WindowsError:
            os.remove(pack_base_name + '.pack')
            os.rename(path, pack_base_name + '.pack')

        # Write the index.
        index_file = GitFile(pack_base_name + '.idx', 'wb')
        try:
            write_pack_index_v2(index_file, entries, pack_sha)
            index_file.close()
        finally:
            index_file.abort()

        # Add the pack to the store and return it.
        final_pack = Pack(pack_base_name)
        final_pack.check_length_and_checksum()
        self._add_known_pack(pack_base_name, final_pack)
        return final_pack
Beispiel #14
0
 def test_readonly(self):
     f = GitFile(self.path('foo'), 'rb')
     self.assertTrue(isinstance(f, io.IOBase))
     self.assertEqual(b'foo contents', f.read())
     self.assertEqual(b'', f.read())
     f.seek(4)
     self.assertEqual(b'contents', f.read())
     f.close()
Beispiel #15
0
 def _put_named_file(self, path, contents):
     """Write a file from the control dir with a specific name and contents.
     """
     f = GitFile(os.path.join(self.controldir(), path), 'wb')
     try:
         f.write(contents)
     finally:
         f.close()
Beispiel #16
0
 def write(self):
     """Write current contents of index to disk."""
     f = GitFile(self._filename, 'wb')
     try:
         f = SHA1Writer(f)
         write_index_dict(f, self._byname)
     finally:
         f.close()
Beispiel #17
0
 def write(self):
     """Write current contents of index to disk."""
     f = GitFile(self._filename, 'wb')
     try:
         f = SHA1Writer(f)
         write_index_dict(f, self._byname)
     finally:
         f.close()
Beispiel #18
0
 def write(self) -> None:
     """Write current contents of index to disk."""
     f = GitFile(self._filename, "wb")
     try:
         f = SHA1Writer(f)
         write_index_dict(f, self._byname, version=self._version)
     finally:
         f.close()
Beispiel #19
0
 def test_readonly(self):
     f = GitFile(self.path("foo"), "rb")
     self.assertTrue(isinstance(f, io.IOBase))
     self.assertEqual(b"foo contents", f.read())
     self.assertEqual(b"", f.read())
     f.seek(4)
     self.assertEqual(b"contents", f.read())
     f.close()
 def test_readonly(self):
     f = GitFile(self.path("foo"), "rb")
     self.assertTrue(isinstance(f, io.IOBase))
     self.assertEqual(b"foo contents", f.read())
     self.assertEqual(b"", f.read())
     f.seek(4)
     self.assertEqual(b"contents", f.read())
     f.close()
Beispiel #21
0
 def test_readonly(self):
     f = GitFile(self.path('foo'), 'rb')
     self.assertTrue(isinstance(f, io.IOBase))
     self.assertEqual(b'foo contents', f.read())
     self.assertEqual(b'', f.read())
     f.seek(4)
     self.assertEqual(b'contents', f.read())
     f.close()
Beispiel #22
0
 def write_to_path(self, path=None):
     """Write configuration to a file on disk."""
     if path is None:
         path = self.path
     f = GitFile(path, 'wb')
     try:
         self.write_to_file(f)
     finally:
         f.close()
Beispiel #23
0
 def from_path(cls, path):
     """Read configuration from a file on disk."""
     f = GitFile(path, 'rb')
     try:
         ret = cls.from_file(f)
         ret.path = path
         return ret
     finally:
         f.close()
Beispiel #24
0
 def test_readonly(self):
     import _io
     f = GitFile(self.path('foo'), 'rb')
     self.assertTrue(isinstance(f, _io.BufferedReader))
     self.assertEqual(b'foo contents', f.read())
     self.assertEqual(b'', f.read())
     f.seek(4)
     self.assertEqual(b'contents', f.read())
     f.close()
Beispiel #25
0
 def write_to_path(self, path=None):
     """Write configuration to a file on disk."""
     if path is None:
         path = self.path
     f = GitFile(path, 'wb')
     try:
         self.write_to_file(f)
     finally:
         f.close()
Beispiel #26
0
 def from_path(cls, path):
     """Read configuration from a file on disk."""
     f = GitFile(path, 'rb')
     try:
         ret = cls.from_file(f)
         ret.path = path
         return ret
     finally:
         f.close()
Beispiel #27
0
    def _complete_thin_pack(self, f, path, copier, indexer):
        """Move a specific file containing a pack into the pack directory.

        :note: The file should be on the same file system as the
            packs directory.

        :param f: Open file object for the pack.
        :param path: Path to the pack file.
        :param copier: A PackStreamCopier to use for writing pack data.
        :param indexer: A PackIndexer for indexing the pack.
        """
        entries = list(indexer)

        # Update the header with the new number of objects.
        f.seek(0)
        write_pack_header(f, len(entries) + len(indexer.ext_refs()))

        # Must flush before reading (http://bugs.python.org/issue3207)
        f.flush()

        # Rescan the rest of the pack, computing the SHA with the new header.
        new_sha = compute_file_sha(f, end_ofs=-20)

        # Must reposition before writing (http://bugs.python.org/issue3207)
        f.seek(0, os.SEEK_CUR)

        # Complete the pack.
        for ext_sha in indexer.ext_refs():
            assert len(ext_sha) == 20
            type_num, data = self.get_raw(ext_sha)
            offset = f.tell()
            crc32 = write_pack_object(f, type_num, data, sha=new_sha)
            entries.append((ext_sha, offset, crc32))
        pack_sha = new_sha.digest()
        f.write(pack_sha)
        f.close()

        # Move the pack in.
        entries.sort()
        pack_base_name = os.path.join(
          self.pack_dir, 'pack-' + iter_sha1(e[0] for e in entries))
        os.rename(path, pack_base_name + '.pack')

        # Write the index.
        index_file = GitFile(pack_base_name + '.idx', 'wb')
        try:
            write_pack_index_v2(index_file, entries, pack_sha)
            index_file.close()
        finally:
            index_file.abort()

        # Add the pack to the store and return it.
        final_pack = Pack(pack_base_name)
        final_pack.check_length_and_checksum()
        self._add_known_pack(final_pack)
        return final_pack
Beispiel #28
0
 def from_file(cls, filename):
     """Get the contents of a SHA file on disk"""
     size = os.path.getsize(filename)
     f = GitFile(filename, 'rb')
     try:
         map = mmap.mmap(f.fileno(), size, access=mmap.ACCESS_READ)
         shafile = cls._parse_file(map)
         return shafile
     finally:
         f.close()
Beispiel #29
0
 def _parse_file(self):
     f = GitFile(self._filename, 'rb')
     try:
         magic = f.read(2)
         if self._is_legacy_object(magic):
             self._parse_legacy_object(f)
         else:
             self._parse_object(f)
     finally:
         f.close()
Beispiel #30
0
 def test_remove_packed_without_peeled(self):
     refs_file = os.path.join(self._repo.path, "packed-refs")
     f = GitFile(refs_file)
     refs_data = f.read()
     f.close()
     f = GitFile(refs_file, "wb")
     f.write("\n".join(l for l in refs_data.split("\n") if not l or l[0] not in "#^"))
     f.close()
     self._repo = Repo(self._repo.path)
     refs = self._repo.refs
     self.assertTrue(refs.remove_if_equals("refs/heads/packed", "42d06bd4b77fed026b154d16493e5deab78f02ec"))
Beispiel #31
0
def load_pack_index(path):
    """Load an index file by path.

    :param filename: Path to the index file
    :return: A PackIndex loaded from the given path
    """
    f = GitFile(path, 'rb')
    try:
        return load_pack_index_file(path, f)
    finally:
        f.close()
Beispiel #32
0
 def from_path(cls, path):
     f = GitFile(path, 'rb')
     try:
         obj = cls.from_file(f)
         obj._path = path
         obj._sha = FixedSha(filename_to_hex(path))
         obj._file = None
         obj._magic = None
         return obj
     finally:
         f.close()
Beispiel #33
0
 def from_path(cls, path):
     f = GitFile(path, 'rb')
     try:
         obj = cls.from_file(f)
         obj._path = path
         obj._sha = FixedSha(filename_to_hex(path))
         obj._file = None
         obj._magic = None
         return obj
     finally:
         f.close()
Beispiel #34
0
    def _put_named_file(self, path, contents):
        """Write a file to the control dir with the given name and contents.

        :param path: The path to the file, relative to the control dir.
        :param contents: A string to write to the file.
        """
        path = path.lstrip(os.path.sep)
        f = GitFile(os.path.join(self.controldir(), path), 'wb')
        try:
            f.write(contents)
        finally:
            f.close()
Beispiel #35
0
 def from_path(cls, path):
     """Open a SHA file from disk."""
     f = GitFile(path, "rb")
     try:
         obj = cls.from_file(f)
         obj._path = path
         obj._sha = FixedSha(filename_to_hex(path))
         obj._file = None
         obj._magic = None
         return obj
     finally:
         f.close()
Beispiel #36
0
 def read(self):
     """Read current contents of index from disk."""
     f = GitFile(self._filename, 'rb')
     try:
         f = SHA1Reader(f)
         for x in read_index(f):
             self[x[0]] = tuple(x[1:])
         # FIXME: Additional data?
         f.read(os.path.getsize(self._filename)-f.tell()-20)
         f.check_sha()
     finally:
         f.close()
Beispiel #37
0
    def _put_named_file(self, path, contents):
        """Write a file to the control dir with the given name and contents.

        :param path: The path to the file, relative to the control dir.
        :param contents: A string to write to the file.
        """
        path = path.lstrip(os.path.sep)
        f = GitFile(os.path.join(self.controldir(), path), 'wb')
        try:
            f.write(contents)
        finally:
            f.close()
Beispiel #38
0
    def create_index_v2(self, filename, progress=None):
        """Create a version 2 index file for this data file.

        :param filename: Index filename.
        :param progress: Progress report function
        :return: Checksum of index file
        """
        entries = self.sorted_entries(progress=progress)
        f = GitFile(filename, 'wb')
        try:
            return write_pack_index_v2(f, entries, self.calculate_checksum())
        finally:
            f.close()
Beispiel #39
0
 def test_remove_packed_without_peeled(self):
     refs_file = os.path.join(self._repo.path, 'packed-refs')
     f = GitFile(refs_file)
     refs_data = f.read()
     f.close()
     f = GitFile(refs_file, 'wb')
     f.write(b'\n'.join(l for l in refs_data.split(b'\n')
                        if not l or l[0] not in b'#^'))
     f.close()
     self._repo = Repo(self._repo.path)
     refs = self._repo.refs
     self.assertTrue(refs.remove_if_equals(
         b'refs/heads/packed', b'42d06bd4b77fed026b154d16493e5deab78f02ec'))
Beispiel #40
0
def write_pack(filename, objects, num_objects):
    """Write a new pack data file.

    :param filename: Path to the new pack file (without .pack extension)
    :param objects: Iterable over (object, path) tuples to write
    :param num_objects: Number of objects to write
    """
    f = GitFile(filename + ".pack", 'wb')
    try:
        entries, data_sum = write_pack_data(f, objects, num_objects)
    finally:
        f.close()
    entries.sort()
    write_pack_index_v2(filename + ".idx", entries, data_sum)
Beispiel #41
0
 def read(self):
     """Read current contents of index from disk."""
     if not os.path.exists(self._filename):
         return
     f = GitFile(self._filename, 'rb')
     try:
         f = SHA1Reader(f)
         for x in read_index(f):
             self[x[0]] = IndexEntry(*x[1:])
         # FIXME: Additional data?
         f.read(os.path.getsize(self._filename)-f.tell()-20)
         f.check_sha()
     finally:
         f.close()
Beispiel #42
0
 def read(self):
     """Read current contents of index from disk."""
     if not os.path.exists(self._filename):
         return
     f = GitFile(self._filename, "rb")
     try:
         f = SHA1Reader(f)
         for name, entry in read_index(f):
             self[name] = entry
         # FIXME: Additional data?
         f.read(os.path.getsize(self._filename) - f.tell() - 20)
         f.check_sha()
     finally:
         f.close()
Beispiel #43
0
 def from_file(cls, filename):
     """Get the contents of a SHA file on disk."""
     f = GitFile(filename, 'rb')
     try:
         try:
             obj = cls._parse_file_header(f)
             obj._sha = FixedSha(filename_to_hex(filename))
             obj._needs_parsing = True
             obj._needs_serialization = True
             return obj
         except (IndexError, ValueError), e:
             raise ObjectFormatException("invalid object header")
     finally:
         f.close()
Beispiel #44
0
 def read(self):
     """Read current contents of index from disk."""
     if not os.path.exists(self._filename):
         return
     f = GitFile(self._filename, "rb")
     try:
         f = SHA1Reader(f)
         for x in read_index(f):
             self[x[0]] = IndexEntry(*x[1:])
         # FIXME: Additional data?
         f.read(os.path.getsize(self._filename) - f.tell() - 20)
         f.check_sha()
     finally:
         f.close()
Beispiel #45
0
    def test_abort_close(self):
        foo = self.path('foo')
        f = GitFile(foo, 'wb')
        f.abort()
        try:
            f.close()
        except (IOError, OSError):
            self.fail()

        f = GitFile(foo, 'wb')
        f.close()
        try:
            f.abort()
        except (IOError, OSError):
            self.fail()
Beispiel #46
0
    def test_abort_close(self):
        foo = self.path('foo')
        f = GitFile(foo, 'wb')
        f.abort()
        try:
            f.close()
        except (IOError, OSError):
            self.fail()

        f = GitFile(foo, 'wb')
        f.close()
        try:
            f.abort()
        except (IOError, OSError):
            self.fail()
Beispiel #47
0
    def test_open_twice(self):
        foo = self.path('foo')
        f1 = GitFile(foo, 'wb')
        f1.write(b'new')
        try:
            f2 = GitFile(foo, 'wb')
            self.fail()
        except OSError as e:
            self.assertEqual(errno.EEXIST, e.errno)
        f1.write(b' contents')
        f1.close()

        # Ensure trying to open twice doesn't affect original.
        with open(foo, 'rb') as f:
            self.assertEqual(b'new contents', f.read())
Beispiel #48
0
    def get_description(self):
        """Retrieve the description of this repository.

        :return: A string describing the repository or None.
        """
        path = os.path.join(self._controldir, 'description')
        try:
            f = GitFile(path, 'rb')
            try:
                return f.read()
            finally:
                f.close()
        except (IOError, OSError), e:
            if e.errno != errno.ENOENT:
                raise
            return None
 def test_remove_packed_without_peeled(self):
     refs_file = os.path.join(self._repo.path, "packed-refs")
     f = GitFile(refs_file)
     refs_data = f.read()
     f.close()
     f = GitFile(refs_file, "wb")
     f.write(b"\n".join(line for line in refs_data.split(b"\n")
                        if not line or line[0] not in b"#^"))
     f.close()
     self._repo = Repo(self._repo.path)
     refs = self._repo.refs
     self.assertTrue(
         refs.remove_if_equals(
             b"refs/heads/packed",
             b"42d06bd4b77fed026b154d16493e5deab78f02ec",
         ))
Beispiel #50
0
    def get_description(self):
        """Retrieve the description of this repository.

        :return: A string describing the repository or None.
        """
        path = os.path.join(self._controldir, 'description')
        try:
            f = GitFile(path, 'rb')
            try:
                return f.read()
            finally:
                f.close()
        except (IOError, OSError) as e:
            if e.errno != errno.ENOENT:
                raise
            return None
Beispiel #51
0
    def test_open_twice(self):
        foo = self.path('foo')
        f1 = GitFile(foo, 'wb')
        f1.write(b'new')
        try:
            f2 = GitFile(foo, 'wb')
            self.fail()
        except OSError as e:
            self.assertEqual(errno.EEXIST, e.errno)
        else:
            f2.close()
        f1.write(b' contents')
        f1.close()

        # Ensure trying to open twice doesn't affect original.
        f = open(foo, 'rb')
        self.assertEqual(b'new contents', f.read())
        f.close()
    def test_open_twice(self):
        foo = self.path("foo")
        f1 = GitFile(foo, "wb")
        f1.write(b"new")
        try:
            f2 = GitFile(foo, "wb")
            self.fail()
        except FileLocked:
            pass
        else:
            f2.close()
        f1.write(b" contents")
        f1.close()

        # Ensure trying to open twice doesn't affect original.
        f = open(foo, "rb")
        self.assertEqual(b"new contents", f.read())
        f.close()
Beispiel #53
0
    def set_symbolic_ref(self, name, other):
        """Make a ref point at another ref.

        :param name: Name of the ref to set
        :param other: Name of the ref to point at
        """
        self._check_refname(name)
        self._check_refname(other)
        filename = self.refpath(name)
        try:
            f = GitFile(filename, 'wb')
            try:
                f.write(SYMREF + other + b'\n')
            except (IOError, OSError):
                f.abort()
                raise
        finally:
            f.close()
Beispiel #54
0
    def add_object(self, obj):
        """Add a single object to this object store.

        :param obj: Object to add
        """
        dir = os.path.join(self.path, obj.id[:2])
        try:
            os.mkdir(dir)
        except OSError as e:
            if e.errno != errno.EEXIST:
                raise
        path = os.path.join(dir, obj.id[2:])
        if os.path.exists(path):
            return # Already there, no need to write again
        f = GitFile(path, 'wb')
        try:
            f.write(obj.as_legacy_object())
        finally:
            f.close()
Beispiel #55
0
 def _read_alternate_paths(self):
     try:
         f = GitFile(os.path.join(self.path, "info", "alternates"), 'rb')
     except (OSError, IOError) as e:
         if e.errno == errno.ENOENT:
             return []
         raise
     ret = []
     try:
         for l in f.readlines():
             l = l.rstrip("\n")
             if l[0] == "#":
                 continue
             if os.path.isabs(l):
                 ret.append(l)
             else:
                 ret.append(os.path.join(self.path, l))
         return ret
     finally:
         f.close()
Beispiel #56
0
    def _remove_packed_ref(self, name):
        if self._packed_refs is None:
            return
        filename = os.path.join(self.path, b'packed-refs')
        # reread cached refs from disk, while holding the lock
        f = GitFile(filename, 'wb')
        try:
            self._packed_refs = None
            self.get_packed_refs()

            if name not in self._packed_refs:
                return

            del self._packed_refs[name]
            if name in self._peeled_refs:
                del self._peeled_refs[name]
            write_packed_refs(f, self._packed_refs, self._peeled_refs)
            f.close()
        finally:
            f.abort()
Beispiel #57
0
    def set_if_equals(self, name, old_ref, new_ref):
        """Set a refname to new_ref only if it currently equals old_ref.

        This method follows all symbolic references, and can be used to perform
        an atomic compare-and-swap operation.

        :param name: The refname to set.
        :param old_ref: The old sha the refname must refer to, or None to set
            unconditionally.
        :param new_ref: The new sha the refname will refer to.
        :return: True if the set was successful, False otherwise.
        """
        self._check_refname(name)
        try:
            realname, _ = self._follow(name)
        except KeyError:
            realname = name
        filename = self.refpath(realname)
        ensure_dir_exists(os.path.dirname(filename))
        f = GitFile(filename, 'wb')
        try:
            if old_ref is not None:
                try:
                    # read again while holding the lock
                    orig_ref = self.read_loose_ref(realname)
                    if orig_ref is None:
                        orig_ref = self.get_packed_refs().get(realname, None)
                    if orig_ref != old_ref:
                        f.abort()
                        return False
                except (OSError, IOError):
                    f.abort()
                    raise
            try:
                f.write(new_ref + "\n")
            except (OSError, IOError):
                f.abort()
                raise
        finally:
            f.close()
        return True
Beispiel #58
0
    def test_write(self):
        foo = self.path('foo')
        foo_lock = '%s.lock' % foo

        orig_f = open(foo, 'rb')
        self.assertEqual(orig_f.read(), b'foo contents')
        orig_f.close()

        self.assertFalse(os.path.exists(foo_lock))
        f = GitFile(foo, 'wb')
        self.assertFalse(f.closed)
        self.assertRaises(AttributeError, getattr, f, 'not_a_file_property')

        self.assertTrue(os.path.exists(foo_lock))
        f.write(b'new stuff')
        f.seek(4)
        f.write(b'contents')
        f.close()
        self.assertFalse(os.path.exists(foo_lock))

        new_f = open(foo, 'rb')
        self.assertEqual(b'new contents', new_f.read())
        new_f.close()