Пример #1
0
    def testCache(self):
        self._request_json.return_value = {'log': []}
        self._request.return_value = 'aGVsbG8='

        repository = 'https://chromium.googlesource.com/repo'
        git_hash = '3a44bc56c4efa42a900a1c22b001559b81e457e9'

        gitiles_service.CommitInfo(repository, git_hash)
        self._request_json.assert_called_with(
            '%s/+/%s?format=JSON' % (repository, git_hash),
            use_cache=True,
            use_auth=True,
            scope=gerrit_service.GERRIT_SCOPE)

        gitiles_service.CommitRange(repository, git_hash, git_hash)
        self._request_json.assert_called_with(
            '%s/+log/%s..%s?format=JSON' % (repository, git_hash, git_hash),
            use_cache=True,
            use_auth=True,
            scope=gerrit_service.GERRIT_SCOPE)

        gitiles_service.FileContents(repository, git_hash, 'path')
        self._request.assert_called_with('%s/+/%s/path?format=TEXT' %
                                         (repository, git_hash),
                                         use_cache=True,
                                         use_auth=True,
                                         scope=gerrit_service.GERRIT_SCOPE)
Пример #2
0
    def Midpoint(cls, dep_a, dep_b):
        """Return a Dep halfway between the two given Deps.

    Uses Gitiles to look up the commit range.

    Args:
      dep_a: The first Dep in the range.
      dep_b: The last Dep in the range.

    Returns:
      A new Dep representing the midpoint.
      The commit before the midpoint if the range has an even number of commits.
      None if the range is empty, or the Deps are given in the wrong order.

    Raises:
      ValueError: The Deps are in different repositories.
    """
        if dep_a.repository != dep_b.repository:
            raise ValueError("Can't find the midpoint of Deps in differing "
                             'repositories: "%s" and "%s"' % (dep_a, dep_b))

        commits = gitiles_service.CommitRange(dep_a.repository_url,
                                              dep_a.git_hash, dep_b.git_hash)
        # We don't handle NotFoundErrors because we assume that all Deps either came
        # from this method or were already validated elsewhere.
        if len(commits) <= 1:
            return None
        commits = commits[1:]  # Remove dep_b from the range.

        return cls(dep_a.repository, commits[len(commits) / 2]['commit'])
    def testCommitRangePaginated(self, mock_fetch):
        return_value_1 = {
            'log': [
                {
                    'commit': 'commit_4_hash'
                },
                {
                    'commit': 'commit_3_hash'
                },
            ],
            'next': 'commit_2_hash',
        }
        return_value_2 = {
            'log': [
                {
                    'commit': 'commit_2_hash'
                },
                {
                    'commit': 'commit_1_hash'
                },
            ],
        }

        _SetFetchReturnValues(mock_fetch, return_value_1, return_value_2)

        self.assertEqual(
            gitiles_service.CommitRange('repo', 'commit_0_hash',
                                        'commit_4_hash'),
            return_value_1['log'] + return_value_2['log'])
Пример #4
0
    def testCommitRangePaginated(self):
        return_value_1 = {
            'log': [
                {
                    'commit': 'commit_4_hash'
                },
                {
                    'commit': 'commit_3_hash'
                },
            ],
            'next': 'commit_2_hash',
        }
        return_value_2 = {
            'log': [
                {
                    'commit': 'commit_2_hash'
                },
                {
                    'commit': 'commit_1_hash'
                },
            ],
        }

        self._request_json.side_effect = return_value_1, return_value_2

        self.assertEqual(
            gitiles_service.CommitRange(
                'https://chromium.googlesource.com/repo', 'commit_0_hash',
                'commit_4_hash'),
            return_value_1['log'] + return_value_2['log'])
Пример #5
0
    def post(self):
        repo = self.request.get('repository')
        git_hash_1 = self.request.get('git_hash',
                                      self.request.get('git_hash_1'))
        git_hash_2 = self.request.get('git_hash_2')

        repositories = namespaced_stored_object.Get(_REPOSITORIES_KEY)
        if repo not in repositories:
            self.response.out.write(
                json.dumps({'error': 'Unknown repository: %s' % repo}))
            return

        repository_url = repositories[repo]['repository_url']

        if not git_hash_1:
            self.response.out.write(
                json.dumps({'error': "No 'git_hash' parameter specified."}))
            return

        if git_hash_2:
            result = gitiles_service.CommitRange(repo, git_hash_1, git_hash_2)
            self.response.out.write(json.dumps(result))
            return

        result = gitiles_service.CommitInfo(repository_url, git_hash_1)
        self.response.out.write(json.dumps(result))
