forked from pieter/gitx
/
history.js
312 lines (258 loc) · 9.22 KB
/
history.js
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
var commit;
// Create a new Commit object
// obj: PBGitCommit object
var Commit = function(obj) {
this.object = obj;
this.refs = obj.refs();
this.author_name = obj.author;
this.sha = obj.realSha();
this.parents = obj.parents;
this.subject = obj.subject;
// TODO:
// this.author_date instant
// This can be called later with the output of
// 'git show' to fill in missing commit details (such as a diff)
this.parseDetails = function(details) {
this.raw = details;
var diffStart = this.raw.indexOf("\ndiff ");
var messageStart = this.raw.indexOf("\n\n") + 2;
if (diffStart > 0) {
this.message = this.raw.substring(messageStart, diffStart).replace(/^ /gm, "").escapeHTML();
this.diff = this.raw.substring(diffStart);
} else {
this.message = this.raw.substring(messageStart).replace(/^ /gm, "").escapeHTML();
this.diff = "";
}
this.header = this.raw.substring(0, messageStart);
var match = this.header.match(/\nauthor (.*) <(.*@.*|.*)> ([0-9].*)/);
if (!(match[2].match(/@[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}/)))
this.author_email = match[2];
this.author_date = new Date(parseInt(match[3]) * 1000);
match = this.header.match(/\ncommitter (.*) <(.*@.*|.*)> ([0-9].*)/);
this.committer_name = match[1];
this.committer_email = match[2];
this.committer_date = new Date(parseInt(match[3]) * 1000);
}
this.reloadRefs = function() {
this.refs = this.object.refs();
}
};
var confirm_gist = function(confirmation_message) {
if (!Controller.isFeatureEnabled_("confirmGist")) {
gistie();
return;
}
// Set optional confirmation_message
confirmation_message = confirmation_message || "Yes. Paste this commit.";
var deleteMessage = Controller.getConfig_("github.token") ? " " : "You might not be able to delete it after posting.<br>";
var publicMessage = Controller.isFeatureEnabled_("publicGist") ? "<b>public</b>" : "private";
// Insert the verification links into div#notification_message
var notification_text = 'This will create a ' + publicMessage + ' paste of your commit to <a href="http://gist.github.com/">http://gist.github.com/</a><br>' +
deleteMessage +
'Are you sure you want to continue?<br/><br/>' +
'<a href="#" onClick="hideNotification();return false;" style="color: red;">No. Cancel.</a> | ' +
'<a href="#" onClick="gistie();return false;" style="color: green;">' + confirmation_message + '</a>';
notify(notification_text, 0);
// Hide img#spinner, since it?s visible by default
$("spinner").style.display = "none";
}
var gistie = function() {
notify("Uploading code to Gistie..", 0);
parameters = {
"file_ext[gistfile1]": "patch",
"file_name[gistfile1]": commit.object.subject.replace(/[^a-zA-Z0-9]/g, "-") + ".patch",
"file_contents[gistfile1]": commit.object.patch(),
};
// TODO: Replace true with private preference
token = Controller.getConfig_("github.token");
login = Controller.getConfig_("github.user");
if (token && login) {
parameters.login = login;
parameters.token = token;
}
if (!Controller.isFeatureEnabled_("publicGist"))
parameters.private = true;
var params = [];
for (var name in parameters)
params.push(encodeURIComponent(name) + "=" + encodeURIComponent(parameters[name]));
params = params.join("&");
var t = new XMLHttpRequest();
t.onreadystatechange = function() {
if (t.readyState == 4 && t.status >= 200 && t.status < 300) {
if (m = t.responseText.match(/gist: ([a-f0-9]+)/))
notify("Code uploaded to gistie <a target='_new' href='http://gist.github.com/" + m[1] + "'>#" + m[1] + "</a>", 1);
else {
notify("Pasting to Gistie failed :(.", -1);
Controller.log_(t.responseText);
}
}
}
t.open('POST', "http://gist.github.com/gists");
t.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
t.setRequestHeader('Accept', 'text/javascript, text/html, application/xml, text/xml, */*');
t.setRequestHeader('Content-type', 'application/x-www-form-urlencoded;charset=UTF-8');
try {
t.send(params);
} catch(e) {
notify("Pasting to Gistie failed: " + e, -1);
}
}
var setGravatar = function(email, image) {
if (Controller && !Controller.isReachable_("www.gravatar.com"))
return;
if(Controller && !Controller.isFeatureEnabled_("gravatar")) {
image.src = "";
return;
}
if (!email) {
image.src = "http://www.gravatar.com/avatar/?d=wavatar&s=60";
return;
}
image.src = "http://www.gravatar.com/avatar/" +
hex_md5(commit.author_email.toLowerCase().replace(/ /g, "")) + "?d=wavatar&s=60";
}
var selectCommit = function(a) {
Controller.selectCommit_(a);
}
// Relead only refs
var reload = function() {
$("notification").style.display = "none";
commit.reloadRefs();
showRefs();
}
var showRefs = function() {
var refs = $("refs");
if (commit.refs) {
refs.parentNode.style.display = "";
refs.innerHTML = "";
for (var i = 0; i < commit.refs.length; i++) {
var ref = commit.refs[i];
refs.innerHTML += '<span class="refs ' + ref.type() + (commit.currentRef == ref.ref ? ' currentBranch' : '') + '">' + ref.shortName() + '</span>';
}
} else
refs.parentNode.style.display = "none";
}
var loadCommit = function(commitObject, currentRef) {
// These are only the things we can do instantly.
// Other information will be loaded later by loadCommitDetails,
// Which will be called from the controller once
// the commit details are in.
commit = new Commit(commitObject);
commit.currentRef = currentRef;
notify("Loading commit…", 0);
$("commitID").innerHTML = commit.sha;
$("authorID").innerHTML = commit.author_name;
$("subjectID").innerHTML = commit.subject.escapeHTML();
$("diff").innerHTML = ""
$("message").innerHTML = ""
$("files").innerHTML = ""
$("date").innerHTML = ""
showRefs();
for (var i = 0; i < $("commit_header").rows.length; ++i) {
var row = $("commit_header").rows[i];
if (row.innerHTML.match(/Parent:/)) {
row.parentNode.removeChild(row);
--i;
}
}
// Scroll to top
scroll(0, 0);
if (!commit.parents)
return;
for (var i = 0; i < commit.parents.length; i++) {
var newRow = $("commit_header").insertRow(-1);
newRow.innerHTML = "<td class='property_name'>Parent:</td><td>" +
"<a href='' onclick='selectCommit(this.innerHTML); return false;'>" +
commit.parents[i] + "</a></td>";
}
}
var showDiff = function() {
// Callback for the diff highlighter. Used to generate a filelist
var newfile = function(name1, name2, id, mode_change, old_mode, new_mode) {
var button = document.createElement("div");
var p = document.createElement("p");
var link = document.createElement("a");
link.setAttribute("href", "#" + id);
p.appendChild(link);
var buttonType = "";
var finalFile = "";
if (name1 == name2) {
buttonType = "changed"
finalFile = name1;
if (mode_change)
p.appendChild(document.createTextNode(" mode " + old_mode + " -> " + new_mode));
}
else if (name1 == "/dev/null") {
buttonType = "created";
finalFile = name2;
}
else if (name2 == "/dev/null") {
buttonType = "deleted";
finalFile = name1;
}
else {
buttonType = "renamed";
finalFile = name2;
p.insertBefore(document.createTextNode(name1 + " -> "), link);
}
link.appendChild(document.createTextNode(finalFile));
button.setAttribute("representedFile", finalFile);
link.setAttribute("representedFile", finalFile);
button.setAttribute("class", "button " + buttonType);
button.appendChild(document.createTextNode(buttonType));
$("files").appendChild(button);
$("files").appendChild(p);
}
var binaryDiff = function(filename) {
if (filename.match(/\.(png|jpg|icns|psd)$/i))
return '<a href="#" onclick="return showImage(this, \'' + filename + '\')">Display image</a>';
else
return "Binary file differs";
}
highlightDiff(commit.diff, $("diff"), { "newfile" : newfile, "binaryFile" : binaryDiff });
}
var showImage = function(element, filename)
{
element.outerHTML = '<img src="GitX://' + commit.sha + '/' + filename + '">';
return false;
}
var enableFeature = function(feature, element)
{
if(!Controller || Controller.isFeatureEnabled_(feature)) {
element.style.display = "";
} else {
element.style.display = "none";
}
}
var enableFeatures = function()
{
enableFeature("gist", $("gist"))
if(commit)
setGravatar(commit.author_email, $("gravatar"));
enableFeature("gravatar", $("gravatar"))
}
var loadCommitDetails = function(data)
{
commit.parseDetails(data);
var formatEmail = function(name, email) {
return email ? name + " <<a href='mailto:" + email + "'>" + email + "</a>>" : name;
}
$("authorID").innerHTML = formatEmail(commit.author_name, commit.author_email);
if (commit.committer_name != commit.author_name) {
$("committerID").parentNode.style.display = "";
$("committerID").innerHTML = formatEmail(commit.committer_name, commit.committer_email);
$("committerDate").parentNode.style.display = "";
$("committerDate").innerHTML = commit.committer_date;
} else {
$("committerID").parentNode.style.display = "none";
$("committerDate").parentNode.style.display = "none";
}
$("date").innerHTML = commit.author_date;
$("message").innerHTML = commit.message.replace(/\n/g,"<br>");
if (commit.diff.length < 200000)
showDiff();
else
$("diff").innerHTML = "<a class='showdiff' href='' onclick='showDiff(); return false;'>This is a large commit. Click here or press 'v' to view.</a>";
hideNotification();
enableFeatures();
}