Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 51 additions & 9 deletions generate/templates/manual/revwalk/file_history_walk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ void GitRevwalk::FileHistoryWalkWorker::Execute()
}

git_diff *diffs;
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
char *file_path = strdup(baton->file_path);
opts.pathspec.strings = &file_path;
opts.pathspec.count = 1;
git_commit *parent;
unsigned int parents = git_commit_parentcount(nextCommit);
if (parents > 1) {
Expand All @@ -67,19 +71,23 @@ void GitRevwalk::FileHistoryWalkWorker::Execute()
}
if (
(baton->error_code = git_commit_tree(&parentTree, parent)) != GIT_OK ||
(baton->error_code = git_diff_tree_to_tree(&diffs, repo, parentTree, thisTree, NULL)) != GIT_OK
(baton->error_code = git_diff_tree_to_tree(&diffs, repo, parentTree, thisTree, &opts)) != GIT_OK
) {
git_commit_free(nextCommit);
git_commit_free(parent);
break;
}
} else {
if ((baton->error_code = git_diff_tree_to_tree(&diffs, repo, NULL, thisTree, NULL)) != GIT_OK) {
if ((baton->error_code = git_diff_tree_to_tree(&diffs, repo, NULL, thisTree, &opts)) != GIT_OK) {
git_commit_free(nextCommit);
break;
}
}

free(file_path);
opts.pathspec.strings = NULL;
opts.pathspec.count = 0;

bool flag = false;
bool doRenamedPass = false;
unsigned int numDeltas = git_diff_num_deltas(diffs);
Expand Down Expand Up @@ -127,10 +135,29 @@ void GitRevwalk::FileHistoryWalkWorker::Execute()
}
}

if (
doRenamedPass &&
(baton->error_code = git_diff_find_similar(diffs, NULL)) == GIT_OK
) {
if (doRenamedPass) {
git_diff_free(diffs);

if (parents == 1) {
if ((baton->error_code = git_diff_tree_to_tree(&diffs, repo, parentTree, thisTree, NULL)) != GIT_OK) {
git_commit_free(nextCommit);
break;
}
if ((baton->error_code = git_diff_find_similar(diffs, NULL)) != GIT_OK) {
git_commit_free(nextCommit);
break;
}
} else {
if ((baton->error_code = git_diff_tree_to_tree(&diffs, repo, NULL, thisTree, NULL)) != GIT_OK) {
git_commit_free(nextCommit);
break;
}
if((baton->error_code = git_diff_find_similar(diffs, NULL)) != GIT_OK) {
git_commit_free(nextCommit);
break;
}
}

flag = false;
numDeltas = git_diff_num_deltas(diffs);
for (unsigned int j = 0; j < numDeltas; ++j) {
Expand All @@ -148,13 +175,20 @@ void GitRevwalk::FileHistoryWalkWorker::Execute()
const git_diff_delta *delta = git_patch_get_delta(nextPatch);
bool isEqualOldFile = !strcmp(delta->old_file.path, baton->file_path);
bool isEqualNewFile = !strcmp(delta->new_file.path, baton->file_path);
int oldLen = strlen(delta->old_file.path);
int newLen = strlen(delta->new_file.path);
char *outPair = new char[oldLen + newLen + 2];
strcpy(outPair, delta->new_file.path);
outPair[newLen] = '\n';
outPair[newLen + 1] = '\0';
strcat(outPair, delta->old_file.path);

if (isEqualNewFile) {
std::pair<git_commit *, std::pair<char *, git_delta_t> > *historyEntry;
if (!isEqualOldFile) {
historyEntry = new std::pair<git_commit *, std::pair<char *, git_delta_t> >(
nextCommit,
std::pair<char *, git_delta_t>(strdup(delta->old_file.path), delta->status)
std::pair<char *, git_delta_t>(strdup(outPair), delta->status)
);
} else {
historyEntry = new std::pair<git_commit *, std::pair<char *, git_delta_t> >(
Expand All @@ -168,12 +202,14 @@ void GitRevwalk::FileHistoryWalkWorker::Execute()
std::pair<git_commit *, std::pair<char *, git_delta_t> > *historyEntry;
historyEntry = new std::pair<git_commit *, std::pair<char *, git_delta_t> >(
nextCommit,
std::pair<char *, git_delta_t>(strdup(delta->new_file.path), delta->status)
std::pair<char *, git_delta_t>(strdup(outPair), delta->status)
);
baton->out->push_back(historyEntry);
flag = true;
}

delete[] outPair;

git_patch_free(nextPatch);

if (flag) {
Expand Down Expand Up @@ -228,7 +264,13 @@ void GitRevwalk::FileHistoryWalkWorker::HandleOKCallback()
Nan::Set(historyEntry, Nan::New("commit").ToLocalChecked(), GitCommit::New(batonResult->first, true));
Nan::Set(historyEntry, Nan::New("status").ToLocalChecked(), Nan::New<Number>(batonResult->second.second));
if (batonResult->second.second == GIT_DELTA_RENAMED) {
Nan::Set(historyEntry, Nan::New("altname").ToLocalChecked(), Nan::New(batonResult->second.first).ToLocalChecked());
char *namePair = batonResult->second.first;
char *split = strchr(namePair, '\n');
*split = '\0';
char *oldName = split + 1;

Nan::Set(historyEntry, Nan::New("oldName").ToLocalChecked(), Nan::New(oldName).ToLocalChecked());
Nan::Set(historyEntry, Nan::New("newName").ToLocalChecked(), Nan::New(namePair).ToLocalChecked());
}
Nan::Set(result, Nan::New<Number>(i), historyEntry);

Expand Down
4 changes: 3 additions & 1 deletion lib/revwalk.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ Revwalk.prototype.getCommits = function(count) {
* @type {Object}
* @property {Commit} commit the commit for this entry
* @property {Number} status the status of the file in the commit
* @property {String} altname the other name that is provided when status is
* @property {String} newName the new name that is provided when status is
* renamed
* @property {String} oldName the old name that is provided when status is
* renamed
*/

Expand Down
6 changes: 4 additions & 2 deletions test/tests/revwalk.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ describe("Revwalk", function() {
})
.then(function(results) {
assert.equal(results[0].status, NodeGit.Diff.DELTA.RENAMED);
assert.equal(results[0].altname, fileNameA);
assert.equal(results[0].newName, fileNameB);
assert.equal(results[0].oldName, fileNameA);
})
.then(function() {
var walker = repo.createRevWalk();
Expand All @@ -296,7 +297,8 @@ describe("Revwalk", function() {
})
.then(function(results) {
assert.equal(results[0].status, NodeGit.Diff.DELTA.RENAMED);
assert.equal(results[0].altname, fileNameB);
assert.equal(results[0].newName, fileNameB);
assert.equal(results[0].oldName, fileNameA);
})
.then(function() {
return fse.remove(repoPath);
Expand Down