Пример #6
0
    def Midpoint(cls, commit_a, commit_b):
        """Return a Commit halfway between the two given Commits.

    Uses Gitiles to look up the commit range.

    Args:
      commit_a: The first Commit in the range.
      commit_b: The last Commit in the range.

    Returns:
      A new Commit representing the midpoint.
      The commit before the midpoint if the range has an even number of commits.
      commit_a if the Commits are the same or adjacent.

    Raises:
      NonLinearError: The Commits are in different repositories or commit_a does
        not come before commit_b.
    """
        if commit_a == commit_b:
            return commit_a

        if commit_a.repository != commit_b.repository:
            raise NonLinearError(
                'Repositories differ between Commits: %s vs %s' %
                (commit_a.repository, commit_b.repository))

        # We need to get the full list of commits in between two git hashes, and
        # only look into the chain formed by following the first parents of each
        # commit. This gives us a linear view of the log even in the presence of
        # merge commits.
        commits = []

        # The commit_range by default is in reverse-chronological (latest commit
        # first) order. This means we should keep following the first parent to get
        # the linear history for a branch that we're exploring.
        expected_parent = commit_b.git_hash
        commit_range = gitiles_service.CommitRange(commit_a.repository_url,
                                                   commit_a.git_hash,
                                                   commit_b.git_hash)
        for commit in commit_range:
            # Skip commits until we find the parent we're looking for.
            if commit['commit'] == expected_parent:
                commits.append(commit)
                if 'parents' in commit and len(commit['parents']):
                    expected_parent = commit['parents'][0]

        # We don't handle NotFoundErrors because we assume that all Commits either
        # came from this method or were already validated elsewhere.
        if len(commits) == 0:
            raise NonLinearError(
                'Commit "%s" does not come before commit "%s".' %
                (commit_a, commit_b))

        if len(commits) == 1:
            return commit_a

        commits.pop(0)  # Remove commit_b from the range.
        return cls(commit_a.repository, commits[len(commits) / 2]['commit'])
Пример #7
0
 def testCommitRange(self):
     return_value = {
         'log': [
             {
                 'commit': 'commit_2_hash',
                 'tree': 'tree_2_hash',
                 'parents': ['parent_2_hash'],
                 'author': {
                     'name': 'username',
                     'email': '*****@*****.**',
                     'time': 'Sat Jan 02 00:00:00 2016',
                 },
                 'committer': {
                     'name': 'Commit bot',
                     'email': '*****@*****.**',
                     'time': 'Sat Jan 02 00:01:00 2016',
                 },
                 'message': 'Subject.\n\nCommit message.',
             },
             {
                 'commit': 'commit_1_hash',
                 'tree': 'tree_1_hash',
                 'parents': ['parent_1_hash'],
                 'author': {
                     'name': 'username',
                     'email': '*****@*****.**',
                     'time': 'Fri Jan 01 00:00:00 2016',
                 },
                 'committer': {
                     'name': 'Commit bot',
                     'email': '*****@*****.**',
                     'time': 'Fri Jan 01 00:01:00 2016',
                 },
                 'message': 'Subject.\n\nCommit message.',
             },
         ],
     }
     self._request_json.return_value = return_value
     self.assertEqual(
         gitiles_service.CommitRange(
             'https://chromium.googlesource.com/repo', 'commit_0_hash',
             'commit_2_hash'), return_value['log'])
     self._request_json.assert_called_once_with(
         'https://chromium.googlesource.com/repo/+log/'
         'commit_0_hash..commit_2_hash?format=JSON',
         use_cache=False,
         use_auth=True,
         scope=gerrit_service.GERRIT_SCOPE)
Пример #8
0
 def testCommitRange(self, mock_fetch):
     return_value = {
         'log': [
             {
                 'commit': 'commit_2_hash',
                 'tree': 'tree_2_hash',
                 'parents': ['parent_2_hash'],
                 'author': {
                     'name': 'username',
                     'email': '*****@*****.**',
                     'time': 'Sat Jan 02 00:00:00 2016',
                 },
                 'committer': {
                     'name': 'Commit bot',
                     'email': '*****@*****.**',
                     'time': 'Sat Jan 02 00:01:00 2016',
                 },
                 'message': 'Subject.\n\nCommit message.',
             },
             {
                 'commit': 'commit_1_hash',
                 'tree': 'tree_1_hash',
                 'parents': ['parent_1_hash'],
                 'author': {
                     'name': 'username',
                     'email': '*****@*****.**',
                     'time': 'Fri Jan 01 00:00:00 2016',
                 },
                 'committer': {
                     'name': 'Commit bot',
                     'email': '*****@*****.**',
                     'time': 'Fri Jan 01 00:01:00 2016',
                 },
                 'message': 'Subject.\n\nCommit message.',
             },
         ],
     }
     _SetFetchReturnValues(mock_fetch, return_value)
     self.assertEqual(
         gitiles_service.CommitRange(
             'https://chromium.googlesource.com/repo', 'commit_0_hash',
             'commit_2_hash'), return_value['log'])
     mock_fetch.assert_called_once_with(
         'https://chromium.googlesource.com/repo/+log/'
         'commit_0_hash..commit_2_hash?format=JSON',
         deadline=60)
