feat(diff): add ability to get patchid

This commit is contained in:
Aleksey Kulikov 2021-09-15 15:18:27 +03:00
parent a7b714c2f3
commit f7f4a395c0
3 changed files with 34 additions and 0 deletions

View file

@ -199,6 +199,27 @@ void findSimilar(
calloc.free(opts);
}
/// Calculate the patch ID for the given patch.
///
/// Calculate a stable patch ID for the given patch by summing the hash of the file diffs,
/// ignoring whitespace and line numbers. This can be used to derive whether two diffs are
/// the same with a high probability.
///
/// Currently, this function only calculates stable patch IDs, as defined in `git-patch-id(1)`,
/// and should in fact generate the same IDs as the upstream git project does.
///
/// Throws a [LibGit2Error] if error occured.
Pointer<git_oid> patchId(Pointer<git_diff> diff) {
final out = calloc<git_oid>();
final error = libgit2.git_diff_patchid(out, diff, nullptr);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
} else {
return out;
}
}
/// Return the diff delta for an entry in the diff list.
///
/// Throws [RangeError] if index out of range.

View file

@ -75,6 +75,18 @@ class Diff {
);
}
/// Calculate the patch ID for the given patch.
///
/// Calculate a stable patch ID for the given patch by summing the hash of the file diffs,
/// ignoring whitespace and line numbers. This can be used to derive whether two diffs are
/// the same with a high probability.
///
/// Currently, this function only calculates stable patch IDs, as defined in `git-patch-id(1)`,
/// and should in fact generate the same IDs as the upstream git project does.
///
/// Throws a [LibGit2Error] if error occured.
Oid get patchId => Oid(bindings.patchId(_diffPointer));
/// Releases memory allocated for diff object.
void free() => bindings.free(_diffPointer);
}

View file

@ -188,6 +188,7 @@ index e69de29..c217c63 100644
expect(diff.length, 1);
expect(stats.filesChanged, 1);
expect(stats.insertions, 1);
expect(diff.patchId.sha, '699556913185bc38632ae20a49d5c18b9233335e');
stats.free();
diff.free();