Skip to content

Commit c654153

Browse files
committed
rewrite more
1 parent b363caa commit c654153

12 files changed

+332
-429
lines changed

requirejs-config.json

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"bluebird": "./thirdparty/bluebird",
44
"eventemitter2": "./thirdparty/eventemitter2",
55
"marked": "./thirdparty/marked",
6+
"md5": "./thirdparty/md5",
67
"moment": "./thirdparty/moment",
78
"URI": "./thirdparty/URI"
89
},

src/Git/GitCli.js

+11-18
Original file line numberDiff line numberDiff line change
@@ -317,24 +317,14 @@ define(function (require, exports) {
317317
var data = line.split(separator),
318318
commit = {};
319319

320-
commit.hashShort = data[0];
321-
commit.hash = data[1];
322-
commit.author = data[2];
323-
commit.date = data[3];
324-
commit.email = data[4];
325-
// TODO: md5 doesn't belong here
326-
// commit.emailHash = md5(data[4]);
327-
// TODO: shortening doesn't belong here
328-
// commit.subject = data[5].substring(0, 49) + ((data[5].length > 50) ? "…" : "");
329-
commit.subject = data[5];
330-
// TODO: marked doesn't belong here
331-
// commit.body = marked(data[6], {gfm: true, breaks: true});
332-
commit.body = data[6];
333-
// TODO: do this elsewhere
334-
// commit.avatarColor = commit.emailHash.substring(0, 6);
335-
// commit.avatarLetter = commit.author.substring(0, 1);
336-
// TODO: why would we stringify this ???
337-
// commit.commit = JSON.stringify(commit);
320+
commit.hashShort = data[0];
321+
commit.hash = data[1];
322+
commit.author = data[2];
323+
commit.date = data[3];
324+
commit.email = data[4];
325+
commit.subject = data[5];
326+
commit.body = data[6];
327+
338328
return commit;
339329

340330
});
@@ -493,6 +483,9 @@ define(function (require, exports) {
493483

494484
if (needReset.length > 0) {
495485
return Promise.all(needReset.map(function (fileName) {
486+
if (fileName.indexOf("->") !== -1) {
487+
fileName = fileName.split("->")[1].trim();
488+
}
496489
return unstage(fileName);
497490
})).then(function () {
498491
if (type === "RECURSIVE_CALL") {

src/History.js

+54-142
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,22 @@ define(function (require) {
1515
HistoryViewer = require("src/HistoryViewer"),
1616
Preferences = require("src/Preferences");
1717

18+
if (Preferences.get("useGravatar")) {
19+
var md5;
20+
require(["md5"], function (_md5) {
21+
md5 = _md5;
22+
});
23+
}
24+
1825
// Templates
1926
var gitPanelHistoryTemplate = require("text!templates/git-panel-history.html"),
20-
panelHistoryTemplate = require("text!templates/git-panel-history-commits.html");
27+
gitPanelHistoryCommitsTemplate = require("text!templates/git-panel-history-commits.html");
2128

2229
// Module variables
2330
var $gitPanel = $(null),
2431
$tableContainer = $(null),
25-
$historyList = $(null);
32+
$historyList = $(null),
33+
commitCache = [];
2634

2735
// Implementation
2836

@@ -39,26 +47,34 @@ define(function (require) {
3947
loadMoreHistory();
4048
})
4149
.on("click.history", ".history-commit", function () {
42-
HistoryViewer.show($(this).data("history-commit"));
50+
var hash = $(this).attr("x-hash");
51+
var commit = _.find(commitCache, function (commit) { return commit.hash === hash; });
52+
HistoryViewer.show(commit);
4353
});
4454
}
4555

4656
// Render history list the first time
4757
function renderHistory(file) {
58+
// clear cache
59+
commitCache = [];
60+
4861
return Git.getCurrentBranchName().then(function (branchName) {
4962
// Get the history commits of the current branch
5063
var p = file ? Git.getFileHistory(file.relative, branchName) : Git.getHistory(branchName);
5164
return p.then(function (commits) {
52-
commits = convertCommitDates(commits);
5365

54-
var template = "<table class='git-history-list bottom-panel-table table table-striped table-condensed row-highlight'>";
55-
template += "<tbody>";
56-
template += gitPanelHistoryTemplate;
57-
template += "</tbody>";
58-
template += "</table>";
66+
// calculate some missing stuff like gravatars
67+
commits = addAdditionalCommitInfo(commits);
68+
commitCache = commitCache.concat(commits);
5969

60-
$tableContainer.append(Mustache.render(template, {
61-
commits: commits
70+
var templateData = {
71+
commits: commits,
72+
useGravatar: Preferences.get("useGravatar"),
73+
Strings: Strings
74+
};
75+
76+
$tableContainer.append(Mustache.render(gitPanelHistoryTemplate, templateData, {
77+
commits: gitPanelHistoryCommitsTemplate
6278
}));
6379

6480
$historyList = $tableContainer.find(".git-history-list")
@@ -91,9 +107,17 @@ define(function (require) {
91107
$historyList.attr("x-finished", "true");
92108
return;
93109
}
94-
commits = convertCommitDates(commits);
95-
$tableContainer.find(".git-history-list > tbody")
96-
.append(Mustache.render(gitPanelHistoryTemplate, {commits: commits}));
110+
111+
commits = addAdditionalCommitInfo(commits);
112+
commitCache = commitCache.concat(commits);
113+
114+
var templateData = {
115+
commits: commits,
116+
useGravatar: Preferences.get("useGravatar"),
117+
Strings: Strings
118+
};
119+
var commitsHtml = Mustache.render(gitPanelHistoryCommitsTemplate, templateData);
120+
$historyList.children("tbody").append(commitsHtml);
97121
})
98122
.catch(function (err) {
99123
ErrorHandler.showError(err, "Failed to load more history rows");
@@ -106,14 +130,25 @@ define(function (require) {
106130
}
107131
}
108132

109-
function convertCommitDates(commits) {
133+
function addAdditionalCommitInfo(commits) {
110134
var mode = Preferences.get("dateMode"),
111135
format = Strings.DATE_FORMAT,
112136
now = moment(),
113137
yesterday = moment().subtract("d", 1).startOf("d"),
114138
ownFormat = Preferences.get("dateFormat") || Strings.DATE_FORMAT;
115139

116140
_.forEach(commits, function (commit) {
141+
142+
if (Preferences.get("useGravatar")) {
143+
// email hash for gravatars
144+
commit.emailHash = md5(commit.email);
145+
} else {
146+
commit.avatarLetter = commit.author.substring(0, 1);
147+
}
148+
149+
// shorten the commit subject
150+
commit.subject = commit.subject.substring(0, 49) + ((commit.subject.length > 50) ? "\u2026" : "");
151+
117152
if (mode === 4) {
118153
// mode 4: Original Git date
119154
commit.date = {
@@ -154,6 +189,7 @@ define(function (require) {
154189
/* mode 4 (Original Git date) is handled above */
155190
}
156191
});
192+
157193
return commits;
158194
}
159195

@@ -195,7 +231,9 @@ define(function (require) {
195231
}
196232

197233
// Render .git-history-list if is not already generated or if the viewed file for file history has changed
198-
if (historyEnabled && ($historyList.length === 0 || currentFile !== (file ? file.absolute : null))) {
234+
var isEmpty = $historyList.find("tr").length === 0,
235+
fileChanged = currentFile !== (file ? file.absolute : null);
236+
if (historyEnabled && (isEmpty || fileChanged)) {
199237
if ($historyList.length > 0) {
200238
$historyList.remove();
201239
}
@@ -237,129 +275,3 @@ define(function (require) {
237275
});
238276

239277
});
240-
241-
/*
242-
// Render history list the first time
243-
function renderHistory() {
244-
return Main.gitControl.getBranchName().then(function (branchName) {
245-
// Get the history commit of the current branch
246-
return Main.gitControl.getHistory(branchName).then(function (commits) {
247-
commits = convertCommitDates(commits);
248-
249-
var partials = {gitPanelHistoryCommits: gitPanelHistoryCommitsTemplate, gitDiffDetails: gitDiffDetailsTemplate},
250-
variables = {commits: commits, useGravatar: Preferences.get("useGravatar")};
251-
252-
$tableContainer
253-
.append(Mustache.render(gitPanelHistoryTemplate, variables, partials))
254-
.find(".history-commits-list")
255-
.off("scroll.history")
256-
.on("scroll.history", function () {
257-
loadMoreHistory();
258-
});
259-
260-
renderHistoryCommit(commits[0], $tableContainer.find(".history-commits-list .history-commit").first());
261-
262-
});
263-
}).catch(function (err) {
264-
ErrorHandler.showError(err, "Failed to get history");
265-
});
266-
}
267-
268-
// Load more rows in the history list on scroll
269-
function loadMoreHistory() {
270-
if ($tableContainer.find(".git-history-list").is(":visible")) {
271-
var $commitsList = $tableContainer.find(".history-commits-list");
272-
if (($commitsList.prop("scrollHeight") - $commitsList.scrollTop()) === $commitsList.height()) {
273-
return Main.gitControl.getBranchName().then(function (branchName) {
274-
return Main.gitControl.getHistory(branchName, $commitsList.find(".history-commit").length).then(function (commits) {
275-
if (commits.length === 0) {
276-
return;
277-
}
278-
commits = convertCommitDates(commits);
279-
var variables = {commits: commits, useGravatar: Preferences.get("useGravatar")};
280-
281-
$commitsList.append(Mustache.render(gitPanelHistoryCommitsTemplate, variables));
282-
})
283-
.catch(function (err) {
284-
ErrorHandler.showError(err, "Failed to load more history rows");
285-
});
286-
})
287-
.catch(function (err) {
288-
ErrorHandler.showError(err, "Failed to get branch name");
289-
});
290-
}
291-
}
292-
}
293-
294-
function convertCommitDates(commits) {
295-
var mode = Preferences.get("dateMode"),
296-
format = Strings.DATE_FORMAT,
297-
now = moment(),
298-
yesterday = moment().subtract("d", 1).startOf("d"),
299-
ownFormat = Preferences.get("dateFormat") || Strings.DATE_FORMAT;
300-
301-
_.forEach(commits, function (commit) {
302-
if (mode === 4) {
303-
// mode 4: Original Git date
304-
commit.date = {
305-
shown: commit.date
306-
};
307-
return;
308-
}
309-
310-
var date = moment(commit.date);
311-
commit.date = {
312-
title: ""
313-
};
314-
switch (mode) {
315-
// mode 0 (default): formatted with Strings.DATE_FORMAT
316-
default:
317-
case 0:
318-
commit.date.shown = date.format(format);
319-
break;
320-
// mode 1: always relative
321-
case 1:
322-
commit.date.shown = date.fromNow();
323-
commit.date.title = date.format(format);
324-
break;
325-
// mode 2: intelligent relative/formatted
326-
case 2:
327-
if (date.diff(yesterday) > 0) {
328-
commit.date.shown = moment.duration(Math.max(date.diff(now), -24 * 60 * 60 * 1000), "ms").humanize(true);
329-
commit.date.title = date.format(format);
330-
} else {
331-
commit.date.shown = date.format(format);
332-
}
333-
break;
334-
// mode 3: formatted with own format (as pref)
335-
case 3:
336-
commit.date.shown = date.format(ownFormat);
337-
commit.date.title = date.format(format);
338-
break;
339-
// mode 4 (Original Git date) is handled above
340-
}
341-
});
342-
return commits;
343-
}
344-
345-
// Show or hide the history list on click of .history button
346-
function handleToggleHistory() {
347-
348-
var $panel = gitPanel.$panel,
349-
historyEnabled = !$panel.find(".git-history-list").is(":visible");
350-
351-
// Render .git-history-list if is not already generated
352-
if ($tableContainer.find(".git-history-list").length === 0) { renderHistory(); }
353-
354-
// Toggle commit button and check-all checkbox
355-
$panel.find(".git-commit, .check-all").prop("disabled", historyEnabled);
356-
357-
// Toggle visibility of .git-edited-list and .git-history-list
358-
$tableContainer.find(".git-edited-list, .git-history-list").toggle();
359-
360-
// Toggle history button
361-
$panel.find(".git-history").toggleClass("active")
362-
.attr("title", historyEnabled ? Strings.TOOLTIP_HIDE_HISTORY : Strings.TOOLTIP_SHOW_HISTORY);
363-
364-
}
365-
*/

0 commit comments

Comments
 (0)