@@ -5,7 +5,6 @@ var promisify = require('promisify-node');
55var fse = promisify ( require ( 'fs-extra' ) ) ;
66fse . ensureDir = promisify ( fse . ensureDir ) ;
77
8- var normalizeOptions = require ( '../lib/util/normalize_options' ) ;
98var ourFileName = 'ourNewFile.txt' ;
109var ourFileContent = 'I like Toll Roads. I have an EZ-Pass!' ;
1110var ourBranchName = "ours" ;
@@ -25,6 +24,7 @@ var theirBranch;
2524var ourSignature = nodegit . Signature . create ( "Ron Paul" , "RonPaul@TollRoadsRBest.info" , 123456789 , 60 ) ;
2625var theirSignature = nodegit . Signature . create ( "Greg Abbott" , "Gregggg@IllTollYourFace.us" , 123456789 , 60 ) ;
2726
27+ // Create a new repository in a clean directory, and add our first file
2828fse . remove ( path . resolve ( __dirname , repoDir ) )
2929. then ( function ( ) {
3030 return fse . ensureDir ( path . resolve ( __dirname , repoDir ) ) ;
@@ -36,6 +36,8 @@ fse.remove(path.resolve(__dirname, repoDir))
3636 repository = repo ;
3737 return fse . writeFile ( path . join ( repository . workdir ( ) , ourFileName ) , ourFileContent ) ;
3838} )
39+
40+ // Load up the repository index and make our initial commit to HEAD
3941. then ( function ( ) {
4042 return repository . openIndex ( ) ;
4143} )
@@ -48,8 +50,10 @@ fse.remove(path.resolve(__dirname, repoDir))
4850} )
4951. then ( function ( oid ) {
5052 return repository . createCommit ( 'HEAD' , ourSignature , ourSignature , 'we made a commit' , oid , [ ] ) ;
51- } ) . then ( function ( commitOid ) {
53+ } )
5254
55+ // Get commit object from the oid, and create our new branches at that position
56+ . then ( function ( commitOid ) {
5357 return repository . getCommit ( commitOid ) . then ( function ( commit ) {
5458 ourCommit = commit ;
5559 } ) . then ( function ( ) {
@@ -58,13 +62,11 @@ fse.remove(path.resolve(__dirname, repoDir))
5862 return repository . createBranch ( theirBranchName , commitOid ) ;
5963 } ) ;
6064 } ) ;
61- } ) . then ( function ( branch ) {
62- theirBranch = branch ;
65+ } )
6366
64- return nodegit . Reference . lookup ( repository , 'HEAD' ) . then ( function ( head ) {
65- return head . symbolicSetTarget ( theirBranch . name ( ) , theirSignature , "" ) ;
66- } ) ;
67- } ) . then ( function ( ) {
67+ // Create a new file, stage it and commit it to our second branch
68+ . then ( function ( branch ) {
69+ theirBranch = branch ;
6870 return fse . writeFile ( path . join ( repository . workdir ( ) , theirFileName ) , theirFileContent ) ;
6971} )
7072. then ( function ( ) {
@@ -78,27 +80,32 @@ fse.remove(path.resolve(__dirname, repoDir))
7880 return index . writeTree ( ) ;
7981} )
8082. then ( function ( oid ) {
81- return repository . createCommit ( 'HEAD' , theirSignature , theirSignature , 'they made a commit' , oid , [ ourCommit ] ) ;
83+ // You don't have to change head to make a commit to a different branch.
84+ return repository . createCommit ( theirBranch . name ( ) , theirSignature , theirSignature , 'they made a commit' , oid , [ ourCommit ] ) ;
8285} )
8386. then ( function ( commitOid ) {
8487 return repository . getCommit ( commitOid ) . then ( function ( commit ) {
8588 theirCommit = commit ;
86- } ) . then ( function ( ) {
87- return nodegit . Reference . lookup ( repository , 'HEAD' ) . then ( function ( head ) {
88- return head . symbolicSetTarget ( ourBranch . name ( ) , ourSignature , "" ) ;
89- } ) ;
9089 } ) ;
9190} )
91+
92+ // Merge the two commits
9293. then ( function ( ) {
93- return nodegit . Merge . commits ( repository , ourCommit , theirCommit , new nodegit . MergeOptions ( ) ) ;
94+ return nodegit . Merge . commits ( repository , ourCommit , theirCommit , null ) ;
9495} )
96+
97+ // Merging returns an index that isn't backed by the repository. You have to write it to the repository,
98+ // instead of just writing it. You have to manually check for merge conflicts, which will reject the promise
9599. then ( function ( index ) {
96100 index . write ( )
97101 return index . writeTreeTo ( repository ) ;
98102} )
103+
104+ // Create our merge commit back on our branch
99105. then ( function ( oid ) {
100106 return repository . createCommit ( ourBranch . name ( ) , ourSignature , ourSignature , 'we merged their commit' , oid , [ ourCommit , theirCommit ] ) ;
101107} )
102108. done ( function ( commitId ) {
109+ // We never changed the HEAD after the initial commit; it should still be the same as master.
103110 console . log ( 'New Commit: ' , commitId ) ;
104111} ) ;
0 commit comments