Пример #9
0
 def testCommitRange(self, mock_fetch):
   return_value = {
       'log': [
           {
               'commit': 'commit_2_hash',
               'tree': 'tree_2_hash',
               'parents': ['parent_2_hash'],
               'author': {
                   'name': 'username',
                   'email': '*****@*****.**',
                   'time': 'Sat Jan 02 00:00:00 2016',
               },
               'committer': {
                   'name': 'Commit bot',
                   'email': '*****@*****.**',
                   'time': 'Sat Jan 02 00:01:00 2016',
               },
               'message': 'Subject.\n\nCommit message.',
           },
           {
               'commit': 'commit_1_hash',
               'tree': 'tree_1_hash',
               'parents': ['parent_1_hash'],
               'author': {
                   'name': 'username',
                   'email': '*****@*****.**',
                   'time': 'Fri Jan 01 00:00:00 2016',
               },
               'committer': {
                   'name': 'Commit bot',
                   'email': '*****@*****.**',
                   'time': 'Fri Jan 01 00:01:00 2016',
               },
               'message': 'Subject.\n\nCommit message.',
           },
       ],
   }
   _SetFetchReturnValues(mock_fetch, return_value)
   self.assertEqual(
       gitiles_service.CommitRange('repo', 'commit_0_hash', 'commit_2_hash'),
       return_value['log'])
Пример #10
0
    def Midpoint(cls, commit_a, commit_b):
        """Return a Commit halfway between the two given Commits.

    Uses Gitiles to look up the commit range.

    Args:
      commit_a: The first Commit in the range.
      commit_b: The last Commit in the range.

    Returns:
      A new Commit representing the midpoint.
      The commit before the midpoint if the range has an even number of commits.
      commit_a if the Commits are the same or adjacent.

    Raises:
      NonLinearError: The Commits are in different repositories or commit_a does
        not come before commit_b.
    """
        if commit_a == commit_b:
            return commit_a

        if commit_a.repository != commit_b.repository:
            raise NonLinearError(
                'Repositories differ between Commits: %s vs %s' %
                (commit_a.repository, commit_b.repository))

        commits = gitiles_service.CommitRange(commit_a.repository_url,
                                              commit_a.git_hash,
                                              commit_b.git_hash)
        # We don't handle NotFoundErrors because we assume that all Commits either
        # came from this method or were already validated elsewhere.
        if len(commits) == 0:
            raise NonLinearError(
                'Commit "%s" does not come before commit "%s".' % commit_a,
                commit_b)
        if len(commits) == 1:
            return commit_a
        commits.pop(0)  # Remove commit_b from the range.

        return cls(commit_a.repository, commits[len(commits) / 2]['commit'])
Пример #11
0
    def CommitRange(cls, commit_a, commit_b):
        # We need to get the full list of commits in between two git hashes, and
        # only look into the chain formed by following the first parents of each
        # commit. This gives us a linear view of the log even in the presence of
        # merge commits.
        commits = []

        # The commit_range by default is in reverse-chronological (latest commit
        # first) order. This means we should keep following the first parent to get
        # the linear history for a branch that we're exploring.
        expected_parent = commit_b.git_hash
        commit_range = gitiles_service.CommitRange(commit_a.repository_url,
                                                   commit_a.git_hash,
                                                   commit_b.git_hash)
        for commit in commit_range:
            # Skip commits until we find the parent we're looking for.
            if commit['commit'] == expected_parent:
                commits.append(commit)
                if 'parents' in commit and len(commit['parents']):
                    expected_parent = commit['parents'][0]

        return commits
Пример #12
0
  def post(self):
    repo = self.request.get('repository')
    git_hash_1 = self.request.get('git_hash', self.request.get('git_hash_1'))
    git_hash_2 = self.request.get('git_hash_2')

    try:
      repository_url = repository.RepositoryUrl(repo)
    except KeyError:
      self.response.out.write(json.dumps(
          {'error': 'Unknown repository: %s' % repo}))
      return

    if not git_hash_1:
      self.response.out.write(json.dumps(
          {'error': "No 'git_hash' parameter specified."}))
      return

    if git_hash_2:
      result = gitiles_service.CommitRange(repo, git_hash_1, git_hash_2)
      self.response.out.write(json.dumps(result))
      return

    result = gitiles_service.CommitInfo(repository_url, git_hash_1)
    self.response.out.write(json.dumps(result))