3 class CommitLog extends DataModelObject {
4 constructor(id, rawData)
6 console.assert(parseInt(id) == id);
8 this._repository = rawData.repository;
9 console.assert(this._repository instanceof Repository);
10 this._rawData = rawData;
11 this._remoteId = rawData.id;
13 this.ensureNamedStaticMap('remoteId')[this._remoteId] = this;
16 updateSingleton(rawData)
18 super.updateSingleton(rawData);
20 console.assert(+this._rawData['time'] == +rawData['time']);
21 console.assert(this._rawData['revision'] == rawData['revision']);
23 if (rawData.authorName)
24 this._rawData.authorName = rawData.authorName;
26 this._rawData.message = rawData.message;
29 repository() { return this._repository; }
30 time() { return new Date(this._rawData['time']); }
31 author() { return this._rawData['authorName']; }
32 revision() { return this._rawData['revision']; }
33 message() { return this._rawData['message']; }
34 url() { return this._repository.urlForRevision(this._rawData['revision']); }
38 var revision = this.revision();
39 if (parseInt(revision) == revision) // e.g. r12345
40 return 'r' + revision;
41 else if (revision.length == 40) // e.g. git hash
42 return revision.substring(0, 8);
45 title() { return this._repository.name() + ' at ' + this.label(); }
49 if (this == previousCommit)
50 previousCommit = null;
52 var repository = this._repository;
54 return {from: null, to: this.revision(), repository: repository, label: this.label(), url: this.url()};
56 var to = this.revision();
57 var from = previousCommit.revision();
59 if (parseInt(to) == to) { // e.g. r12345.
60 from = parseInt(from) + 1;
61 label = `r${from}-r${this.revision()}`;
62 } else if (to.length == 40) { // e.g. git hash
63 label = `${from}..${to}`;
65 label = `${from} - ${to}`;
67 return {from: from, to: to, repository: repository, label: label, url: repository.urlForRevisionRange(from, to)};
70 static fetchBetweenRevisions(repository, from, to)
74 params.push(['from', from]);
75 params.push(['to', to]);
78 var url = '../api/commits/' + repository.id() + '/?' + params.map(function (keyValue) {
79 return encodeURIComponent(keyValue[0]) + '=' + encodeURIComponent(keyValue[1]);
83 var cachedLogs = this._cachedCommitLogs(repository, from, to);
85 return new Promise(function (resolve) { resolve(cachedLogs); });
88 return RemoteAPI.getJSONWithStatus(url).then(function (data) {
89 var commits = data['commits'].map(function (rawData) {
90 rawData.repository = repository;
91 return CommitLog.ensureSingleton(rawData.id, rawData);
93 self._cacheCommitLogs(repository, from, to, commits);
98 static _cachedCommitLogs(repository, from, to)
102 var cache = this._caches[repository.id()];
105 // FIXME: Make each commit know of its parent, then we can do a better caching.
106 return cache[from + '|' + to];
109 static _cacheCommitLogs(repository, from, to, logs)
113 if (!this._caches[repository.id()])
114 this._caches[repository.id()] = {};
115 this._caches[repository.id()][from + '|' + to] = logs;
119 if (typeof module != 'undefined')
120 module.exports.CommitLog = CommitLog;