FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Merge pull request #900 from nodegit/optimize-history-walk · nodegit/nodegit@01a322c · GitHub

Repository navigation

Commit 01a322c

Browse files
committed
Merge pull request #900 from nodegit/optimize-history-walk
Optimize rename detection in historywalk
2 parents 7967475 + d23ba87 commit 01a322c

3 files changed

Lines changed: 58 additions & 12 deletions

File tree

‎generate/templates/manual/revwalk/file_history_walk.cc‎

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ void GitRevwalk::FileHistoryWalkWorker::Execute()
5555
}
5656

5757
git_diff *diffs;
58+
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
59+
char *file_path = strdup(baton->file_path);
60+
opts.pathspec.strings = &file_path;
61+
opts.pathspec.count = 1;
5862
git_commit *parent;
5963
unsigned int parents = git_commit_parentcount(nextCommit);
6064
if (parents > 1) {
@@ -67,19 +71,23 @@ void GitRevwalk::FileHistoryWalkWorker::Execute()
6771
}
6872
if (
6973
(baton->error_code = git_commit_tree(&parentTree, parent)) != GIT_OK ||
70-
(baton->error_code = git_diff_tree_to_tree(&diffs, repo, parentTree, thisTree, NULL)) != GIT_OK
74+
(baton->error_code = git_diff_tree_to_tree(&diffs, repo, parentTree, thisTree, &opts)) != GIT_OK
7175
) {
7276
git_commit_free(nextCommit);
7377
git_commit_free(parent);
7478
break;
7579
}
7680
} else {
77-
if ((baton->error_code = git_diff_tree_to_tree(&diffs, repo, NULL, thisTree, NULL)) != GIT_OK) {
81+
if ((baton->error_code = git_diff_tree_to_tree(&diffs, repo, NULL, thisTree, &opts)) != GIT_OK) {
7882
git_commit_free(nextCommit);
7983
break;
8084
}
8185
}
8286

87+
free(file_path);
88+
opts.pathspec.strings = NULL;
89+
opts.pathspec.count = 0;
90+
8391
bool flag = false;
8492
bool doRenamedPass = false;
8593
unsigned int numDeltas = git_diff_num_deltas(diffs);
@@ -127,10 +135,29 @@ void GitRevwalk::FileHistoryWalkWorker::Execute()
127135
}
128136
}
129137

