45229abc6eedf7197aed8ac0540a32a007111b90
[WebKit.git] / Websites / perf.webkit.org / public / v3 / models / commit-log.js
1 'use strict';
2
3 class CommitLog extends DataModelObject {
4     constructor(id, rawData)
5     {
6         super(id);
7         this._repository = rawData.repository;
8         console.assert(this._repository instanceof Repository);
9         this._rawData = rawData;
10         this._remoteId = rawData.id;
11         if (this._remoteId)
12             this.ensureNamedStaticMap('remoteId')[this._remoteId] = this;
13     }
14
15     updateSingleton(rawData)
16     {
17         super.updateSingleton(rawData);
18
19         console.assert(+this._rawData['time'] == +rawData['time']);
20         console.assert(this._rawData['revision'] == rawData['revision']);
21
22         if (rawData.authorName)
23             this._rawData.authorName = rawData.authorName;
24         if (rawData.message)
25             this._rawData.message = rawData.message;
26     }
27
28     repository() { return this._repository; }
29     time() { return new Date(this._rawData['time']); }
30     author() { return this._rawData['authorName']; }
31     revision() { return this._rawData['revision']; }
32     message() { return this._rawData['message']; }
33     url() { return this._repository.urlForRevision(this._rawData['revision']); }
34
35     label()
36     {
37         var revision = this.revision();
38         if (parseInt(revision) == revision) // e.g. r12345
39             return 'r' + revision;
40         else if (revision.length == 40) // e.g. git hash
41             return revision.substring(0, 8);
42         return revision;
43     }
44     title() { return this._repository.name() + ' at ' + this.label(); }
45
46     diff(previousCommit)
47     {
48         if (this == previousCommit)
49             previousCommit = null;
50
51         var repository = this._repository;
52         if (!previousCommit)
53             return {from: null, to: this.revision(), repository: repository, label: this.label(), url: this.url()};
54
55         var to = this.revision();
56         var from = previousCommit.revision();
57         var label = null;
58         if (parseInt(to) == to) { // e.g. r12345.
59             from = parseInt(from) + 1;
60             label = `r${from}-r${this.revision()}`;
61         } else if (to.length == 40) { // e.g. git hash
62             label = `${from}..${to}`;
63         } else
64             label = `${from} - ${to}`;
65
66         return {from: from, to: to, repository: repository, label: label, url: repository.urlForRevisionRange(from, to)};
67     }
68
69     static fetchBetweenRevisions(repository, from, to)
70     {
71         var params = [];
72         if (from && to) {
73             params.push(['from', from]);
74             params.push(['to', to]);
75         }
76
77         var url = '../api/commits/' + repository.id() + '/?' + params.map(function (keyValue) {
78             return encodeURIComponent(keyValue[0]) + '=' + encodeURIComponent(keyValue[1]);
79         }).join('&');
80
81
82         var cachedLogs = this._cachedCommitLogs(repository, from, to);
83         if (cachedLogs)
84             return new Promise(function (resolve) { resolve(cachedLogs); });
85
86         var self = this;
87         return RemoteAPI.getJSONWithStatus(url).then(function (data) {
88             var commits = data['commits'].map(function (rawData) {
89                 rawData.repository = repository;
90                 return CommitLog.ensureSingleton(rawData.id, rawData);
91             });
92             self._cacheCommitLogs(repository, from, to, commits);
93             return commits;
94         });
95     }
96
97     static _cachedCommitLogs(repository, from, to)
98     {
99         if (!this._caches)
100             return null;
101         var cache = this._caches[repository.id()];
102         if (!cache)
103             return null;
104         // FIXME: Make each commit know of its parent, then we can do a better caching. 
105         return cache[from + '|' + to];
106     }
107
108     static _cacheCommitLogs(repository, from, to, logs)
109     {
110         if (!this._caches)
111             this._caches = {};
112         if (!this._caches[repository.id()])
113             this._caches[repository.id()] = {};
114         this._caches[repository.id()][from + '|' + to] = logs;
115     }
116 }
117
118 if (typeof module != 'undefined')
119     module.exports.CommitLog = CommitLog;