diff options
author | Patrick Steinhardt <ps@pks.im> | 2017-06-16 13:34:43 +0200 |
---|---|---|
committer | Patrick Steinhardt <ps@pks.im> | 2017-06-26 15:39:26 +0200 |
commit | 89a3482829c77590b3cc4fe95a33b93eebaecff5 (patch) | |
tree | b2c599e261657d61b3a3d182fdd3d84dc0491c0f /include/git2/diff.h | |
parent | fa94875295bbd7a4afa0f7724e77dcff8cd3adab (diff) | |
download | libgit2-89a3482829c77590b3cc4fe95a33b93eebaecff5.tar.gz |
diff: implement function to calculate patch ID
The upstream git project provides the ability to calculate a so-called
patch ID. Quoting from git-patch-id(1):
A "patch ID" is nothing but a sum of SHA-1 of the file diffs
associated with a patch, with whitespace and line numbers ignored."
Patch IDs can be used to identify two patches which are probably the
same thing, e.g. when a patch has been cherry-picked to another branch.
This commit implements a new function `git_diff_patchid`, which gets a
patch and derives an OID from the diff. Note the different terminology
here: a patch in libgit2 are the differences in a single file and a diff
can contain multiple patches for different files. The implementation
matches the upstream implementation and should derive the same OID for
the same diff. In fact, some code has been directly derived from the
upstream implementation.
The upstream implementation has two different modes to calculate patch
IDs, which is the stable and unstable mode. The old way of calculating
the patch IDs was unstable in a sense that a different ordering the
diffs was leading to different results. This oversight was fixed in git
1.9, but as git tries hard to never break existing workflows, the old
and unstable way is still default. The newer and stable way does not
care for ordering of the diff hunks, and in fact it is the mode that
should probably be used today. So right now, we only implement the
stable way of generating the patch ID.
Diffstat (limited to 'include/git2/diff.h')
-rw-r--r-- | include/git2/diff.h | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/include/git2/diff.h b/include/git2/diff.h index 4f0871dab..40536cb8b 100644 --- a/include/git2/diff.h +++ b/include/git2/diff.h @@ -1400,6 +1400,51 @@ GIT_EXTERN(int) git_diff_format_email_init_options( git_diff_format_email_options *opts, unsigned int version); +/** + * Patch ID options structure + * + * Initialize with `GIT_DIFF_PATCHID_OPTIONS_INIT` macro to + * correctly set the default values and version. + */ +typedef struct git_diff_patchid_options { + unsigned int version; +} git_diff_patchid_options; + +#define GIT_DIFF_PATCHID_OPTIONS_VERSION 1 +#define GIT_DIFF_PATCHID_OPTIONS_INIT { GIT_DIFF_PATCHID_OPTIONS_VERSION } + +/** + * Initialize `git_diff_patchid_options` structure. + * + * Initializes the structure with default values. Equivalent to + * creating an instance with `GIT_DIFF_PATCHID_OPTIONS_INIT`. + */ +GIT_EXTERN(int) git_diff_patchid_init_options( + git_diff_patchid_options *opts, + unsigned int version); + +/** + * 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. + * + * @param out Pointer where the calculated patch ID shoul be + * stored + * @param diff The diff to calculate the ID for + * @param opts Options for how to calculate the patch ID. This is + * intended for future changes, as currently no options are + * available. + * @return 0 on success, an error code otherwise. + */ +GIT_EXTERN(int) git_diff_patchid(git_oid *out, git_diff *diff, git_diff_patchid_options *opts); + GIT_END_DECL /** @} */ |