130-
if (
131-
doRenamedPass &&
132-
(baton->error_code = git_diff_find_similar(diffs, NULL)) == GIT_OK
133-
) {
138+
if (doRenamedPass) {
139+
git_diff_free(diffs);
140+
141+
if (parents == 1) {
142+
if ((baton->error_code = git_diff_tree_to_tree(&diffs, repo, parentTree, thisTree, NULL)) != GIT_OK) {
143+
git_commit_free(nextCommit);
144+
break;
145+
}
146+
if ((baton->error_code = git_diff_find_similar(diffs, NULL)) != GIT_OK) {
147+
git_commit_free(nextCommit);
148+
break;
149+
}
150+
} else {
151+
if ((baton->error_code = git_diff_tree_to_tree(&diffs, repo, NULL, thisTree, NULL)) != GIT_OK) {
152+
git_commit_free(nextCommit);
153+
break;
154+
}
155+
if((baton->error_code = git_diff_find_similar(diffs, NULL)) != GIT_OK) {
156+
git_commit_free(nextCommit);
157+
break;
158+
}
159+
}
160+
134161
flag = false;
135162
numDeltas = git_diff_num_deltas(diffs);
136163
for (unsigned int j = 0; j < numDeltas; ++j) {
@@ -148,13 +175,20 @@ void GitRevwalk::FileHistoryWalkWorker::Execute()
148175
const git_diff_delta *delta = git_patch_get_delta(nextPatch);
149176
bool isEqualOldFile = !strcmp(delta->old_file.path, baton->file_path);
150177
bool isEqualNewFile = !strcmp(delta->new_file.path, baton->file_path);
178+
int oldLen = strlen(delta->old_file.path);
179+
int newLen = strlen(delta->new_file.path);
180+
char *outPair = new char[oldLen + newLen + 2];
181+
strcpy(outPair, delta->new_file.path);
182+
outPair[newLen] = '\n';
183+
outPair[newLen + 1] = '\0';
184+
strcat(outPair, delta->old_file.path);
151185

152186
if (isEqualNewFile) {
153187
std::pair<git_commit *, std::pair<char *, git_delta_t> > *historyEntry;
154188
if (!isEqualOldFile) {
155189
historyEntry = new std::pair<git_commit *, std::pair<char *, git_delta_t> >(
156190
nextCommit,
157-
std::pair<char *, git_delta_t>(strdup(delta->old_file.path), delta->status)
191+
std::pair<char *, git_delta_t>(strdup(outPair), delta->status)
158192
);
159193
} else {
160194
historyEntry = new std::pair<git_commit *, std::pair<char *, git_delta_t> >(
@@ -168,12 +202,14 @@ void GitRevwalk::FileHistoryWalkWorker::Execute()
168202
std::pair<git_commit *, std::pair<char *, git_delta_t> > *historyEntry;
169203
historyEntry = new std::pair<git_commit *, std::pair<char *, git_delta_t> >(
170204
nextCommit,
171-
std::pair<char *, git_delta_t>(strdup(delta->new_file.path), delta->status)
205+
std::pair<char *, git_delta_t>(strdup(outPair), delta->status)
172206
);
173207
baton->out->push_back(historyEntry);
174208
flag = true;
175209
}
176210

211+
delete[] outPair;
212+
177213
git_patch_free(nextPatch);
178214

179215
if (flag) {
@@ -228,7 +264,13 @@ void GitRevwalk::FileHistoryWalkWorker::HandleOKCallback()
228264
Nan::Set(historyEntry, Nan::New("commit").ToLocalChecked(), GitCommit::New(batonResult->first, true));
229265
Nan::Set(historyEntry, Nan::New("status").ToLocalChecked(), Nan::New<Number>(batonResult->second.second));
230266
if (batonResult->second.second == GIT_DELTA_RENAMED) {
231-
Nan::Set(historyEntry, Nan::New("altname").ToLocalChecked(), Nan::New(batonResult->second.first).ToLocalChecked());
267+
char *namePair = batonResult->second.first;
268+
char *split = strchr(namePair, '\n');
269+
*split = '\0';
270+
char *oldName = split + 1;
271+
272+
Nan::Set(historyEntry, Nan::New("oldName").ToLocalChecked(), Nan::New(oldName).ToLocalChecked());
273+
Nan::Set(historyEntry, Nan::New("newName").ToLocalChecked(), Nan::New(namePair).ToLocalChecked());
232274
}
233275
Nan::Set(result, Nan::New<Number>(i), historyEntry);
234276

‎lib/revwalk.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ Revwalk.prototype.getCommits = function(count) {
129129
* @type {Object}
130130
* @property {Commit} commit the commit for this entry
131131
* @property {Number} status the status of the file in the commit
132-
* @property {String} altname the other name that is provided when status is
132+
* @property {String} newName the new name that is provided when status is
133+
* renamed
134+
* @property {String} oldName the old name that is provided when status is
133135
* renamed
134136
*/
135137

‎test/tests/revwalk.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,8 @@ describe("Revwalk", function() {
286286
})
287287
.then(function(results) {
288288
assert.equal(results[0].status, NodeGit.Diff.DELTA.RENAMED);
289-
assert.equal(results[0].altname, fileNameA);
289+
assert.equal(results[0].newName, fileNameB);
290+
assert.equal(results[0].oldName, fileNameA);
290291
})
291292
.then(function() {
292293
var walker = repo.createRevWalk();
@@ -296,7 +297,8 @@ describe("Revwalk", function() {
296297
})
297298
.then(function(results) {
298299
assert.equal(results[0].status, NodeGit.Diff.DELTA.RENAMED);
299-
assert.equal(results[0].altname, fileNameB);
300+
assert.equal(results[0].newName, fileNameB);
301+
assert.equal(results[0].oldName, fileNameA);
300302
})
301303
.then(function() {
302304
return fse.remove(repoPath);

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL