Skip to content

Commit 2e58866

Browse files
committed
get a merge example up and running!
1 parent f6b7c51 commit 2e58866

File tree

3 files changed

+140
-6
lines changed

3 files changed

+140
-6
lines changed

example/merge.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
var nodegit = require('../');
2+
var path = require('path');
3+
var Promise = require('nodegit-promise');
4+
var promisify = require('promisify-node');
5+
var fse = promisify(require('fs-extra'));
6+
fse.ensureDir = promisify(fse.ensureDir);
7+
8+
var normalizeOptions = require('../lib/util/normalize_options');
9+
var ourFileName = 'ourNewFile.txt';
10+
var ourFileContent = 'I like Toll Roads. I have an EZ-Pass!';
11+
var ourBranchName = "ours";
12+
13+
var theirFileName = 'theirNewFile.txt';
14+
var theirFileContent = "I'm skeptical about Toll Roads";
15+
var theirBranchName = "theirs";
16+
17+
var repoDir = '../../newRepo';
18+
19+
var repository;
20+
var ourCommit;
21+
var theirCommit;
22+
var ourBranch;
23+
var theirBranch;
24+
25+
var ourSignature = nodegit.Signature.create("Ron Paul", "RonPaul@TollRoadsRBest.info", 123456789, 60);
26+
var theirSignature = nodegit.Signature.create("Greg Abbott", "Gregggg@IllTollYourFace.us", 123456789, 60);
27+
28+
fse.remove(path.resolve(__dirname, repoDir))
29+
.then(function() {
30+
return fse.ensureDir(path.resolve(__dirname, repoDir));
31+
})
32+
.then(function() {
33+
return nodegit.Repository.init(path.resolve(__dirname, repoDir), 0);
34+
})
35+
.then(function(repo) {
36+
repository = repo;
37+
return fse.writeFile(path.join(repository.workdir(), ourFileName), ourFileContent);
38+
})
39+
.then(function() {
40+
return repository.openIndex();
41+
})
42+
.then(function(index) {
43+
index.read(1);
44+
index.addByPath(ourFileName);
45+
index.write()
46+
47+
return index.writeTree();
48+
})
49+
.then(function(oid) {
50+
return repository.createCommit('HEAD', ourSignature, ourSignature, 'we made a commit', oid, []);
51+
}).then(function(commitOid) {
52+
53+
return repository.getCommit(commitOid).then(function(commit) {
54+
ourCommit = commit;
55+
}).then(function() {
56+
return repository.createBranch(ourBranchName, commitOid).then(function(branch) {
57+
ourBranch = branch;
58+
return repository.createBranch(theirBranchName, commitOid);
59+
});
60+
});
61+
}).then(function(branch) {
62+
theirBranch = branch;
63+
64+
return nodegit.Reference.lookup(repository, 'HEAD').then(function(head) {
65+
return head.symbolicSetTarget(theirBranch.name(), theirSignature, "");
66+
});
67+
}).then(function() {
68+
return fse.writeFile(path.join(repository.workdir(), theirFileName), theirFileContent);
69+
})
70+
.then(function() {
71+
return repository.openIndex();
72+
})
73+
.then(function(index) {
74+
index.read(1);
75+
index.addByPath(theirFileName);
76+
index.write()
77+
78+
return index.writeTree();
79+
})
80+
.then(function(oid) {
81+
return repository.createCommit('HEAD', theirSignature, theirSignature, 'they made a commit', oid, [ourCommit]);
82+
})
83+
.then(function(commitOid) {
84+
return repository.getCommit(commitOid).then(function(commit) {
85+
theirCommit = commit;
86+
}).then(function() {
87+
return nodegit.Reference.lookup(repository, 'HEAD').then(function(head) {
88+
return head.symbolicSetTarget(ourBranch.name(), ourSignature, "");
89+
});
90+
});
91+
})
92+
.then(function() {
93+
return nodegit.Merge.commits(repository, ourCommit, theirCommit, new nodegit.MergeOptions());
94+
})
95+
.then(function(index) {
96+
index.write()
97+
return index.writeTreeTo(repository);
98+
})
99+
.then(function(oid) {
100+
return repository.createCommit(ourBranch.name(), ourSignature, ourSignature, 'we merged their commit', oid, [ourCommit, theirCommit]);
101+
})
102+
.done(function(commitId) {
103+
console.log('New Commit: ', commitId);
104+
});

generate/descriptor.json

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -630,9 +630,6 @@
630630
"isOptional": true
631631
}
632632
}
633-
},
634-
"git_index_write_tree_to": {
635-
"ignore": true
636633
}
637634
}
638635
},
@@ -668,15 +665,19 @@
668665
"git_merge_analysis": {
669666
"ignore": true
670667
},
671-
"git_merge_base": {
672-
"ignore": true
673-
},
674668
"git_merge_base_many": {
675669
"ignore": true
676670
},
677671
"git_merge_base_octopus": {
678672
"ignore": true
679673
},
674+
"git_merge_commits": {
675+
"args": {
676+
"opts": {
677+
"isOptional": true
678+
}
679+
}
680+
},
680681
"git_merge_file": {
681682
"ignore": true
682683
},
@@ -1027,6 +1028,16 @@
10271028
},
10281029
"git_reference_next_name": {
10291030
"ignore": true
1031+
},
1032+
"git_reference_symbolic_set_target": {
1033+
"args": {
1034+
"signature": {
1035+
"isOptional": true
1036+
},
1037+
"log_message": {
1038+
"isOptional": true
1039+
}
1040+
}
10301041
}
10311042
}
10321043
},

generate/libgit2-supplement.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,16 @@
261261
"git_odb_object_size",
262262
"git_odb_object_type"
263263
]
264+
],
265+
[
266+
"merge_head",
267+
[
268+
"git_merge_head_free",
269+
"git_merge_head_from_fetchhead",
270+
"git_merge_head_from_id",
271+
"git_merge_head_from_ref",
272+
"git_merge_head_id"
273+
]
264274
]
265275
]
266276
},
@@ -274,6 +284,15 @@
274284
"git_odb_object_size",
275285
"git_odb_object_type"
276286
]
287+
},
288+
"merge": {
289+
"functions": [
290+
"git_merge_head_free",
291+
"git_merge_head_from_fetchhead",
292+
"git_merge_head_from_id",
293+
"git_merge_head_from_ref",
294+
"git_merge_head_id"
295+
]
277296
}
278297
}
279298
}

0 commit comments

Comments
 (0)