Ejemplo n.º 1
0
        def _get_readme_from_cache(key, kind):
            readme_data = None
            readme_file = None
            try:
                # gets the landing revision! or tip if fails
                cs = db_repo.get_landing_changeset()
                if isinstance(cs, EmptyChangeset):
                    raise EmptyRepositoryError()
                renderer = MarkupRenderer()
                for f in README_FILES:
                    try:
                        readme = cs.get_node(f)
                        if not isinstance(readme, FileNode):
                            continue
                        readme_file = f
                        log.debug('Found README file `%s` rendering...',
                                  readme_file)
                        readme_data = renderer.render(readme.content,
                                                      filename=f)
                        break
                    except NodeDoesNotExistError:
                        continue
            except ChangesetError:
                log.error(traceback.format_exc())
                pass
            except EmptyRepositoryError:
                pass

            return readme_data, readme_file
Ejemplo n.º 2
0
    def _get_revision(self, revision):
        """
        Gets an ID revision given as str. This will always return a fill
        40 char revision number

        :param revision: str or int or None
        """
        if isinstance(revision, unicode):
            revision = safe_str(revision)

        if self._empty:
            raise EmptyRepositoryError("There are no changesets yet")

        if revision in [-1, 'tip', None]:
            revision = 'tip'

        try:
            revision = hex(self._repo.lookup(revision))
        except (IndexError, ValueError, RepoLookupError, TypeError):
            msg = ("Revision %s does not exist for %s" % (revision, self))
            raise ChangesetDoesNotExistError(msg)
        except (LookupError, ):
            msg = ("Ambiguous identifier `%s` for %s" % (revision, self))
            raise ChangesetDoesNotExistError(msg)

        return revision
Ejemplo n.º 3
0
    def _get_revision(self, revision):
        """
        Given any revision identifier, returns a 40 char string with revision hash.

        :param revision: str or int or None
        """
        if self._empty:
            raise EmptyRepositoryError("There are no changesets yet")

        if revision in [-1, None]:
            revision = b'tip'
        elif isinstance(revision, str):
            revision = safe_bytes(revision)

        try:
            if isinstance(revision, int):
                return ascii_str(self._repo[revision].hex())
            return ascii_str(
                mercurial.scmutil.revsymbol(self._repo, revision).hex())
        except (IndexError, ValueError, mercurial.error.RepoLookupError,
                TypeError):
            msg = "Revision %r does not exist for %s" % (safe_str(revision),
                                                         self.name)
            raise ChangesetDoesNotExistError(msg)
        except (LookupError, ):
            msg = "Ambiguous identifier `%s` for %s" % (safe_str(revision),
                                                        self.name)
            raise ChangesetDoesNotExistError(msg)
Ejemplo n.º 4
0
    def change(self, *filenodes):
        """
        Marks given ``FileNode`` objects to be *changed* in next commit.

        :raises ``EmptyRepositoryError``: if there are no changesets yet
        :raises ``NodeAlreadyExistsError``: if node with same path is already
          marked to be *changed*
        :raises ``NodeAlreadyRemovedError``: if node with same path is already
          marked to be *removed*
        :raises ``NodeDoesNotExistError``: if node doesn't exist in latest
          changeset
        :raises ``NodeNotChangedError``: if node hasn't really be changed
        """
        for node in filenodes:
            if node.path in (n.path for n in self.removed):
                raise NodeAlreadyRemovedError("Node at %s is already marked "
                                              "as removed" % node.path)
        try:
            self.repository.get_changeset()
        except EmptyRepositoryError:
            raise EmptyRepositoryError("Nothing to change - try to *add* new "
                                       "nodes rather than changing them")
        for node in filenodes:
            if node.path in (n.path for n in self.changed):
                raise NodeAlreadyChangedError("Node at '%s' is already "
                                              "marked as changed" % node.path)
            self.changed.append(node)
Ejemplo n.º 5
0
    def _get_revision(self, revision):
        """
        Given any revision identifier, returns a 40 char string with revision hash.
        """
        if self._empty:
            raise EmptyRepositoryError("There are no changesets yet")

        if revision in (None, '', 'tip', 'HEAD', 'head', -1):
            revision = -1

        if isinstance(revision, int):
            try:
                return self.revisions[revision]
            except IndexError:
                msg = "Revision %r does not exist for %s" % (revision,
                                                             self.name)
                raise ChangesetDoesNotExistError(msg)

        if isinstance(revision, str):
            if revision.isdigit() and (len(revision) < 12 or len(revision)
                                       == revision.count('0')):
                try:
                    return self.revisions[int(revision)]
                except IndexError:
                    msg = "Revision %r does not exist for %s" % (revision,
                                                                 self)
                    raise ChangesetDoesNotExistError(msg)

            # get by branch/tag name
            _ref_revision = self._parsed_refs.get(safe_bytes(revision))
            if _ref_revision:  # and _ref_revision[1] in [b'H', b'RH', b'T']:
                return ascii_str(_ref_revision[0])

            if revision in self.revisions:
                return revision

            # maybe it's a tag ? we don't have them in self.revisions
            if revision in self.tags.values():
                return revision

            if SHA_PATTERN.match(revision):
                msg = "Revision %r does not exist for %s" % (revision,
                                                             self.name)
                raise ChangesetDoesNotExistError(msg)

        raise ChangesetDoesNotExistError("Given revision %r not recognized" %
                                         revision)
