Skip to content

Commit 027d8f2

Browse files
author
Michelle Tilley
committed
Merge pull request #15 from kuychaco/mkt-better-ref-matching
Better refspec support
2 parents f29b993 + d87de85 commit 027d8f2

File tree

7 files changed

+77
-30
lines changed

7 files changed

+77
-30
lines changed

css/explaingit.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ span.cmd {
134134

135135
.control-box input[type="text"] {
136136
position: absolute;
137-
bottom: 0;
137+
bottom: 0;
138138
padding-left: 15px;
139139
color: #FFF;
140140
line-height: 14px;

index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -523,12 +523,12 @@ <h2>Specific Examples</h2>
523523

524524
window.addEventListener('hashchange', open, false);
525525
window.addEventListener('load', open, false);
526-
526+
527527
function open() {
528528
var hash = window.location.hash.substr(1),
529529
linkId = 'open-' + hash,
530530
example = examples[hash];
531-
531+
532532
if (example) {
533533
explainGit.reset();
534534
document.getElementById(linkId).classList.add('selected');
@@ -540,7 +540,7 @@ <h2>Specific Examples</h2>
540540
elements[i].style.display = 'none';
541541
}
542542
document.getElementById('fork-me').style.display = 'none';
543-
543+
544544
explainGit.reset();
545545

546546
explainGit.open({

js/controlbox.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ define(['d3'], function () {
239239

240240
return;
241241
}
242-
242+
243243
while (args.length > 0) {
244244
var arg = args.shift();
245245

js/explaingit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,4 @@ define(['historyview', 'controlbox', 'd3'], function (HistoryView, ControlBox, d
7171
window.explainGit = explainGit;
7272

7373
return explainGit;
74-
});
74+
});

js/historyview.js

Lines changed: 69 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -269,18 +269,33 @@ define(['d3'], function () {
269269
* @return {Object} the commit datum object
270270
*/
271271
getCommit: function getCommit(ref) {
272+
// Optimization, doesn't seem to break anything
273+
if (!ref) return null;
274+
272275
var commitData = this.commitData,
273-
headMatcher = /HEAD(\^+)/.exec(ref),
274276
matchedCommit = null;
275277

278+
var parts = /^([^\^\~]+)(.*)$/.exec(ref),
279+
ref = parts[1],
280+
modifier = parts[2];
281+
276282
if (ref === 'initial') {
277283
return this.initialCommit;
278284
}
279285

280-
if (headMatcher) {
286+
if (ref.toLowerCase() === 'head') {
281287
ref = 'HEAD';
282288
}
283289

290+
var commitsThatStartWith = commitData
291+
.filter(function(c) { return c.id.indexOf(ref) === 0})
292+
293+
if (commitsThatStartWith.length === 1) {
294+
return commitsThatStartWith[0]
295+
} else if (commitsThatStartWith.length > 1) {
296+
throw new Error("Ref " + ref + " is ambiguous")
297+
}
298+
284299
for (var i = 0; i < commitData.length; i++) {
285300
var commit = commitData[i];
286301
if (commit === ref) {
@@ -293,14 +308,14 @@ define(['d3'], function () {
293308
break;
294309
}
295310

296-
var matchedTag = function() {
311+
var matchedTag = function() {
297312
for (var j = 0; j < commit.tags.length; j++) {
298313
var tag = commit.tags[j];
299314
if (tag === ref) {
300315
matchedCommit = commit;
301316
return true;
302317
}
303-
318+
304319
if (tag.indexOf('[') === 0 && tag.indexOf(']') === tag.length - 1) {
305320
tag = tag.substring(1, tag.length - 1);
306321
}
@@ -315,10 +330,35 @@ define(['d3'], function () {
315330
}
316331
}
317332

318-
if (headMatcher && matchedCommit) {
319-
for (var h = 0; h < headMatcher[1].length; h++) {
320-
matchedCommit = getCommit.call(this, matchedCommit.parent);
333+
if (matchedCommit && modifier) {
334+
while(modifier) {
335+
var nextToken = modifier[0]
336+
modifier = modifier.substr(1)
337+
var amountMatch = modifier.match(/^(\d+)(.*)$/),
338+
amount = 1;
339+
340+
if (amountMatch) {
341+
var amount = ~~amountMatch[1]
321342
}
343+
344+
if (nextToken === '^') {
345+
if (amount === 0) {
346+
/* do nothing, refers to this commit */
347+
} else if (amount === 1) {
348+
matchedCommit = this.getCommit(matchedCommit.parent)
349+
} else if (amount === 2) {
350+
matchedCommit = this.getCommit(matchedCommit.parent2)
351+
} else {
352+
matchedCommit = null
353+
}
354+
} else if (nextToken === '~') {
355+
for (var i = 0; i < amount; i++) {
356+
if (matchedCommit && matchedCommit.parent) {
357+
matchedCommit = this.getCommit(matchedCommit.parent)
358+
}
359+
}
360+
}
361+
}
322362
}
323363

324364
return matchedCommit;
@@ -360,7 +400,7 @@ define(['d3'], function () {
360400
svgContainer = container.append('div')
361401
.classed('svg-container', true)
362402
.classed('remote-container', this.isRemote);
363-
403+
364404
svg = svgContainer.append('svg:svg');
365405

366406
svg.attr('id', this.name)
@@ -417,7 +457,7 @@ define(['d3'], function () {
417457
preventOverlap(commit, this);
418458
}
419459
},
420-
460+
421461
_resizeSvg: function() {
422462
var ele = document.getElementById(this.svg.node().id);
423463
var container = ele.parentNode;
@@ -453,7 +493,7 @@ define(['d3'], function () {
453493
this._renderMergePointers();
454494
this._renderIdLabels();
455495
this._resizeSvg();
456-
this.checkout(this.currentBranch);
496+
this.currentBranch && this.checkout(this.currentBranch);
457497
},
458498

459499
_renderCircles: function () {
@@ -726,8 +766,8 @@ define(['d3'], function () {
726766
newTags.append('svg:text')
727767
.text(function (d) {
728768
if (d.name.indexOf('[') === 0 && d.name.indexOf(']') === d.name.length - 1)
729-
return d.name.substring(1, d.name.length - 1);
730-
return d.name;
769+
return d.name.substring(1, d.name.length - 1);
770+
return d.name;
731771
})
732772
.attr('y', function (d) {
733773
return tagY(d, view) + 14;
@@ -815,19 +855,23 @@ define(['d3'], function () {
815855

816856
commit.message = message;
817857
if (!commit.parent) {
818-
if (!this.currentBranch) {
819-
throw new Error('Not a good idea to make commits while in a detached HEAD state.');
820-
}
821-
822-
commit.parent = this.getCommit(this.currentBranch).id;
858+
commit.parent = this.getCommit('HEAD').id;
823859
}
824860

825861
this.commitData.push(commit);
826-
this.moveTag(this.currentBranch, commit.id);
862+
if (this.currentBranch) {
863+
this.moveTag(this.currentBranch, commit.id);
864+
}
827865

828866
this.renderCommits();
829867

830-
this.checkout(this.currentBranch);
868+
if (this.currentBranch) {
869+
console.log('branch', this.currentBranch)
870+
this.checkout(this.currentBranch);
871+
} else {
872+
console.log('commit', commit.id)
873+
this.checkout(commit.id)
874+
}
831875
return this;
832876
},
833877

@@ -887,6 +931,7 @@ define(['d3'], function () {
887931
},
888932

889933
checkout: function (ref) {
934+
console.log("checking out", ref)
890935
var commit = this.getCommit(ref);
891936

892937
if (!commit) {
@@ -900,7 +945,9 @@ define(['d3'], function () {
900945
previousHead.classed('checked-out', false);
901946
}
902947

903-
this._setCurrentBranch(ref === commit.id ? null : ref);
948+
var startsWithCommit = commit.id.indexOf(ref) === 0
949+
var startsWithHead = ref.toLowerCase().indexOf('head') === 0
950+
this._setCurrentBranch(startsWithCommit || startsWithHead ? null : ref);
904951
this.moveTag('HEAD', commit.id);
905952
this.renderTags();
906953

@@ -969,9 +1016,9 @@ define(['d3'], function () {
9691016
while (branchStartCommit.parent !== currentCommit.id) {
9701017
branchStartCommit = this.getCommit(branchStartCommit.parent);
9711018
}
972-
1019+
9731020
branchStartCommit.isNoFFBranch = true;
974-
1021+
9751022
this.commit({parent2: mergeTarget.id, isNoFFCommit: true});
9761023
} else if (this.isAncestor(currentCommit, mergeTarget)) {
9771024
this.fastForward(mergeTarget);

js/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,4 @@ require.config({
5252
exports: 'd3'
5353
}
5454
}
55-
});
55+
});

memtest.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ <h3><a id="start-test" href="#">Start Test</a></h3>
4141
</script>
4242
<h3><a href="index.html">Back to Home</a></h3>
4343
</body>
44-
</html>
44+
</html>

0 commit comments

Comments
 (0)