forked from atom/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit.js
More file actions
195 lines (156 loc) · 4.89 KB
/
commit.js
File metadata and controls
195 lines (156 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import {buildMultiFilePatch} from './patch';
const UNBORN = Symbol('unborn');
// Truncation elipsis styles
const WORD_ELIPSES = '...';
const NEWLINE_ELIPSES = '\n...';
const PARAGRAPH_ELIPSES = '\n\n...';
export default class Commit {
static LONG_MESSAGE_THRESHOLD = 400;
static NEWLINE_THRESHOLD = 5;
static createUnborn() {
return new Commit({unbornRef: UNBORN});
}
constructor({sha, authorEmail, coAuthors, authorDate, messageSubject, messageBody, unbornRef, patch}) {
this.sha = sha;
this.authorEmail = authorEmail;
this.coAuthors = coAuthors || [];
this.authorDate = authorDate;
this.messageSubject = messageSubject;
this.messageBody = messageBody;
this.unbornRef = unbornRef === UNBORN;
this.multiFileDiff = patch ? buildMultiFilePatch(patch) : buildMultiFilePatch([]);
}
getSha() {
return this.sha;
}
getAuthorEmail() {
return this.authorEmail;
}
getAuthorDate() {
return this.authorDate;
}
getCoAuthors() {
return this.coAuthors;
}
getMessageSubject() {
return this.messageSubject;
}
getMessageBody() {
return this.messageBody;
}
isBodyLong() {
if (this.getMessageBody().length > this.constructor.LONG_MESSAGE_THRESHOLD) {
return true;
}
if ((this.getMessageBody().match(/\r?\n/g) || []).length > this.constructor.NEWLINE_THRESHOLD) {
return true;
}
return false;
}
getFullMessage() {
return `${this.getMessageSubject()}\n\n${this.getMessageBody()}`.trim();
}
/*
* Return the messageBody truncated to at most LONG_MESSAGE_THRESHOLD characters or NEWLINE_THRESHOLD newlines,
* whichever comes first.
*
* If NEWLINE_THRESHOLD newlines are encountered before LONG_MESSAGE_THRESHOLD characters, the body will be truncated
* at the last counted newline and elipses added.
*
* If a paragraph boundary is found before LONG_MESSAGE_THRESHOLD characters, the message will be truncated at the end
* of the previous paragraph and an elipses added. If no paragraph boundary is found, but a word boundary is, the text
* is truncated at the last word boundary and an elipsis added. If neither are found, the text is truncated hard at
* LONG_MESSAGE_THRESHOLD - 3 characters and an elipsis is added.
*/
abbreviatedBody() {
if (!this.isBodyLong()) {
return this.getMessageBody();
}
const {LONG_MESSAGE_THRESHOLD, NEWLINE_THRESHOLD} = this.constructor;
let lastNewlineCutoff = null;
let lastParagraphCutoff = null;
let lastWordCutoff = null;
const searchText = this.getMessageBody().substring(0, LONG_MESSAGE_THRESHOLD);
const boundaryRx = /\s+/g;
let result;
let lineCount = 0;
while ((result = boundaryRx.exec(searchText)) !== null) {
const newlineCount = (result[0].match(/\r?\n/g) || []).length;
lineCount += newlineCount;
if (lineCount > NEWLINE_THRESHOLD) {
lastNewlineCutoff = result.index;
break;
}
if (newlineCount < 2 && result.index <= LONG_MESSAGE_THRESHOLD - WORD_ELIPSES.length) {
lastWordCutoff = result.index;
} else if (result.index < LONG_MESSAGE_THRESHOLD - PARAGRAPH_ELIPSES.length) {
lastParagraphCutoff = result.index;
}
}
let elipses = WORD_ELIPSES;
let cutoffIndex = LONG_MESSAGE_THRESHOLD - WORD_ELIPSES.length;
if (lastNewlineCutoff !== null) {
elipses = NEWLINE_ELIPSES;
cutoffIndex = lastNewlineCutoff;
} else if (lastParagraphCutoff !== null) {
elipses = PARAGRAPH_ELIPSES;
cutoffIndex = lastParagraphCutoff;
} else if (lastWordCutoff !== null) {
cutoffIndex = lastWordCutoff;
}
return this.getMessageBody().substring(0, cutoffIndex) + elipses;
}
setMultiFileDiff(multiFileDiff) {
this.multiFileDiff = multiFileDiff;
}
getMultiFileDiff() {
return this.multiFileDiff;
}
isUnbornRef() {
return this.unbornRef;
}
isPresent() {
return true;
}
isEqual(other) {
// Directly comparable properties
for (const property of ['sha', 'authorEmail', 'authorDate', 'messageSubject', 'messageBody', 'unbornRef']) {
if (this[property] !== other[property]) {
return false;
}
}
// Co-author array
if (this.coAuthors.length !== other.coAuthors.length) {
return false;
}
for (let i = 0; i < this.coAuthors.length; i++) {
const thisCoAuthor = this.coAuthors[i];
const otherCoAuthor = other.coAuthors[i];
if (thisCoAuthor.name !== otherCoAuthor.name || thisCoAuthor.email !== otherCoAuthor.email) {
return false;
}
}
// Multi-file patch
if (!this.multiFileDiff.isEqual(other.multiFileDiff)) {
return false;
}
return true;
}
}
export const nullCommit = {
getSha() {
return '';
},
getMessageSubject() {
return '';
},
isUnbornRef() {
return false;
},
isPresent() {
return false;
},
isBodyLong() {
return false;
},
};