Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gitgraph: Make reset work with parent ref carets #350

Merged
merged 1 commit into from
May 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/diagrams/gitGraph/gitGraphAst.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,21 @@ exports.checkout = function(branch) {
head = commits[id];
}

exports.reset = function(ref) {
log.debug('in reset');
exports.reset = function(commitRef) {
log.debug('in reset', commitRef);
var ref = commitRef.split(':')[0];
var parentCount = parseInt(commitRef.split(':')[1]);
var commit = ref == 'HEAD' ? head : commits[branches[ref]];
log.debug(commit, parentCount);
while (parentCount > 0) {
commit = commits[commit.parent];
parentCount--;
if (!commit) {
var err = 'Critical error - unique parent commit not found during reset';
log.error(err);
throw err;
}
}
head = commit;
branches[curBranch] = commit.id;
}
Expand Down
22 changes: 20 additions & 2 deletions src/diagrams/gitGraph/gitGraphParser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe('when parsing a gitGraph',function() {
});

it('should handle set direction', function () {
var str = 'gitGraph TB:\n' +
var str = 'gitGraph BT:\n' +
'commit\n';

parser.parse(str);
Expand All @@ -81,7 +81,7 @@ describe('when parsing a gitGraph',function() {

expect(Object.keys(commits).length).toBe(1);
expect(parser.yy.getCurrentBranch()).toBe('master');
expect(parser.yy.getDirection()).toBe('TB');
expect(parser.yy.getDirection()).toBe('BT');
expect(Object.keys(parser.yy.getBranches()).length).toBe(1);
});

Expand Down Expand Up @@ -145,6 +145,24 @@ describe('when parsing a gitGraph',function() {
expect(parser.yy.getHead().id).toEqual(parser.yy.getBranches()['newbranch']);
});

it('reset can take an argument', function () {
var str = 'gitGraph:\n' +
'commit\n' +
'commit\n' +
'branch newbranch\n' +
'checkout newbranch\n' +
'commit\n' +
'reset master^\n';

parser.parse(str);

var commits = parser.yy.getCommits();
expect(Object.keys(commits).length).toBe(3);
expect(parser.yy.getCurrentBranch()).toBe('newbranch');
var master = commits[parser.yy.getBranches()['master']];
expect(parser.yy.getHead().id).toEqual(master.parent);
})

it('it should handle fast forwardable merges', function () {
var str = 'gitGraph:\n' +
'commit\n' +
Expand Down
10 changes: 7 additions & 3 deletions src/diagrams/gitGraph/parser/gitGraph.jison
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
"reset" return 'RESET';
"checkout" return 'CHECKOUT';
"LR" return 'DIR';
"TB" return 'DIR';
"BT" return 'DIR';
":" return ':';
"^" return 'CARET'
"options"\r?\n this.begin("options");
<options>"end"\r?\n this.popState();
<options>[^\n]+\r?\n return 'OPT';
Expand Down Expand Up @@ -83,6 +83,10 @@ commit_arg
;

reset_arg
: 'HEAD'
| ID
: 'HEAD' reset_parents{$$ = $1+ ":" + $2 }
| ID reset_parents{$$ = $1+ ":" + yy.count; yy.count = 0}
;
reset_parents
: /* empty */ {yy.count = 0}
| CARET reset_parents { yy.count += 1 }
;
42 changes: 26 additions & 16 deletions src/diagrams/gitGraph/parser/gitGraph.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion testgitgraph.mm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
gitGraph BT:
options
{"key": "value",
{
"nodeSpacing": 150
}
end
Expand All @@ -13,4 +13,7 @@
commit
commit
merge newbranch
reset newbranch^^
commit
commit