Ejemplo n.º 6
0
    def _get_revision(self, revision):
        """
        For git backend we always return integer here. This way we ensure
        that changeset's revision attribute would become integer.
        """

        is_null = lambda o: len(o) == revision.count('0')

        if self._empty:
            raise EmptyRepositoryError("There are no changesets yet")

        if revision in (None, '', 'tip', 'HEAD', 'head', -1):
            return self.revisions[-1]

        is_bstr = isinstance(revision, (str, unicode))
        if ((is_bstr and revision.isdigit() and len(revision) < 12)
                or isinstance(revision, int) or is_null(revision)):
            try:
                revision = self.revisions[int(revision)]
            except IndexError:
                msg = ("Revision %s does not exist for %s" % (revision, self))
                raise ChangesetDoesNotExistError(msg)

        elif is_bstr:
            # get by branch/tag name
            _ref_revision = self._parsed_refs.get(revision)
            if _ref_revision:  # and _ref_revision[1] in ['H', 'RH', 'T']:
                return _ref_revision[0]

            _tags_shas = self.tags.values()
            # maybe it's a tag ? we don't have them in self.revisions
            if revision in _tags_shas:
                return _tags_shas[_tags_shas.index(revision)]

            elif not SHA_PATTERN.match(
                    revision) or revision not in self.revisions:
                msg = ("Revision %s does not exist for %s" % (revision, self))
                raise ChangesetDoesNotExistError(msg)

        # Ensure we return full id
        if not SHA_PATTERN.match(str(revision)):
            raise ChangesetDoesNotExistError(
                "Given revision %s not recognized" % revision)
        return revision
Ejemplo n.º 7
0
    def get_changesets(self,
                       start=None,
                       end=None,
                       start_date=None,
                       end_date=None,
                       branch_name=None,
                       reverse=False,
                       max_revisions=None):
        """
        Returns iterator of ``GitChangeset`` objects from start to end (both
        are inclusive), in ascending date order (unless ``reverse`` is set).

        :param start: changeset ID, as str; first returned changeset
        :param end: changeset ID, as str; last returned changeset
        :param start_date: if specified, changesets with commit date less than
          ``start_date`` would be filtered out from returned set
        :param end_date: if specified, changesets with commit date greater than
          ``end_date`` would be filtered out from returned set
        :param branch_name: if specified, changesets not reachable from given
          branch would be filtered out from returned set
        :param reverse: if ``True``, returned generator would be reversed
          (meaning that returned changesets would have descending date order)

        :raise BranchDoesNotExistError: If given ``branch_name`` does not
            exist.
        :raise ChangesetDoesNotExistError: If changeset for given ``start`` or
          ``end`` could not be found.

        """
        if branch_name and branch_name not in self.branches:
            raise BranchDoesNotExistError("Branch '%s' not found" %
                                          branch_name)
        # actually we should check now if it's not an empty repo to not spaw
        # subprocess commands
        if self._empty:
            raise EmptyRepositoryError("There are no changesets yet")

        # %H at format means (full) commit hash, initial hashes are retrieved
        # in ascending date order
        cmd = ['log', '--date-order', '--reverse', '--pretty=format:%H']
        if max_revisions:
            cmd += ['--max-count=%s' % max_revisions]
        if start_date:
            cmd += ['--since', start_date.strftime('%m/%d/%y %H:%M:%S')]
        if end_date:
            cmd += ['--until', end_date.strftime('%m/%d/%y %H:%M:%S')]
        if branch_name:
            cmd.append(branch_name)
        else:
            cmd.append(settings.GIT_REV_FILTER)

        revs = self.run_git_command(cmd).splitlines()
        start_pos = 0
        end_pos = len(revs)
        if start:
            _start = self._get_revision(start)
            try:
                start_pos = revs.index(_start)
            except ValueError:
                pass

        if end is not None:
            _end = self._get_revision(end)
            try:
                end_pos = revs.index(_end)
            except ValueError:
                pass

        if None not in [start, end] and start_pos > end_pos:
            raise RepositoryError('start cannot be after end')

        if end_pos is not None:
            end_pos += 1

        revs = revs[start_pos:end_pos]
        if reverse:
            revs.reverse()

        return CollectionGenerator(self, revs)