示例#1
0
 def _getLog(self, start, limit=None, stop=None, union_repository=None,
             enable_hosting=None, enable_memcache=None, logger=None):
     if enable_hosting is None:
         enable_hosting = not getFeatureFlag(
             u"code.git.log.disable_hosting")
     if enable_memcache is None:
         enable_memcache = not getFeatureFlag(
             u"code.git.log.disable_memcache")
     path = self.repository.getInternalPath()
     if (union_repository is not None and
             union_repository != self.repository):
         path = "%s:%s" % (union_repository.getInternalPath(), path)
     log = None
     if enable_memcache:
         memcache_client = getUtility(IMemcacheClient)
         instance_name = urlsplit(
             config.codehosting.internal_git_api_endpoint).hostname
         memcache_key = "%s:git-log:%s:%s" % (instance_name, path, start)
         if limit is not None:
             memcache_key += ":limit=%s" % limit
         if stop is not None:
             memcache_key += ":stop=%s" % stop
         memcache_key = six.ensure_binary(memcache_key)
         cached_log = memcache_client.get(memcache_key)
         if cached_log is not None:
             try:
                 log = json.loads(cached_log)
             except Exception:
                 if logger is not None:
                     logger.exception(
                         "Cannot load cached log information for %s:%s; "
                         "deleting" % (path, start))
                 memcache_client.delete(memcache_key)
     if log is None:
         if enable_hosting:
             hosting_client = getUtility(IGitHostingClient)
             log = removeSecurityProxy(hosting_client.getLog(
                 path, start, limit=limit, stop=stop, logger=logger))
             if enable_memcache:
                 memcache_client.set(memcache_key, json.dumps(log))
         else:
             # Fall back to synthesising something reasonable based on
             # information in our own database.
             log = [{
                 "sha1": self.commit_sha1,
                 "message": self.commit_message,
                 "author": None if self.author is None else {
                     "name": self.author.name_without_email,
                     "email": self.author.email,
                     "time": seconds_since_epoch(self.author_date),
                     },
                 "committer": None if self.committer is None else {
                     "name": self.committer.name_without_email,
                     "email": self.committer.email,
                     "time": seconds_since_epoch(self.committer_date),
                     },
                 }]
     return log
示例#2
0
 def setUp(self):
     super(TestGitRefGetCommits, self).setUp()
     [self.ref] = self.factory.makeGitRefs()
     self.authors = [self.factory.makePerson() for _ in range(2)]
     with admin_logged_in():
         self.author_emails = [
             author.preferredemail.email for author in self.authors
         ]
     self.dates = [
         datetime(2015, 1, 1, 0, 0, 0, tzinfo=pytz.UTC),
         datetime(2015, 1, 2, 0, 0, 0, tzinfo=pytz.UTC),
     ]
     self.sha1_tip = unicode(hashlib.sha1("tip").hexdigest())
     self.sha1_root = unicode(hashlib.sha1("root").hexdigest())
     self.log = [
         {
             "sha1": self.sha1_tip,
             "message": "tip",
             "author": {
                 "name": self.authors[0].display_name,
                 "email": self.author_emails[0],
                 "time": int(seconds_since_epoch(self.dates[1])),
             },
             "committer": {
                 "name": self.authors[1].display_name,
                 "email": self.author_emails[1],
                 "time": int(seconds_since_epoch(self.dates[1])),
             },
             "parents": [self.sha1_root],
             "tree": unicode(hashlib.sha1("").hexdigest()),
         },
         {
             "sha1": self.sha1_root,
             "message": "root",
             "author": {
                 "name": self.authors[1].display_name,
                 "email": self.author_emails[1],
                 "time": int(seconds_since_epoch(self.dates[0])),
             },
             "committer": {
                 "name": self.authors[0].display_name,
                 "email": self.author_emails[0],
                 "time": int(seconds_since_epoch(self.dates[0])),
             },
             "parents": [],
             "tree": unicode(hashlib.sha1("").hexdigest()),
         },
     ]
     self.hosting_fixture = self.useFixture(GitHostingFixture(log=self.log))
示例#3
0
 def makeFakeCommits(author, author_date_gen, paths):
     dates = {path: next(author_date_gen) for path in paths}
     return [{
         "sha1": unicode(hashlib.sha1(path).hexdigest()),
         "message": "tip of %s" % path,
         "author": {
             "name": author.displayname,
             "email": author.preferredemail.email,
             "time": int(seconds_since_epoch(dates[path])),
             },
         "committer": {
             "name": author.displayname,
             "email": author.preferredemail.email,
             "time": int(seconds_since_epoch(dates[path])),
             },
         "parents": [],
         "tree": unicode(hashlib.sha1("").hexdigest()),
         } for path in paths]
示例#4
0
 def _sort_key(item):
     for date_attr in date_attrs:
         if getattr(item, date_attr, None) is not None:
             return -seconds_since_epoch(getattr(item, date_attr))
     return 0
示例#5
0
 def test_start_of_2018(self):
     dt = datetime(2018, 1, 1, tzinfo=UTC)
     self.assertEqual(1514764800, seconds_since_epoch(dt))
示例#6
0
 def test_epoch(self):
     epoch = datetime.fromtimestamp(0, tz=UTC)
     self.assertEqual(0, seconds_since_epoch(epoch))