@@ -5,7 +5,6 @@ var promisify = require('promisify-node');
5
5
var fse = promisify ( require ( 'fs-extra' ) ) ;
6
6
fse . ensureDir = promisify ( fse . ensureDir ) ;
7
7
8
- var normalizeOptions = require ( '../lib/util/normalize_options' ) ;
9
8
var ourFileName = 'ourNewFile.txt' ;
10
9
var ourFileContent = 'I like Toll Roads. I have an EZ-Pass!' ;
11
10
var ourBranchName = "ours" ;
@@ -25,6 +24,7 @@ var theirBranch;
25
24
var ourSignature = nodegit . Signature . create ( "Ron Paul" , "[email protected] " , 123456789 , 60 ) ;
26
25
var theirSignature = nodegit . Signature . create ( "Greg Abbott" , "[email protected] " , 123456789 , 60 ) ;
27
26
27
+ // Create a new repository in a clean directory, and add our first file
28
28
fse . remove ( path . resolve ( __dirname , repoDir ) )
29
29
. then ( function ( ) {
30
30
return fse . ensureDir ( path . resolve ( __dirname , repoDir ) ) ;
@@ -36,6 +36,8 @@ fse.remove(path.resolve(__dirname, repoDir))
36
36
repository = repo ;
37
37
return fse . writeFile ( path . join ( repository . workdir ( ) , ourFileName ) , ourFileContent ) ;
38
38
} )
39
+
40
+ // Load up the repository index and make our initial commit to HEAD
39
41
. then ( function ( ) {
40
42
return repository . openIndex ( ) ;
41
43
} )
@@ -48,8 +50,10 @@ fse.remove(path.resolve(__dirname, repoDir))
48
50
} )
49
51
. then ( function ( oid ) {
50
52
return repository . createCommit ( 'HEAD' , ourSignature , ourSignature , 'we made a commit' , oid , [ ] ) ;
51
- } ) . then ( function ( commitOid ) {
53
+ } )
52
54
55
+ // Get commit object from the oid, and create our new branches at that position
56
+ . then ( function ( commitOid ) {
53
57
return repository . getCommit ( commitOid ) . then ( function ( commit ) {
54
58
ourCommit = commit ;
55
59
} ) . then ( function ( ) {
@@ -58,13 +62,11 @@ fse.remove(path.resolve(__dirname, repoDir))
58
62
return repository . createBranch ( theirBranchName , commitOid ) ;
59
63
} ) ;
60
64
} ) ;
61
- } ) . then ( function ( branch ) {
62
- theirBranch = branch ;
65
+ } )
63
66
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 ;
68
70
return fse . writeFile ( path . join ( repository . workdir ( ) , theirFileName ) , theirFileContent ) ;
69
71
} )
70
72
. then ( function ( ) {
@@ -78,27 +80,32 @@ fse.remove(path.resolve(__dirname, repoDir))
78
80
return index . writeTree ( ) ;
79
81
} )
80
82
. 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 ] ) ;
82
85
} )
83
86
. then ( function ( commitOid ) {
84
87
return repository . getCommit ( commitOid ) . then ( function ( commit ) {
85
88
theirCommit = commit ;
86
- } ) . then ( function ( ) {
87
- return nodegit . Reference . lookup ( repository , 'HEAD' ) . then ( function ( head ) {
88
- return head . symbolicSetTarget ( ourBranch . name ( ) , ourSignature , "" ) ;
89
- } ) ;
90
89
} ) ;
91
90
} )
91
+
92
+ // Merge the two commits
92
93
. then ( function ( ) {
93
- return nodegit . Merge . commits ( repository , ourCommit , theirCommit , new nodegit . MergeOptions ( ) ) ;
94
+ return nodegit . Merge . commits ( repository , ourCommit , theirCommit , null ) ;
94
95
} )
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
95
99
. then ( function ( index ) {
96
100
index . write ( )
97
101
return index . writeTreeTo ( repository ) ;
98
102
} )
103
+
104
+ // Create our merge commit back on our branch
99
105
. then ( function ( oid ) {
100
106
return repository . createCommit ( ourBranch . name ( ) , ourSignature , ourSignature , 'we merged their commit' , oid , [ ourCommit , theirCommit ] ) ;
101
107
} )
102
108
. done ( function ( commitId ) {
109
+ // We never changed the HEAD after the initial commit; it should still be the same as master.
103
110
console . log ( 'New Commit: ' , commitId ) ;
104
111
} ) ;
0 commit comments