Skip to content

Commit 1cd6320

Browse files
author
John Haley
committed
Make index methods async
Anytime the index reads/writes to the disk it should be non-blocking. Additionally this will give us thread safety protection since async methods are piped through the `LockManager`
1 parent f645360 commit 1cd6320

File tree

6 files changed

+154
-43
lines changed

6 files changed

+154
-43
lines changed

examples/merge-cleanly.js

+18-10
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,13 @@ fse.remove(path.resolve(__dirname, repoDir))
4646
return repository.refreshIndex();
4747
})
4848
.then(function(index) {
49-
index.addByPath(ourFileName);
50-
index.write();
51-
52-
return index.writeTree();
49+
return index.addByPath(ourFileName)
50+
.then(function() {
51+
return index.write();
52+
})
53+
.then(function() {
54+
return index.writeTree();
55+
});
5356
})
5457
.then(function(oid) {
5558
return repository.createCommit("HEAD", ourSignature,
@@ -81,10 +84,13 @@ fse.remove(path.resolve(__dirname, repoDir))
8184
return repository.refreshIndex();
8285
})
8386
.then(function(index) {
84-
index.addByPath(theirFileName);
85-
index.write();
86-
87-
return index.writeTree();
87+
return index.addByPath(theirFileName)
88+
.then(function() {
89+
return index.write();
90+
})
91+
.then(funcion() {
92+
return index.writeTree();
93+
});
8894
})
8995
.then(function(oid) {
9096
// You don"t have to change head to make a commit to a different branch.
@@ -110,8 +116,10 @@ fse.remove(path.resolve(__dirname, repoDir))
110116
// the repository instead of just writing it.
111117
.then(function(index) {
112118
if (!index.hasConflicts()) {
113-
index.write();
114-
return index.writeTreeTo(repository);
119+
return index.write()
120+
.then(function() {
121+
return index.writeTreeTo(repository);
122+
});
115123
}
116124
})
117125

examples/merge-with-conflicts.js

+32-19
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,13 @@ fse.remove(path.resolve(__dirname, repoDir))
5252
return repository.refreshIndex();
5353
})
5454
.then(function(index) {
55-
index.addByPath(fileName);
56-
index.write();
57-
58-
return index.writeTree();
55+
return index.addByPath(fileName)
56+
.then(function() {
57+
return index.write();
58+
})
59+
.then(function() {
60+
return index.writeTree();
61+
});
5962
})
6063
.then(function(oid) {
6164
return repository.createCommit("HEAD", baseSignature,
@@ -95,10 +98,13 @@ fse.remove(path.resolve(__dirname, repoDir))
9598
.then(function() {
9699
return repository.refreshIndex()
97100
.then(function(index) {
98-
index.addByPath(fileName);
99-
index.write();
100-
101-
return index.writeTree();
101+
return index.addByPath(fileName)
102+
.then(function() {
103+
return index.write();
104+
})
105+
.then(function() {
106+
return index.writeTree();
107+
});
102108
});
103109
})
104110
.then(function(oid) {
@@ -120,11 +126,15 @@ fse.remove(path.resolve(__dirname, repoDir))
120126
);
121127
})
122128
.then(function() {
123-
return repository.refreshIndex().then(function(index) {
124-
index.addByPath(fileName);
125-
index.write();
126-
127-
return index.writeTree();
129+
return repository.refreshIndex()
130+
.then(function(index) {
131+
return index.addByPath(fileName)
132+
.then(function() {
133+
return index.write();
134+
})
135+
.then(function() {
136+
return index.writeTree();
137+
});
128138
});
129139
})
130140
.then(function(oid) {
@@ -170,12 +180,15 @@ fse.remove(path.resolve(__dirname, repoDir))
170180
// we need to get a new index as the other one isnt backed to
171181
// the repository in the usual fashion, and just behaves weirdly
172182
.then(function() {
173-
return repository.refreshIndex().then(function(index) {
174-
175-
index.addByPath(fileName);
176-
index.write();
177-
178-
return index.writeTree();
183+
return repository.refreshIndex()
184+
.then(function(index) {
185+
return index.addByPath(fileName)
186+
.then(function() {
187+
return index.write();
188+
})
189+
.then(function() {
190+
return index.writeTree();
191+
});
179192
});
180193
})
181194
.then(function(oid) {

examples/push.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,13 @@ fse.remove(path.resolve(__dirname, repoDir))
3333
return repository.refreshIndex();
3434
})
3535
.then(function(index) {
36-
index.addByPath(fileName);
37-
index.write();
38-
39-
return index.writeTree();
36+
return index.addByPath(fileName)
37+
.then(function() {
38+
return index.write();
39+
})
40+
.then(function() {
41+
return index.writeTree();
42+
});
4043
})
4144
.then(function(oid) {
4245
return repository.createCommit("HEAD", signature, signature,

examples/remove-and-commit.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ nodegit.Repository.open(path.resolve(__dirname, "../.git"))
2424
})
2525
.then(function() {
2626
//remove the file from the index...
27-
_index.removeByPath(fileName);
27+
return _index.removeByPath(fileName);
28+
})
29+
.then(function() {
2830
return _index.write();
2931
})
3032
.then(function() {

generate/input/descriptor.json

+82-2
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,12 @@
959959
},
960960
"index": {
961961
"functions": {
962+
"git_index_add": {
963+
"isAsync": true,
964+
"return": {
965+
"isErrorCode": true
966+
}
967+
},
962968
"git_index_add_all": {
963969
"args": {
964970
"pathspec": {
@@ -979,8 +985,25 @@
979985
"git_index_add_frombuffer": {
980986
"ignore": true
981987
},
982-
"git_index_conflict_get": {
988+
"git_index_clear": {
989+
"isAsync": true,
990+
"return": {
991+
"isErrorCode": true
992+
}
993+
},
994+
"git_index_conflict_add": {
983995
"isAsync": true,
996+
"return": {
997+
"isErrorCode": true
998+
}
999+
},
1000+
"git_index_conflict_cleanup": {
1001+
"isAsync": true,
1002+
"return": {
1003+
"isErrorCode": true
1004+
}
1005+
},
1006+
"git_index_conflict_get": {
9841007
"args": {
9851008
"ancestor_out": {
9861009
"isReturn": true
@@ -992,6 +1015,13 @@
9921015
"isReturn": true
9931016
}
9941017
},
1018+
"isAsync": true,
1019+
"return": {
1020+
"isErrorCode": true
1021+
}
1022+
},
1023+
"git_index_conflict_remove": {
1024+
"isAsync": true,
9951025
"return": {
9961026
"isErrorCode": true
9971027
}
@@ -1024,11 +1054,33 @@
10241054
"git_index_new": {
10251055
"ignore": true
10261056
},
1057+
"git_index_open": {
1058+
"isAsync": true,
1059+
"return": {
1060+
"isErrorCode": true
1061+
}
1062+
},
10271063
"git_index_read": {
10281064
"args": {
10291065
"force": {
10301066
"isOptional": true
10311067
}
1068+
},
1069+
"isAsync": true,
1070+
"return": {
1071+
"isErrorCode": true
1072+
}
1073+
},
1074+
"git_index_read_tree": {
1075+
"isAsync": true,
1076+
"return": {
1077+
"isErrorCode": true
1078+
}
1079+
},
1080+
"git_index_remove": {
1081+
"isAsync": true,
1082+
"return": {
1083+
"isErrorCode": true
10321084
}
10331085
},
10341086
"git_index_remove_all": {
@@ -1048,6 +1100,18 @@
10481100
"isErrorCode": true
10491101
}
10501102
},
1103+
"git_index_remove_bypath": {
1104+
"isAsync": true,
1105+
"return": {
1106+
"isErrorCode": true
1107+
}
1108+
},
1109+
"git_index_remove_directory": {
1110+
"isAsync": true,
1111+
"return": {
1112+
"isErrorCode": true
1113+
}
1114+
},
10511115
"git_index_update_all": {
10521116
"args": {
10531117
"pathspec": {
@@ -1070,7 +1134,23 @@
10701134
"force": {
10711135
"isOptional": true
10721136
}
1073-
}
1137+
},
1138+
"isAsync": true,
1139+
"return": {
1140+
"isErrorCode": true
1141+
}
1142+
},
1143+
"git_index_write_tree": {
1144+
"isAsync": true,
1145+
"return": {
1146+
"isErrorCode": true
1147+
}
1148+
},
1149+
"git_index_write_tree_to": {
1150+
"isAsync": true,
1151+
"return": {
1152+
"isErrorCode": true
1153+
}
10741154
}
10751155
},
10761156
"dependencies": [

lib/repository.js

+12-7
Original file line numberDiff line numberDiff line change
@@ -535,16 +535,21 @@ Repository.prototype.createCommitOnHead = function(
535535
message,
536536
callback) {
537537

538-
var index;
539538
var repo = this;
540539

541540
return repo.refreshIndex()
542-
.then(function(index_) {
543-
index = index_;
544-
if (!filesToAdd) { filesToAdd = []; }
545-
filesToAdd.forEach(function(filePath) {
546-
index.addByPath(filePath);
547-
});
541+
.then(function(index) {
542+
if (!filesToAdd) {
543+
filesToAdd = [];
544+
}
545+
546+
return filesToAdd
547+
.reduce(function(lastFilePromise, filePath) {
548+
return lastFilePromise
549+
.then(function() {
550+
return index.addByPath(filePath);
551+
});
552+
}, Promise.resolve());
548553
index.write();
549554
return index.writeTree();
550555
})

0 commit comments

Comments
 (0)