From 3ff1d123736e5686fb9ec16e65828d5b8ffa2b30 Mon Sep 17 00:00:00 2001 From: Russell Belfer Date: Fri, 11 Oct 2013 14:51:54 -0700 Subject: [PATCH 1/7] Rename diff objects and split patch.h This makes no functional change to diff but renames a couple of the objects and splits the new git_patch (formerly git_diff_patch) into a new header file. --- examples/diff.c | 10 +- examples/log.c | 10 +- include/git2.h | 1 + include/git2/diff.h | 380 ++++++--------------------------- include/git2/patch.h | 270 +++++++++++++++++++++++ include/git2/pathspec.h | 2 +- src/checkout.c | 4 +- src/diff.c | 64 +++--- src/diff.h | 10 +- src/diff_file.c | 2 +- src/diff_file.h | 2 +- src/diff_patch.c | 156 +++++++------- src/diff_patch.h | 19 +- src/diff_print.c | 50 ++--- src/diff_tform.c | 22 +- src/diff_xdiff.c | 40 ++-- src/pathspec.c | 2 +- src/reset.c | 4 +- src/stash.c | 10 +- src/status.c | 6 +- src/status.h | 4 +- src/submodule.c | 6 +- tests-clar/diff/blob.c | 222 +++++++++---------- tests-clar/diff/diff_helpers.c | 36 ++-- tests-clar/diff/diff_helpers.h | 12 +- tests-clar/diff/diffiter.c | 110 +++++----- tests-clar/diff/drivers.c | 58 ++--- tests-clar/diff/index.c | 12 +- tests-clar/diff/notify.c | 16 +- tests-clar/diff/patch.c | 146 ++++++------- tests-clar/diff/pathspec.c | 8 +- tests-clar/diff/rename.c | 108 +++++----- tests-clar/diff/submodules.c | 84 ++++---- tests-clar/diff/tree.c | 38 ++-- tests-clar/diff/workdir.c | 150 ++++++------- tests-clar/stress/diff.c | 6 +- 36 files changed, 1057 insertions(+), 1023 deletions(-) create mode 100644 include/git2/patch.h diff --git a/examples/diff.c b/examples/diff.c index 11efa21ec..3200b53c8 100644 --- a/examples/diff.c +++ b/examples/diff.c @@ -45,7 +45,7 @@ char *colors[] = { static int printer( const git_diff_delta *delta, - const git_diff_range *range, + const git_diff_hunk *range, char usage, const char *line, size_t line_len, @@ -126,7 +126,7 @@ int main(int argc, char *argv[]) git_tree *t1 = NULL, *t2 = NULL; git_diff_options opts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT; - git_diff_list *diff; + git_diff *diff; int i, color = -1, format = FORMAT_PATCH, cached = 0; char *a, *treeish1 = NULL, *treeish2 = NULL; const char *dir = "."; @@ -218,11 +218,11 @@ int main(int argc, char *argv[]) else if (t1 && cached) check(git_diff_tree_to_index(&diff, repo, t1, NULL, &opts), "Diff"); else if (t1) { - git_diff_list *diff2; + git_diff *diff2; check(git_diff_tree_to_index(&diff, repo, t1, NULL, &opts), "Diff"); check(git_diff_index_to_workdir(&diff2, repo, NULL, &opts), "Diff"); check(git_diff_merge(diff, diff2), "Merge diffs"); - git_diff_list_free(diff2); + git_diff_free(diff2); } else if (cached) { check(resolve_to_tree(repo, "HEAD", &t1), "looking up HEAD"); @@ -253,7 +253,7 @@ int main(int argc, char *argv[]) if (color >= 0) fputs(colors[0], stdout); - git_diff_list_free(diff); + git_diff_free(diff); git_tree_free(t1); git_tree_free(t2); git_repository_free(repo); diff --git a/examples/log.c b/examples/log.c index 413c211e4..2ba5aa52a 100644 --- a/examples/log.c +++ b/examples/log.c @@ -184,7 +184,7 @@ static void print_commit(git_commit *commit) static int print_diff( const git_diff_delta *delta, - const git_diff_range *range, + const git_diff_hunk *range, char usage, const char *line, size_t line_len, @@ -218,7 +218,7 @@ static int match_with_parent( { git_commit *parent; git_tree *a, *b; - git_diff_list *diff; + git_diff *diff; int ndeltas; check(git_commit_parent(&parent, commit, (size_t)i), "Get parent", NULL); @@ -229,7 +229,7 @@ static int match_with_parent( ndeltas = (int)git_diff_num_deltas(diff); - git_diff_list_free(diff); + git_diff_free(diff); git_tree_free(a); git_tree_free(b); git_commit_free(parent); @@ -373,7 +373,7 @@ int main(int argc, char *argv[]) if (opt.show_diff) { git_tree *a = NULL, *b = NULL; - git_diff_list *diff = NULL; + git_diff *diff = NULL; if (parents > 1) continue; @@ -391,7 +391,7 @@ int main(int argc, char *argv[]) check(git_diff_print_patch(diff, print_diff, NULL), "Displaying diff", NULL); - git_diff_list_free(diff); + git_diff_free(diff); git_tree_free(a); git_tree_free(b); } diff --git a/include/git2.h b/include/git2.h index 93049aad9..b90673920 100644 --- a/include/git2.h +++ b/include/git2.h @@ -32,6 +32,7 @@ #include "git2/odb.h" #include "git2/oid.h" #include "git2/pack.h" +#include "git2/patch.h" #include "git2/pathspec.h" #include "git2/push.h" #include "git2/refdb.h" diff --git a/include/git2/diff.h b/include/git2/diff.h index 596098574..11c3bce20 100644 --- a/include/git2/diff.h +++ b/include/git2/diff.h @@ -20,34 +20,38 @@ * Overview * -------- * - * Calculating diffs is generally done in two phases: building a diff list - * then traversing the diff list. This makes is easier to share logic - * across the various types of diffs (tree vs tree, workdir vs index, etc.), - * and also allows you to insert optional diff list post-processing phases, - * such as rename detected, in between the steps. When you are done with a - * diff list object, it must be freed. + * Calculating diffs is generally done in two phases: building a list of + * diffs then traversing it. This makes is easier to share logic across + * the various types of diffs (tree vs tree, workdir vs index, etc.), and + * also allows you to insert optional diff list post-processing phases, + * such as rename detection, in between the steps. When you are done with + * a diff object, it must be freed. * * Terminology * ----------- * * To understand the diff APIs, you should know the following terms: * - * - A `diff` or `diff list` represents the cumulative list of differences - * between two snapshots of a repository (possibly filtered by a set of - * file name patterns). This is the `git_diff_list` object. + * - A `diff` represents the cumulative list of differences between two + * snapshots of a repository (possibly filtered by a set of file name + * patterns). This is the `git_diff` object. + * * - A `delta` is a file pair with an old and new revision. The old version * may be absent if the file was just created and the new version may be * absent if the file was deleted. A diff is mostly just a list of deltas. + * * - A `binary` file / delta is a file (or pair) for which no text diffs - * should be generated. A diff list can contain delta entries that are + * should be generated. A diff can contain delta entries that are * binary, but no diff content will be output for those files. There is * a base heuristic for binary detection and you can further tune the * behavior with git attributes or diff flags and option settings. + * * - A `hunk` is a span of modified lines in a delta along with some stable * surrounding context. You can configure the amount of context and other * properties of how hunks are generated. Each hunk also comes with a * header that described where it starts and ends in both the old and new * versions in the delta. + * * - A `line` is a range of characters inside a hunk. It could be a context * line (i.e. in both old and new versions), an added line (i.e. only in * the new version), or a removed line (i.e. only in the old version). @@ -154,14 +158,14 @@ typedef enum { } git_diff_option_t; /** - * The diff list object that contains all individual file deltas. + * The diff object that contains all individual file deltas. * * This is an opaque structure which will be allocated by one of the diff * generator functions below (such as `git_diff_tree_to_tree`). You are * responsible for releasing the object memory when done, using the - * `git_diff_list_free()` function. + * `git_diff_free()` function. */ -typedef struct git_diff_list git_diff_list; +typedef struct git_diff git_diff; /** * Flags for the delta object and the file objects on each side. @@ -200,11 +204,11 @@ typedef enum { } git_delta_t; /** - * Description of one side of a diff entry. + * Description of one side of a delta. * - * Although this is called a "file", it may actually represent a file, a - * symbolic link, a submodule commit id, or even a tree (although that only - * if you are tracking type changes or ignored/untracked directories). + * Although this is called a "file", it could represent a file, a symbolic + * link, a submodule commit id, or even a tree (although that only if you + * are tracking type changes or ignored/untracked directories). * * The `oid` is the `git_oid` of the item. If the entry represents an * absent side of a diff (e.g. the `old_file` of a `GIT_DELTA_ADDED` delta), @@ -231,9 +235,8 @@ typedef struct { /** * Description of changes to one entry. * - * When iterating over a diff list object, this will be passed to most - * callback functions and you can use the contents to understand exactly - * what has changed. + * When iterating over a diff, this will be passed to most callbacks and + * you can use the contents to understand exactly what has changed. * * The `old_file` represents the "from" side of the diff and the `new_file` * represents to "to" side of the diff. What those means depend on the @@ -266,18 +269,18 @@ typedef struct { * the score (a la `printf("M%03d", 100 - delta->similarity)`). */ typedef struct { - git_diff_file old_file; - git_diff_file new_file; git_delta_t status; uint32_t similarity; /**< for RENAMED and COPIED, value 0-100 */ uint32_t flags; + git_diff_file old_file; + git_diff_file new_file; } git_diff_delta; /** * Diff notification callback function. * * The callback will be called for each file, just before the `git_delta_t` - * gets inserted into the diff list. + * gets inserted into the diff. * * When the callback: * - returns < 0, the diff process will be aborted. @@ -287,7 +290,7 @@ typedef struct { * continues. */ typedef int (*git_diff_notify_cb)( - const git_diff_list *diff_so_far, + const git_diff *diff_so_far, const git_diff_delta *delta_to_add, const char *matched_pathspec, void *payload); @@ -349,19 +352,20 @@ typedef int (*git_diff_file_cb)( /** * Structure describing a hunk of a diff. */ -typedef struct { +typedef struct git_diff_hunk git_diff_hunk; +struct git_diff_hunk { int old_start; /** Starting line number in old_file */ int old_lines; /** Number of lines in old_file */ int new_start; /** Starting line number in new_file */ int new_lines; /** Number of lines in new_file */ -} git_diff_range; +}; /** * When iterating over a diff, callback that will be made per hunk. */ typedef int (*git_diff_hunk_cb)( const git_diff_delta *delta, - const git_diff_range *range, + const git_diff_hunk *hunk, const char *header, size_t header_len, void *payload); @@ -370,13 +374,13 @@ typedef int (*git_diff_hunk_cb)( * Line origin constants. * * These values describe where a line came from and will be passed to - * the git_diff_data_cb when iterating over a diff. There are some + * the git_diff_line_cb when iterating over a diff. There are some * special origin constants at the end that are used for the text * output callbacks to demarcate lines that are actually part of * the file or hunk headers. */ typedef enum { - /* These values will be sent to `git_diff_data_cb` along with the line */ + /* These values will be sent to `git_diff_line_cb` along with the line */ GIT_DIFF_LINE_CONTEXT = ' ', GIT_DIFF_LINE_ADDITION = '+', GIT_DIFF_LINE_DELETION = '-', @@ -385,7 +389,7 @@ typedef enum { GIT_DIFF_LINE_ADD_EOFNL = '>', /**< Old has no LF at end, new does */ GIT_DIFF_LINE_DEL_EOFNL = '<', /**< Old has LF at end, new does not */ - /* The following values will only be sent to a `git_diff_data_cb` when + /* The following values will only be sent to a `git_diff_line_cb` when * the content of a diff is being formatted (eg. through * git_diff_print_patch() or git_diff_print_compact(), for instance). */ @@ -402,22 +406,14 @@ typedef enum { * of text. This uses some extra GIT_DIFF_LINE_... constants for output * of lines of file and hunk headers. */ -typedef int (*git_diff_data_cb)( +typedef int (*git_diff_line_cb)( const git_diff_delta *delta, /** delta that contains this data */ - const git_diff_range *range, /** range of lines containing this data */ - char line_origin, /** git_diff_list_t value from above */ + const git_diff_hunk *hunk, /** range of lines containing this data */ + char line_origin, /** git_diff_t value from above */ const char *content, /** diff data - not NUL terminated */ size_t content_len, /** number of bytes of diff data */ void *payload); /** user reference data */ -/** - * The diff patch is used to store all the text diffs for a delta. - * - * You can easily loop over the content of patches and get information about - * them. - */ -typedef struct git_diff_patch git_diff_patch; - /** * Flags to control the behavior of diff rename/copy detection. */ @@ -524,7 +520,7 @@ typedef struct { /** @name Diff List Generator Functions * * These are the functions you would use to create (or destroy) a - * git_diff_list from various objects in a repository. + * git_diff from various objects in a repository. */ /**@{*/ @@ -533,7 +529,7 @@ typedef struct { * * @param diff The previously created diff list; cannot be used after free. */ -GIT_EXTERN(void) git_diff_list_free(git_diff_list *diff); +GIT_EXTERN(void) git_diff_free(git_diff *diff); /** * Create a diff list with the difference between two tree objects. @@ -545,14 +541,14 @@ GIT_EXTERN(void) git_diff_list_free(git_diff_list *diff); * pass NULL to indicate an empty tree, although it is an error to pass * NULL for both the `old_tree` and `new_tree`. * - * @param diff Output pointer to a git_diff_list pointer to be allocated. + * @param diff Output pointer to a git_diff pointer to be allocated. * @param repo The repository containing the trees. * @param old_tree A git_tree object to diff from, or NULL for empty tree. * @param new_tree A git_tree object to diff to, or NULL for empty tree. * @param opts Structure with options to influence diff or NULL for defaults. */ GIT_EXTERN(int) git_diff_tree_to_tree( - git_diff_list **diff, + git_diff **diff, git_repository *repo, git_tree *old_tree, git_tree *new_tree, @@ -567,14 +563,14 @@ GIT_EXTERN(int) git_diff_tree_to_tree( * The tree you pass will be used for the "old_file" side of the delta, and * the index will be used for the "new_file" side of the delta. * - * @param diff Output pointer to a git_diff_list pointer to be allocated. + * @param diff Output pointer to a git_diff pointer to be allocated. * @param repo The repository containing the tree and index. * @param old_tree A git_tree object to diff from, or NULL for empty tree. * @param index The index to diff with; repo index used if NULL. * @param opts Structure with options to influence diff or NULL for defaults. */ GIT_EXTERN(int) git_diff_tree_to_index( - git_diff_list **diff, + git_diff **diff, git_repository *repo, git_tree *old_tree, git_index *index, @@ -591,13 +587,13 @@ GIT_EXTERN(int) git_diff_tree_to_index( * The index will be used for the "old_file" side of the delta, and the * working directory will be used for the "new_file" side of the delta. * - * @param diff Output pointer to a git_diff_list pointer to be allocated. + * @param diff Output pointer to a git_diff pointer to be allocated. * @param repo The repository. * @param index The index to diff from; repo index used if NULL. * @param opts Structure with options to influence diff or NULL for defaults. */ GIT_EXTERN(int) git_diff_index_to_workdir( - git_diff_list **diff, + git_diff **diff, git_repository *repo, git_index *index, const git_diff_options *opts); /**< can be NULL for defaults */ @@ -619,20 +615,20 @@ GIT_EXTERN(int) git_diff_index_to_workdir( * * To emulate `git diff `, call both `git_diff_tree_to_index` and * `git_diff_index_to_workdir`, then call `git_diff_merge` on the results. - * That will yield a `git_diff_list` that matches the git output. + * That will yield a `git_diff` that matches the git output. * * If this seems confusing, take the case of a file with a staged deletion * where the file has then been put back into the working dir and modified. * The tree-to-workdir diff for that file is 'modified', but core git would * show status 'deleted' since there is a pending deletion in the index. * - * @param diff A pointer to a git_diff_list pointer that will be allocated. + * @param diff A pointer to a git_diff pointer that will be allocated. * @param repo The repository containing the tree. * @param old_tree A git_tree object to diff from, or NULL for empty tree. * @param opts Structure with options to influence diff or NULL for defaults. */ GIT_EXTERN(int) git_diff_tree_to_workdir( - git_diff_list **diff, + git_diff **diff, git_repository *repo, git_tree *old_tree, const git_diff_options *opts); /**< can be NULL for defaults */ @@ -651,8 +647,8 @@ GIT_EXTERN(int) git_diff_tree_to_workdir( * @param from Diff to merge. */ GIT_EXTERN(int) git_diff_merge( - git_diff_list *onto, - const git_diff_list *from); + git_diff *onto, + const git_diff *from); /** * Transform a diff list marking file renames, copies, etc. @@ -667,7 +663,7 @@ GIT_EXTERN(int) git_diff_merge( * @return 0 on success, -1 on failure */ GIT_EXTERN(int) git_diff_find_similar( - git_diff_list *diff, + git_diff *diff, git_diff_find_options *options); /**@}*/ @@ -694,7 +690,7 @@ GIT_EXTERN(int) git_diff_find_similar( * Returning a non-zero value from any of the callbacks will terminate * the iteration and cause this return `GIT_EUSER`. * - * @param diff A git_diff_list generated by one of the above functions. + * @param diff A git_diff generated by one of the above functions. * @param file_cb Callback function to make per file in the diff. * @param hunk_cb Optional callback to make per hunk of text diff. This * callback is called to describe a range of lines in the @@ -706,10 +702,10 @@ GIT_EXTERN(int) git_diff_find_similar( * @return 0 on success, GIT_EUSER on non-zero callback, or error code */ GIT_EXTERN(int) git_diff_foreach( - git_diff_list *diff, + git_diff *diff, git_diff_file_cb file_cb, git_diff_hunk_cb hunk_cb, - git_diff_data_cb line_cb, + git_diff_line_cb line_cb, void *payload); /** @@ -718,14 +714,14 @@ GIT_EXTERN(int) git_diff_foreach( * Returning a non-zero value from the callbacks will terminate the * iteration and cause this return `GIT_EUSER`. * - * @param diff A git_diff_list generated by one of the above functions. + * @param diff A git_diff generated by one of the above functions. * @param print_cb Callback to make per line of diff text. * @param payload Reference pointer that will be passed to your callback. * @return 0 on success, GIT_EUSER on non-zero callback, or error code */ GIT_EXTERN(int) git_diff_print_compact( - git_diff_list *diff, - git_diff_data_cb print_cb, + git_diff *diff, + git_diff_line_cb print_cb, void *payload); /** @@ -734,14 +730,14 @@ GIT_EXTERN(int) git_diff_print_compact( * Returning a non-zero value from the callbacks will terminate the * iteration and cause this return `GIT_EUSER`. * - * @param diff A git_diff_list generated by one of the above functions. + * @param diff A git_diff generated by one of the above functions. * @param print_cb Callback to make per line of diff text. * @param payload Reference pointer that will be passed to your callback. * @return 0 on success, GIT_EUSER on non-zero callback, or error code */ GIT_EXTERN(int) git_diff_print_raw( - git_diff_list *diff, - git_diff_data_cb print_cb, + git_diff *diff, + git_diff_line_cb print_cb, void *payload); /** @@ -766,7 +762,7 @@ GIT_EXTERN(char) git_diff_status_char(git_delta_t status); * Returning a non-zero value from the callbacks will terminate the * iteration and cause this return `GIT_EUSER`. * - * @param diff A git_diff_list generated by one of the above functions. + * @param diff A git_diff generated by one of the above functions. * @param payload Reference pointer that will be passed to your callbacks. * @param print_cb Callback function to output lines of the diff. This * same function will be called for file headers, hunk @@ -776,17 +772,17 @@ GIT_EXTERN(char) git_diff_status_char(git_delta_t status); * @return 0 on success, GIT_EUSER on non-zero callback, or error code */ GIT_EXTERN(int) git_diff_print_patch( - git_diff_list *diff, - git_diff_data_cb print_cb, + git_diff *diff, + git_diff_line_cb print_cb, void *payload); /** * Query how many diff records are there in a diff list. * - * @param diff A git_diff_list generated by one of the above functions + * @param diff A git_diff generated by one of the above functions * @return Count of number of deltas in the list */ -GIT_EXTERN(size_t) git_diff_num_deltas(git_diff_list *diff); +GIT_EXTERN(size_t) git_diff_num_deltas(git_diff *diff); /** * Query how many diff deltas are there in a diff list filtered by type. @@ -795,12 +791,12 @@ GIT_EXTERN(size_t) git_diff_num_deltas(git_diff_list *diff); * that is a `git_delta_t` and returns just the count of how many deltas * match that particular type. * - * @param diff A git_diff_list generated by one of the above functions + * @param diff A git_diff generated by one of the above functions * @param type A git_delta_t value to filter the count * @return Count of number of deltas matching delta_t type */ GIT_EXTERN(size_t) git_diff_num_deltas_of_type( - git_diff_list *diff, + git_diff *diff, git_delta_t type); /** @@ -809,191 +805,7 @@ GIT_EXTERN(size_t) git_diff_num_deltas_of_type( * @param diff Diff list to check * @return 0 if case sensitive, 1 if case is ignored */ -GIT_EXTERN(int) git_diff_is_sorted_icase(const git_diff_list *diff); - -/** - * Return the diff delta and patch for an entry in the diff list. - * - * The `git_diff_patch` is a newly created object contains the text diffs - * for the delta. You have to call `git_diff_patch_free()` when you are - * done with it. You can use the patch object to loop over all the hunks - * and lines in the diff of the one delta. - * - * For an unchanged file or a binary file, no `git_diff_patch` will be - * created, the output will be set to NULL, and the `binary` flag will be - * set true in the `git_diff_delta` structure. - * - * The `git_diff_delta` pointer points to internal data and you do not have - * to release it when you are done with it. It will go away when the - * `git_diff_list` and `git_diff_patch` go away. - * - * It is okay to pass NULL for either of the output parameters; if you pass - * NULL for the `git_diff_patch`, then the text diff will not be calculated. - * - * @param patch_out Output parameter for the delta patch object - * @param delta_out Output parameter for the delta object - * @param diff Diff list object - * @param idx Index into diff list - * @return 0 on success, other value < 0 on error - */ -GIT_EXTERN(int) git_diff_get_patch( - git_diff_patch **patch_out, - const git_diff_delta **delta_out, - git_diff_list *diff, - size_t idx); - -/** - * Free a git_diff_patch object. - */ -GIT_EXTERN(void) git_diff_patch_free( - git_diff_patch *patch); - -/** - * Get the delta associated with a patch - */ -GIT_EXTERN(const git_diff_delta *) git_diff_patch_delta( - git_diff_patch *patch); - -/** - * Get the number of hunks in a patch - */ -GIT_EXTERN(size_t) git_diff_patch_num_hunks( - git_diff_patch *patch); - -/** - * Get line counts of each type in a patch. - * - * This helps imitate a diff --numstat type of output. For that purpose, - * you only need the `total_additions` and `total_deletions` values, but we - * include the `total_context` line count in case you want the total number - * of lines of diff output that will be generated. - * - * All outputs are optional. Pass NULL if you don't need a particular count. - * - * @param total_context Count of context lines in output, can be NULL. - * @param total_additions Count of addition lines in output, can be NULL. - * @param total_deletions Count of deletion lines in output, can be NULL. - * @param patch The git_diff_patch object - * @return 0 on success, <0 on error - */ -GIT_EXTERN(int) git_diff_patch_line_stats( - size_t *total_context, - size_t *total_additions, - size_t *total_deletions, - const git_diff_patch *patch); - -/** - * Get the information about a hunk in a patch - * - * Given a patch and a hunk index into the patch, this returns detailed - * information about that hunk. Any of the output pointers can be passed - * as NULL if you don't care about that particular piece of information. - * - * @param range Output pointer to git_diff_range of hunk - * @param header Output pointer to header string for hunk. Unlike the - * content pointer for each line, this will be NUL-terminated - * @param header_len Output value of characters in header string - * @param lines_in_hunk Output count of total lines in this hunk - * @param patch Input pointer to patch object - * @param hunk_idx Input index of hunk to get information about - * @return 0 on success, GIT_ENOTFOUND if hunk_idx out of range, <0 on error - */ -GIT_EXTERN(int) git_diff_patch_get_hunk( - const git_diff_range **range, - const char **header, - size_t *header_len, - size_t *lines_in_hunk, - git_diff_patch *patch, - size_t hunk_idx); - -/** - * Get the number of lines in a hunk. - * - * @param patch The git_diff_patch object - * @param hunk_idx Index of the hunk - * @return Number of lines in hunk or -1 if invalid hunk index - */ -GIT_EXTERN(int) git_diff_patch_num_lines_in_hunk( - git_diff_patch *patch, - size_t hunk_idx); - -/** - * Get data about a line in a hunk of a patch. - * - * Given a patch, a hunk index, and a line index in the hunk, this - * will return a lot of details about that line. If you pass a hunk - * index larger than the number of hunks or a line index larger than - * the number of lines in the hunk, this will return -1. - * - * @param line_origin A GIT_DIFF_LINE constant from above - * @param content Pointer to content of diff line, not NUL-terminated - * @param content_len Number of characters in content - * @param old_lineno Line number in old file or -1 if line is added - * @param new_lineno Line number in new file or -1 if line is deleted - * @param patch The patch to look in - * @param hunk_idx The index of the hunk - * @param line_of_hunk The index of the line in the hunk - * @return 0 on success, <0 on failure - */ -GIT_EXTERN(int) git_diff_patch_get_line_in_hunk( - char *line_origin, - const char **content, - size_t *content_len, - int *old_lineno, - int *new_lineno, - git_diff_patch *patch, - size_t hunk_idx, - size_t line_of_hunk); - -/** - * Look up size of patch diff data in bytes - * - * This returns the raw size of the patch data. This only includes the - * actual data from the lines of the diff, not the file or hunk headers. - * - * If you pass `include_context` as true (non-zero), this will be the size - * of all of the diff output; if you pass it as false (zero), this will - * only include the actual changed lines (as if `context_lines` was 0). - * - * @param patch A git_diff_patch representing changes to one file - * @param include_context Include context lines in size if non-zero - * @param include_hunk_headers Include hunk header lines if non-zero - * @param include_file_headers Include file header lines if non-zero - * @return The number of bytes of data - */ -GIT_EXTERN(size_t) git_diff_patch_size( - git_diff_patch *patch, - int include_context, - int include_hunk_headers, - int include_file_headers); - -/** - * Serialize the patch to text via callback. - * - * Returning a non-zero value from the callback will terminate the iteration - * and cause this return `GIT_EUSER`. - * - * @param patch A git_diff_patch representing changes to one file - * @param print_cb Callback function to output lines of the patch. Will be - * called for file headers, hunk headers, and diff lines. - * @param payload Reference pointer that will be passed to your callbacks. - * @return 0 on success, GIT_EUSER on non-zero callback, or error code - */ -GIT_EXTERN(int) git_diff_patch_print( - git_diff_patch *patch, - git_diff_data_cb print_cb, - void *payload); - -/** - * Get the content of a patch as a single diff text. - * - * @param string Allocated string; caller must free. - * @param patch A git_diff_patch representing changes to one file - * @return 0 on success, <0 on failure. - */ -GIT_EXTERN(int) git_diff_patch_to_str( - char **string, - git_diff_patch *patch); +GIT_EXTERN(int) git_diff_is_sorted_icase(const git_diff *diff); /**@}*/ @@ -1037,33 +849,9 @@ GIT_EXTERN(int) git_diff_blobs( const git_diff_options *options, git_diff_file_cb file_cb, git_diff_hunk_cb hunk_cb, - git_diff_data_cb line_cb, + git_diff_line_cb line_cb, void *payload); -/** - * Directly generate a patch from the difference between two blobs. - * - * This is just like `git_diff_blobs()` except it generates a patch object - * for the difference instead of directly making callbacks. You can use the - * standard `git_diff_patch` accessor functions to read the patch data, and - * you must call `git_diff_patch_free()` on the patch when done. - * - * @param out The generated patch; NULL on error - * @param old_blob Blob for old side of diff, or NULL for empty blob - * @param old_as_path Treat old blob as if it had this filename; can be NULL - * @param new_blob Blob for new side of diff, or NULL for empty blob - * @param new_as_path Treat new blob as if it had this filename; can be NULL - * @param opts Options for diff, or NULL for default options - * @return 0 on success or error code < 0 - */ -GIT_EXTERN(int) git_diff_patch_from_blobs( - git_diff_patch **out, - const git_blob *old_blob, - const char *old_as_path, - const git_blob *new_blob, - const char *new_as_path, - const git_diff_options *opts); - /** * Directly run a diff between a blob and a buffer. * @@ -1084,7 +872,7 @@ GIT_EXTERN(int) git_diff_patch_from_blobs( * @param options Options for diff, or NULL for default options * @param file_cb Callback for "file"; made once if there is a diff; can be NULL * @param hunk_cb Callback for each hunk in diff; can be NULL - * @param data_cb Callback for each line in diff; can be NULL + * @param line_cb Callback for each line in diff; can be NULL * @param payload Payload passed to each callback function * @return 0 on success, GIT_EUSER on non-zero callback return, or error code */ @@ -1097,35 +885,9 @@ GIT_EXTERN(int) git_diff_blob_to_buffer( const git_diff_options *options, git_diff_file_cb file_cb, git_diff_hunk_cb hunk_cb, - git_diff_data_cb data_cb, + git_diff_line_cb line_cb, void *payload); -/** - * Directly generate a patch from the difference between a blob and a buffer. - * - * This is just like `git_diff_blob_to_buffer()` except it generates a patch - * object for the difference instead of directly making callbacks. You can - * use the standard `git_diff_patch` accessor functions to read the patch - * data, and you must call `git_diff_patch_free()` on the patch when done. - * - * @param out The generated patch; NULL on error - * @param old_blob Blob for old side of diff, or NULL for empty blob - * @param old_as_path Treat old blob as if it had this filename; can be NULL - * @param buffer Raw data for new side of diff, or NULL for empty - * @param buffer_len Length of raw data for new side of diff - * @param buffer_as_path Treat buffer as if it had this filename; can be NULL - * @param opts Options for diff, or NULL for default options - * @return 0 on success or error code < 0 - */ -GIT_EXTERN(int) git_diff_patch_from_blob_and_buffer( - git_diff_patch **out, - const git_blob *old_blob, - const char *old_as_path, - const char *buffer, - size_t buffer_len, - const char *buffer_as_path, - const git_diff_options *opts); - GIT_END_DECL diff --git a/include/git2/patch.h b/include/git2/patch.h new file mode 100644 index 000000000..ad9be7516 --- /dev/null +++ b/include/git2/patch.h @@ -0,0 +1,270 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_git_patch_h__ +#define INCLUDE_git_patch_h__ + +#include "common.h" +#include "types.h" +#include "oid.h" +#include "diff.h" + +/** + * @file git2/patch.h + * @brief Patch handling routines. + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * The diff patch is used to store all the text diffs for a delta. + * + * You can easily loop over the content of patches and get information about + * them. + */ +typedef struct git_patch git_patch; + +/** + * Return the diff delta and patch for an entry in the diff list. + * + * The `git_patch` is a newly created object contains the text diffs + * for the delta. You have to call `git_patch_free()` when you are + * done with it. You can use the patch object to loop over all the hunks + * and lines in the diff of the one delta. + * + * For an unchanged file or a binary file, no `git_patch` will be + * created, the output will be set to NULL, and the `binary` flag will be + * set true in the `git_diff_delta` structure. + * + * The `git_diff_delta` pointer points to internal data and you do not have + * to release it when you are done with it. It will go away when the + * `git_diff` and `git_patch` go away. + * + * It is okay to pass NULL for either of the output parameters; if you pass + * NULL for the `git_patch`, then the text diff will not be calculated. + * + * @param patch_out Output parameter for the delta patch object + * @param delta_out Output parameter for the delta object + * @param diff Diff list object + * @param idx Index into diff list + * @return 0 on success, other value < 0 on error + */ +GIT_EXTERN(int) git_patch_from_diff( + git_patch **patch_out, + const git_diff_delta **delta_out, + git_diff *diff, + size_t idx); + +/** + * Directly generate a patch from the difference between two blobs. + * + * This is just like `git_diff_blobs()` except it generates a patch object + * for the difference instead of directly making callbacks. You can use the + * standard `git_patch` accessor functions to read the patch data, and + * you must call `git_patch_free()` on the patch when done. + * + * @param out The generated patch; NULL on error + * @param old_blob Blob for old side of diff, or NULL for empty blob + * @param old_as_path Treat old blob as if it had this filename; can be NULL + * @param new_blob Blob for new side of diff, or NULL for empty blob + * @param new_as_path Treat new blob as if it had this filename; can be NULL + * @param opts Options for diff, or NULL for default options + * @return 0 on success or error code < 0 + */ +GIT_EXTERN(int) git_patch_from_blobs( + git_patch **out, + const git_blob *old_blob, + const char *old_as_path, + const git_blob *new_blob, + const char *new_as_path, + const git_diff_options *opts); + +/** + * Directly generate a patch from the difference between a blob and a buffer. + * + * This is just like `git_diff_blob_to_buffer()` except it generates a patch + * object for the difference instead of directly making callbacks. You can + * use the standard `git_patch` accessor functions to read the patch + * data, and you must call `git_patch_free()` on the patch when done. + * + * @param out The generated patch; NULL on error + * @param old_blob Blob for old side of diff, or NULL for empty blob + * @param old_as_path Treat old blob as if it had this filename; can be NULL + * @param buffer Raw data for new side of diff, or NULL for empty + * @param buffer_len Length of raw data for new side of diff + * @param buffer_as_path Treat buffer as if it had this filename; can be NULL + * @param opts Options for diff, or NULL for default options + * @return 0 on success or error code < 0 + */ +GIT_EXTERN(int) git_patch_from_blob_and_buffer( + git_patch **out, + const git_blob *old_blob, + const char *old_as_path, + const char *buffer, + size_t buffer_len, + const char *buffer_as_path, + const git_diff_options *opts); + +/** + * Free a git_patch object. + */ +GIT_EXTERN(void) git_patch_free( + git_patch *patch); + +/** + * Get the delta associated with a patch + */ +GIT_EXTERN(const git_diff_delta *) git_patch_delta( + git_patch *patch); + +/** + * Get the number of hunks in a patch + */ +GIT_EXTERN(size_t) git_patch_num_hunks( + git_patch *patch); + +/** + * Get line counts of each type in a patch. + * + * This helps imitate a diff --numstat type of output. For that purpose, + * you only need the `total_additions` and `total_deletions` values, but we + * include the `total_context` line count in case you want the total number + * of lines of diff output that will be generated. + * + * All outputs are optional. Pass NULL if you don't need a particular count. + * + * @param total_context Count of context lines in output, can be NULL. + * @param total_additions Count of addition lines in output, can be NULL. + * @param total_deletions Count of deletion lines in output, can be NULL. + * @param patch The git_patch object + * @return 0 on success, <0 on error + */ +GIT_EXTERN(int) git_patch_line_stats( + size_t *total_context, + size_t *total_additions, + size_t *total_deletions, + const git_patch *patch); + +/** + * Get the information about a hunk in a patch + * + * Given a patch and a hunk index into the patch, this returns detailed + * information about that hunk. Any of the output pointers can be passed + * as NULL if you don't care about that particular piece of information. + * + * @param out Output pointer to git_diff_hunk of hunk + * @param header Output pointer to header string for hunk. Unlike the + * content pointer for each line, this will be NUL-terminated + * @param header_len Output value of characters in header string + * @param lines_in_hunk Output count of total lines in this hunk + * @param patch Input pointer to patch object + * @param hunk_idx Input index of hunk to get information about + * @return 0 on success, GIT_ENOTFOUND if hunk_idx out of range, <0 on error + */ +GIT_EXTERN(int) git_patch_get_hunk( + const git_diff_hunk **out, + const char **header, + size_t *header_len, + size_t *lines_in_hunk, + git_patch *patch, + size_t hunk_idx); + +/** + * Get the number of lines in a hunk. + * + * @param patch The git_patch object + * @param hunk_idx Index of the hunk + * @return Number of lines in hunk or -1 if invalid hunk index + */ +GIT_EXTERN(int) git_patch_num_lines_in_hunk( + git_patch *patch, + size_t hunk_idx); + +/** + * Get data about a line in a hunk of a patch. + * + * Given a patch, a hunk index, and a line index in the hunk, this + * will return a lot of details about that line. If you pass a hunk + * index larger than the number of hunks or a line index larger than + * the number of lines in the hunk, this will return -1. + * + * @param line_origin A GIT_DIFF_LINE constant from above + * @param content Pointer to content of diff line, not NUL-terminated + * @param content_len Number of characters in content + * @param old_lineno Line number in old file or -1 if line is added + * @param new_lineno Line number in new file or -1 if line is deleted + * @param patch The patch to look in + * @param hunk_idx The index of the hunk + * @param line_of_hunk The index of the line in the hunk + * @return 0 on success, <0 on failure + */ +GIT_EXTERN(int) git_patch_get_line_in_hunk( + char *line_origin, + const char **content, + size_t *content_len, + int *old_lineno, + int *new_lineno, + git_patch *patch, + size_t hunk_idx, + size_t line_of_hunk); + +/** + * Look up size of patch diff data in bytes + * + * This returns the raw size of the patch data. This only includes the + * actual data from the lines of the diff, not the file or hunk headers. + * + * If you pass `include_context` as true (non-zero), this will be the size + * of all of the diff output; if you pass it as false (zero), this will + * only include the actual changed lines (as if `context_lines` was 0). + * + * @param patch A git_patch representing changes to one file + * @param include_context Include context lines in size if non-zero + * @param include_hunk_headers Include hunk header lines if non-zero + * @param include_file_headers Include file header lines if non-zero + * @return The number of bytes of data + */ +GIT_EXTERN(size_t) git_patch_size( + git_patch *patch, + int include_context, + int include_hunk_headers, + int include_file_headers); + +/** + * Serialize the patch to text via callback. + * + * Returning a non-zero value from the callback will terminate the iteration + * and cause this return `GIT_EUSER`. + * + * @param patch A git_patch representing changes to one file + * @param print_cb Callback function to output lines of the patch. Will be + * called for file headers, hunk headers, and diff lines. + * @param payload Reference pointer that will be passed to your callbacks. + * @return 0 on success, GIT_EUSER on non-zero callback, or error code + */ +GIT_EXTERN(int) git_patch_print( + git_patch *patch, + git_diff_line_cb print_cb, + void *payload); + +/** + * Get the content of a patch as a single diff text. + * + * @param string Allocated string; caller must free. + * @param patch A git_patch representing changes to one file + * @return 0 on success, <0 on failure. + */ +GIT_EXTERN(int) git_patch_to_str( + char **string, + git_patch *patch); + + +GIT_END_DECL + +/**@}*/ + +#endif diff --git a/include/git2/pathspec.h b/include/git2/pathspec.h index a835f8e52..2fb0bb716 100644 --- a/include/git2/pathspec.h +++ b/include/git2/pathspec.h @@ -187,7 +187,7 @@ GIT_EXTERN(int) git_pathspec_match_tree( */ GIT_EXTERN(int) git_pathspec_match_diff( git_pathspec_match_list **out, - git_diff_list *diff, + git_diff *diff, uint32_t flags, git_pathspec *ps); diff --git a/src/checkout.c b/src/checkout.c index d3f673d40..6519ab550 100644 --- a/src/checkout.c +++ b/src/checkout.c @@ -43,7 +43,7 @@ enum { typedef struct { git_repository *repo; - git_diff_list *diff; + git_diff *diff; git_checkout_opts opts; bool opts_free_baseline; char *pfx; @@ -1320,7 +1320,7 @@ cleanup: (data.strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) == 0) error = git_index_write(data.index); - git_diff_list_free(data.diff); + git_diff_free(data.diff); git_iterator_free(workdir); git_iterator_free(baseline); git__free(actions); diff --git a/src/diff.c b/src/diff.c index 39facce60..1efde98e4 100644 --- a/src/diff.c +++ b/src/diff.c @@ -21,7 +21,7 @@ (VAL) ? ((DIFF)->opts.flags | (FLAG)) : ((DIFF)->opts.flags & ~(VAL)) static git_diff_delta *diff_delta__alloc( - git_diff_list *diff, + git_diff *diff, git_delta_t status, const char *path) { @@ -50,7 +50,7 @@ static git_diff_delta *diff_delta__alloc( } static int diff_notify( - const git_diff_list *diff, + const git_diff *diff, const git_diff_delta *delta, const char *matched_pathspec) { @@ -62,7 +62,7 @@ static int diff_notify( } static int diff_delta__from_one( - git_diff_list *diff, + git_diff *diff, git_delta_t status, const git_index_entry *entry) { @@ -120,7 +120,7 @@ static int diff_delta__from_one( } static int diff_delta__from_two( - git_diff_list *diff, + git_diff *diff, git_delta_t status, const git_index_entry *old_entry, uint32_t old_mode, @@ -181,7 +181,7 @@ static int diff_delta__from_two( } static git_diff_delta *diff_delta__last_for_item( - git_diff_list *diff, + git_diff *diff, const git_index_entry *item) { git_diff_delta *delta = git_vector_last(&diff->deltas); @@ -340,13 +340,13 @@ static const char *diff_mnemonic_prefix( return pfx; } -static git_diff_list *diff_list_alloc( +static git_diff *diff_list_alloc( git_repository *repo, git_iterator *old_iter, git_iterator *new_iter) { git_diff_options dflt = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = git__calloc(1, sizeof(git_diff_list)); + git_diff *diff = git__calloc(1, sizeof(git_diff)); if (!diff) return NULL; @@ -360,7 +360,7 @@ static git_diff_list *diff_list_alloc( if (git_vector_init(&diff->deltas, 0, git_diff_delta__cmp) < 0 || git_pool_init(&diff->pool, 1, 0) < 0) { - git_diff_list_free(diff); + git_diff_free(diff); return NULL; } @@ -389,7 +389,7 @@ static git_diff_list *diff_list_alloc( } static int diff_list_apply_options( - git_diff_list *diff, + git_diff *diff, const git_diff_options *opts) { git_config *cfg; @@ -490,7 +490,7 @@ static int diff_list_apply_options( return 0; } -static void diff_list_free(git_diff_list *diff) +static void diff_list_free(git_diff *diff) { git_diff_delta *delta; unsigned int i; @@ -508,7 +508,7 @@ static void diff_list_free(git_diff_list *diff) git__free(diff); } -void git_diff_list_free(git_diff_list *diff) +void git_diff_free(git_diff *diff) { if (!diff) return; @@ -516,7 +516,7 @@ void git_diff_list_free(git_diff_list *diff) GIT_REFCOUNT_DEC(diff, diff_list_free); } -void git_diff_list_addref(git_diff_list *diff) +void git_diff_addref(git_diff *diff) { GIT_REFCOUNT_INC(diff); } @@ -612,7 +612,7 @@ typedef struct { static int maybe_modified_submodule( git_delta_t *status, git_oid *found_oid, - git_diff_list *diff, + git_diff *diff, diff_in_progress *info) { int error = 0; @@ -659,7 +659,7 @@ static int maybe_modified_submodule( } static int maybe_modified( - git_diff_list *diff, + git_diff *diff, diff_in_progress *info) { git_oid noid; @@ -778,7 +778,7 @@ static int maybe_modified( } static bool entry_is_prefixed( - git_diff_list *diff, + git_diff *diff, const git_index_entry *item, const git_index_entry *prefix_item) { @@ -795,7 +795,7 @@ static bool entry_is_prefixed( } static int diff_scan_inside_untracked_dir( - git_diff_list *diff, diff_in_progress *info, git_delta_t *delta_type) + git_diff *diff, diff_in_progress *info, git_delta_t *delta_type) { int error = 0; git_buf base = GIT_BUF_INIT; @@ -861,7 +861,7 @@ done: } static int handle_unmatched_new_item( - git_diff_list *diff, diff_in_progress *info) + git_diff *diff, diff_in_progress *info) { int error = 0; const git_index_entry *nitem = info->nitem; @@ -1016,7 +1016,7 @@ static int handle_unmatched_new_item( } static int handle_unmatched_old_item( - git_diff_list *diff, diff_in_progress *info) + git_diff *diff, diff_in_progress *info) { int error = diff_delta__from_one(diff, GIT_DELTA_DELETED, info->oitem); if (error < 0) @@ -1048,7 +1048,7 @@ static int handle_unmatched_old_item( } static int handle_matched_item( - git_diff_list *diff, diff_in_progress *info) + git_diff *diff, diff_in_progress *info) { int error = 0; @@ -1063,7 +1063,7 @@ static int handle_matched_item( } int git_diff__from_iterators( - git_diff_list **diff_ptr, + git_diff **diff_ptr, git_repository *repo, git_iterator *old_iter, git_iterator *new_iter, @@ -1071,7 +1071,7 @@ int git_diff__from_iterators( { int error = 0; diff_in_progress info; - git_diff_list *diff; + git_diff *diff; *diff_ptr = NULL; @@ -1132,7 +1132,7 @@ cleanup: if (!error) *diff_ptr = diff; else - git_diff_list_free(diff); + git_diff_free(diff); git_buf_free(&info.ignore_prefix); @@ -1149,7 +1149,7 @@ cleanup: } while (0) int git_diff_tree_to_tree( - git_diff_list **diff, + git_diff **diff, git_repository *repo, git_tree *old_tree, git_tree *new_tree, @@ -1176,7 +1176,7 @@ int git_diff_tree_to_tree( } int git_diff_tree_to_index( - git_diff_list **diff, + git_diff **diff, git_repository *repo, git_tree *old_tree, git_index *index, @@ -1204,7 +1204,7 @@ int git_diff_tree_to_index( git_index__set_ignore_case(index, true); if (!error) { - git_diff_list *d = *diff; + git_diff *d = *diff; d->opts.flags |= GIT_DIFF_DELTAS_ARE_ICASE; d->strcomp = git__strcasecmp; @@ -1221,7 +1221,7 @@ int git_diff_tree_to_index( } int git_diff_index_to_workdir( - git_diff_list **diff, + git_diff **diff, git_repository *repo, git_index *index, const git_diff_options *opts) @@ -1244,7 +1244,7 @@ int git_diff_index_to_workdir( int git_diff_tree_to_workdir( - git_diff_list **diff, + git_diff **diff, git_repository *repo, git_tree *old_tree, const git_diff_options *opts) @@ -1262,13 +1262,13 @@ int git_diff_tree_to_workdir( return error; } -size_t git_diff_num_deltas(git_diff_list *diff) +size_t git_diff_num_deltas(git_diff *diff) { assert(diff); return (size_t)diff->deltas.length; } -size_t git_diff_num_deltas_of_type(git_diff_list *diff, git_delta_t type) +size_t git_diff_num_deltas_of_type(git_diff *diff, git_delta_t type) { size_t i, count = 0; git_diff_delta *delta; @@ -1282,14 +1282,14 @@ size_t git_diff_num_deltas_of_type(git_diff_list *diff, git_delta_t type) return count; } -int git_diff_is_sorted_icase(const git_diff_list *diff) +int git_diff_is_sorted_icase(const git_diff *diff) { return (diff->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) != 0; } int git_diff__paired_foreach( - git_diff_list *head2idx, - git_diff_list *idx2wd, + git_diff *head2idx, + git_diff *idx2wd, int (*cb)(git_diff_delta *h2i, git_diff_delta *i2w, void *payload), void *payload) { diff --git a/src/diff.h b/src/diff.h index bec7e27d7..270bea071 100644 --- a/src/diff.h +++ b/src/diff.h @@ -52,7 +52,7 @@ enum { #define GIT_DIFF__VERBOSE (1 << 30) -struct git_diff_list { +struct git_diff { git_refcount rc; git_repository *repo; git_diff_options opts; @@ -72,7 +72,7 @@ struct git_diff_list { extern void git_diff__cleanup_modes( uint32_t diffcaps, uint32_t *omode, uint32_t *nmode); -extern void git_diff_list_addref(git_diff_list *diff); +extern void git_diff_addref(git_diff *diff); extern int git_diff_delta__cmp(const void *a, const void *b); extern int git_diff_delta__casecmp(const void *a, const void *b); @@ -93,15 +93,15 @@ extern int git_diff__oid_for_file( git_repository *, const char *, uint16_t, git_off_t, git_oid *); extern int git_diff__from_iterators( - git_diff_list **diff_ptr, + git_diff **diff_ptr, git_repository *repo, git_iterator *old_iter, git_iterator *new_iter, const git_diff_options *opts); extern int git_diff__paired_foreach( - git_diff_list *idx2head, - git_diff_list *wd2idx, + git_diff *idx2head, + git_diff *wd2idx, int (*cb)(git_diff_delta *i2h, git_diff_delta *w2i, void *payload), void *payload); diff --git a/src/diff_file.c b/src/diff_file.c index 5939ee8b8..a7bca4dca 100644 --- a/src/diff_file.c +++ b/src/diff_file.c @@ -88,7 +88,7 @@ static int diff_file_content_init_common( int git_diff_file_content__init_from_diff( git_diff_file_content *fc, - git_diff_list *diff, + git_diff *diff, size_t delta_index, bool use_old) { diff --git a/src/diff_file.h b/src/diff_file.h index fb08cca6a..84bf255aa 100644 --- a/src/diff_file.h +++ b/src/diff_file.h @@ -27,7 +27,7 @@ typedef struct { extern int git_diff_file_content__init_from_diff( git_diff_file_content *fc, - git_diff_list *diff, + git_diff *diff, size_t delta_index, bool use_old); diff --git a/src/diff_patch.c b/src/diff_patch.c index cc45b6ddb..a106944c7 100644 --- a/src/diff_patch.c +++ b/src/diff_patch.c @@ -24,16 +24,16 @@ struct diff_patch_line { /* cached information about a hunk in a diff */ typedef struct diff_patch_hunk diff_patch_hunk; struct diff_patch_hunk { - git_diff_range range; + git_diff_hunk hunk; char header[128]; size_t header_len; size_t line_start; size_t line_count; }; -struct git_diff_patch { +struct git_patch { git_refcount rc; - git_diff_list *diff; /* for refcount purposes, maybe NULL for blob diffs */ + git_diff *diff; /* for refcount purposes, maybe NULL for blob diffs */ git_diff_delta *delta; size_t delta_index; git_diff_file_content ofile; @@ -56,11 +56,11 @@ enum { }; static void diff_output_init(git_diff_output*, const git_diff_options*, - git_diff_file_cb, git_diff_hunk_cb, git_diff_data_cb, void*); + git_diff_file_cb, git_diff_hunk_cb, git_diff_line_cb, void*); -static void diff_output_to_patch(git_diff_output *, git_diff_patch *); +static void diff_output_to_patch(git_diff_output *, git_patch *); -static void diff_patch_update_binary(git_diff_patch *patch) +static void diff_patch_update_binary(git_patch *patch) { if ((patch->delta->flags & DIFF_FLAGS_KNOWN_BINARY) != 0) return; @@ -74,7 +74,7 @@ static void diff_patch_update_binary(git_diff_patch *patch) patch->delta->flags |= GIT_DIFF_FLAG_NOT_BINARY; } -static void diff_patch_init_common(git_diff_patch *patch) +static void diff_patch_init_common(git_patch *patch) { diff_patch_update_binary(patch); @@ -84,11 +84,11 @@ static void diff_patch_init_common(git_diff_patch *patch) patch->flags |= GIT_DIFF_PATCH_INITIALIZED; if (patch->diff) - git_diff_list_addref(patch->diff); + git_diff_addref(patch->diff); } static int diff_patch_init_from_diff( - git_diff_patch *patch, git_diff_list *diff, size_t delta_index) + git_patch *patch, git_diff *diff, size_t delta_index) { int error = 0; @@ -109,12 +109,12 @@ static int diff_patch_init_from_diff( } static int diff_patch_alloc_from_diff( - git_diff_patch **out, - git_diff_list *diff, + git_patch **out, + git_diff *diff, size_t delta_index) { int error; - git_diff_patch *patch = git__calloc(1, sizeof(git_diff_patch)); + git_patch *patch = git__calloc(1, sizeof(git_patch)); GITERR_CHECK_ALLOC(patch); if (!(error = diff_patch_init_from_diff(patch, diff, delta_index))) { @@ -129,7 +129,7 @@ static int diff_patch_alloc_from_diff( return error; } -static int diff_patch_load(git_diff_patch *patch, git_diff_output *output) +static int diff_patch_load(git_patch *patch, git_diff_output *output) { int error = 0; bool incomplete_data; @@ -207,7 +207,7 @@ cleanup: } static int diff_patch_file_callback( - git_diff_patch *patch, git_diff_output *output) + git_patch *patch, git_diff_output *output) { float progress; @@ -223,7 +223,7 @@ static int diff_patch_file_callback( return output->error; } -static int diff_patch_generate(git_diff_patch *patch, git_diff_output *output) +static int diff_patch_generate(git_patch *patch, git_diff_output *output) { int error = 0; @@ -248,7 +248,7 @@ static int diff_patch_generate(git_diff_patch *patch, git_diff_output *output) return error; } -static void diff_patch_free(git_diff_patch *patch) +static void diff_patch_free(git_patch *patch) { git_diff_file_content__clear(&patch->ofile); git_diff_file_content__clear(&patch->nfile); @@ -256,7 +256,7 @@ static void diff_patch_free(git_diff_patch *patch) git_array_clear(patch->lines); git_array_clear(patch->hunks); - git_diff_list_free(patch->diff); /* decrements refcount */ + git_diff_free(patch->diff); /* decrements refcount */ patch->diff = NULL; git_pool_clear(&patch->flattened); @@ -265,7 +265,7 @@ static void diff_patch_free(git_diff_patch *patch) git__free(patch); } -static int diff_required(git_diff_list *diff, const char *action) +static int diff_required(git_diff *diff, const char *action) { if (diff) return 0; @@ -274,16 +274,16 @@ static int diff_required(git_diff_list *diff, const char *action) } int git_diff_foreach( - git_diff_list *diff, + git_diff *diff, git_diff_file_cb file_cb, git_diff_hunk_cb hunk_cb, - git_diff_data_cb data_cb, + git_diff_line_cb data_cb, void *payload) { int error = 0; git_xdiff_output xo; size_t idx; - git_diff_patch patch; + git_patch patch; if (diff_required(diff, "git_diff_foreach") < 0) return -1; @@ -305,7 +305,7 @@ int git_diff_foreach( if (!error) error = diff_patch_generate(&patch, &xo.output); - git_diff_patch_free(&patch); + git_patch_free(&patch); } if (error < 0) @@ -318,7 +318,7 @@ int git_diff_foreach( } typedef struct { - git_diff_patch patch; + git_patch patch; git_diff_delta delta; char paths[GIT_FLEX_ARRAY]; } diff_patch_with_delta; @@ -326,7 +326,7 @@ typedef struct { static int diff_single_generate(diff_patch_with_delta *pd, git_xdiff_output *xo) { int error = 0; - git_diff_patch *patch = &pd->patch; + git_patch *patch = &pd->patch; bool has_old = ((patch->ofile.flags & GIT_DIFF_FLAG__NO_DATA) == 0); bool has_new = ((patch->nfile.flags & GIT_DIFF_FLAG__NO_DATA) == 0); @@ -430,7 +430,7 @@ int git_diff_blobs( const git_diff_options *opts, git_diff_file_cb file_cb, git_diff_hunk_cb hunk_cb, - git_diff_data_cb data_cb, + git_diff_line_cb data_cb, void *payload) { int error = 0; @@ -452,13 +452,13 @@ int git_diff_blobs( error = diff_patch_from_blobs( &pd, &xo, old_blob, old_path, new_blob, new_path, opts); - git_diff_patch_free(&pd.patch); + git_patch_free(&pd.patch); return error; } -int git_diff_patch_from_blobs( - git_diff_patch **out, +int git_patch_from_blobs( + git_patch **out, const git_blob *old_blob, const char *old_path, const git_blob *new_blob, @@ -484,9 +484,9 @@ int git_diff_patch_from_blobs( pd, &xo, old_blob, old_path, new_blob, new_path, opts); if (!error) - *out = (git_diff_patch *)pd; + *out = (git_patch *)pd; else - git_diff_patch_free((git_diff_patch *)pd); + git_patch_free((git_patch *)pd); return error; } @@ -542,7 +542,7 @@ int git_diff_blob_to_buffer( const git_diff_options *opts, git_diff_file_cb file_cb, git_diff_hunk_cb hunk_cb, - git_diff_data_cb data_cb, + git_diff_line_cb data_cb, void *payload) { int error = 0; @@ -564,13 +564,13 @@ int git_diff_blob_to_buffer( error = diff_patch_from_blob_and_buffer( &pd, &xo, old_blob, old_path, buf, buflen, buf_path, opts); - git_diff_patch_free(&pd.patch); + git_patch_free(&pd.patch); return error; } -int git_diff_patch_from_blob_and_buffer( - git_diff_patch **out, +int git_patch_from_blob_and_buffer( + git_patch **out, const git_blob *old_blob, const char *old_path, const char *buf, @@ -597,28 +597,28 @@ int git_diff_patch_from_blob_and_buffer( pd, &xo, old_blob, old_path, buf, buflen, buf_path, opts); if (!error) - *out = (git_diff_patch *)pd; + *out = (git_patch *)pd; else - git_diff_patch_free((git_diff_patch *)pd); + git_patch_free((git_patch *)pd); return error; } -int git_diff_get_patch( - git_diff_patch **patch_ptr, +int git_patch_from_diff( + git_patch **patch_ptr, const git_diff_delta **delta_ptr, - git_diff_list *diff, + git_diff *diff, size_t idx) { int error = 0; git_xdiff_output xo; git_diff_delta *delta = NULL; - git_diff_patch *patch = NULL; + git_patch *patch = NULL; if (patch_ptr) *patch_ptr = NULL; if (delta_ptr) *delta_ptr = NULL; - if (diff_required(diff, "git_diff_get_patch") < 0) + if (diff_required(diff, "git_patch_from_diff") < 0) return -1; delta = git_vector_get(&diff->deltas, idx); @@ -656,7 +656,7 @@ int git_diff_get_patch( } if (error || !patch_ptr) - git_diff_patch_free(patch); + git_patch_free(patch); else *patch_ptr = patch; @@ -665,29 +665,29 @@ int git_diff_get_patch( return error; } -void git_diff_patch_free(git_diff_patch *patch) +void git_patch_free(git_patch *patch) { if (patch) GIT_REFCOUNT_DEC(patch, diff_patch_free); } -const git_diff_delta *git_diff_patch_delta(git_diff_patch *patch) +const git_diff_delta *git_patch_delta(git_patch *patch) { assert(patch); return patch->delta; } -size_t git_diff_patch_num_hunks(git_diff_patch *patch) +size_t git_patch_num_hunks(git_patch *patch) { assert(patch); return git_array_size(patch->hunks); } -int git_diff_patch_line_stats( +int git_patch_line_stats( size_t *total_ctxt, size_t *total_adds, size_t *total_dels, - const git_diff_patch *patch) + const git_patch *patch) { size_t totals[3], idx; @@ -726,12 +726,12 @@ static int diff_error_outofrange(const char *thing) return GIT_ENOTFOUND; } -int git_diff_patch_get_hunk( - const git_diff_range **range, +int git_patch_get_hunk( + const git_diff_hunk **out, const char **header, size_t *header_len, size_t *lines_in_hunk, - git_diff_patch *patch, + git_patch *patch, size_t hunk_idx) { diff_patch_hunk *hunk; @@ -740,21 +740,21 @@ int git_diff_patch_get_hunk( hunk = git_array_get(patch->hunks, hunk_idx); if (!hunk) { - if (range) *range = NULL; + if (out) *out = NULL; if (header) *header = NULL; if (header_len) *header_len = 0; if (lines_in_hunk) *lines_in_hunk = 0; return diff_error_outofrange("hunk"); } - if (range) *range = &hunk->range; + if (out) *out = &hunk->hunk; if (header) *header = hunk->header; if (header_len) *header_len = hunk->header_len; if (lines_in_hunk) *lines_in_hunk = hunk->line_count; return 0; } -int git_diff_patch_num_lines_in_hunk(git_diff_patch *patch, size_t hunk_idx) +int git_patch_num_lines_in_hunk(git_patch *patch, size_t hunk_idx) { diff_patch_hunk *hunk; assert(patch); @@ -764,13 +764,13 @@ int git_diff_patch_num_lines_in_hunk(git_diff_patch *patch, size_t hunk_idx) return (int)hunk->line_count; } -int git_diff_patch_get_line_in_hunk( +int git_patch_get_line_in_hunk( char *line_origin, const char **content, size_t *content_len, int *old_lineno, int *new_lineno, - git_diff_patch *patch, + git_patch *patch, size_t hunk_idx, size_t line_of_hunk) { @@ -810,8 +810,8 @@ notfound: return diff_error_outofrange(thing); } -size_t git_diff_patch_size( - git_diff_patch *patch, +size_t git_patch_size( + git_patch *patch, int include_context, int include_hunk_headers, int include_file_headers) @@ -843,36 +843,36 @@ size_t git_diff_patch_size( return out; } -git_diff_list *git_diff_patch__diff(git_diff_patch *patch) +git_diff *git_patch__diff(git_patch *patch) { return patch->diff; } -git_diff_driver *git_diff_patch__driver(git_diff_patch *patch) +git_diff_driver *git_patch__driver(git_patch *patch) { /* ofile driver is representative for whole patch */ return patch->ofile.driver; } -void git_diff_patch__old_data( - char **ptr, size_t *len, git_diff_patch *patch) +void git_patch__old_data( + char **ptr, size_t *len, git_patch *patch) { *ptr = patch->ofile.map.data; *len = patch->ofile.map.len; } -void git_diff_patch__new_data( - char **ptr, size_t *len, git_diff_patch *patch) +void git_patch__new_data( + char **ptr, size_t *len, git_patch *patch) { *ptr = patch->nfile.map.data; *len = patch->nfile.map.len; } -int git_diff_patch__invoke_callbacks( - git_diff_patch *patch, +int git_patch__invoke_callbacks( + git_patch *patch, git_diff_file_cb file_cb, git_diff_hunk_cb hunk_cb, - git_diff_data_cb line_cb, + git_diff_line_cb line_cb, void *payload) { int error = 0; @@ -888,7 +888,7 @@ int git_diff_patch__invoke_callbacks( diff_patch_hunk *h = git_array_get(patch->hunks, i); error = hunk_cb( - patch->delta, &h->range, h->header, h->header_len, payload); + patch->delta, &h->hunk, h->header, h->header_len, payload); if (!line_cb) continue; @@ -898,7 +898,7 @@ int git_diff_patch__invoke_callbacks( git_array_get(patch->lines, h->line_start + j); error = line_cb( - patch->delta, &h->range, l->origin, l->ptr, l->len, payload); + patch->delta, &h->hunk, l->origin, l->ptr, l->len, payload); } } @@ -917,12 +917,12 @@ static int diff_patch_file_cb( static int diff_patch_hunk_cb( const git_diff_delta *delta, - const git_diff_range *range, + const git_diff_hunk *hunk_, const char *header, size_t header_len, void *payload) { - git_diff_patch *patch = payload; + git_patch *patch = payload; diff_patch_hunk *hunk; GIT_UNUSED(delta); @@ -930,7 +930,7 @@ static int diff_patch_hunk_cb( hunk = git_array_alloc(patch->hunks); GITERR_CHECK_ALLOC(hunk); - memcpy(&hunk->range, range, sizeof(hunk->range)); + memcpy(&hunk->hunk, hunk_, sizeof(hunk->hunk)); assert(header_len + 1 < sizeof(hunk->header)); memcpy(&hunk->header, header, header_len); @@ -942,27 +942,27 @@ static int diff_patch_hunk_cb( hunk->line_start = git_array_size(patch->lines); hunk->line_count = 0; - patch->oldno = range->old_start; - patch->newno = range->new_start; + patch->oldno = hunk_->old_start; + patch->newno = hunk_->new_start; return 0; } static int diff_patch_line_cb( const git_diff_delta *delta, - const git_diff_range *range, + const git_diff_hunk *hunk_, char line_origin, const char *content, size_t content_len, void *payload) { - git_diff_patch *patch = payload; + git_patch *patch = payload; diff_patch_hunk *hunk; diff_patch_line *line; const char *content_end = content + content_len; GIT_UNUSED(delta); - GIT_UNUSED(range); + GIT_UNUSED(hunk_); hunk = git_array_last(patch->hunks); GITERR_CHECK_ALLOC(hunk); @@ -1023,7 +1023,7 @@ static void diff_output_init( const git_diff_options *opts, git_diff_file_cb file_cb, git_diff_hunk_cb hunk_cb, - git_diff_data_cb data_cb, + git_diff_line_cb data_cb, void *payload) { GIT_UNUSED(opts); @@ -1036,7 +1036,7 @@ static void diff_output_init( out->payload = payload; } -static void diff_output_to_patch(git_diff_output *out, git_diff_patch *patch) +static void diff_output_to_patch(git_diff_output *out, git_patch *patch) { diff_output_init( out, NULL, diff --git a/src/diff_patch.h b/src/diff_patch.h index 56af14600..df2ba4c31 100644 --- a/src/diff_patch.h +++ b/src/diff_patch.h @@ -11,19 +11,20 @@ #include "diff.h" #include "diff_file.h" #include "array.h" +#include "git2/patch.h" -extern git_diff_list *git_diff_patch__diff(git_diff_patch *); +extern git_diff *git_patch__diff(git_patch *); -extern git_diff_driver *git_diff_patch__driver(git_diff_patch *); +extern git_diff_driver *git_patch__driver(git_patch *); -extern void git_diff_patch__old_data(char **, size_t *, git_diff_patch *); -extern void git_diff_patch__new_data(char **, size_t *, git_diff_patch *); +extern void git_patch__old_data(char **, size_t *, git_patch *); +extern void git_patch__new_data(char **, size_t *, git_patch *); -extern int git_diff_patch__invoke_callbacks( - git_diff_patch *patch, +extern int git_patch__invoke_callbacks( + git_patch *patch, git_diff_file_cb file_cb, git_diff_hunk_cb hunk_cb, - git_diff_data_cb line_cb, + git_diff_line_cb line_cb, void *payload); typedef struct git_diff_output git_diff_output; @@ -31,7 +32,7 @@ struct git_diff_output { /* these callbacks are issued with the diff data */ git_diff_file_cb file_cb; git_diff_hunk_cb hunk_cb; - git_diff_data_cb data_cb; + git_diff_line_cb data_cb; void *payload; /* this records the actual error in cases where it may be obscured */ @@ -40,7 +41,7 @@ struct git_diff_output { /* this callback is used to do the diff and drive the other callbacks. * see diff_xdiff.h for how to use this in practice for now. */ - int (*diff_cb)(git_diff_output *output, git_diff_patch *patch); + int (*diff_cb)(git_diff_output *output, git_patch *patch); }; #endif diff --git a/src/diff_print.c b/src/diff_print.c index fd18b67e3..1cf6da2e3 100644 --- a/src/diff_print.c +++ b/src/diff_print.c @@ -10,8 +10,8 @@ #include "fileops.h" typedef struct { - git_diff_list *diff; - git_diff_data_cb print_cb; + git_diff *diff; + git_diff_line_cb print_cb; void *payload; git_buf *buf; int oid_strlen; @@ -19,7 +19,7 @@ typedef struct { static int diff_print_info_init( diff_print_info *pi, - git_buf *out, git_diff_list *diff, git_diff_data_cb cb, void *payload) + git_buf *out, git_diff *diff, git_diff_line_cb cb, void *payload) { pi->diff = diff; pi->print_cb = cb; @@ -119,10 +119,10 @@ static int diff_print_one_compact( return 0; } -/* print a git_diff_list to a print callback in compact format */ +/* print a git_diff to a print callback in compact format */ int git_diff_print_compact( - git_diff_list *diff, - git_diff_data_cb print_cb, + git_diff *diff, + git_diff_line_cb print_cb, void *payload) { int error; @@ -180,10 +180,10 @@ static int diff_print_one_raw( return 0; } -/* print a git_diff_list to a print callback in raw output format */ +/* print a git_diff to a print callback in raw output format */ int git_diff_print_raw( - git_diff_list *diff, - git_diff_data_cb print_cb, + git_diff *diff, + git_diff_line_cb print_cb, void *payload) { int error; @@ -325,7 +325,7 @@ static int diff_print_patch_file( static int diff_print_patch_hunk( const git_diff_delta *d, - const git_diff_range *r, + const git_diff_hunk *r, const char *header, size_t header_len, void *data) @@ -348,7 +348,7 @@ static int diff_print_patch_hunk( static int diff_print_patch_line( const git_diff_delta *delta, - const git_diff_range *range, + const git_diff_hunk *range, char line_origin, /* GIT_DIFF_LINE value from above */ const char *content, size_t content_len, @@ -379,10 +379,10 @@ static int diff_print_patch_line( return 0; } -/* print a git_diff_list to an output callback in patch format */ +/* print a git_diff to an output callback in patch format */ int git_diff_print_patch( - git_diff_list *diff, - git_diff_data_cb print_cb, + git_diff *diff, + git_diff_line_cb print_cb, void *payload) { int error; @@ -399,10 +399,10 @@ int git_diff_print_patch( return error; } -/* print a git_diff_patch to an output callback */ -int git_diff_patch_print( - git_diff_patch *patch, - git_diff_data_cb print_cb, +/* print a git_patch to an output callback */ +int git_patch_print( + git_patch *patch, + git_diff_line_cb print_cb, void *payload) { int error; @@ -412,8 +412,8 @@ int git_diff_patch_print( assert(patch && print_cb); if (!(error = diff_print_info_init( - &pi, &temp, git_diff_patch__diff(patch), print_cb, payload))) - error = git_diff_patch__invoke_callbacks( + &pi, &temp, git_patch__diff(patch), print_cb, payload))) + error = git_patch__invoke_callbacks( patch, diff_print_patch_file, diff_print_patch_hunk, diff_print_patch_line, &pi); @@ -424,7 +424,7 @@ int git_diff_patch_print( static int diff_print_to_buffer_cb( const git_diff_delta *delta, - const git_diff_range *range, + const git_diff_hunk *range, char line_origin, const char *content, size_t content_len, @@ -435,15 +435,15 @@ static int diff_print_to_buffer_cb( return git_buf_put(output, content, content_len); } -/* print a git_diff_patch to a string buffer */ -int git_diff_patch_to_str( +/* print a git_patch to a string buffer */ +int git_patch_to_str( char **string, - git_diff_patch *patch) + git_patch *patch) { int error; git_buf output = GIT_BUF_INIT; - error = git_diff_patch_print(patch, diff_print_to_buffer_cb, &output); + error = git_patch_print(patch, diff_print_to_buffer_cb, &output); /* GIT_EUSER means git_buf_put in print_to_buffer_cb returned -1, * meaning a memory allocation failure, so just map to -1... diff --git a/src/diff_tform.c b/src/diff_tform.c index cbe8bafbd..c0a60672c 100644 --- a/src/diff_tform.c +++ b/src/diff_tform.c @@ -97,8 +97,8 @@ static git_diff_delta *diff_delta__merge_like_cgit( } int git_diff_merge( - git_diff_list *onto, - const git_diff_list *from) + git_diff *onto, + const git_diff *from) { int error = 0; git_pool onto_pool; @@ -230,7 +230,7 @@ int git_diff_find_similar__calc_similarity( #define DEFAULT_RENAME_LIMIT 200 static int normalize_find_opts( - git_diff_list *diff, + git_diff *diff, git_diff_find_options *opts, git_diff_find_options *given) { @@ -328,7 +328,7 @@ static int normalize_find_opts( } static int apply_splits_and_deletes( - git_diff_list *diff, size_t expected_size, bool actually_split) + git_diff *diff, size_t expected_size, bool actually_split) { git_vector onto = GIT_VECTOR_INIT; size_t i; @@ -402,7 +402,7 @@ on_error: return -1; } -GIT_INLINE(git_diff_file *) similarity_get_file(git_diff_list *diff, size_t idx) +GIT_INLINE(git_diff_file *) similarity_get_file(git_diff *diff, size_t idx) { git_diff_delta *delta = git_vector_get(&diff->deltas, idx / 2); return (idx & 1) ? &delta->new_file : &delta->old_file; @@ -419,7 +419,7 @@ typedef struct { } similarity_info; static int similarity_init( - similarity_info *info, git_diff_list *diff, size_t file_idx) + similarity_info *info, git_diff *diff, size_t file_idx) { info->idx = file_idx; info->src = (file_idx & 1) ? diff->new_src : diff->old_src; @@ -509,7 +509,7 @@ static void similarity_unload(similarity_info *info) */ static int similarity_measure( int *score, - git_diff_list *diff, + git_diff *diff, const git_diff_find_options *opts, void **cache, size_t a_idx, @@ -595,7 +595,7 @@ cleanup: } static int calc_self_similarity( - git_diff_list *diff, + git_diff *diff, const git_diff_find_options *opts, size_t delta_idx, void **cache) @@ -620,7 +620,7 @@ static int calc_self_similarity( } static bool is_rename_target( - git_diff_list *diff, + git_diff *diff, const git_diff_find_options *opts, size_t delta_idx, void **cache) @@ -675,7 +675,7 @@ static bool is_rename_target( } static bool is_rename_source( - git_diff_list *diff, + git_diff *diff, const git_diff_find_options *opts, size_t delta_idx, void **cache) @@ -759,7 +759,7 @@ typedef struct { } diff_find_match; int git_diff_find_similar( - git_diff_list *diff, + git_diff *diff, git_diff_find_options *given_opts) { size_t s, t; diff --git a/src/diff_xdiff.c b/src/diff_xdiff.c index 7694fb996..e3aa8f3e1 100644 --- a/src/diff_xdiff.c +++ b/src/diff_xdiff.c @@ -24,26 +24,26 @@ static int git_xdiff_scan_int(const char **str, int *value) return (digits > 0) ? 0 : -1; } -static int git_xdiff_parse_hunk(git_diff_range *range, const char *header) +static int git_xdiff_parse_hunk(git_diff_hunk *hunk, const char *header) { /* expect something of the form "@@ -%d[,%d] +%d[,%d] @@" */ if (*header != '@') return -1; - if (git_xdiff_scan_int(&header, &range->old_start) < 0) + if (git_xdiff_scan_int(&header, &hunk->old_start) < 0) return -1; if (*header == ',') { - if (git_xdiff_scan_int(&header, &range->old_lines) < 0) + if (git_xdiff_scan_int(&header, &hunk->old_lines) < 0) return -1; } else - range->old_lines = 1; - if (git_xdiff_scan_int(&header, &range->new_start) < 0) + hunk->old_lines = 1; + if (git_xdiff_scan_int(&header, &hunk->new_start) < 0) return -1; if (*header == ',') { - if (git_xdiff_scan_int(&header, &range->new_lines) < 0) + if (git_xdiff_scan_int(&header, &hunk->new_lines) < 0) return -1; } else - range->new_lines = 1; - if (range->old_start < 0 || range->new_start < 0) + hunk->new_lines = 1; + if (hunk->old_start < 0 || hunk->new_start < 0) return -1; return 0; @@ -51,24 +51,24 @@ static int git_xdiff_parse_hunk(git_diff_range *range, const char *header) typedef struct { git_xdiff_output *xo; - git_diff_patch *patch; - git_diff_range range; + git_patch *patch; + git_diff_hunk hunk; } git_xdiff_info; static int git_xdiff_cb(void *priv, mmbuffer_t *bufs, int len) { git_xdiff_info *info = priv; - git_diff_patch *patch = info->patch; - const git_diff_delta *delta = git_diff_patch_delta(patch); + git_patch *patch = info->patch; + const git_diff_delta *delta = git_patch_delta(patch); git_diff_output *output = &info->xo->output; if (len == 1) { - output->error = git_xdiff_parse_hunk(&info->range, bufs[0].ptr); + output->error = git_xdiff_parse_hunk(&info->hunk, bufs[0].ptr); if (output->error < 0) return output->error; if (output->hunk_cb != NULL && - output->hunk_cb(delta, &info->range, + output->hunk_cb(delta, &info->hunk, bufs[0].ptr, bufs[0].size, output->payload)) output->error = GIT_EUSER; } @@ -81,7 +81,7 @@ static int git_xdiff_cb(void *priv, mmbuffer_t *bufs, int len) GIT_DIFF_LINE_CONTEXT; if (output->data_cb != NULL && - output->data_cb(delta, &info->range, + output->data_cb(delta, &info->hunk, origin, bufs[1].ptr, bufs[1].size, output->payload)) output->error = GIT_EUSER; } @@ -98,7 +98,7 @@ static int git_xdiff_cb(void *priv, mmbuffer_t *bufs, int len) GIT_DIFF_LINE_CONTEXT_EOFNL; if (output->data_cb != NULL && - output->data_cb(delta, &info->range, + output->data_cb(delta, &info->hunk, origin, bufs[2].ptr, bufs[2].size, output->payload)) output->error = GIT_EUSER; } @@ -106,7 +106,7 @@ static int git_xdiff_cb(void *priv, mmbuffer_t *bufs, int len) return output->error; } -static int git_xdiff(git_diff_output *output, git_diff_patch *patch) +static int git_xdiff(git_diff_output *output, git_patch *patch) { git_xdiff_output *xo = (git_xdiff_output *)output; git_xdiff_info info; @@ -120,7 +120,7 @@ static int git_xdiff(git_diff_output *output, git_diff_patch *patch) xo->callback.priv = &info; git_diff_find_context_init( - &xo->config.find_func, &findctxt, git_diff_patch__driver(patch)); + &xo->config.find_func, &findctxt, git_patch__driver(patch)); xo->config.find_func_priv = &findctxt; if (xo->config.find_func != NULL) @@ -132,8 +132,8 @@ static int git_xdiff(git_diff_output *output, git_diff_patch *patch) * updates are needed to xo->params.flags */ - git_diff_patch__old_data(&xd_old_data.ptr, &xd_old_data.size, patch); - git_diff_patch__new_data(&xd_new_data.ptr, &xd_new_data.size, patch); + git_patch__old_data(&xd_old_data.ptr, &xd_old_data.size, patch); + git_patch__new_data(&xd_new_data.ptr, &xd_new_data.size, patch); xdl_diff(&xd_old_data, &xd_new_data, &xo->params, &xo->config, &xo->callback); diff --git a/src/pathspec.c b/src/pathspec.c index d56d03918..1e7e65e90 100644 --- a/src/pathspec.c +++ b/src/pathspec.c @@ -585,7 +585,7 @@ int git_pathspec_match_tree( int git_pathspec_match_diff( git_pathspec_match_list **out, - git_diff_list *diff, + git_diff *diff, uint32_t flags, git_pathspec *ps) { diff --git a/src/reset.c b/src/reset.c index cea212a93..3fd4b9165 100644 --- a/src/reset.c +++ b/src/reset.c @@ -24,7 +24,7 @@ int git_reset_default( { git_object *commit = NULL; git_tree *tree = NULL; - git_diff_list *diff = NULL; + git_diff *diff = NULL; git_diff_options opts = GIT_DIFF_OPTIONS_INIT; size_t i; git_diff_delta *delta; @@ -85,7 +85,7 @@ cleanup: git_object_free(commit); git_tree_free(tree); git_index_free(index); - git_diff_list_free(diff); + git_diff_free(diff); return error; } diff --git a/src/stash.c b/src/stash.c index 7742eee19..6cf26f711 100644 --- a/src/stash.c +++ b/src/stash.c @@ -221,7 +221,7 @@ static int build_untracked_tree( uint32_t flags) { git_tree *i_tree = NULL; - git_diff_list *diff = NULL; + git_diff *diff = NULL; git_diff_options opts = GIT_DIFF_OPTIONS_INIT; struct cb_data data = {0}; int error; @@ -259,7 +259,7 @@ static int build_untracked_tree( error = build_tree_from_index(tree_out, index); cleanup: - git_diff_list_free(diff); + git_diff_free(diff); git_tree_free(i_tree); return error; } @@ -311,7 +311,7 @@ static int build_workdir_tree( { git_repository *repo = git_index_owner(index); git_tree *b_tree = NULL; - git_diff_list *diff = NULL, *diff2 = NULL; + git_diff *diff = NULL, *diff2 = NULL; git_diff_options opts = GIT_DIFF_OPTIONS_INIT; struct cb_data data = {0}; int error; @@ -346,8 +346,8 @@ static int build_workdir_tree( goto cleanup; cleanup: - git_diff_list_free(diff); - git_diff_list_free(diff2); + git_diff_free(diff); + git_diff_free(diff2); git_tree_free(b_tree); return error; diff --git a/src/status.c b/src/status.c index be40b9f83..2b84794b5 100644 --- a/src/status.c +++ b/src/status.c @@ -52,7 +52,7 @@ static unsigned int index_delta2status(const git_diff_delta *head2idx) } static unsigned int workdir_delta2status( - git_diff_list *diff, git_diff_delta *idx2wd) + git_diff *diff, git_diff_delta *idx2wd) { git_status_t st = GIT_STATUS_CURRENT; @@ -361,8 +361,8 @@ void git_status_list_free(git_status_list *status) if (status == NULL) return; - git_diff_list_free(status->head2idx); - git_diff_list_free(status->idx2wd); + git_diff_free(status->head2idx); + git_diff_free(status->idx2wd); git_vector_foreach(&status->paired, i, status_entry) git__free(status_entry); diff --git a/src/status.h b/src/status.h index b58e0ebd6..33008b89c 100644 --- a/src/status.h +++ b/src/status.h @@ -14,8 +14,8 @@ struct git_status_list { git_status_options opts; - git_diff_list *head2idx; - git_diff_list *idx2wd; + git_diff *head2idx; + git_diff *idx2wd; git_vector paired; }; diff --git a/src/submodule.c b/src/submodule.c index 12ade83fe..18d80f0a9 100644 --- a/src/submodule.c +++ b/src/submodule.c @@ -1528,7 +1528,7 @@ static void submodule_get_wd_status( (sm->flags & GIT_SUBMODULE_STATUS__WD_OID_VALID) ? &sm->wd_oid : NULL; git_tree *sm_head = NULL; git_diff_options opt = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff; + git_diff *diff; *status = *status & ~GIT_SUBMODULE_STATUS__WD_FLAGS; @@ -1568,7 +1568,7 @@ static void submodule_get_wd_status( else { if (git_diff_num_deltas(diff) > 0) *status |= GIT_SUBMODULE_STATUS_WD_INDEX_MODIFIED; - git_diff_list_free(diff); + git_diff_free(diff); diff = NULL; } @@ -1588,7 +1588,7 @@ static void submodule_get_wd_status( if (git_diff_num_deltas(diff) != untracked) *status |= GIT_SUBMODULE_STATUS_WD_WD_MODIFIED; - git_diff_list_free(diff); + git_diff_free(diff); diff = NULL; } } diff --git a/tests-clar/diff/blob.c b/tests-clar/diff/blob.c index 42b9fcd5f..bed0da0bf 100644 --- a/tests-clar/diff/blob.c +++ b/tests-clar/diff/blob.c @@ -142,7 +142,7 @@ void test_diff_blob__can_compare_text_blobs_with_patch(void) { git_blob *a, *b, *c; git_oid a_oid, b_oid, c_oid; - git_diff_patch *p; + git_patch *p; const git_diff_delta *delta; size_t tc, ta, td; @@ -161,11 +161,11 @@ void test_diff_blob__can_compare_text_blobs_with_patch(void) /* Doing the equivalent of a `git diff -U1` on these files */ /* diff on tests/resources/attr/root_test1 */ - cl_git_pass(git_diff_patch_from_blobs(&p, a, NULL, b, NULL, &opts)); + cl_git_pass(git_patch_from_blobs(&p, a, NULL, b, NULL, &opts)); cl_assert(p != NULL); - delta = git_diff_patch_delta(p); + delta = git_patch_delta(p); cl_assert(delta != NULL); cl_assert_equal_i(GIT_DELTA_MODIFIED, delta->status); cl_assert(git_oid_equal(git_blob_id(a), &delta->old_file.oid)); @@ -173,22 +173,22 @@ void test_diff_blob__can_compare_text_blobs_with_patch(void) cl_assert(git_oid_equal(git_blob_id(b), &delta->new_file.oid)); cl_assert_equal_sz(git_blob_rawsize(b), delta->new_file.size); - cl_assert_equal_i(1, (int)git_diff_patch_num_hunks(p)); - cl_assert_equal_i(6, git_diff_patch_num_lines_in_hunk(p, 0)); + cl_assert_equal_i(1, (int)git_patch_num_hunks(p)); + cl_assert_equal_i(6, git_patch_num_lines_in_hunk(p, 0)); - cl_git_pass(git_diff_patch_line_stats(&tc, &ta, &td, p)); + cl_git_pass(git_patch_line_stats(&tc, &ta, &td, p)); cl_assert_equal_i(1, (int)tc); cl_assert_equal_i(5, (int)ta); cl_assert_equal_i(0, (int)td); - git_diff_patch_free(p); + git_patch_free(p); /* diff on tests/resources/attr/root_test2 */ - cl_git_pass(git_diff_patch_from_blobs(&p, b, NULL, c, NULL, &opts)); + cl_git_pass(git_patch_from_blobs(&p, b, NULL, c, NULL, &opts)); cl_assert(p != NULL); - delta = git_diff_patch_delta(p); + delta = git_patch_delta(p); cl_assert(delta != NULL); cl_assert_equal_i(GIT_DELTA_MODIFIED, delta->status); cl_assert(git_oid_equal(git_blob_id(b), &delta->old_file.oid)); @@ -196,22 +196,22 @@ void test_diff_blob__can_compare_text_blobs_with_patch(void) cl_assert(git_oid_equal(git_blob_id(c), &delta->new_file.oid)); cl_assert_equal_sz(git_blob_rawsize(c), delta->new_file.size); - cl_assert_equal_i(1, (int)git_diff_patch_num_hunks(p)); - cl_assert_equal_i(15, git_diff_patch_num_lines_in_hunk(p, 0)); + cl_assert_equal_i(1, (int)git_patch_num_hunks(p)); + cl_assert_equal_i(15, git_patch_num_lines_in_hunk(p, 0)); - cl_git_pass(git_diff_patch_line_stats(&tc, &ta, &td, p)); + cl_git_pass(git_patch_line_stats(&tc, &ta, &td, p)); cl_assert_equal_i(3, (int)tc); cl_assert_equal_i(9, (int)ta); cl_assert_equal_i(3, (int)td); - git_diff_patch_free(p); + git_patch_free(p); /* diff on tests/resources/attr/root_test3 */ - cl_git_pass(git_diff_patch_from_blobs(&p, a, NULL, c, NULL, &opts)); + cl_git_pass(git_patch_from_blobs(&p, a, NULL, c, NULL, &opts)); cl_assert(p != NULL); - delta = git_diff_patch_delta(p); + delta = git_patch_delta(p); cl_assert(delta != NULL); cl_assert_equal_i(GIT_DELTA_MODIFIED, delta->status); cl_assert(git_oid_equal(git_blob_id(a), &delta->old_file.oid)); @@ -219,19 +219,19 @@ void test_diff_blob__can_compare_text_blobs_with_patch(void) cl_assert(git_oid_equal(git_blob_id(c), &delta->new_file.oid)); cl_assert_equal_sz(git_blob_rawsize(c), delta->new_file.size); - cl_git_pass(git_diff_patch_line_stats(&tc, &ta, &td, p)); + cl_git_pass(git_patch_line_stats(&tc, &ta, &td, p)); cl_assert_equal_i(0, (int)tc); cl_assert_equal_i(12, (int)ta); cl_assert_equal_i(1, (int)td); - git_diff_patch_free(p); + git_patch_free(p); /* one more */ - cl_git_pass(git_diff_patch_from_blobs(&p, c, NULL, d, NULL, &opts)); + cl_git_pass(git_patch_from_blobs(&p, c, NULL, d, NULL, &opts)); cl_assert(p != NULL); - delta = git_diff_patch_delta(p); + delta = git_patch_delta(p); cl_assert(delta != NULL); cl_assert_equal_i(GIT_DELTA_MODIFIED, delta->status); cl_assert(git_oid_equal(git_blob_id(c), &delta->old_file.oid)); @@ -239,16 +239,16 @@ void test_diff_blob__can_compare_text_blobs_with_patch(void) cl_assert(git_oid_equal(git_blob_id(d), &delta->new_file.oid)); cl_assert_equal_sz(git_blob_rawsize(d), delta->new_file.size); - cl_assert_equal_i(2, (int)git_diff_patch_num_hunks(p)); - cl_assert_equal_i(5, git_diff_patch_num_lines_in_hunk(p, 0)); - cl_assert_equal_i(9, git_diff_patch_num_lines_in_hunk(p, 1)); + cl_assert_equal_i(2, (int)git_patch_num_hunks(p)); + cl_assert_equal_i(5, git_patch_num_lines_in_hunk(p, 0)); + cl_assert_equal_i(9, git_patch_num_lines_in_hunk(p, 1)); - cl_git_pass(git_diff_patch_line_stats(&tc, &ta, &td, p)); + cl_git_pass(git_patch_line_stats(&tc, &ta, &td, p)); cl_assert_equal_i(4, (int)tc); cl_assert_equal_i(6, (int)ta); cl_assert_equal_i(4, (int)td); - git_diff_patch_free(p); + git_patch_free(p); git_blob_free(a); git_blob_free(b); @@ -317,16 +317,16 @@ void test_diff_blob__can_compare_against_null_blobs(void) void test_diff_blob__can_compare_against_null_blobs_with_patch(void) { git_blob *e = NULL; - git_diff_patch *p; + git_patch *p; const git_diff_delta *delta; int line; char origin; - cl_git_pass(git_diff_patch_from_blobs(&p, d, NULL, e, NULL, &opts)); + cl_git_pass(git_patch_from_blobs(&p, d, NULL, e, NULL, &opts)); cl_assert(p != NULL); - delta = git_diff_patch_delta(p); + delta = git_patch_delta(p); cl_assert(delta != NULL); cl_assert_equal_i(GIT_DELTA_DELETED, delta->status); cl_assert(git_oid_equal(git_blob_id(d), &delta->old_file.oid)); @@ -334,24 +334,24 @@ void test_diff_blob__can_compare_against_null_blobs_with_patch(void) cl_assert(git_oid_iszero(&delta->new_file.oid)); cl_assert_equal_sz(0, delta->new_file.size); - cl_assert_equal_i(1, (int)git_diff_patch_num_hunks(p)); - cl_assert_equal_i(14, git_diff_patch_num_lines_in_hunk(p, 0)); + cl_assert_equal_i(1, (int)git_patch_num_hunks(p)); + cl_assert_equal_i(14, git_patch_num_lines_in_hunk(p, 0)); - for (line = 0; line < git_diff_patch_num_lines_in_hunk(p, 0); ++line) { - cl_git_pass(git_diff_patch_get_line_in_hunk( + for (line = 0; line < git_patch_num_lines_in_hunk(p, 0); ++line) { + cl_git_pass(git_patch_get_line_in_hunk( &origin, NULL, NULL, NULL, NULL, p, 0, line)); cl_assert_equal_i(GIT_DIFF_LINE_DELETION, (int)origin); } - git_diff_patch_free(p); + git_patch_free(p); opts.flags |= GIT_DIFF_REVERSE; - cl_git_pass(git_diff_patch_from_blobs(&p, d, NULL, e, NULL, &opts)); + cl_git_pass(git_patch_from_blobs(&p, d, NULL, e, NULL, &opts)); cl_assert(p != NULL); - delta = git_diff_patch_delta(p); + delta = git_patch_delta(p); cl_assert(delta != NULL); cl_assert_equal_i(GIT_DELTA_ADDED, delta->status); cl_assert(git_oid_iszero(&delta->old_file.oid)); @@ -359,44 +359,44 @@ void test_diff_blob__can_compare_against_null_blobs_with_patch(void) cl_assert(git_oid_equal(git_blob_id(d), &delta->new_file.oid)); cl_assert_equal_sz(git_blob_rawsize(d), delta->new_file.size); - cl_assert_equal_i(1, (int)git_diff_patch_num_hunks(p)); - cl_assert_equal_i(14, git_diff_patch_num_lines_in_hunk(p, 0)); + cl_assert_equal_i(1, (int)git_patch_num_hunks(p)); + cl_assert_equal_i(14, git_patch_num_lines_in_hunk(p, 0)); - for (line = 0; line < git_diff_patch_num_lines_in_hunk(p, 0); ++line) { - cl_git_pass(git_diff_patch_get_line_in_hunk( + for (line = 0; line < git_patch_num_lines_in_hunk(p, 0); ++line) { + cl_git_pass(git_patch_get_line_in_hunk( &origin, NULL, NULL, NULL, NULL, p, 0, line)); cl_assert_equal_i(GIT_DIFF_LINE_ADDITION, (int)origin); } - git_diff_patch_free(p); + git_patch_free(p); opts.flags ^= GIT_DIFF_REVERSE; - cl_git_pass(git_diff_patch_from_blobs(&p, alien, NULL, NULL, NULL, &opts)); + cl_git_pass(git_patch_from_blobs(&p, alien, NULL, NULL, NULL, &opts)); cl_assert(p != NULL); - delta = git_diff_patch_delta(p); + delta = git_patch_delta(p); cl_assert(delta != NULL); cl_assert_equal_i(GIT_DELTA_DELETED, delta->status); cl_assert((delta->flags & GIT_DIFF_FLAG_BINARY) != 0); - cl_assert_equal_i(0, (int)git_diff_patch_num_hunks(p)); + cl_assert_equal_i(0, (int)git_patch_num_hunks(p)); - git_diff_patch_free(p); + git_patch_free(p); - cl_git_pass(git_diff_patch_from_blobs(&p, NULL, NULL, alien, NULL, &opts)); + cl_git_pass(git_patch_from_blobs(&p, NULL, NULL, alien, NULL, &opts)); cl_assert(p != NULL); - delta = git_diff_patch_delta(p); + delta = git_patch_delta(p); cl_assert(delta != NULL); cl_assert_equal_i(GIT_DELTA_ADDED, delta->status); cl_assert((delta->flags & GIT_DIFF_FLAG_BINARY) != 0); - cl_assert_equal_i(0, (int)git_diff_patch_num_hunks(p)); + cl_assert_equal_i(0, (int)git_patch_num_hunks(p)); - git_diff_patch_free(p); + git_patch_free(p); } static void assert_identical_blobs_comparison(diff_expects *expected) @@ -437,13 +437,13 @@ void test_diff_blob__can_compare_identical_blobs(void) void test_diff_blob__can_compare_identical_blobs_with_patch(void) { - git_diff_patch *p; + git_patch *p; const git_diff_delta *delta; - cl_git_pass(git_diff_patch_from_blobs(&p, d, NULL, d, NULL, &opts)); + cl_git_pass(git_patch_from_blobs(&p, d, NULL, d, NULL, &opts)); cl_assert(p != NULL); - delta = git_diff_patch_delta(p); + delta = git_patch_delta(p); cl_assert(delta != NULL); cl_assert_equal_i(GIT_DELTA_UNMODIFIED, delta->status); cl_assert_equal_sz(delta->old_file.size, git_blob_rawsize(d)); @@ -451,13 +451,13 @@ void test_diff_blob__can_compare_identical_blobs_with_patch(void) cl_assert_equal_sz(delta->new_file.size, git_blob_rawsize(d)); cl_assert(git_oid_equal(git_blob_id(d), &delta->new_file.oid)); - cl_assert_equal_i(0, (int)git_diff_patch_num_hunks(p)); - git_diff_patch_free(p); + cl_assert_equal_i(0, (int)git_patch_num_hunks(p)); + git_patch_free(p); - cl_git_pass(git_diff_patch_from_blobs(&p, NULL, NULL, NULL, NULL, &opts)); + cl_git_pass(git_patch_from_blobs(&p, NULL, NULL, NULL, NULL, &opts)); cl_assert(p != NULL); - delta = git_diff_patch_delta(p); + delta = git_patch_delta(p); cl_assert(delta != NULL); cl_assert_equal_i(GIT_DELTA_UNMODIFIED, delta->status); cl_assert_equal_sz(0, delta->old_file.size); @@ -465,14 +465,14 @@ void test_diff_blob__can_compare_identical_blobs_with_patch(void) cl_assert_equal_sz(0, delta->new_file.size); cl_assert(git_oid_iszero(&delta->new_file.oid)); - cl_assert_equal_i(0, (int)git_diff_patch_num_hunks(p)); - git_diff_patch_free(p); + cl_assert_equal_i(0, (int)git_patch_num_hunks(p)); + git_patch_free(p); - cl_git_pass(git_diff_patch_from_blobs(&p, alien, NULL, alien, NULL, &opts)); + cl_git_pass(git_patch_from_blobs(&p, alien, NULL, alien, NULL, &opts)); cl_assert(p != NULL); - cl_assert_equal_i(GIT_DELTA_UNMODIFIED, git_diff_patch_delta(p)->status); - cl_assert_equal_i(0, (int)git_diff_patch_num_hunks(p)); - git_diff_patch_free(p); + cl_assert_equal_i(GIT_DELTA_UNMODIFIED, git_patch_delta(p)->status); + cl_assert_equal_i(0, (int)git_patch_num_hunks(p)); + git_patch_free(p); } static void assert_binary_blobs_comparison(diff_expects *expected) @@ -693,7 +693,7 @@ void test_diff_blob__can_compare_blob_to_buffer(void) void test_diff_blob__can_compare_blob_to_buffer_with_patch(void) { - git_diff_patch *p; + git_patch *p; git_blob *a; git_oid a_oid; const char *a_content = "Hello from the root\n"; @@ -705,58 +705,58 @@ void test_diff_blob__can_compare_blob_to_buffer_with_patch(void) cl_git_pass(git_blob_lookup_prefix(&a, g_repo, &a_oid, 4)); /* diff from blob a to content of b */ - cl_git_pass(git_diff_patch_from_blob_and_buffer( + cl_git_pass(git_patch_from_blob_and_buffer( &p, a, NULL, b_content, strlen(b_content), NULL, &opts)); cl_assert(p != NULL); - cl_assert_equal_i(GIT_DELTA_MODIFIED, git_diff_patch_delta(p)->status); - cl_assert_equal_i(1, (int)git_diff_patch_num_hunks(p)); - cl_assert_equal_i(6, git_diff_patch_num_lines_in_hunk(p, 0)); + cl_assert_equal_i(GIT_DELTA_MODIFIED, git_patch_delta(p)->status); + cl_assert_equal_i(1, (int)git_patch_num_hunks(p)); + cl_assert_equal_i(6, git_patch_num_lines_in_hunk(p, 0)); - cl_git_pass(git_diff_patch_line_stats(&tc, &ta, &td, p)); + cl_git_pass(git_patch_line_stats(&tc, &ta, &td, p)); cl_assert_equal_i(1, (int)tc); cl_assert_equal_i(5, (int)ta); cl_assert_equal_i(0, (int)td); - git_diff_patch_free(p); + git_patch_free(p); /* diff from blob a to content of a */ opts.flags |= GIT_DIFF_INCLUDE_UNMODIFIED; - cl_git_pass(git_diff_patch_from_blob_and_buffer( + cl_git_pass(git_patch_from_blob_and_buffer( &p, a, NULL, a_content, strlen(a_content), NULL, &opts)); cl_assert(p != NULL); - cl_assert_equal_i(GIT_DELTA_UNMODIFIED, git_diff_patch_delta(p)->status); - cl_assert_equal_i(0, (int)git_diff_patch_num_hunks(p)); - git_diff_patch_free(p); + cl_assert_equal_i(GIT_DELTA_UNMODIFIED, git_patch_delta(p)->status); + cl_assert_equal_i(0, (int)git_patch_num_hunks(p)); + git_patch_free(p); /* diff from NULL blob to content of a */ - cl_git_pass(git_diff_patch_from_blob_and_buffer( + cl_git_pass(git_patch_from_blob_and_buffer( &p, NULL, NULL, a_content, strlen(a_content), NULL, &opts)); cl_assert(p != NULL); - cl_assert_equal_i(GIT_DELTA_ADDED, git_diff_patch_delta(p)->status); - cl_assert_equal_i(1, (int)git_diff_patch_num_hunks(p)); - cl_assert_equal_i(1, git_diff_patch_num_lines_in_hunk(p, 0)); - git_diff_patch_free(p); + cl_assert_equal_i(GIT_DELTA_ADDED, git_patch_delta(p)->status); + cl_assert_equal_i(1, (int)git_patch_num_hunks(p)); + cl_assert_equal_i(1, git_patch_num_lines_in_hunk(p, 0)); + git_patch_free(p); /* diff from blob a to NULL buffer */ - cl_git_pass(git_diff_patch_from_blob_and_buffer( + cl_git_pass(git_patch_from_blob_and_buffer( &p, a, NULL, NULL, 0, NULL, &opts)); cl_assert(p != NULL); - cl_assert_equal_i(GIT_DELTA_DELETED, git_diff_patch_delta(p)->status); - cl_assert_equal_i(1, (int)git_diff_patch_num_hunks(p)); - cl_assert_equal_i(1, git_diff_patch_num_lines_in_hunk(p, 0)); - git_diff_patch_free(p); + cl_assert_equal_i(GIT_DELTA_DELETED, git_patch_delta(p)->status); + cl_assert_equal_i(1, (int)git_patch_num_hunks(p)); + cl_assert_equal_i(1, git_patch_num_lines_in_hunk(p, 0)); + git_patch_free(p); /* diff with reverse */ opts.flags ^= GIT_DIFF_REVERSE; - cl_git_pass(git_diff_patch_from_blob_and_buffer( + cl_git_pass(git_patch_from_blob_and_buffer( &p, a, NULL, NULL, 0, NULL, &opts)); cl_assert(p != NULL); - cl_assert_equal_i(GIT_DELTA_ADDED, git_diff_patch_delta(p)->status); - cl_assert_equal_i(1, (int)git_diff_patch_num_hunks(p)); - cl_assert_equal_i(1, git_diff_patch_num_lines_in_hunk(p, 0)); - git_diff_patch_free(p); + cl_assert_equal_i(GIT_DELTA_ADDED, git_patch_delta(p)->status); + cl_assert_equal_i(1, (int)git_patch_num_hunks(p)); + cl_assert_equal_i(1, git_patch_num_lines_in_hunk(p, 0)); + git_patch_free(p); git_blob_free(a); } @@ -853,7 +853,7 @@ void test_diff_blob__using_path_and_attributes(void) "0123456789\n\x01\x02\x03\x04\x05\x06\x07\x08\x09\x00\n0123456789\n"; size_t bin_len = 33; const char *changed; - git_diff_patch *p; + git_patch *p; char *pout; /* set up custom diff drivers and 'diff' attribute mappings for them */ @@ -950,9 +950,9 @@ void test_diff_blob__using_path_and_attributes(void) cl_assert_equal_i(3, expected.line_adds); cl_assert_equal_i(0, expected.line_dels); - cl_git_pass(git_diff_patch_from_blob_and_buffer( + cl_git_pass(git_patch_from_blob_and_buffer( &p, nonbin, "zzz.normal", changed, strlen(changed), NULL, &opts)); - cl_git_pass(git_diff_patch_to_str(&pout, p)); + cl_git_pass(git_patch_to_str(&pout, p)); cl_assert_equal_s( "diff --git a/zzz.normal b/zzz.normal\n" "index 45141a7..75b0dbb 100644\n" @@ -963,21 +963,21 @@ void test_diff_blob__using_path_and_attributes(void) "+And more\n" "+Go here\n", pout); git__free(pout); - git_diff_patch_free(p); + git_patch_free(p); - cl_git_pass(git_diff_patch_from_blob_and_buffer( + cl_git_pass(git_patch_from_blob_and_buffer( &p, nonbin, "zzz.binary", changed, strlen(changed), NULL, &opts)); - cl_git_pass(git_diff_patch_to_str(&pout, p)); + cl_git_pass(git_patch_to_str(&pout, p)); cl_assert_equal_s( "diff --git a/zzz.binary b/zzz.binary\n" "index 45141a7..75b0dbb 100644\n" "Binary files a/zzz.binary and b/zzz.binary differ\n", pout); git__free(pout); - git_diff_patch_free(p); + git_patch_free(p); - cl_git_pass(git_diff_patch_from_blob_and_buffer( + cl_git_pass(git_patch_from_blob_and_buffer( &p, nonbin, "zzz.alphary", changed, strlen(changed), NULL, &opts)); - cl_git_pass(git_diff_patch_to_str(&pout, p)); + cl_git_pass(git_patch_to_str(&pout, p)); cl_assert_equal_s( "diff --git a/zzz.alphary b/zzz.alphary\n" "index 45141a7..75b0dbb 100644\n" @@ -988,11 +988,11 @@ void test_diff_blob__using_path_and_attributes(void) "+And more\n" "+Go here\n", pout); git__free(pout); - git_diff_patch_free(p); + git_patch_free(p); - cl_git_pass(git_diff_patch_from_blob_and_buffer( + cl_git_pass(git_patch_from_blob_and_buffer( &p, nonbin, "zzz.numary", changed, strlen(changed), NULL, &opts)); - cl_git_pass(git_diff_patch_to_str(&pout, p)); + cl_git_pass(git_patch_to_str(&pout, p)); cl_assert_equal_s( "diff --git a/zzz.numary b/zzz.numary\n" "index 45141a7..75b0dbb 100644\n" @@ -1003,7 +1003,7 @@ void test_diff_blob__using_path_and_attributes(void) "+And more\n" "+Go here\n", pout); git__free(pout); - git_diff_patch_free(p); + git_patch_free(p); /* "0123456789\n\x01\x02\x03\x04\x05\x06\x07\x08\x09\x00\n0123456789\n" * 33 bytes @@ -1011,19 +1011,19 @@ void test_diff_blob__using_path_and_attributes(void) changed = "0123456789\n\x01\x02\x03\x04\x05\x06\x07\x08\x09\x00\nreplace a line\n"; - cl_git_pass(git_diff_patch_from_blob_and_buffer( + cl_git_pass(git_patch_from_blob_and_buffer( &p, bin, "zzz.normal", changed, 37, NULL, &opts)); - cl_git_pass(git_diff_patch_to_str(&pout, p)); + cl_git_pass(git_patch_to_str(&pout, p)); cl_assert_equal_s( "diff --git a/zzz.normal b/zzz.normal\n" "index b435cd5..1604519 100644\n" "Binary files a/zzz.normal and b/zzz.normal differ\n", pout); git__free(pout); - git_diff_patch_free(p); + git_patch_free(p); - cl_git_pass(git_diff_patch_from_blob_and_buffer( + cl_git_pass(git_patch_from_blob_and_buffer( &p, bin, "zzz.textary", changed, 37, NULL, &opts)); - cl_git_pass(git_diff_patch_to_str(&pout, p)); + cl_git_pass(git_patch_to_str(&pout, p)); cl_assert_equal_s( "diff --git a/zzz.textary b/zzz.textary\n" "index b435cd5..1604519 100644\n" @@ -1033,11 +1033,11 @@ void test_diff_blob__using_path_and_attributes(void) "-0123456789\n" "+replace a line\n", pout); git__free(pout); - git_diff_patch_free(p); + git_patch_free(p); - cl_git_pass(git_diff_patch_from_blob_and_buffer( + cl_git_pass(git_patch_from_blob_and_buffer( &p, bin, "zzz.textalphary", changed, 37, NULL, &opts)); - cl_git_pass(git_diff_patch_to_str(&pout, p)); + cl_git_pass(git_patch_to_str(&pout, p)); cl_assert_equal_s( "diff --git a/zzz.textalphary b/zzz.textalphary\n" "index b435cd5..1604519 100644\n" @@ -1047,11 +1047,11 @@ void test_diff_blob__using_path_and_attributes(void) "-0123456789\n" "+replace a line\n", pout); git__free(pout); - git_diff_patch_free(p); + git_patch_free(p); - cl_git_pass(git_diff_patch_from_blob_and_buffer( + cl_git_pass(git_patch_from_blob_and_buffer( &p, bin, "zzz.textnumary", changed, 37, NULL, &opts)); - cl_git_pass(git_diff_patch_to_str(&pout, p)); + cl_git_pass(git_patch_to_str(&pout, p)); cl_assert_equal_s( "diff --git a/zzz.textnumary b/zzz.textnumary\n" "index b435cd5..1604519 100644\n" @@ -1061,7 +1061,7 @@ void test_diff_blob__using_path_and_attributes(void) "-0123456789\n" "+replace a line\n", pout); git__free(pout); - git_diff_patch_free(p); + git_patch_free(p); git_blob_free(nonbin); git_blob_free(bin); diff --git a/tests-clar/diff/diff_helpers.c b/tests-clar/diff/diff_helpers.c index 3452f231d..34ef1df5a 100644 --- a/tests-clar/diff/diff_helpers.c +++ b/tests-clar/diff/diff_helpers.c @@ -95,7 +95,7 @@ int diff_print_file_cb( int diff_hunk_cb( const git_diff_delta *delta, - const git_diff_range *range, + const git_diff_hunk *range, const char *header, size_t header_len, void *payload) @@ -115,7 +115,7 @@ int diff_hunk_cb( int diff_line_cb( const git_diff_delta *delta, - const git_diff_range *range, + const git_diff_hunk *range, char line_origin, const char *content, size_t content_len, @@ -149,25 +149,25 @@ int diff_line_cb( } int diff_foreach_via_iterator( - git_diff_list *diff, + git_diff *diff, git_diff_file_cb file_cb, git_diff_hunk_cb hunk_cb, - git_diff_data_cb line_cb, + git_diff_line_cb line_cb, void *data) { size_t d, num_d = git_diff_num_deltas(diff); for (d = 0; d < num_d; ++d) { - git_diff_patch *patch; + git_patch *patch; const git_diff_delta *delta; size_t h, num_h; - cl_git_pass(git_diff_get_patch(&patch, &delta, diff, d)); + cl_git_pass(git_patch_from_diff(&patch, &delta, diff, d)); cl_assert(delta); /* call file_cb for this file */ if (file_cb != NULL && file_cb(delta, (float)d / num_d, data) != 0) { - git_diff_patch_free(patch); + git_patch_free(patch); goto abort; } @@ -179,22 +179,22 @@ int diff_foreach_via_iterator( } if (!hunk_cb && !line_cb) { - git_diff_patch_free(patch); + git_patch_free(patch); continue; } - num_h = git_diff_patch_num_hunks(patch); + num_h = git_patch_num_hunks(patch); for (h = 0; h < num_h; h++) { - const git_diff_range *range; + const git_diff_hunk *range; const char *hdr; size_t hdr_len, l, num_l; - cl_git_pass(git_diff_patch_get_hunk( + cl_git_pass(git_patch_get_hunk( &range, &hdr, &hdr_len, &num_l, patch, h)); if (hunk_cb && hunk_cb(delta, range, hdr, hdr_len, data) != 0) { - git_diff_patch_free(patch); + git_patch_free(patch); goto abort; } @@ -204,19 +204,19 @@ int diff_foreach_via_iterator( size_t line_len; int old_lineno, new_lineno; - cl_git_pass(git_diff_patch_get_line_in_hunk( + cl_git_pass(git_patch_get_line_in_hunk( &origin, &line, &line_len, &old_lineno, &new_lineno, patch, h, l)); if (line_cb && line_cb(delta, range, origin, line, line_len, data) != 0) { - git_diff_patch_free(patch); + git_patch_free(patch); goto abort; } } } - git_diff_patch_free(patch); + git_patch_free(patch); } return 0; @@ -228,7 +228,7 @@ abort: static int diff_print_cb( const git_diff_delta *delta, - const git_diff_range *range, + const git_diff_hunk *range, char line_origin, /**< GIT_DIFF_LINE_... value from above */ const char *content, size_t content_len, @@ -243,12 +243,12 @@ static int diff_print_cb( return 0; } -void diff_print(FILE *fp, git_diff_list *diff) +void diff_print(FILE *fp, git_diff *diff) { cl_git_pass(git_diff_print_patch(diff, diff_print_cb, fp ? fp : stderr)); } -void diff_print_raw(FILE *fp, git_diff_list *diff) +void diff_print_raw(FILE *fp, git_diff *diff) { cl_git_pass(git_diff_print_raw(diff, diff_print_cb, fp ? fp : stderr)); } diff --git a/tests-clar/diff/diff_helpers.h b/tests-clar/diff/diff_helpers.h index bb76d0076..e3ad61f29 100644 --- a/tests-clar/diff/diff_helpers.h +++ b/tests-clar/diff/diff_helpers.h @@ -44,25 +44,25 @@ extern int diff_print_file_cb( extern int diff_hunk_cb( const git_diff_delta *delta, - const git_diff_range *range, + const git_diff_hunk *range, const char *header, size_t header_len, void *cb_data); extern int diff_line_cb( const git_diff_delta *delta, - const git_diff_range *range, + const git_diff_hunk *range, char line_origin, const char *content, size_t content_len, void *cb_data); extern int diff_foreach_via_iterator( - git_diff_list *diff, + git_diff *diff, git_diff_file_cb file_cb, git_diff_hunk_cb hunk_cb, - git_diff_data_cb line_cb, + git_diff_line_cb line_cb, void *data); -extern void diff_print(FILE *fp, git_diff_list *diff); -extern void diff_print_raw(FILE *fp, git_diff_list *diff); +extern void diff_print(FILE *fp, git_diff *diff); +extern void diff_print_raw(FILE *fp, git_diff *diff); diff --git a/tests-clar/diff/diffiter.c b/tests-clar/diff/diffiter.c index ea5908475..48b56e20e 100644 --- a/tests-clar/diff/diffiter.c +++ b/tests-clar/diff/diffiter.c @@ -13,7 +13,7 @@ void test_diff_diffiter__cleanup(void) void test_diff_diffiter__create(void) { git_repository *repo = cl_git_sandbox_init("attr"); - git_diff_list *diff; + git_diff *diff; size_t d, num_d; cl_git_pass(git_diff_index_to_workdir(&diff, repo, NULL, NULL)); @@ -21,16 +21,16 @@ void test_diff_diffiter__create(void) num_d = git_diff_num_deltas(diff); for (d = 0; d < num_d; ++d) { const git_diff_delta *delta; - cl_git_pass(git_diff_get_patch(NULL, &delta, diff, d)); + cl_git_pass(git_patch_from_diff(NULL, &delta, diff, d)); } - git_diff_list_free(diff); + git_diff_free(diff); } void test_diff_diffiter__iterate_files_1(void) { git_repository *repo = cl_git_sandbox_init("attr"); - git_diff_list *diff; + git_diff *diff; size_t d, num_d; diff_expects exp = { 0 }; @@ -40,20 +40,20 @@ void test_diff_diffiter__iterate_files_1(void) for (d = 0; d < num_d; ++d) { const git_diff_delta *delta; - cl_git_pass(git_diff_get_patch(NULL, &delta, diff, d)); + cl_git_pass(git_patch_from_diff(NULL, &delta, diff, d)); cl_assert(delta != NULL); diff_file_cb(delta, (float)d / (float)num_d, &exp); } cl_assert_equal_sz(6, exp.files); - git_diff_list_free(diff); + git_diff_free(diff); } void test_diff_diffiter__iterate_files_2(void) { git_repository *repo = cl_git_sandbox_init("status"); - git_diff_list *diff; + git_diff *diff; size_t d, num_d; int count = 0; @@ -64,20 +64,20 @@ void test_diff_diffiter__iterate_files_2(void) for (d = 0; d < num_d; ++d) { const git_diff_delta *delta; - cl_git_pass(git_diff_get_patch(NULL, &delta, diff, d)); + cl_git_pass(git_patch_from_diff(NULL, &delta, diff, d)); cl_assert(delta != NULL); count++; } cl_assert_equal_i(8, count); - git_diff_list_free(diff); + git_diff_free(diff); } void test_diff_diffiter__iterate_files_and_hunks(void) { git_repository *repo = cl_git_sandbox_init("status"); git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; + git_diff *diff = NULL; size_t d, num_d; int file_count = 0, hunk_count = 0; @@ -90,25 +90,25 @@ void test_diff_diffiter__iterate_files_and_hunks(void) num_d = git_diff_num_deltas(diff); for (d = 0; d < num_d; ++d) { - git_diff_patch *patch; + git_patch *patch; const git_diff_delta *delta; size_t h, num_h; - cl_git_pass(git_diff_get_patch(&patch, &delta, diff, d)); + cl_git_pass(git_patch_from_diff(&patch, &delta, diff, d)); cl_assert(delta); cl_assert(patch); file_count++; - num_h = git_diff_patch_num_hunks(patch); + num_h = git_patch_num_hunks(patch); for (h = 0; h < num_h; h++) { - const git_diff_range *range; + const git_diff_hunk *range; const char *header; size_t header_len, num_l; - cl_git_pass(git_diff_patch_get_hunk( + cl_git_pass(git_patch_get_hunk( &range, &header, &header_len, &num_l, patch, h)); cl_assert(range); @@ -117,20 +117,20 @@ void test_diff_diffiter__iterate_files_and_hunks(void) hunk_count++; } - git_diff_patch_free(patch); + git_patch_free(patch); } cl_assert_equal_i(13, file_count); cl_assert_equal_i(8, hunk_count); - git_diff_list_free(diff); + git_diff_free(diff); } void test_diff_diffiter__max_size_threshold(void) { git_repository *repo = cl_git_sandbox_init("status"); git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; + git_diff *diff = NULL; int file_count = 0, binary_count = 0, hunk_count = 0; size_t d, num_d; @@ -142,27 +142,27 @@ void test_diff_diffiter__max_size_threshold(void) num_d = git_diff_num_deltas(diff); for (d = 0; d < num_d; ++d) { - git_diff_patch *patch; + git_patch *patch; const git_diff_delta *delta; - cl_git_pass(git_diff_get_patch(&patch, &delta, diff, d)); + cl_git_pass(git_patch_from_diff(&patch, &delta, diff, d)); cl_assert(delta); cl_assert(patch); file_count++; - hunk_count += (int)git_diff_patch_num_hunks(patch); + hunk_count += (int)git_patch_num_hunks(patch); assert((delta->flags & (GIT_DIFF_FLAG_BINARY|GIT_DIFF_FLAG_NOT_BINARY)) != 0); binary_count += ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0); - git_diff_patch_free(patch); + git_patch_free(patch); } cl_assert_equal_i(13, file_count); cl_assert_equal_i(0, binary_count); cl_assert_equal_i(8, hunk_count); - git_diff_list_free(diff); + git_diff_free(diff); /* try again with low file size threshold */ @@ -177,18 +177,18 @@ void test_diff_diffiter__max_size_threshold(void) num_d = git_diff_num_deltas(diff); for (d = 0; d < num_d; ++d) { - git_diff_patch *patch; + git_patch *patch; const git_diff_delta *delta; - cl_git_pass(git_diff_get_patch(&patch, &delta, diff, d)); + cl_git_pass(git_patch_from_diff(&patch, &delta, diff, d)); file_count++; - hunk_count += (int)git_diff_patch_num_hunks(patch); + hunk_count += (int)git_patch_num_hunks(patch); assert((delta->flags & (GIT_DIFF_FLAG_BINARY|GIT_DIFF_FLAG_NOT_BINARY)) != 0); binary_count += ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0); - git_diff_patch_free(patch); + git_patch_free(patch); } cl_assert_equal_i(13, file_count); @@ -200,7 +200,7 @@ void test_diff_diffiter__max_size_threshold(void) cl_assert_equal_i(3, binary_count); cl_assert_equal_i(5, hunk_count); - git_diff_list_free(diff); + git_diff_free(diff); } @@ -208,7 +208,7 @@ void test_diff_diffiter__iterate_all(void) { git_repository *repo = cl_git_sandbox_init("status"); git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; + git_diff *diff = NULL; diff_expects exp = {0}; size_t d, num_d; @@ -220,21 +220,21 @@ void test_diff_diffiter__iterate_all(void) num_d = git_diff_num_deltas(diff); for (d = 0; d < num_d; ++d) { - git_diff_patch *patch; + git_patch *patch; const git_diff_delta *delta; size_t h, num_h; - cl_git_pass(git_diff_get_patch(&patch, &delta, diff, d)); + cl_git_pass(git_patch_from_diff(&patch, &delta, diff, d)); cl_assert(patch && delta); exp.files++; - num_h = git_diff_patch_num_hunks(patch); + num_h = git_patch_num_hunks(patch); for (h = 0; h < num_h; h++) { - const git_diff_range *range; + const git_diff_hunk *range; const char *header; size_t header_len, l, num_l; - cl_git_pass(git_diff_patch_get_hunk( + cl_git_pass(git_patch_get_hunk( &range, &header, &header_len, &num_l, patch, h)); cl_assert(range && header); exp.hunks++; @@ -244,33 +244,33 @@ void test_diff_diffiter__iterate_all(void) const char *content; size_t content_len; - cl_git_pass(git_diff_patch_get_line_in_hunk( + cl_git_pass(git_patch_get_line_in_hunk( &origin, &content, &content_len, NULL, NULL, patch, h, l)); cl_assert(content); exp.lines++; } } - git_diff_patch_free(patch); + git_patch_free(patch); } cl_assert_equal_i(13, exp.files); cl_assert_equal_i(8, exp.hunks); cl_assert_equal_i(14, exp.lines); - git_diff_list_free(diff); + git_diff_free(diff); } -static void iterate_over_patch(git_diff_patch *patch, diff_expects *exp) +static void iterate_over_patch(git_patch *patch, diff_expects *exp) { - size_t h, num_h = git_diff_patch_num_hunks(patch), num_l; + size_t h, num_h = git_patch_num_hunks(patch), num_l; exp->files++; exp->hunks += (int)num_h; /* let's iterate in reverse, just because we can! */ for (h = 1, num_l = 0; h <= num_h; ++h) - num_l += git_diff_patch_num_lines_in_hunk(patch, num_h - h); + num_l += git_patch_num_lines_in_hunk(patch, num_h - h); exp->lines += (int)num_l; } @@ -281,9 +281,9 @@ void test_diff_diffiter__iterate_randomly_while_saving_state(void) { git_repository *repo = cl_git_sandbox_init("status"); git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; + git_diff *diff = NULL; diff_expects exp = {0}; - git_diff_patch *patches[PATCH_CACHE]; + git_patch *patches[PATCH_CACHE]; size_t p, d, num_d; memset(patches, 0, sizeof(patches)); @@ -308,32 +308,32 @@ void test_diff_diffiter__iterate_randomly_while_saving_state(void) for (d = 0; d < num_d; ++d) { /* take old patch */ - git_diff_patch *patch = patches[p]; + git_patch *patch = patches[p]; patches[p] = NULL; /* cache new patch */ - cl_git_pass(git_diff_get_patch(&patches[p], NULL, diff, d)); + cl_git_pass(git_patch_from_diff(&patches[p], NULL, diff, d)); cl_assert(patches[p] != NULL); /* process old patch if non-NULL */ if (patch != NULL) { iterate_over_patch(patch, &exp); - git_diff_patch_free(patch); + git_patch_free(patch); } p = rand() % PATCH_CACHE; } /* free diff list now - refcounts should keep things safe */ - git_diff_list_free(diff); + git_diff_free(diff); /* process remaining unprocessed patches */ for (p = 0; p < PATCH_CACHE; p++) { - git_diff_patch *patch = patches[p]; + git_patch *patch = patches[p]; if (patch != NULL) { iterate_over_patch(patch, &exp); - git_diff_patch_free(patch); + git_patch_free(patch); } } @@ -416,7 +416,7 @@ static const char *expected_patch_text[8] = { void test_diff_diffiter__iterate_and_generate_patch_text(void) { git_repository *repo = cl_git_sandbox_init("status"); - git_diff_list *diff; + git_diff *diff; size_t d, num_d; cl_git_pass(git_diff_index_to_workdir(&diff, repo, NULL, NULL)); @@ -425,28 +425,28 @@ void test_diff_diffiter__iterate_and_generate_patch_text(void) cl_assert_equal_i(8, (int)num_d); for (d = 0; d < num_d; ++d) { - git_diff_patch *patch; + git_patch *patch; char *text; - cl_git_pass(git_diff_get_patch(&patch, NULL, diff, d)); + cl_git_pass(git_patch_from_diff(&patch, NULL, diff, d)); cl_assert(patch != NULL); - cl_git_pass(git_diff_patch_to_str(&text, patch)); + cl_git_pass(git_patch_to_str(&text, patch)); cl_assert_equal_s(expected_patch_text[d], text); git__free(text); - git_diff_patch_free(patch); + git_patch_free(patch); } - git_diff_list_free(diff); + git_diff_free(diff); } void test_diff_diffiter__checks_options_version(void) { git_repository *repo = cl_git_sandbox_init("status"); git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; + git_diff *diff = NULL; const git_error *err; opts.version = 0; diff --git a/tests-clar/diff/drivers.c b/tests-clar/diff/drivers.c index 719d229fc..518f24e7b 100644 --- a/tests-clar/diff/drivers.c +++ b/tests-clar/diff/drivers.c @@ -20,8 +20,8 @@ void test_diff_drivers__patterns(void) git_config *cfg; const char *one_sha = "19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13"; git_tree *one; - git_diff_list *diff; - git_diff_patch *patch; + git_diff *diff; + git_patch *patch; char *text; const char *expected0 = "diff --git a/untimely.txt b/untimely.txt\nindex 9a69d96..57fd0cf 100644\n--- a/untimely.txt\n+++ b/untimely.txt\n@@ -22,3 +22,5 @@ Comes through the blood of the vanguards who\n dreamed--too soon--it had sounded.\r\n \r\n -- Rudyard Kipling\r\n+\r\n+Some new stuff\r\n"; const char *expected1 = "diff --git a/untimely.txt b/untimely.txt\nindex 9a69d96..57fd0cf 100644\nBinary files a/untimely.txt and b/untimely.txt differ\n"; @@ -35,7 +35,7 @@ void test_diff_drivers__patterns(void) cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, one, NULL)); cl_assert_equal_i(0, (int)git_diff_num_deltas(diff)); - git_diff_list_free(diff); + git_diff_free(diff); /* default diff */ @@ -44,13 +44,13 @@ void test_diff_drivers__patterns(void) cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, one, NULL)); cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); - cl_git_pass(git_diff_get_patch(&patch, NULL, diff, 0)); - cl_git_pass(git_diff_patch_to_str(&text, patch)); + cl_git_pass(git_patch_from_diff(&patch, NULL, diff, 0)); + cl_git_pass(git_patch_to_str(&text, patch)); cl_assert_equal_s(expected0, text); git__free(text); - git_diff_patch_free(patch); - git_diff_list_free(diff); + git_patch_free(patch); + git_diff_free(diff); /* attribute diff set to false */ @@ -59,13 +59,13 @@ void test_diff_drivers__patterns(void) cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, one, NULL)); cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); - cl_git_pass(git_diff_get_patch(&patch, NULL, diff, 0)); - cl_git_pass(git_diff_patch_to_str(&text, patch)); + cl_git_pass(git_patch_from_diff(&patch, NULL, diff, 0)); + cl_git_pass(git_patch_to_str(&text, patch)); cl_assert_equal_s(expected1, text); git__free(text); - git_diff_patch_free(patch); - git_diff_list_free(diff); + git_patch_free(patch); + git_diff_free(diff); /* attribute diff set to unconfigured value (should use default) */ @@ -74,13 +74,13 @@ void test_diff_drivers__patterns(void) cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, one, NULL)); cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); - cl_git_pass(git_diff_get_patch(&patch, NULL, diff, 0)); - cl_git_pass(git_diff_patch_to_str(&text, patch)); + cl_git_pass(git_patch_from_diff(&patch, NULL, diff, 0)); + cl_git_pass(git_patch_to_str(&text, patch)); cl_assert_equal_s(expected0, text); git__free(text); - git_diff_patch_free(patch); - git_diff_list_free(diff); + git_patch_free(patch); + git_diff_free(diff); /* let's define that driver */ @@ -91,13 +91,13 @@ void test_diff_drivers__patterns(void) cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, one, NULL)); cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); - cl_git_pass(git_diff_get_patch(&patch, NULL, diff, 0)); - cl_git_pass(git_diff_patch_to_str(&text, patch)); + cl_git_pass(git_patch_from_diff(&patch, NULL, diff, 0)); + cl_git_pass(git_patch_to_str(&text, patch)); cl_assert_equal_s(expected1, text); git__free(text); - git_diff_patch_free(patch); - git_diff_list_free(diff); + git_patch_free(patch); + git_diff_free(diff); /* let's use a real driver with some regular expressions */ @@ -112,13 +112,13 @@ void test_diff_drivers__patterns(void) cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, one, NULL)); cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); - cl_git_pass(git_diff_get_patch(&patch, NULL, diff, 0)); - cl_git_pass(git_diff_patch_to_str(&text, patch)); + cl_git_pass(git_patch_from_diff(&patch, NULL, diff, 0)); + cl_git_pass(git_patch_to_str(&text, patch)); cl_assert_equal_s(expected2, text); git__free(text); - git_diff_patch_free(patch); - git_diff_list_free(diff); + git_patch_free(patch); + git_diff_free(diff); git_tree_free(one); } @@ -127,8 +127,8 @@ void test_diff_drivers__long_lines(void) { const char *base = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non nisi ligula. Ut viverra enim sed lobortis suscipit.\nPhasellus eget erat odio. Praesent at est iaculis, ultricies augue vel, dignissim risus. Suspendisse at nisi quis turpis fringilla rutrum id sit amet nulla.\nNam eget dolor fermentum, aliquet nisl at, convallis tellus. Pellentesque rhoncus erat enim, id porttitor elit euismod quis.\nMauris sollicitudin magna odio, non egestas libero vehicula ut. Etiam et quam velit. Fusce eget libero rhoncus, ultricies felis sit amet, egestas purus.\nAliquam in semper tellus. Pellentesque adipiscing rutrum velit, quis malesuada lacus consequat eget.\n"; git_index *idx; - git_diff_list *diff; - git_diff_patch *patch; + git_diff *diff; + git_patch *patch; char *actual; const char *expected = "diff --git a/longlines.txt b/longlines.txt\nindex c1ce6ef..0134431 100644\n--- a/longlines.txt\n+++ b/longlines.txt\n@@ -3,3 +3,5 @@ Phasellus eget erat odio. Praesent at est iaculis, ultricies augue vel, dignissi\n Nam eget dolor fermentum, aliquet nisl at, convallis tellus. Pellentesque rhoncus erat enim, id porttitor elit euismod quis.\n Mauris sollicitudin magna odio, non egestas libero vehicula ut. Etiam et quam velit. Fusce eget libero rhoncus, ultricies felis sit amet, egestas purus.\n Aliquam in semper tellus. Pellentesque adipiscing rutrum velit, quis malesuada lacus consequat eget.\n+newline\n+newline\n"; @@ -144,8 +144,8 @@ void test_diff_drivers__long_lines(void) cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, NULL)); cl_assert_equal_sz(1, git_diff_num_deltas(diff)); - cl_git_pass(git_diff_get_patch(&patch, NULL, diff, 0)); - cl_git_pass(git_diff_patch_to_str(&actual, patch)); + cl_git_pass(git_patch_from_diff(&patch, NULL, diff, 0)); + cl_git_pass(git_patch_to_str(&actual, patch)); /* if chmod not supported, overwrite mode bits since anything is possible */ if (!cl_is_chmod_supported()) { @@ -157,7 +157,7 @@ void test_diff_drivers__long_lines(void) cl_assert_equal_s(expected, actual); free(actual); - git_diff_patch_free(patch); - git_diff_list_free(diff); + git_patch_free(patch); + git_diff_free(diff); } diff --git a/tests-clar/diff/index.c b/tests-clar/diff/index.c index e1c617dae..8f4567137 100644 --- a/tests-clar/diff/index.c +++ b/tests-clar/diff/index.c @@ -21,7 +21,7 @@ void test_diff_index__0(void) git_tree *a = resolve_commit_oid_to_tree(g_repo, a_commit); git_tree *b = resolve_commit_oid_to_tree(g_repo, b_commit); git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; + git_diff *diff = NULL; diff_expects exp; cl_assert(a); @@ -56,7 +56,7 @@ void test_diff_index__0(void) cl_assert_equal_i(6, exp.line_adds); cl_assert_equal_i(2, exp.line_dels); - git_diff_list_free(diff); + git_diff_free(diff); diff = NULL; memset(&exp, 0, sizeof(exp)); @@ -84,7 +84,7 @@ void test_diff_index__0(void) cl_assert_equal_i(11, exp.line_adds); cl_assert_equal_i(2, exp.line_dels); - git_diff_list_free(diff); + git_diff_free(diff); diff = NULL; git_tree_free(a); @@ -114,7 +114,7 @@ void test_diff_index__1(void) git_tree *a = resolve_commit_oid_to_tree(g_repo, a_commit); git_tree *b = resolve_commit_oid_to_tree(g_repo, b_commit); git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; + git_diff *diff = NULL; diff_expects exp; cl_assert(a); @@ -134,7 +134,7 @@ void test_diff_index__1(void) cl_assert_equal_i(2, exp.files); - git_diff_list_free(diff); + git_diff_free(diff); diff = NULL; git_tree_free(a); @@ -146,7 +146,7 @@ void test_diff_index__checks_options_version(void) const char *a_commit = "26a125ee1bf"; git_tree *a = resolve_commit_oid_to_tree(g_repo, a_commit); git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; + git_diff *diff = NULL; const git_error *err; opts.version = 0; diff --git a/tests-clar/diff/notify.c b/tests-clar/diff/notify.c index 433b4a9c1..cc33cb71c 100644 --- a/tests-clar/diff/notify.c +++ b/tests-clar/diff/notify.c @@ -13,7 +13,7 @@ void test_diff_notify__cleanup(void) } static int assert_called_notifications( - const git_diff_list *diff_so_far, + const git_diff *diff_so_far, const git_diff_delta *delta_to_add, const char *matched_pathspec, void *payload) @@ -45,7 +45,7 @@ static void test_notify( int expected_diffed_files_count) { git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; + git_diff *diff = NULL; diff_expects exp; g_repo = cl_git_sandbox_init("status"); @@ -63,7 +63,7 @@ static void test_notify( cl_assert_equal_i(expected_diffed_files_count, exp.files); - git_diff_list_free(diff); + git_diff_free(diff); } void test_diff_notify__notify_single_pathspec(void) @@ -155,7 +155,7 @@ void test_diff_notify__notify_catchall(void) } static int abort_diff( - const git_diff_list *diff_so_far, + const git_diff *diff_so_far, const git_diff_delta *delta_to_add, const char *matched_pathspec, void *payload) @@ -171,7 +171,7 @@ static int abort_diff( void test_diff_notify__notify_cb_can_abort_diff(void) { git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; + git_diff *diff = NULL; char *pathspec = NULL; g_repo = cl_git_sandbox_init("status"); @@ -189,7 +189,7 @@ void test_diff_notify__notify_cb_can_abort_diff(void) } static int filter_all( - const git_diff_list *diff_so_far, + const git_diff *diff_so_far, const git_diff_delta *delta_to_add, const char *matched_pathspec, void *payload) @@ -205,7 +205,7 @@ static int filter_all( void test_diff_notify__notify_cb_can_be_used_as_filtering_function(void) { git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; + git_diff *diff = NULL; char *pathspec = NULL; diff_expects exp; @@ -224,5 +224,5 @@ void test_diff_notify__notify_cb_can_be_used_as_filtering_function(void) cl_assert_equal_i(0, exp.files); - git_diff_list_free(diff); + git_diff_free(diff); } diff --git a/tests-clar/diff/patch.c b/tests-clar/diff/patch.c index 171abc819..e5d8fca9a 100644 --- a/tests-clar/diff/patch.c +++ b/tests-clar/diff/patch.c @@ -26,7 +26,7 @@ void test_diff_patch__cleanup(void) static int check_removal_cb( const git_diff_delta *delta, - const git_diff_range *range, + const git_diff_hunk *range, char line_origin, const char *formatted_output, size_t output_len, @@ -86,7 +86,7 @@ void test_diff_patch__can_properly_display_the_removal_of_a_file(void) const char *one_sha = "26a125e"; const char *another_sha = "735b6a2"; git_tree *one, *another; - git_diff_list *diff; + git_diff *diff; g_repo = cl_git_sandbox_init("status"); @@ -97,7 +97,7 @@ void test_diff_patch__can_properly_display_the_removal_of_a_file(void) cl_git_pass(git_diff_print_patch(diff, check_removal_cb, NULL)); - git_diff_list_free(diff); + git_diff_free(diff); git_tree_free(another); git_tree_free(one); @@ -108,8 +108,8 @@ void test_diff_patch__to_string(void) const char *one_sha = "26a125e"; const char *another_sha = "735b6a2"; git_tree *one, *another; - git_diff_list *diff; - git_diff_patch *patch; + git_diff *diff; + git_patch *patch; char *text; const char *expected = "diff --git a/subdir.txt b/subdir.txt\ndeleted file mode 100644\nindex e8ee89e..0000000\n--- a/subdir.txt\n+++ /dev/null\n@@ -1,2 +0,0 @@\n-Is it a bird?\n-Is it a plane?\n"; @@ -122,20 +122,20 @@ void test_diff_patch__to_string(void) cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); - cl_git_pass(git_diff_get_patch(&patch, NULL, diff, 0)); + cl_git_pass(git_patch_from_diff(&patch, NULL, diff, 0)); - cl_git_pass(git_diff_patch_to_str(&text, patch)); + cl_git_pass(git_patch_to_str(&text, patch)); cl_assert_equal_s(expected, text); - cl_assert_equal_sz(31, git_diff_patch_size(patch, 0, 0, 0)); - cl_assert_equal_sz(31, git_diff_patch_size(patch, 1, 0, 0)); - cl_assert_equal_sz(31 + 16, git_diff_patch_size(patch, 1, 1, 0)); - cl_assert_equal_sz(strlen(expected), git_diff_patch_size(patch, 1, 1, 1)); + cl_assert_equal_sz(31, git_patch_size(patch, 0, 0, 0)); + cl_assert_equal_sz(31, git_patch_size(patch, 1, 0, 0)); + cl_assert_equal_sz(31 + 16, git_patch_size(patch, 1, 1, 0)); + cl_assert_equal_sz(strlen(expected), git_patch_size(patch, 1, 1, 1)); git__free(text); - git_diff_patch_free(patch); - git_diff_list_free(diff); + git_patch_free(patch); + git_diff_free(diff); git_tree_free(another); git_tree_free(one); } @@ -145,8 +145,8 @@ void test_diff_patch__config_options(void) const char *one_sha = "26a125e"; /* current HEAD */ git_tree *one; git_config *cfg; - git_diff_list *diff; - git_diff_patch *patch; + git_diff *diff; + git_patch *patch; char *text; git_diff_options opts = GIT_DIFF_OPTIONS_INIT; char *onefile = "staged_changes_modified_file"; @@ -167,24 +167,24 @@ void test_diff_patch__config_options(void) cl_git_pass(git_diff_tree_to_index(&diff, g_repo, one, NULL, &opts)); cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); - cl_git_pass(git_diff_get_patch(&patch, NULL, diff, 0)); - cl_git_pass(git_diff_patch_to_str(&text, patch)); + cl_git_pass(git_patch_from_diff(&patch, NULL, diff, 0)); + cl_git_pass(git_patch_to_str(&text, patch)); cl_assert_equal_s(expected1, text); git__free(text); - git_diff_patch_free(patch); - git_diff_list_free(diff); + git_patch_free(patch); + git_diff_free(diff); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); - cl_git_pass(git_diff_get_patch(&patch, NULL, diff, 0)); - cl_git_pass(git_diff_patch_to_str(&text, patch)); + cl_git_pass(git_patch_from_diff(&patch, NULL, diff, 0)); + cl_git_pass(git_patch_to_str(&text, patch)); cl_assert_equal_s(expected2, text); git__free(text); - git_diff_patch_free(patch); - git_diff_list_free(diff); + git_patch_free(patch); + git_diff_free(diff); cl_git_pass(git_config_set_string(cfg, "diff.noprefix", "true")); @@ -192,13 +192,13 @@ void test_diff_patch__config_options(void) cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); - cl_git_pass(git_diff_get_patch(&patch, NULL, diff, 0)); - cl_git_pass(git_diff_patch_to_str(&text, patch)); + cl_git_pass(git_patch_from_diff(&patch, NULL, diff, 0)); + cl_git_pass(git_patch_to_str(&text, patch)); cl_assert_equal_s(expected3, text); git__free(text); - git_diff_patch_free(patch); - git_diff_list_free(diff); + git_patch_free(patch); + git_diff_free(diff); cl_git_pass(git_config_set_int32(cfg, "core.abbrev", 12)); @@ -206,13 +206,13 @@ void test_diff_patch__config_options(void) cl_git_pass(git_diff_tree_to_index(&diff, g_repo, one, NULL, &opts)); cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); - cl_git_pass(git_diff_get_patch(&patch, NULL, diff, 0)); - cl_git_pass(git_diff_patch_to_str(&text, patch)); + cl_git_pass(git_patch_from_diff(&patch, NULL, diff, 0)); + cl_git_pass(git_patch_to_str(&text, patch)); cl_assert_equal_s(expected4, text); git__free(text); - git_diff_patch_free(patch); - git_diff_list_free(diff); + git_patch_free(patch); + git_diff_free(diff); git_tree_free(one); git_config_free(cfg); @@ -223,10 +223,10 @@ void test_diff_patch__hunks_have_correct_line_numbers(void) git_config *cfg; git_tree *head; git_diff_options opt = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff; - git_diff_patch *patch; + git_diff *diff; + git_patch *patch; const git_diff_delta *delta; - const git_diff_range *range; + const git_diff_hunk *range; const char *hdr, *text; size_t hdrlen, hunklen, textlen; char origin; @@ -253,15 +253,15 @@ void test_diff_patch__hunks_have_correct_line_numbers(void) cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); - cl_git_pass(git_diff_get_patch(&patch, &delta, diff, 0)); + cl_git_pass(git_patch_from_diff(&patch, &delta, diff, 0)); cl_assert_equal_i(GIT_DELTA_MODIFIED, (int)delta->status); - cl_assert_equal_i(2, (int)git_diff_patch_num_hunks(patch)); + cl_assert_equal_i(2, (int)git_patch_num_hunks(patch)); /* check hunk 0 */ cl_git_pass( - git_diff_patch_get_hunk(&range, &hdr, &hdrlen, &hunklen, patch, 0)); + git_patch_get_hunk(&range, &hdr, &hdrlen, &hunklen, patch, 0)); cl_assert_equal_i(18, (int)hunklen); @@ -270,9 +270,9 @@ void test_diff_patch__hunks_have_correct_line_numbers(void) cl_assert_equal_i(6, (int)range->new_start); cl_assert_equal_i(9, (int)range->new_lines); - cl_assert_equal_i(18, (int)git_diff_patch_num_lines_in_hunk(patch, 0)); + cl_assert_equal_i(18, (int)git_patch_num_lines_in_hunk(patch, 0)); - cl_git_pass(git_diff_patch_get_line_in_hunk( + cl_git_pass(git_patch_get_line_in_hunk( &origin, &text, &textlen, &oldno, &newno, patch, 0, 0)); cl_assert_equal_i(GIT_DIFF_LINE_CONTEXT, (int)origin); cl_git_pass(git_buf_set(&actual, text, textlen)); @@ -280,7 +280,7 @@ void test_diff_patch__hunks_have_correct_line_numbers(void) cl_assert_equal_i(6, oldno); cl_assert_equal_i(6, newno); - cl_git_pass(git_diff_patch_get_line_in_hunk( + cl_git_pass(git_patch_get_line_in_hunk( &origin, &text, &textlen, &oldno, &newno, patch, 0, 3)); cl_assert_equal_i(GIT_DIFF_LINE_DELETION, (int)origin); cl_git_pass(git_buf_set(&actual, text, textlen)); @@ -288,7 +288,7 @@ void test_diff_patch__hunks_have_correct_line_numbers(void) cl_assert_equal_i(9, oldno); cl_assert_equal_i(-1, newno); - cl_git_pass(git_diff_patch_get_line_in_hunk( + cl_git_pass(git_patch_get_line_in_hunk( &origin, &text, &textlen, &oldno, &newno, patch, 0, 12)); cl_assert_equal_i(GIT_DIFF_LINE_ADDITION, (int)origin); cl_git_pass(git_buf_set(&actual, text, textlen)); @@ -299,7 +299,7 @@ void test_diff_patch__hunks_have_correct_line_numbers(void) /* check hunk 1 */ cl_git_pass( - git_diff_patch_get_hunk(&range, &hdr, &hdrlen, &hunklen, patch, 1)); + git_patch_get_hunk(&range, &hdr, &hdrlen, &hunklen, patch, 1)); cl_assert_equal_i(18, (int)hunklen); @@ -308,9 +308,9 @@ void test_diff_patch__hunks_have_correct_line_numbers(void) cl_assert_equal_i(25, (int)range->new_start); cl_assert_equal_i(9, (int)range->new_lines); - cl_assert_equal_i(18, (int)git_diff_patch_num_lines_in_hunk(patch, 1)); + cl_assert_equal_i(18, (int)git_patch_num_lines_in_hunk(patch, 1)); - cl_git_pass(git_diff_patch_get_line_in_hunk( + cl_git_pass(git_patch_get_line_in_hunk( &origin, &text, &textlen, &oldno, &newno, patch, 1, 0)); cl_assert_equal_i(GIT_DIFF_LINE_CONTEXT, (int)origin); cl_git_pass(git_buf_set(&actual, text, textlen)); @@ -318,7 +318,7 @@ void test_diff_patch__hunks_have_correct_line_numbers(void) cl_assert_equal_i(31, oldno); cl_assert_equal_i(25, newno); - cl_git_pass(git_diff_patch_get_line_in_hunk( + cl_git_pass(git_patch_get_line_in_hunk( &origin, &text, &textlen, &oldno, &newno, patch, 1, 3)); cl_assert_equal_i(GIT_DIFF_LINE_DELETION, (int)origin); cl_git_pass(git_buf_set(&actual, text, textlen)); @@ -326,7 +326,7 @@ void test_diff_patch__hunks_have_correct_line_numbers(void) cl_assert_equal_i(34, oldno); cl_assert_equal_i(-1, newno); - cl_git_pass(git_diff_patch_get_line_in_hunk( + cl_git_pass(git_patch_get_line_in_hunk( &origin, &text, &textlen, &oldno, &newno, patch, 1, 12)); cl_assert_equal_i(GIT_DIFF_LINE_ADDITION, (int)origin); cl_git_pass(git_buf_set(&actual, text, textlen)); @@ -334,8 +334,8 @@ void test_diff_patch__hunks_have_correct_line_numbers(void) cl_assert_equal_i(-1, oldno); cl_assert_equal_i(28, newno); - git_diff_patch_free(patch); - git_diff_list_free(diff); + git_patch_free(patch); + git_diff_free(diff); /* Let's check line numbers when there is no newline */ @@ -346,15 +346,15 @@ void test_diff_patch__hunks_have_correct_line_numbers(void) cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); - cl_git_pass(git_diff_get_patch(&patch, &delta, diff, 0)); + cl_git_pass(git_patch_from_diff(&patch, &delta, diff, 0)); cl_assert_equal_i(GIT_DELTA_MODIFIED, (int)delta->status); - cl_assert_equal_i(1, (int)git_diff_patch_num_hunks(patch)); + cl_assert_equal_i(1, (int)git_patch_num_hunks(patch)); /* check hunk 0 */ cl_git_pass( - git_diff_patch_get_hunk(&range, &hdr, &hdrlen, &hunklen, patch, 0)); + git_patch_get_hunk(&range, &hdr, &hdrlen, &hunklen, patch, 0)); cl_assert_equal_i(6, (int)hunklen); @@ -363,9 +363,9 @@ void test_diff_patch__hunks_have_correct_line_numbers(void) cl_assert_equal_i(46, (int)range->new_start); cl_assert_equal_i(4, (int)range->new_lines); - cl_assert_equal_i(6, (int)git_diff_patch_num_lines_in_hunk(patch, 0)); + cl_assert_equal_i(6, (int)git_patch_num_lines_in_hunk(patch, 0)); - cl_git_pass(git_diff_patch_get_line_in_hunk( + cl_git_pass(git_patch_get_line_in_hunk( &origin, &text, &textlen, &oldno, &newno, patch, 0, 1)); cl_assert_equal_i(GIT_DIFF_LINE_CONTEXT, (int)origin); cl_git_pass(git_buf_set(&actual, text, textlen)); @@ -373,7 +373,7 @@ void test_diff_patch__hunks_have_correct_line_numbers(void) cl_assert_equal_i(47, oldno); cl_assert_equal_i(47, newno); - cl_git_pass(git_diff_patch_get_line_in_hunk( + cl_git_pass(git_patch_get_line_in_hunk( &origin, &text, &textlen, &oldno, &newno, patch, 0, 2)); cl_assert_equal_i(GIT_DIFF_LINE_CONTEXT, (int)origin); cl_git_pass(git_buf_set(&actual, text, textlen)); @@ -381,7 +381,7 @@ void test_diff_patch__hunks_have_correct_line_numbers(void) cl_assert_equal_i(48, oldno); cl_assert_equal_i(48, newno); - cl_git_pass(git_diff_patch_get_line_in_hunk( + cl_git_pass(git_patch_get_line_in_hunk( &origin, &text, &textlen, &oldno, &newno, patch, 0, 3)); cl_assert_equal_i(GIT_DIFF_LINE_DELETION, (int)origin); cl_git_pass(git_buf_set(&actual, text, textlen)); @@ -389,7 +389,7 @@ void test_diff_patch__hunks_have_correct_line_numbers(void) cl_assert_equal_i(49, oldno); cl_assert_equal_i(-1, newno); - cl_git_pass(git_diff_patch_get_line_in_hunk( + cl_git_pass(git_patch_get_line_in_hunk( &origin, &text, &textlen, &oldno, &newno, patch, 0, 4)); cl_assert_equal_i(GIT_DIFF_LINE_ADDITION, (int)origin); cl_git_pass(git_buf_set(&actual, text, textlen)); @@ -397,7 +397,7 @@ void test_diff_patch__hunks_have_correct_line_numbers(void) cl_assert_equal_i(-1, oldno); cl_assert_equal_i(49, newno); - cl_git_pass(git_diff_patch_get_line_in_hunk( + cl_git_pass(git_patch_get_line_in_hunk( &origin, &text, &textlen, &oldno, &newno, patch, 0, 5)); cl_assert_equal_i(GIT_DIFF_LINE_DEL_EOFNL, (int)origin); cl_git_pass(git_buf_set(&actual, text, textlen)); @@ -405,8 +405,8 @@ void test_diff_patch__hunks_have_correct_line_numbers(void) cl_assert_equal_i(-1, oldno); cl_assert_equal_i(49, newno); - git_diff_patch_free(patch); - git_diff_list_free(diff); + git_patch_free(patch); + git_diff_free(diff); git_buf_free(&actual); git_buf_free(&old_content); @@ -418,8 +418,8 @@ static void check_single_patch_stats( size_t adds, size_t dels, size_t ctxt, size_t *sizes, const char *expected) { - git_diff_list *diff; - git_diff_patch *patch; + git_diff *diff; + git_patch *patch; const git_diff_delta *delta; size_t actual_ctxt, actual_adds, actual_dels; @@ -427,12 +427,12 @@ static void check_single_patch_stats( cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); - cl_git_pass(git_diff_get_patch(&patch, &delta, diff, 0)); + cl_git_pass(git_patch_from_diff(&patch, &delta, diff, 0)); cl_assert_equal_i(GIT_DELTA_MODIFIED, (int)delta->status); - cl_assert_equal_i((int)hunks, (int)git_diff_patch_num_hunks(patch)); + cl_assert_equal_i((int)hunks, (int)git_patch_num_hunks(patch)); - cl_git_pass( git_diff_patch_line_stats( + cl_git_pass( git_patch_line_stats( &actual_ctxt, &actual_adds, &actual_dels, patch) ); cl_assert_equal_sz(ctxt, actual_ctxt); @@ -441,21 +441,21 @@ static void check_single_patch_stats( if (expected != NULL) { char *text; - cl_git_pass(git_diff_patch_to_str(&text, patch)); + cl_git_pass(git_patch_to_str(&text, patch)); cl_assert_equal_s(expected, text); git__free(text); cl_assert_equal_sz( - strlen(expected), git_diff_patch_size(patch, 1, 1, 1)); + strlen(expected), git_patch_size(patch, 1, 1, 1)); } if (sizes) { if (sizes[0]) - cl_assert_equal_sz(sizes[0], git_diff_patch_size(patch, 0, 0, 0)); + cl_assert_equal_sz(sizes[0], git_patch_size(patch, 0, 0, 0)); if (sizes[1]) - cl_assert_equal_sz(sizes[1], git_diff_patch_size(patch, 1, 0, 0)); + cl_assert_equal_sz(sizes[1], git_patch_size(patch, 1, 0, 0)); if (sizes[2]) - cl_assert_equal_sz(sizes[2], git_diff_patch_size(patch, 1, 1, 0)); + cl_assert_equal_sz(sizes[2], git_patch_size(patch, 1, 1, 0)); } /* walk lines in hunk with basic sanity checks */ @@ -464,12 +464,12 @@ static void check_single_patch_stats( int lastoldno = -1, oldno, lastnewno = -1, newno; char origin; - max_i = git_diff_patch_num_lines_in_hunk(patch, hunks - 1); + max_i = git_patch_num_lines_in_hunk(patch, hunks - 1); for (i = 0; i < max_i; ++i) { int expected = 1; - cl_git_pass(git_diff_patch_get_line_in_hunk( + cl_git_pass(git_patch_get_line_in_hunk( &origin, NULL, NULL, &oldno, &newno, patch, hunks - 1, i)); if (origin == GIT_DIFF_LINE_ADD_EOFNL || @@ -490,8 +490,8 @@ static void check_single_patch_stats( } } - git_diff_patch_free(patch); - git_diff_list_free(diff); + git_patch_free(patch); + git_diff_free(diff); } void test_diff_patch__line_counts_with_eofnl(void) diff --git a/tests-clar/diff/pathspec.c b/tests-clar/diff/pathspec.c index 7b15ea04c..5761d2d2b 100644 --- a/tests-clar/diff/pathspec.c +++ b/tests-clar/diff/pathspec.c @@ -20,7 +20,7 @@ void test_diff_pathspec__0(void) git_tree *a = resolve_commit_oid_to_tree(g_repo, a_commit); git_tree *b = resolve_commit_oid_to_tree(g_repo, b_commit); git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; + git_diff *diff = NULL; git_strarray paths = { NULL, 1 }; char *path; git_pathspec *ps; @@ -52,7 +52,7 @@ void test_diff_pathspec__0(void) (int)git_pathspec_match_list_diff_entry(matches,0)->status); git_pathspec_match_list_free(matches); - git_diff_list_free(diff); + git_diff_free(diff); diff = NULL; cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, a, b, &opts)); @@ -68,7 +68,7 @@ void test_diff_pathspec__0(void) (int)git_pathspec_match_list_diff_entry(matches,0)->status); git_pathspec_match_list_free(matches); - git_diff_list_free(diff); + git_diff_free(diff); diff = NULL; cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, a, &opts)); @@ -84,7 +84,7 @@ void test_diff_pathspec__0(void) (int)git_pathspec_match_list_diff_entry(matches,0)->status); git_pathspec_match_list_free(matches); - git_diff_list_free(diff); + git_diff_free(diff); diff = NULL; git_tree_free(a); diff --git a/tests-clar/diff/rename.c b/tests-clar/diff/rename.c index 9864c5896..45706fb47 100644 --- a/tests-clar/diff/rename.c +++ b/tests-clar/diff/rename.c @@ -43,7 +43,7 @@ void test_diff_rename__match_oid(void) const char *old_sha = "31e47d8c1fa36d7f8d537b96158e3f024de0a9f2"; const char *new_sha = "2bc7f351d20b53f1c72c16c4b036e491c478c49a"; git_tree *old_tree, *new_tree; - git_diff_list *diff; + git_diff *diff; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT; diff_expects exp; @@ -88,7 +88,7 @@ void test_diff_rename__match_oid(void) cl_assert_equal_i(1, exp.file_status[GIT_DELTA_ADDED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]); - git_diff_list_free(diff); + git_diff_free(diff); cl_git_pass(git_diff_tree_to_tree( &diff, g_repo, old_tree, new_tree, &diffopts)); @@ -109,7 +109,7 @@ void test_diff_rename__match_oid(void) cl_assert_equal_i(1, exp.file_status[GIT_DELTA_COPIED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]); - git_diff_list_free(diff); + git_diff_free(diff); git_tree_free(old_tree); git_tree_free(new_tree); @@ -120,7 +120,7 @@ void test_diff_rename__checks_options_version(void) const char *old_sha = "31e47d8c1fa36d7f8d537b96158e3f024de0a9f2"; const char *new_sha = "2bc7f351d20b53f1c72c16c4b036e491c478c49a"; git_tree *old_tree, *new_tree; - git_diff_list *diff; + git_diff *diff; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT; const git_error *err; @@ -142,7 +142,7 @@ void test_diff_rename__checks_options_version(void) err = giterr_last(); cl_assert_equal_i(GITERR_INVALID, err->klass); - git_diff_list_free(diff); + git_diff_free(diff); git_tree_free(old_tree); git_tree_free(new_tree); } @@ -153,7 +153,7 @@ void test_diff_rename__not_exact_match(void) const char *sha1 = "1c068dee5790ef1580cfc4cd670915b48d790084"; const char *sha2 = "19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13"; git_tree *old_tree, *new_tree; - git_diff_list *diff; + git_diff *diff; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT; diff_expects exp; @@ -207,7 +207,7 @@ void test_diff_rename__not_exact_match(void) cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_ADDED]); - git_diff_list_free(diff); + git_diff_free(diff); /* git diff -M -C \ * 2bc7f351d20b53f1c72c16c4b036e491c478c49a \ @@ -228,7 +228,7 @@ void test_diff_rename__not_exact_match(void) cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_COPIED]); - git_diff_list_free(diff); + git_diff_free(diff); /* git diff -M -C --find-copies-harder --break-rewrites \ * 2bc7f351d20b53f1c72c16c4b036e491c478c49a \ @@ -253,7 +253,7 @@ void test_diff_rename__not_exact_match(void) cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_COPIED]); - git_diff_list_free(diff); + git_diff_free(diff); /* == Changes ===================================================== * songofseven.txt -> untimely.txt (rename, convert to crlf) @@ -281,7 +281,7 @@ void test_diff_rename__not_exact_match(void) cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_ADDED]); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_DELETED]); - git_diff_list_free(diff); + git_diff_free(diff); /* git diff -M -C \ * 1c068dee5790ef1580cfc4cd670915b48d790084 \ @@ -301,7 +301,7 @@ void test_diff_rename__not_exact_match(void) cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_RENAMED]); - git_diff_list_free(diff); + git_diff_free(diff); /* git diff -M -C --find-copies-harder --break-rewrites \ * 1c068dee5790ef1580cfc4cd670915b48d790084 \ @@ -330,7 +330,7 @@ void test_diff_rename__not_exact_match(void) cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_RENAMED]); - git_diff_list_free(diff); + git_diff_free(diff); /* git diff -M -C --find-copies-harder --break-rewrites \ * 1c068dee5790ef1580cfc4cd670915b48d790084 \ @@ -353,7 +353,7 @@ void test_diff_rename__not_exact_match(void) cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_RENAMED]); - git_diff_list_free(diff); + git_diff_free(diff); git_tree_free(old_tree); git_tree_free(new_tree); @@ -364,7 +364,7 @@ void test_diff_rename__handles_small_files(void) const char *tree_sha = "2bc7f351d20b53f1c72c16c4b036e491c478c49a"; git_index *index; git_tree *tree; - git_diff_list *diff; + git_diff *diff; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT; @@ -388,7 +388,7 @@ void test_diff_rename__handles_small_files(void) GIT_DIFF_FIND_AND_BREAK_REWRITES; cl_git_pass(git_diff_find_similar(diff, &opts)); - git_diff_list_free(diff); + git_diff_free(diff); git_tree_free(tree); git_index_free(index); } @@ -400,7 +400,7 @@ void test_diff_rename__working_directory_changes(void) git_oid id; git_tree *tree; git_blob *blob; - git_diff_list *diff; + git_diff *diff; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT; diff_expects exp; @@ -451,7 +451,7 @@ void test_diff_rename__working_directory_changes(void) cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_UNTRACKED]); - git_diff_list_free(diff); + git_diff_free(diff); /* rewrite files in the working directory with / without CRLF changes */ @@ -477,7 +477,7 @@ void test_diff_rename__working_directory_changes(void) cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_UNTRACKED]); - git_diff_list_free(diff); + git_diff_free(diff); /* try a different whitespace option */ @@ -496,7 +496,7 @@ void test_diff_rename__working_directory_changes(void) cl_assert_equal_i(2, exp.file_status[GIT_DELTA_DELETED]); cl_assert_equal_i(3, exp.file_status[GIT_DELTA_UNTRACKED]); - git_diff_list_free(diff); + git_diff_free(diff); /* try a different matching option */ @@ -514,7 +514,7 @@ void test_diff_rename__working_directory_changes(void) cl_assert_equal_i(3, exp.file_status[GIT_DELTA_UNTRACKED]); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_DELETED]); - git_diff_list_free(diff); + git_diff_free(diff); /* again with exact match blob */ @@ -545,7 +545,7 @@ void test_diff_rename__working_directory_changes(void) cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_UNTRACKED]); - git_diff_list_free(diff); + git_diff_free(diff); git_tree_free(tree); git_buf_free(&content); @@ -557,10 +557,10 @@ void test_diff_rename__patch(void) const char *sha0 = "2bc7f351d20b53f1c72c16c4b036e491c478c49a"; const char *sha1 = "1c068dee5790ef1580cfc4cd670915b48d790084"; git_tree *old_tree, *new_tree; - git_diff_list *diff; + git_diff *diff; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT; - git_diff_patch *patch; + git_patch *patch; const git_diff_delta *delta; char *text; const char *expected = "diff --git a/sixserving.txt b/ikeepsix.txt\nindex ad0a8e5..36020db 100644\n--- a/sixserving.txt\n+++ b/ikeepsix.txt\n@@ -1,3 +1,6 @@\n+I Keep Six Honest Serving-Men\n+=============================\n+\n I KEEP six honest serving-men\n (They taught me all I knew);\n Their names are What and Why and When\n@@ -21,4 +24,4 @@ She sends'em abroad on her own affairs,\n One million Hows, two million Wheres,\n And seven million Whys!\n \n- -- Rudyard Kipling\n+ -- Rudyard Kipling\n"; @@ -584,25 +584,25 @@ void test_diff_rename__patch(void) cl_assert_equal_i(4, (int)git_diff_num_deltas(diff)); - cl_git_pass(git_diff_get_patch(&patch, &delta, diff, 0)); + cl_git_pass(git_patch_from_diff(&patch, &delta, diff, 0)); cl_assert_equal_i(GIT_DELTA_COPIED, (int)delta->status); - cl_git_pass(git_diff_patch_to_str(&text, patch)); + cl_git_pass(git_patch_to_str(&text, patch)); cl_assert_equal_s(expected, text); git__free(text); - git_diff_patch_free(patch); + git_patch_free(patch); - cl_git_pass(git_diff_get_patch(NULL, &delta, diff, 1)); + cl_git_pass(git_patch_from_diff(NULL, &delta, diff, 1)); cl_assert_equal_i(GIT_DELTA_UNMODIFIED, (int)delta->status); - cl_git_pass(git_diff_get_patch(NULL, &delta, diff, 2)); + cl_git_pass(git_patch_from_diff(NULL, &delta, diff, 2)); cl_assert_equal_i(GIT_DELTA_MODIFIED, (int)delta->status); - cl_git_pass(git_diff_get_patch(NULL, &delta, diff, 3)); + cl_git_pass(git_patch_from_diff(NULL, &delta, diff, 3)); cl_assert_equal_i(GIT_DELTA_MODIFIED, (int)delta->status); - git_diff_list_free(diff); + git_diff_free(diff); git_tree_free(old_tree); git_tree_free(new_tree); } @@ -612,7 +612,7 @@ void test_diff_rename__file_exchange(void) git_buf c1 = GIT_BUF_INIT, c2 = GIT_BUF_INIT; git_index *index; git_tree *tree; - git_diff_list *diff; + git_diff *diff; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT; diff_expects exp; @@ -647,7 +647,7 @@ void test_diff_rename__file_exchange(void) cl_assert_equal_i(2, exp.files); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_RENAMED]); - git_diff_list_free(diff); + git_diff_free(diff); git_tree_free(tree); git_index_free(index); @@ -660,7 +660,7 @@ void test_diff_rename__file_exchange_three(void) git_buf c1 = GIT_BUF_INIT, c2 = GIT_BUF_INIT, c3 = GIT_BUF_INIT; git_index *index; git_tree *tree; - git_diff_list *diff; + git_diff *diff; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT; diff_expects exp; @@ -699,7 +699,7 @@ void test_diff_rename__file_exchange_three(void) cl_assert_equal_i(3, exp.files); cl_assert_equal_i(3, exp.file_status[GIT_DELTA_RENAMED]); - git_diff_list_free(diff); + git_diff_free(diff); git_tree_free(tree); git_index_free(index); @@ -713,7 +713,7 @@ void test_diff_rename__file_partial_exchange(void) git_buf c1 = GIT_BUF_INIT, c2 = GIT_BUF_INIT; git_index *index; git_tree *tree; - git_diff_list *diff; + git_diff *diff; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT; diff_expects exp; @@ -752,7 +752,7 @@ void test_diff_rename__file_partial_exchange(void) cl_assert_equal_i(1, exp.file_status[GIT_DELTA_ADDED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]); - git_diff_list_free(diff); + git_diff_free(diff); git_tree_free(tree); git_index_free(index); @@ -765,7 +765,7 @@ void test_diff_rename__rename_and_copy_from_same_source(void) git_buf c1 = GIT_BUF_INIT, c2 = GIT_BUF_INIT; git_index *index; git_tree *tree; - git_diff_list *diff; + git_diff *diff; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT; diff_expects exp; @@ -809,7 +809,7 @@ void test_diff_rename__rename_and_copy_from_same_source(void) cl_assert_equal_i(2, exp.file_status[GIT_DELTA_COPIED]); cl_assert_equal_i(4, exp.file_status[GIT_DELTA_UNMODIFIED]); - git_diff_list_free(diff); + git_diff_free(diff); git_tree_free(tree); git_index_free(index); @@ -822,7 +822,7 @@ void test_diff_rename__from_deleted_to_split(void) git_buf c1 = GIT_BUF_INIT; git_index *index; git_tree *tree; - git_diff_list *diff; + git_diff *diff; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT; diff_expects exp; @@ -863,7 +863,7 @@ void test_diff_rename__from_deleted_to_split(void) cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_UNMODIFIED]); - git_diff_list_free(diff); + git_diff_free(diff); git_tree_free(tree); git_index_free(index); @@ -907,7 +907,7 @@ void test_diff_rename__rejected_match_can_match_others(void) git_index *index; git_tree *tree; git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; - git_diff_list *diff; + git_diff *diff; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT; git_buf one = GIT_BUF_INIT, two = GIT_BUF_INIT; @@ -961,7 +961,7 @@ void test_diff_rename__rejected_match_can_match_others(void) cl_git_pass( git_diff_foreach(diff, test_names_expected, NULL, NULL, &expect)); - git_diff_list_free(diff); + git_diff_free(diff); git_tree_free(tree); git_index_free(index); git_reference_free(head); @@ -993,7 +993,7 @@ void test_diff_rename__rejected_match_can_match_others_two(void) git_index *index; git_tree *tree; git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; - git_diff_list *diff; + git_diff *diff; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT; unsigned int status[] = { GIT_DELTA_RENAMED, GIT_DELTA_RENAMED }; @@ -1035,7 +1035,7 @@ void test_diff_rename__rejected_match_can_match_others_two(void) git_diff_foreach(diff, test_names_expected, NULL, NULL, &expect)); cl_assert(expect.idx > 0); - git_diff_list_free(diff); + git_diff_free(diff); git_tree_free(tree); git_index_free(index); git_reference_free(head); @@ -1048,7 +1048,7 @@ void test_diff_rename__rejected_match_can_match_others_three(void) git_index *index; git_tree *tree; git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT; - git_diff_list *diff; + git_diff *diff; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT; @@ -1091,7 +1091,7 @@ void test_diff_rename__rejected_match_can_match_others_three(void) cl_assert(expect.idx == expect.len); - git_diff_list_free(diff); + git_diff_free(diff); git_tree_free(tree); git_index_free(index); git_reference_free(head); @@ -1102,7 +1102,7 @@ void test_diff_rename__can_rename_from_rewrite(void) { git_index *index; git_tree *tree; - git_diff_list *diff; + git_diff *diff; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT; @@ -1140,7 +1140,7 @@ void test_diff_rename__can_rename_from_rewrite(void) cl_assert(expect.idx == expect.len); - git_diff_list_free(diff); + git_diff_free(diff); git_tree_free(tree); git_index_free(index); } @@ -1149,7 +1149,7 @@ void test_diff_rename__case_changes_are_split(void) { git_index *index; git_tree *tree; - git_diff_list *diff = NULL; + git_diff *diff = NULL; diff_expects exp; git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT; @@ -1182,7 +1182,7 @@ void test_diff_rename__case_changes_are_split(void) cl_assert_equal_i(1, exp.files); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]); - git_diff_list_free(diff); + git_diff_free(diff); git_index_free(index); git_tree_free(tree); } @@ -1191,7 +1191,7 @@ void test_diff_rename__unmodified_can_be_renamed(void) { git_index *index; git_tree *tree; - git_diff_list *diff = NULL; + git_diff *diff = NULL; diff_expects exp; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT; @@ -1230,7 +1230,7 @@ void test_diff_rename__unmodified_can_be_renamed(void) cl_assert_equal_i(1, exp.files); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]); - git_diff_list_free(diff); + git_diff_free(diff); git_index_free(index); git_tree_free(tree); } @@ -1238,7 +1238,7 @@ void test_diff_rename__unmodified_can_be_renamed(void) void test_diff_rename__rewrite_on_single_file(void) { git_index *index; - git_diff_list *diff = NULL; + git_diff *diff = NULL; diff_expects exp; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT; @@ -1280,6 +1280,6 @@ void test_diff_rename__rewrite_on_single_file(void) cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNTRACKED]); - git_diff_list_free(diff); + git_diff_free(diff); git_index_free(index); } diff --git a/tests-clar/diff/submodules.c b/tests-clar/diff/submodules.c index 036ff09aa..24283e2fa 100644 --- a/tests-clar/diff/submodules.c +++ b/tests-clar/diff/submodules.c @@ -14,15 +14,15 @@ void test_diff_submodules__cleanup(void) } static void check_diff_patches_at_line( - git_diff_list *diff, const char **expected, const char *file, int line) + git_diff *diff, const char **expected, const char *file, int line) { const git_diff_delta *delta; - git_diff_patch *patch = NULL; + git_patch *patch = NULL; size_t d, num_d = git_diff_num_deltas(diff); char *patch_text; - for (d = 0; d < num_d; ++d, git_diff_patch_free(patch)) { - cl_git_pass(git_diff_get_patch(&patch, &delta, diff, d)); + for (d = 0; d < num_d; ++d, git_patch_free(patch)) { + cl_git_pass(git_patch_from_diff(&patch, &delta, diff, d)); if (delta->status == GIT_DELTA_UNMODIFIED) { cl_assert_at_line(expected[d] == NULL, file, line); @@ -32,11 +32,11 @@ static void check_diff_patches_at_line( if (expected[d] && !strcmp(expected[d], "")) continue; if (expected[d] && !strcmp(expected[d], "")) { - cl_git_pass(git_diff_patch_to_str(&patch_text, patch)); + cl_git_pass(git_patch_to_str(&patch_text, patch)); cl_assert_at_line(!strcmp(expected[d], ""), file, line); } - cl_git_pass(git_diff_patch_to_str(&patch_text, patch)); + cl_git_pass(git_patch_to_str(&patch_text, patch)); clar__assert_equal( file, line, "expected diff did not match actual diff", 1, @@ -53,7 +53,7 @@ static void check_diff_patches_at_line( void test_diff_submodules__unmodified_submodule(void) { git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; + git_diff *diff = NULL; static const char *expected[] = { "", /* .gitmodules */ NULL, /* added */ @@ -74,13 +74,13 @@ void test_diff_submodules__unmodified_submodule(void) cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected); - git_diff_list_free(diff); + git_diff_free(diff); } void test_diff_submodules__dirty_submodule(void) { git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; + git_diff *diff = NULL; static const char *expected[] = { "", /* .gitmodules */ NULL, /* added */ @@ -104,13 +104,13 @@ void test_diff_submodules__dirty_submodule(void) cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected); - git_diff_list_free(diff); + git_diff_free(diff); } void test_diff_submodules__dirty_submodule_2(void) { git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL, *diff2 = NULL; + git_diff *diff = NULL, *diff2 = NULL; char *smpath = "testrepo"; static const char *expected_none[] = { "" }; static const char *expected_dirty[] = { @@ -132,7 +132,7 @@ void test_diff_submodules__dirty_submodule_2(void) cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_none); - git_diff_list_free(diff); + git_diff_free(diff); cl_git_rewritefile("submodules/testrepo/README", "heyheyhey"); cl_git_mkfile("submodules/testrepo/all_new.txt", "never seen before"); @@ -146,25 +146,25 @@ void test_diff_submodules__dirty_submodule_2(void) cl_git_pass(git_repository_head_tree(&head, g_repo)); cl_git_pass(git_diff_tree_to_index(&diff2, g_repo, head, NULL, &opts)); cl_git_pass(git_diff_merge(diff, diff2)); - git_diff_list_free(diff2); + git_diff_free(diff2); git_tree_free(head); check_diff_patches(diff, expected_dirty); } - git_diff_list_free(diff); + git_diff_free(diff); cl_git_pass(git_submodule_reload_all(g_repo)); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_dirty); - git_diff_list_free(diff); + git_diff_free(diff); } void test_diff_submodules__submod2_index_to_wd(void) { git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; + git_diff *diff = NULL; static const char *expected[] = { "", /* .gitmodules */ "diff --git a/sm_changed_file b/sm_changed_file\nindex 4800958..4800958 160000\n--- a/sm_changed_file\n+++ b/sm_changed_file\n@@ -1 +1 @@\n-Subproject commit 480095882d281ed676fe5b863569520e54a7d5c0\n+Subproject commit 480095882d281ed676fe5b863569520e54a7d5c0-dirty\n", /* sm_changed_file */ @@ -182,14 +182,14 @@ void test_diff_submodules__submod2_index_to_wd(void) cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected); - git_diff_list_free(diff); + git_diff_free(diff); } void test_diff_submodules__submod2_head_to_index(void) { git_diff_options opts = GIT_DIFF_OPTIONS_INIT; git_tree *head; - git_diff_list *diff = NULL; + git_diff *diff = NULL; static const char *expected[] = { "", /* .gitmodules */ "diff --git a/sm_added_and_uncommited b/sm_added_and_uncommited\nnew file mode 160000\nindex 0000000..4800958\n--- /dev/null\n+++ b/sm_added_and_uncommited\n@@ -0,0 +1 @@\n+Subproject commit 480095882d281ed676fe5b863569520e54a7d5c0\n", /* sm_added_and_uncommited */ @@ -205,7 +205,7 @@ void test_diff_submodules__submod2_head_to_index(void) cl_git_pass(git_diff_tree_to_index(&diff, g_repo, head, NULL, &opts)); check_diff_patches(diff, expected); - git_diff_list_free(diff); + git_diff_free(diff); git_tree_free(head); } @@ -213,7 +213,7 @@ void test_diff_submodules__submod2_head_to_index(void) void test_diff_submodules__invalid_cache(void) { git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; + git_diff *diff = NULL; git_submodule *sm; char *smpath = "sm_changed_head"; git_repository *smrepo; @@ -246,7 +246,7 @@ void test_diff_submodules__invalid_cache(void) /* baseline */ cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_baseline); - git_diff_list_free(diff); + git_diff_free(diff); /* update index with new HEAD */ cl_git_pass(git_submodule_lookup(&sm, g_repo, smpath)); @@ -254,7 +254,7 @@ void test_diff_submodules__invalid_cache(void) cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_unchanged); - git_diff_list_free(diff); + git_diff_free(diff); /* create untracked file in submodule working directory */ cl_git_mkfile("submod2/sm_changed_head/new_around_here", "hello"); @@ -262,13 +262,13 @@ void test_diff_submodules__invalid_cache(void) cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_dirty); - git_diff_list_free(diff); + git_diff_free(diff); git_submodule_set_ignore(sm, GIT_SUBMODULE_IGNORE_UNTRACKED); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_unchanged); - git_diff_list_free(diff); + git_diff_free(diff); /* modify tracked file in submodule working directory */ cl_git_append2file( @@ -276,20 +276,20 @@ void test_diff_submodules__invalid_cache(void) cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_dirty); - git_diff_list_free(diff); + git_diff_free(diff); cl_git_pass(git_submodule_reload_all(g_repo)); cl_git_pass(git_submodule_lookup(&sm, g_repo, smpath)); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_dirty); - git_diff_list_free(diff); + git_diff_free(diff); git_submodule_set_ignore(sm, GIT_SUBMODULE_IGNORE_DIRTY); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_unchanged); - git_diff_list_free(diff); + git_diff_free(diff); /* add file to index in submodule */ cl_git_pass(git_submodule_open(&smrepo, sm)); @@ -300,13 +300,13 @@ void test_diff_submodules__invalid_cache(void) cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_dirty); - git_diff_list_free(diff); + git_diff_free(diff); git_submodule_set_ignore(sm, GIT_SUBMODULE_IGNORE_DIRTY); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_unchanged); - git_diff_list_free(diff); + git_diff_free(diff); /* commit changed index of submodule */ cl_repo_commit_from_index(NULL, smrepo, NULL, 1372350000, "Move it"); @@ -315,25 +315,25 @@ void test_diff_submodules__invalid_cache(void) cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_moved); - git_diff_list_free(diff); + git_diff_free(diff); git_submodule_set_ignore(sm, GIT_SUBMODULE_IGNORE_ALL); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_unchanged); - git_diff_list_free(diff); + git_diff_free(diff); git_submodule_set_ignore(sm, GIT_SUBMODULE_IGNORE_NONE); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_moved_dirty); - git_diff_list_free(diff); + git_diff_free(diff); p_unlink("submod2/sm_changed_head/new_around_here"); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_moved); - git_diff_list_free(diff); + git_diff_free(diff); git_index_free(smindex); git_repository_free(smrepo); @@ -342,7 +342,7 @@ void test_diff_submodules__invalid_cache(void) void test_diff_submodules__diff_ignore_options(void) { git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; + git_diff *diff = NULL; git_config *cfg; static const char *expected_normal[] = { "", /* .gitmodules */ @@ -371,26 +371,26 @@ void test_diff_submodules__diff_ignore_options(void) cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_normal); - git_diff_list_free(diff); + git_diff_free(diff); opts.flags |= GIT_DIFF_IGNORE_SUBMODULES; cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_ignore_all); - git_diff_list_free(diff); + git_diff_free(diff); opts.flags &= ~GIT_DIFF_IGNORE_SUBMODULES; opts.ignore_submodules = GIT_SUBMODULE_IGNORE_ALL; cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_ignore_all); - git_diff_list_free(diff); + git_diff_free(diff); opts.ignore_submodules = GIT_SUBMODULE_IGNORE_DIRTY; cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_ignore_dirty); - git_diff_list_free(diff); + git_diff_free(diff); opts.ignore_submodules = 0; cl_git_pass(git_repository_config(&cfg, g_repo)); @@ -398,25 +398,25 @@ void test_diff_submodules__diff_ignore_options(void) cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_normal); - git_diff_list_free(diff); + git_diff_free(diff); cl_git_pass(git_config_set_bool(cfg, "diff.ignoreSubmodules", true)); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_ignore_all); - git_diff_list_free(diff); + git_diff_free(diff); cl_git_pass(git_config_set_string(cfg, "diff.ignoreSubmodules", "none")); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_normal); - git_diff_list_free(diff); + git_diff_free(diff); cl_git_pass(git_config_set_string(cfg, "diff.ignoreSubmodules", "dirty")); cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); check_diff_patches(diff, expected_ignore_dirty); - git_diff_list_free(diff); + git_diff_free(diff); git_config_free(cfg); } diff --git a/tests-clar/diff/tree.c b/tests-clar/diff/tree.c index f05c7869e..7286ee121 100644 --- a/tests-clar/diff/tree.c +++ b/tests-clar/diff/tree.c @@ -3,7 +3,7 @@ static git_repository *g_repo = NULL; static git_diff_options opts; -static git_diff_list *diff; +static git_diff *diff; static git_tree *a, *b; static diff_expects expect; @@ -22,7 +22,7 @@ void test_diff_tree__initialize(void) void test_diff_tree__cleanup(void) { - git_diff_list_free(diff); + git_diff_free(diff); git_tree_free(a); git_tree_free(b); @@ -65,7 +65,7 @@ void test_diff_tree__0(void) cl_assert_equal_i(24 + 1 + 5 + 5, expect.line_adds); cl_assert_equal_i(7 + 1, expect.line_dels); - git_diff_list_free(diff); + git_diff_free(diff); diff = NULL; memset(&expect, 0, sizeof(expect)); @@ -168,7 +168,7 @@ void test_diff_tree__options(void) cl_assert_equal_i(actual.line_adds, expected->line_adds); cl_assert_equal_i(actual.line_dels, expected->line_dels); - git_diff_list_free(diff); + git_diff_free(diff); diff = NULL; } @@ -214,7 +214,7 @@ void test_diff_tree__merge(void) const char *b_commit = "370fe9ec22"; const char *c_commit = "f5b0af1fb4f5c"; git_tree *c; - git_diff_list *diff1 = NULL, *diff2 = NULL; + git_diff *diff1 = NULL, *diff2 = NULL; g_repo = cl_git_sandbox_init("attr"); @@ -230,7 +230,7 @@ void test_diff_tree__merge(void) cl_git_pass(git_diff_merge(diff1, diff2)); - git_diff_list_free(diff2); + git_diff_free(diff2); cl_git_pass(git_diff_foreach( diff1, diff_file_cb, diff_hunk_cb, diff_line_cb, &expect)); @@ -247,7 +247,7 @@ void test_diff_tree__merge(void) cl_assert_equal_i(36, expect.line_adds); cl_assert_equal_i(22, expect.line_dels); - git_diff_list_free(diff1); + git_diff_free(diff1); } void test_diff_tree__larger_hunks(void) @@ -256,8 +256,8 @@ void test_diff_tree__larger_hunks(void) const char *b_commit = "7a9e0b02e63179929fed24f0a3e0f19168114d10"; size_t d, num_d, h, num_h, l, num_l, header_len, line_len; const git_diff_delta *delta; - git_diff_patch *patch; - const git_diff_range *range; + git_patch *patch; + const git_diff_hunk *range; const char *header, *line; char origin; @@ -273,31 +273,31 @@ void test_diff_tree__larger_hunks(void) num_d = git_diff_num_deltas(diff); for (d = 0; d < num_d; ++d) { - cl_git_pass(git_diff_get_patch(&patch, &delta, diff, d)); + cl_git_pass(git_patch_from_diff(&patch, &delta, diff, d)); cl_assert(patch && delta); - num_h = git_diff_patch_num_hunks(patch); + num_h = git_patch_num_hunks(patch); for (h = 0; h < num_h; h++) { - cl_git_pass(git_diff_patch_get_hunk( + cl_git_pass(git_patch_get_hunk( &range, &header, &header_len, &num_l, patch, h)); for (l = 0; l < num_l; ++l) { - cl_git_pass(git_diff_patch_get_line_in_hunk( + cl_git_pass(git_patch_get_line_in_hunk( &origin, &line, &line_len, NULL, NULL, patch, h, l)); cl_assert(line); } - cl_git_fail(git_diff_patch_get_line_in_hunk( + cl_git_fail(git_patch_get_line_in_hunk( &origin, &line, &line_len, NULL, NULL, patch, h, num_l)); } - cl_git_fail(git_diff_patch_get_hunk( + cl_git_fail(git_patch_get_hunk( &range, &header, &header_len, &num_l, patch, num_h)); - git_diff_patch_free(patch); + git_patch_free(patch); } - cl_git_fail(git_diff_get_patch(&patch, &delta, diff, num_d)); + cl_git_fail(git_patch_from_diff(&patch, &delta, diff, num_d)); cl_assert_equal_i(2, (int)num_d); } @@ -487,7 +487,7 @@ void test_diff_tree__diff_configs(void) cl_assert_equal_i(7, expect.line_adds); cl_assert_equal_i(15, expect.line_dels); - git_diff_list_free(diff); + git_diff_free(diff); diff = NULL; set_config_int(g_repo, "diff.context", 1); @@ -507,7 +507,7 @@ void test_diff_tree__diff_configs(void) cl_assert_equal_i(7, expect.line_adds); cl_assert_equal_i(15, expect.line_dels); - git_diff_list_free(diff); + git_diff_free(diff); diff = NULL; set_config_int(g_repo, "diff.context", 0); diff --git a/tests-clar/diff/workdir.c b/tests-clar/diff/workdir.c index aeef7b963..4af667196 100644 --- a/tests-clar/diff/workdir.c +++ b/tests-clar/diff/workdir.c @@ -16,7 +16,7 @@ void test_diff_workdir__cleanup(void) void test_diff_workdir__to_index(void) { git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; + git_diff *diff = NULL; diff_expects exp; int use_iterator; @@ -60,7 +60,7 @@ void test_diff_workdir__to_index(void) cl_assert_equal_i(5, exp.line_dels); } - git_diff_list_free(diff); + git_diff_free(diff); } void test_diff_workdir__to_tree(void) @@ -70,8 +70,8 @@ void test_diff_workdir__to_tree(void) const char *b_commit = "0017bd4ab1ec3"; /* the start */ git_tree *a, *b; git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; - git_diff_list *diff2 = NULL; + git_diff *diff = NULL; + git_diff *diff2 = NULL; diff_expects exp; int use_iterator; @@ -119,7 +119,7 @@ void test_diff_workdir__to_tree(void) * do more apples-to-apples test comparison below. */ - git_diff_list_free(diff); + git_diff_free(diff); diff = NULL; memset(&exp, 0, sizeof(exp)); @@ -130,7 +130,7 @@ void test_diff_workdir__to_tree(void) cl_git_pass(git_diff_tree_to_index(&diff, g_repo, a, NULL, &opts)); cl_git_pass(git_diff_index_to_workdir(&diff2, g_repo, NULL, &opts)); cl_git_pass(git_diff_merge(diff, diff2)); - git_diff_list_free(diff2); + git_diff_free(diff2); for (use_iterator = 0; use_iterator <= 1; use_iterator++) { memset(&exp, 0, sizeof(exp)); @@ -157,7 +157,7 @@ void test_diff_workdir__to_tree(void) cl_assert_equal_i(5, exp.line_dels); } - git_diff_list_free(diff); + git_diff_free(diff); diff = NULL; memset(&exp, 0, sizeof(exp)); @@ -167,7 +167,7 @@ void test_diff_workdir__to_tree(void) cl_git_pass(git_diff_tree_to_index(&diff, g_repo, b, NULL, &opts)); cl_git_pass(git_diff_index_to_workdir(&diff2, g_repo, NULL, &opts)); cl_git_pass(git_diff_merge(diff, diff2)); - git_diff_list_free(diff2); + git_diff_free(diff2); for (use_iterator = 0; use_iterator <= 1; use_iterator++) { memset(&exp, 0, sizeof(exp)); @@ -194,7 +194,7 @@ void test_diff_workdir__to_tree(void) cl_assert_equal_i(4, exp.line_dels); } - git_diff_list_free(diff); + git_diff_free(diff); git_tree_free(a); git_tree_free(b); @@ -203,7 +203,7 @@ void test_diff_workdir__to_tree(void) void test_diff_workdir__to_index_with_pathspec(void) { git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; + git_diff *diff = NULL; diff_expects exp; char *pathspec = NULL; int use_iterator; @@ -235,7 +235,7 @@ void test_diff_workdir__to_index_with_pathspec(void) cl_assert_equal_i(4, exp.file_status[GIT_DELTA_UNTRACKED]); } - git_diff_list_free(diff); + git_diff_free(diff); pathspec = "modified_file"; @@ -258,7 +258,7 @@ void test_diff_workdir__to_index_with_pathspec(void) cl_assert_equal_i(0, exp.file_status[GIT_DELTA_UNTRACKED]); } - git_diff_list_free(diff); + git_diff_free(diff); pathspec = "subdir"; @@ -281,7 +281,7 @@ void test_diff_workdir__to_index_with_pathspec(void) cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNTRACKED]); } - git_diff_list_free(diff); + git_diff_free(diff); pathspec = "*_deleted"; @@ -304,12 +304,12 @@ void test_diff_workdir__to_index_with_pathspec(void) cl_assert_equal_i(0, exp.file_status[GIT_DELTA_UNTRACKED]); } - git_diff_list_free(diff); + git_diff_free(diff); } void test_diff_workdir__filemode_changes(void) { - git_diff_list *diff = NULL; + git_diff *diff = NULL; diff_expects exp; int use_iterator; @@ -339,7 +339,7 @@ void test_diff_workdir__filemode_changes(void) cl_assert_equal_i(0, exp.hunks); } - git_diff_list_free(diff); + git_diff_free(diff); /* chmod file and test again */ @@ -362,14 +362,14 @@ void test_diff_workdir__filemode_changes(void) cl_assert_equal_i(0, exp.hunks); } - git_diff_list_free(diff); + git_diff_free(diff); cl_assert(cl_toggle_filemode("issue_592/a.txt")); } void test_diff_workdir__filemode_changes_with_filemode_false(void) { - git_diff_list *diff = NULL; + git_diff *diff = NULL; diff_expects exp; if (!cl_is_chmod_supported()) @@ -391,7 +391,7 @@ void test_diff_workdir__filemode_changes_with_filemode_false(void) cl_assert_equal_i(0, exp.file_status[GIT_DELTA_MODIFIED]); cl_assert_equal_i(0, exp.hunks); - git_diff_list_free(diff); + git_diff_free(diff); /* chmod file and test again */ @@ -407,7 +407,7 @@ void test_diff_workdir__filemode_changes_with_filemode_false(void) cl_assert_equal_i(0, exp.file_status[GIT_DELTA_MODIFIED]); cl_assert_equal_i(0, exp.hunks); - git_diff_list_free(diff); + git_diff_free(diff); cl_assert(cl_toggle_filemode("issue_592/a.txt")); } @@ -415,7 +415,7 @@ void test_diff_workdir__filemode_changes_with_filemode_false(void) void test_diff_workdir__head_index_and_workdir_all_differ(void) { git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff_i2t = NULL, *diff_w2i = NULL; + git_diff *diff_i2t = NULL, *diff_w2i = NULL; diff_expects exp; char *pathspec = "staged_changes_modified_file"; git_tree *tree; @@ -504,8 +504,8 @@ void test_diff_workdir__head_index_and_workdir_all_differ(void) cl_assert_equal_i(0, exp.line_dels); } - git_diff_list_free(diff_i2t); - git_diff_list_free(diff_w2i); + git_diff_free(diff_i2t); + git_diff_free(diff_w2i); git_tree_free(tree); } @@ -513,7 +513,7 @@ void test_diff_workdir__head_index_and_workdir_all_differ(void) void test_diff_workdir__eof_newline_changes(void) { git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; + git_diff *diff = NULL; diff_expects exp; char *pathspec = "current_file"; int use_iterator; @@ -546,7 +546,7 @@ void test_diff_workdir__eof_newline_changes(void) cl_assert_equal_i(0, exp.line_dels); } - git_diff_list_free(diff); + git_diff_free(diff); cl_git_append2file("status/current_file", "\n"); @@ -573,7 +573,7 @@ void test_diff_workdir__eof_newline_changes(void) cl_assert_equal_i(0, exp.line_dels); } - git_diff_list_free(diff); + git_diff_free(diff); cl_git_rewritefile("status/current_file", "current_file"); @@ -600,7 +600,7 @@ void test_diff_workdir__eof_newline_changes(void) cl_assert_equal_i(2, exp.line_dels); } - git_diff_list_free(diff); + git_diff_free(diff); } /* PREPARATION OF TEST DATA @@ -684,9 +684,9 @@ void test_diff_workdir__larger_hunks(void) opts.interhunk_lines = 0; for (i = 0; i <= 2; ++i) { - git_diff_list *diff = NULL; - git_diff_patch *patch; - const git_diff_range *range; + git_diff *diff = NULL; + git_patch *patch; + const git_diff_hunk *range; const char *header, *line; char origin; @@ -707,33 +707,33 @@ void test_diff_workdir__larger_hunks(void) cl_assert_equal_i(2, (int)num_d); for (d = 0; d < num_d; ++d) { - cl_git_pass(git_diff_get_patch(&patch, NULL, diff, d)); + cl_git_pass(git_patch_from_diff(&patch, NULL, diff, d)); cl_assert(patch); - num_h = git_diff_patch_num_hunks(patch); + num_h = git_patch_num_hunks(patch); for (h = 0; h < num_h; h++) { - cl_git_pass(git_diff_patch_get_hunk( + cl_git_pass(git_patch_get_hunk( &range, &header, &header_len, &num_l, patch, h)); for (l = 0; l < num_l; ++l) { - cl_git_pass(git_diff_patch_get_line_in_hunk( + cl_git_pass(git_patch_get_line_in_hunk( &origin, &line, &line_len, NULL, NULL, patch, h, l)); cl_assert(line); } /* confirm fail after the last item */ - cl_git_fail(git_diff_patch_get_line_in_hunk( + cl_git_fail(git_patch_get_line_in_hunk( &origin, &line, &line_len, NULL, NULL, patch, h, num_l)); } /* confirm fail after the last item */ - cl_git_fail(git_diff_patch_get_hunk( + cl_git_fail(git_patch_get_hunk( &range, &header, &header_len, &num_l, patch, num_h)); - git_diff_patch_free(patch); + git_patch_free(patch); } - git_diff_list_free(diff); + git_diff_free(diff); } git_tree_free(a); @@ -758,7 +758,7 @@ void test_diff_workdir__submodules(void) const char *a_commit = "873585b94bdeabccea991ea5e3ec1a277895b698"; git_tree *a; git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; + git_diff *diff = NULL; diff_expects exp; g_repo = setup_fixture_submod2(); @@ -819,14 +819,14 @@ void test_diff_workdir__submodules(void) cl_assert_equal_i(30, exp.line_adds); cl_assert_equal_i(1, exp.line_dels); - git_diff_list_free(diff); + git_diff_free(diff); git_tree_free(a); } void test_diff_workdir__cannot_diff_against_a_bare_repository(void) { git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; + git_diff *diff = NULL; git_tree *tree; g_repo = cl_git_sandbox_init("testrepo.git"); @@ -844,7 +844,7 @@ void test_diff_workdir__cannot_diff_against_a_bare_repository(void) void test_diff_workdir__to_null_tree(void) { - git_diff_list *diff; + git_diff *diff; diff_expects exp; git_diff_options opts = GIT_DIFF_OPTIONS_INIT; @@ -862,12 +862,12 @@ void test_diff_workdir__to_null_tree(void) cl_assert_equal_i(exp.files, exp.file_status[GIT_DELTA_UNTRACKED]); - git_diff_list_free(diff); + git_diff_free(diff); } void test_diff_workdir__checks_options_version(void) { - git_diff_list *diff; + git_diff *diff; git_diff_options opts = GIT_DIFF_OPTIONS_INIT; const git_error *err; @@ -887,11 +887,11 @@ void test_diff_workdir__checks_options_version(void) void test_diff_workdir__can_diff_empty_file(void) { - git_diff_list *diff; + git_diff *diff; git_tree *tree; git_diff_options opts = GIT_DIFF_OPTIONS_INIT; struct stat st; - git_diff_patch *patch; + git_patch *patch; g_repo = cl_git_sandbox_init("attr_index"); @@ -901,7 +901,7 @@ void test_diff_workdir__can_diff_empty_file(void) cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, tree, &opts)); cl_assert_equal_i(2, (int)git_diff_num_deltas(diff)); - git_diff_list_free(diff); + git_diff_free(diff); /* empty contents of file */ @@ -912,9 +912,9 @@ void test_diff_workdir__can_diff_empty_file(void) cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, tree, &opts)); cl_assert_equal_i(3, (int)git_diff_num_deltas(diff)); /* diffs are: .gitattributes, README.txt, sub/sub/.gitattributes */ - cl_git_pass(git_diff_get_patch(&patch, NULL, diff, 1)); - git_diff_patch_free(patch); - git_diff_list_free(diff); + cl_git_pass(git_patch_from_diff(&patch, NULL, diff, 1)); + git_patch_free(patch); + git_diff_free(diff); /* remove a file altogether */ @@ -923,9 +923,9 @@ void test_diff_workdir__can_diff_empty_file(void) cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, tree, &opts)); cl_assert_equal_i(3, (int)git_diff_num_deltas(diff)); - cl_git_pass(git_diff_get_patch(&patch, NULL, diff, 1)); - git_diff_patch_free(patch); - git_diff_list_free(diff); + cl_git_pass(git_patch_from_diff(&patch, NULL, diff, 1)); + git_patch_free(patch); + git_diff_free(diff); git_tree_free(tree); } @@ -933,7 +933,7 @@ void test_diff_workdir__can_diff_empty_file(void) void test_diff_workdir__to_index_issue_1397(void) { git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; + git_diff *diff = NULL; diff_expects exp; g_repo = cl_git_sandbox_init("issue_1397"); @@ -953,7 +953,7 @@ void test_diff_workdir__to_index_issue_1397(void) cl_assert_equal_i(0, exp.hunks); cl_assert_equal_i(0, exp.lines); - git_diff_list_free(diff); + git_diff_free(diff); diff = NULL; cl_git_rewritefile("issue_1397/crlf_file.txt", @@ -975,7 +975,7 @@ void test_diff_workdir__to_index_issue_1397(void) cl_assert_equal_i(1, exp.line_adds); cl_assert_equal_i(1, exp.line_dels); - git_diff_list_free(diff); + git_diff_free(diff); } void test_diff_workdir__to_tree_issue_1397(void) @@ -983,8 +983,8 @@ void test_diff_workdir__to_tree_issue_1397(void) const char *a_commit = "7f483a738"; /* the current HEAD */ git_tree *a; git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; - git_diff_list *diff2 = NULL; + git_diff *diff = NULL; + git_diff *diff2 = NULL; diff_expects exp; g_repo = cl_git_sandbox_init("issue_1397"); @@ -1006,13 +1006,13 @@ void test_diff_workdir__to_tree_issue_1397(void) cl_assert_equal_i(0, exp.hunks); cl_assert_equal_i(0, exp.lines); - git_diff_list_free(diff); + git_diff_free(diff); diff = NULL; cl_git_pass(git_diff_tree_to_index(&diff, g_repo, a, NULL, &opts)); cl_git_pass(git_diff_index_to_workdir(&diff2, g_repo, NULL, &opts)); cl_git_pass(git_diff_merge(diff, diff2)); - git_diff_list_free(diff2); + git_diff_free(diff2); memset(&exp, 0, sizeof(exp)); cl_git_pass(git_diff_foreach( @@ -1022,14 +1022,14 @@ void test_diff_workdir__to_tree_issue_1397(void) cl_assert_equal_i(0, exp.hunks); cl_assert_equal_i(0, exp.lines); - git_diff_list_free(diff); + git_diff_free(diff); git_tree_free(a); } void test_diff_workdir__untracked_directory_scenarios(void) { git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; + git_diff *diff = NULL; diff_expects exp; char *pathspec = NULL; static const char *files0[] = { @@ -1079,7 +1079,7 @@ void test_diff_workdir__untracked_directory_scenarios(void) cl_assert_equal_i(0, exp.file_status[GIT_DELTA_IGNORED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNTRACKED]); - git_diff_list_free(diff); + git_diff_free(diff); /* empty directory */ @@ -1099,7 +1099,7 @@ void test_diff_workdir__untracked_directory_scenarios(void) cl_assert_equal_i(1, exp.file_status[GIT_DELTA_IGNORED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNTRACKED]); - git_diff_list_free(diff); + git_diff_free(diff); /* empty directory in empty directory */ @@ -1119,7 +1119,7 @@ void test_diff_workdir__untracked_directory_scenarios(void) cl_assert_equal_i(1, exp.file_status[GIT_DELTA_IGNORED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNTRACKED]); - git_diff_list_free(diff); + git_diff_free(diff); /* directory with only ignored files */ @@ -1143,7 +1143,7 @@ void test_diff_workdir__untracked_directory_scenarios(void) cl_assert_equal_i(1, exp.file_status[GIT_DELTA_IGNORED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNTRACKED]); - git_diff_list_free(diff); + git_diff_free(diff); /* directory with ignored directory (contents irrelevant) */ @@ -1166,7 +1166,7 @@ void test_diff_workdir__untracked_directory_scenarios(void) cl_assert_equal_i(1, exp.file_status[GIT_DELTA_IGNORED]); cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNTRACKED]); - git_diff_list_free(diff); + git_diff_free(diff); /* quick version avoids directory scan */ @@ -1186,7 +1186,7 @@ void test_diff_workdir__untracked_directory_scenarios(void) cl_assert_equal_i(0, exp.file_status[GIT_DELTA_IGNORED]); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_UNTRACKED]); - git_diff_list_free(diff); + git_diff_free(diff); /* directory with nested non-ignored content */ @@ -1209,7 +1209,7 @@ void test_diff_workdir__untracked_directory_scenarios(void) cl_assert_equal_i(0, exp.file_status[GIT_DELTA_IGNORED]); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_UNTRACKED]); - git_diff_list_free(diff); + git_diff_free(diff); /* use RECURSE_UNTRACKED_DIRS to get actual untracked files (no ignores) */ @@ -1230,14 +1230,14 @@ void test_diff_workdir__untracked_directory_scenarios(void) cl_assert_equal_i(0, exp.file_status[GIT_DELTA_IGNORED]); cl_assert_equal_i(2, exp.file_status[GIT_DELTA_UNTRACKED]); - git_diff_list_free(diff); + git_diff_free(diff); } void test_diff_workdir__untracked_directory_comes_last(void) { git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; + git_diff *diff = NULL; g_repo = cl_git_sandbox_init("renames"); @@ -1255,13 +1255,13 @@ void test_diff_workdir__untracked_directory_comes_last(void) cl_assert(diff != NULL); - git_diff_list_free(diff); + git_diff_free(diff); } void test_diff_workdir__untracked_with_bom(void) { git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - git_diff_list *diff = NULL; + git_diff *diff = NULL; const git_diff_delta *delta; g_repo = cl_git_sandbox_init("empty_standard_repo"); @@ -1276,9 +1276,9 @@ void test_diff_workdir__untracked_with_bom(void) cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); cl_assert_equal_i(1, git_diff_num_deltas(diff)); - cl_git_pass(git_diff_get_patch(NULL, &delta, diff, 0)); + cl_git_pass(git_patch_from_diff(NULL, &delta, diff, 0)); cl_assert_equal_i(GIT_DELTA_UNTRACKED, delta->status); cl_assert((delta->flags & GIT_DIFF_FLAG_BINARY) != 0); - git_diff_list_free(diff); + git_diff_free(diff); } diff --git a/tests-clar/stress/diff.c b/tests-clar/stress/diff.c index 1d319738e..3d2092614 100644 --- a/tests-clar/stress/diff.c +++ b/tests-clar/stress/diff.c @@ -19,7 +19,7 @@ static void test_with_many(int expected_new) { git_index *index; git_tree *tree, *new_tree; - git_diff_list *diff = NULL; + git_diff *diff = NULL; diff_expects exp; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT; @@ -52,7 +52,7 @@ static void test_with_many(int expected_new) cl_assert_equal_i(expected_new, exp.file_status[GIT_DELTA_ADDED]); cl_assert_equal_i(expected_new + 1, exp.files); - git_diff_list_free(diff); + git_diff_free(diff); cl_repo_commit_from_index(NULL, g_repo, NULL, 1372350000, "yoyoyo"); cl_git_pass(git_revparse_single( @@ -78,7 +78,7 @@ static void test_with_many(int expected_new) cl_assert_equal_i(expected_new, exp.file_status[GIT_DELTA_ADDED]); cl_assert_equal_i(expected_new + 1, exp.files); - git_diff_list_free(diff); + git_diff_free(diff); git_tree_free(new_tree); git_tree_free(tree); From 10672e3e455eba2d4ca983070ed427caeeb24a6f Mon Sep 17 00:00:00 2001 From: Russell Belfer Date: Tue, 15 Oct 2013 15:10:07 -0700 Subject: [PATCH 2/7] Diff API cleanup This lays groundwork for separating formatting options from diff creation options. This groups the formatting flags separately from the diff list creation flags and reorders the options. This also tweaks some APIs to further separate code that uses patches from code that just looks at git_diffs. --- examples/diff.c | 29 +-- examples/log.c | 2 +- include/git2/diff.h | 408 ++++++++++++++++++--------------- include/git2/patch.h | 17 +- src/diff.c | 72 ++++-- src/diff_file.c | 2 +- src/diff_patch.c | 19 +- src/diff_print.c | 126 ++++++---- src/diff_tform.c | 12 +- src/diff_xdiff.c | 4 +- src/reset.c | 7 +- src/stash.c | 127 +++++----- tests-clar/diff/blob.c | 32 +-- tests-clar/diff/diff_helpers.c | 10 +- tests-clar/diff/diffiter.c | 34 ++- tests-clar/diff/drivers.c | 12 +- tests-clar/diff/patch.c | 22 +- tests-clar/diff/rename.c | 9 +- tests-clar/diff/submodules.c | 5 +- tests-clar/diff/tree.c | 28 +-- tests-clar/diff/workdir.c | 21 +- 21 files changed, 530 insertions(+), 468 deletions(-) diff --git a/examples/diff.c b/examples/diff.c index 3200b53c8..2542f4269 100644 --- a/examples/diff.c +++ b/examples/diff.c @@ -114,12 +114,6 @@ static void usage(const char *message, const char *arg) exit(1); } -enum { - FORMAT_PATCH = 0, - FORMAT_COMPACT = 1, - FORMAT_RAW = 2 -}; - int main(int argc, char *argv[]) { git_repository *repo = NULL; @@ -127,7 +121,8 @@ int main(int argc, char *argv[]) git_diff_options opts = GIT_DIFF_OPTIONS_INIT; git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT; git_diff *diff; - int i, color = -1, format = FORMAT_PATCH, cached = 0; + int i, color = -1, cached = 0; + git_diff_format_t format = GIT_DIFF_FORMAT_PATCH; char *a, *treeish1 = NULL, *treeish2 = NULL; const char *dir = "."; @@ -148,13 +143,15 @@ int main(int argc, char *argv[]) } else if (!strcmp(a, "-p") || !strcmp(a, "-u") || !strcmp(a, "--patch")) - format = FORMAT_PATCH; + format = GIT_DIFF_FORMAT_PATCH; else if (!strcmp(a, "--cached")) cached = 1; + else if (!strcmp(a, "--name-only")) + format = GIT_DIFF_FORMAT_NAME_ONLY; else if (!strcmp(a, "--name-status")) - format = FORMAT_COMPACT; + format = GIT_DIFF_FORMAT_NAME_STATUS; else if (!strcmp(a, "--raw")) - format = FORMAT_RAW; + format = GIT_DIFF_FORMAT_RAW; else if (!strcmp(a, "--color")) color = 0; else if (!strcmp(a, "--no-color")) @@ -238,17 +235,7 @@ int main(int argc, char *argv[]) if (color >= 0) fputs(colors[0], stdout); - switch (format) { - case FORMAT_PATCH: - check(git_diff_print_patch(diff, printer, &color), "Displaying diff"); - break; - case FORMAT_COMPACT: - check(git_diff_print_compact(diff, printer, &color), "Displaying diff"); - break; - case FORMAT_RAW: - check(git_diff_print_raw(diff, printer, &color), "Displaying diff"); - break; - } + check(git_diff_print(diff, format, printer, &color), "Displaying diff"); if (color >= 0) fputs(colors[0], stdout); diff --git a/examples/log.c b/examples/log.c index 2ba5aa52a..30de16ae9 100644 --- a/examples/log.c +++ b/examples/log.c @@ -388,7 +388,7 @@ int main(int argc, char *argv[]) check(git_diff_tree_to_tree( &diff, git_commit_owner(commit), a, b, &diffopts), "Diff commit with parent", NULL); - check(git_diff_print_patch(diff, print_diff, NULL), + check(git_diff_print(diff, GIT_DIFF_FORMAT_PATCH, print_diff, NULL), "Displaying diff", NULL); git_diff_free(diff); diff --git a/include/git2/diff.h b/include/git2/diff.h index 11c3bce20..fd79ccf04 100644 --- a/include/git2/diff.h +++ b/include/git2/diff.h @@ -23,7 +23,7 @@ * Calculating diffs is generally done in two phases: building a list of * diffs then traversing it. This makes is easier to share logic across * the various types of diffs (tree vs tree, workdir vs index, etc.), and - * also allows you to insert optional diff list post-processing phases, + * also allows you to insert optional diff post-processing phases, * such as rename detection, in between the steps. When you are done with * a diff object, it must be freed. * @@ -72,89 +72,111 @@ GIT_BEGIN_DECL typedef enum { /** Normal diff, the default */ GIT_DIFF_NORMAL = 0, + + /* + * Options controlling which files will be in the diff + */ + /** Reverse the sides of the diff */ - GIT_DIFF_REVERSE = (1 << 0), - /** Treat all files as text, disabling binary attributes & detection */ - GIT_DIFF_FORCE_TEXT = (1 << 1), - /** Ignore all whitespace */ - GIT_DIFF_IGNORE_WHITESPACE = (1 << 2), - /** Ignore changes in amount of whitespace */ - GIT_DIFF_IGNORE_WHITESPACE_CHANGE = (1 << 3), - /** Ignore whitespace at end of line */ - GIT_DIFF_IGNORE_WHITESPACE_EOL = (1 << 4), - /** Treat all submodules as unmodified */ - GIT_DIFF_IGNORE_SUBMODULES = (1 << 5), - /** Use the "patience diff" algorithm (currently unimplemented) */ - GIT_DIFF_PATIENCE = (1 << 6), - /** Include ignored files in the diff list */ - GIT_DIFF_INCLUDE_IGNORED = (1 << 7), - /** Include untracked files in the diff list */ - GIT_DIFF_INCLUDE_UNTRACKED = (1 << 8), - /** Include unmodified files in the diff list */ - GIT_DIFF_INCLUDE_UNMODIFIED = (1 << 9), + GIT_DIFF_REVERSE = (1u << 0), + + /** Include ignored files in the diff */ + GIT_DIFF_INCLUDE_IGNORED = (1u << 1), + + /** Even with GIT_DIFF_INCLUDE_IGNORED, an entire ignored directory + * will be marked with only a single entry in the diff; this flag + * adds all files under the directory as IGNORED entries, too. + */ + GIT_DIFF_RECURSE_IGNORED_DIRS = (1u << 2), + + /** Include untracked files in the diff */ + GIT_DIFF_INCLUDE_UNTRACKED = (1u << 3), /** Even with GIT_DIFF_INCLUDE_UNTRACKED, an entire untracked - * directory will be marked with only a single entry in the diff list + * directory will be marked with only a single entry in the diff * (a la what core Git does in `git status`); this flag adds *all* * files under untracked directories as UNTRACKED entries, too. */ - GIT_DIFF_RECURSE_UNTRACKED_DIRS = (1 << 10), + GIT_DIFF_RECURSE_UNTRACKED_DIRS = (1u << 4), - /** If the pathspec is set in the diff options, this flags means to - * apply it as an exact match instead of as an fnmatch pattern. - */ - GIT_DIFF_DISABLE_PATHSPEC_MATCH = (1 << 11), - - /** Use case insensitive filename comparisons */ - GIT_DIFF_DELTAS_ARE_ICASE = (1 << 12), - - /** When generating patch text, include the content of untracked - * files. This automatically turns on GIT_DIFF_INCLUDE_UNTRACKED but - * it does not turn on GIT_DIFF_RECURSE_UNTRACKED_DIRS. Add that - * flag if you want the content of every single UNTRACKED file. - */ - GIT_DIFF_INCLUDE_UNTRACKED_CONTENT = (1 << 13), - - /** Disable updating of the `binary` flag in delta records. This is - * useful when iterating over a diff if you don't need hunk and data - * callbacks and want to avoid having to load file completely. - */ - GIT_DIFF_SKIP_BINARY_CHECK = (1 << 14), + /** Include unmodified files in the diff */ + GIT_DIFF_INCLUDE_UNMODIFIED = (1u << 5), /** Normally, a type change between files will be converted into a * DELETED record for the old and an ADDED record for the new; this * options enabled the generation of TYPECHANGE delta records. */ - GIT_DIFF_INCLUDE_TYPECHANGE = (1 << 15), + GIT_DIFF_INCLUDE_TYPECHANGE = (1u << 6), /** Even with GIT_DIFF_INCLUDE_TYPECHANGE, blob->tree changes still * generally show as a DELETED blob. This flag tries to correctly * label blob->tree transitions as TYPECHANGE records with new_file's * mode set to tree. Note: the tree SHA will not be available. */ - GIT_DIFF_INCLUDE_TYPECHANGE_TREES = (1 << 16), + GIT_DIFF_INCLUDE_TYPECHANGE_TREES = (1u << 7), /** Ignore file mode changes */ - GIT_DIFF_IGNORE_FILEMODE = (1 << 17), + GIT_DIFF_IGNORE_FILEMODE = (1u << 8), - /** Even with GIT_DIFF_INCLUDE_IGNORED, an entire ignored directory - * will be marked with only a single entry in the diff list; this flag - * adds all files under the directory as IGNORED entries, too. + /** Treat all submodules as unmodified */ + GIT_DIFF_IGNORE_SUBMODULES = (1u << 9), + + /** Use case insensitive filename comparisons */ + GIT_DIFF_IGNORE_CASE = (1u << 10), + + /** If the pathspec is set in the diff options, this flags means to + * apply it as an exact match instead of as an fnmatch pattern. */ - GIT_DIFF_RECURSE_IGNORED_DIRS = (1 << 18), + GIT_DIFF_DISABLE_PATHSPEC_MATCH = (1u << 12), - /** Core Git scans inside untracked directories, labeling them IGNORED - * if they are empty or only contain ignored files; a directory is - * consider UNTRACKED only if it has an actual untracked file in it. - * This scan is extra work for a case you often don't care about. This - * flag makes libgit2 immediately label an untracked directory as - * UNTRACKED without looking inside it (which differs from core Git). - * Of course, ignore rules are still checked for the directory itself. + /** Disable updating of the `binary` flag in delta records. This is + * useful when iterating over a diff if you don't need hunk and data + * callbacks and want to avoid having to load file completely. */ - GIT_DIFF_FAST_UNTRACKED_DIRS = (1 << 19), + GIT_DIFF_SKIP_BINARY_CHECK = (1u << 13), + /** When diff finds an untracked directory, to match the behavior of + * core Git, it scans the contents for IGNORED and UNTRACKED files. + * If *all* contents are IGNORED, then the directory is IGNORED; if + * any contents are not IGNORED, then the directory is UNTRACKED. + * This is extra work that may not matter in many cases. This flag + * turns off that scan and immediately labels an untracked directory + * as UNTRACKED (changing the behavior to not match core Git). + */ + GIT_DIFF_ENABLE_FAST_UNTRACKED_DIRS = (1u << 14), + + /* + * Options controlling how output will be generated + */ + + /** Treat all files as text, disabling binary attributes & detection */ + GIT_DIFF_FORCE_TEXT = (1u << 20), /** Treat all files as binary, disabling text diffs */ - GIT_DIFF_FORCE_BINARY = (1 << 20), + GIT_DIFF_FORCE_BINARY = (1u << 21), + + /** Ignore all whitespace */ + GIT_DIFF_IGNORE_WHITESPACE = (1u << 22), + /** Ignore changes in amount of whitespace */ + GIT_DIFF_IGNORE_WHITESPACE_CHANGE = (1u << 23), + /** Ignore whitespace at end of line */ + GIT_DIFF_IGNORE_WHITESPACE_EOL = (1u << 24), + + /** When generating patch text, include the content of untracked + * files. This automatically turns on GIT_DIFF_INCLUDE_UNTRACKED but + * it does not turn on GIT_DIFF_RECURSE_UNTRACKED_DIRS. Add that + * flag if you want the content of every single UNTRACKED file. + */ + GIT_DIFF_SHOW_UNTRACKED_CONTENT = (1u << 25), + + /** When generating output, include the names of unmodified files if + * they are included in the git_diff. Normally these are skipped in + * the formats that list files (e.g. name-only, name-status, raw). + * Even with this, these will not be included in patch format. + */ + GIT_DIFF_SHOW_UNMODIFIED = (1u << 26), + + /** Use the "patience diff" algorithm (currently unimplemented) */ + GIT_DIFF_PATIENCE = (1u << 28), } git_diff_option_t; /** @@ -176,16 +198,16 @@ typedef struct git_diff git_diff; * considered reserved for internal or future use. */ typedef enum { - GIT_DIFF_FLAG_BINARY = (1 << 0), /** file(s) treated as binary data */ - GIT_DIFF_FLAG_NOT_BINARY = (1 << 1), /** file(s) treated as text data */ - GIT_DIFF_FLAG_VALID_OID = (1 << 2), /** `oid` value is known correct */ + GIT_DIFF_FLAG_BINARY = (1u << 0), /** file(s) treated as binary data */ + GIT_DIFF_FLAG_NOT_BINARY = (1u << 1), /** file(s) treated as text data */ + GIT_DIFF_FLAG_VALID_OID = (1u << 2), /** `oid` value is known correct */ } git_diff_flag_t; /** * What type of change is described by a git_diff_delta? * * `GIT_DELTA_RENAMED` and `GIT_DELTA_COPIED` will only show up if you run - * `git_diff_find_similar()` on the diff list object. + * `git_diff_find_similar()` on the diff object. * * `GIT_DELTA_TYPECHANGE` only shows up given `GIT_DIFF_INCLUDE_TYPECHANGE` * in the option flags (otherwise type changes will be split into ADDED / @@ -284,9 +306,9 @@ typedef struct { * * When the callback: * - returns < 0, the diff process will be aborted. - * - returns > 0, the delta will not be inserted into the diff list, but the + * - returns > 0, the delta will not be inserted into the diff, but the * diff process continues. - * - returns 0, the delta is inserted into the diff list, and the diff process + * - returns 0, the delta is inserted into the diff, and the diff process * continues. */ typedef int (*git_diff_notify_cb)( @@ -323,25 +345,33 @@ typedef int (*git_diff_notify_cb)( typedef struct { unsigned int version; /**< version for the struct */ uint32_t flags; /**< defaults to GIT_DIFF_NORMAL */ - uint16_t context_lines; /**< defaults to 3 */ - uint16_t interhunk_lines; /**< defaults to 0 */ - const char *old_prefix; /**< defaults to "a" */ - const char *new_prefix; /**< defaults to "b" */ + + /* options controlling which files are in the diff */ + + git_submodule_ignore_t ignore_submodules; /** << submodule ignore rule */ git_strarray pathspec; /**< defaults to include all paths */ - git_off_t max_size; /**< defaults to 512MB */ git_diff_notify_cb notify_cb; void *notify_payload; - git_submodule_ignore_t ignore_submodules; /** << submodule ignore rule */ + + /* options controlling how to diff text is generated */ + + uint16_t context_lines; /**< defaults to 3 */ + uint16_t interhunk_lines; /**< defaults to 0 */ + uint16_t oid_abbrev; /**< default 'core.abbrev' or 7 if unset */ + git_off_t max_size; /**< defaults to 512MB */ + const char *old_prefix; /**< defaults to "a" */ + const char *new_prefix; /**< defaults to "b" */ } git_diff_options; #define GIT_DIFF_OPTIONS_VERSION 1 -#define GIT_DIFF_OPTIONS_INIT {GIT_DIFF_OPTIONS_VERSION, GIT_DIFF_NORMAL, 3} +#define GIT_DIFF_OPTIONS_INIT \ + {GIT_DIFF_OPTIONS_VERSION, 0, 0, {NULL,0}, NULL, NULL, 3} /** * When iterating over a diff, callback that will be made per file. * * @param delta A pointer to the delta data for the file - * @param progress Goes from 0 to 1 over the diff list + * @param progress Goes from 0 to 1 over the diff * @param payload User-specified pointer from foreach function */ typedef int (*git_diff_file_cb)( @@ -390,8 +420,7 @@ typedef enum { GIT_DIFF_LINE_DEL_EOFNL = '<', /**< Old has LF at end, new does not */ /* The following values will only be sent to a `git_diff_line_cb` when - * the content of a diff is being formatted (eg. through - * git_diff_print_patch() or git_diff_print_compact(), for instance). + * the content of a diff is being formatted through `git_diff_print`. */ GIT_DIFF_LINE_FILE_HDR = 'F', GIT_DIFF_LINE_HUNK_HDR = 'H', @@ -419,25 +448,25 @@ typedef int (*git_diff_line_cb)( */ typedef enum { /** look for renames? (`--find-renames`) */ - GIT_DIFF_FIND_RENAMES = (1 << 0), + GIT_DIFF_FIND_RENAMES = (1u << 0), /** consider old side of modified for renames? (`--break-rewrites=N`) */ - GIT_DIFF_FIND_RENAMES_FROM_REWRITES = (1 << 1), + GIT_DIFF_FIND_RENAMES_FROM_REWRITES = (1u << 1), /** look for copies? (a la `--find-copies`) */ - GIT_DIFF_FIND_COPIES = (1 << 2), + GIT_DIFF_FIND_COPIES = (1u << 2), /** consider unmodified as copy sources? (`--find-copies-harder`) */ - GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED = (1 << 3), + GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED = (1u << 3), /** mark large rewrites for split (`--break-rewrites=/M`) */ - GIT_DIFF_FIND_REWRITES = (1 << 4), + GIT_DIFF_FIND_REWRITES = (1u << 4), /** actually split large rewrites into delete/add pairs */ - GIT_DIFF_BREAK_REWRITES = (1 << 5), + GIT_DIFF_BREAK_REWRITES = (1u << 5), /** mark rewrites for split and break into delete/add pairs */ GIT_DIFF_FIND_AND_BREAK_REWRITES = (GIT_DIFF_FIND_REWRITES | GIT_DIFF_BREAK_REWRITES), /** find renames/copies for untracked items in working directory */ - GIT_DIFF_FIND_FOR_UNTRACKED = (1 << 6), + GIT_DIFF_FIND_FOR_UNTRACKED = (1u << 6), /** turn on all finding features */ GIT_DIFF_FIND_ALL = (0x0ff), @@ -445,14 +474,14 @@ typedef enum { /** measure similarity ignoring leading whitespace (default) */ GIT_DIFF_FIND_IGNORE_LEADING_WHITESPACE = 0, /** measure similarity ignoring all whitespace */ - GIT_DIFF_FIND_IGNORE_WHITESPACE = (1 << 12), + GIT_DIFF_FIND_IGNORE_WHITESPACE = (1u << 12), /** measure similarity including all data */ - GIT_DIFF_FIND_DONT_IGNORE_WHITESPACE = (1 << 13), + GIT_DIFF_FIND_DONT_IGNORE_WHITESPACE = (1u << 13), /** measure similarity only by comparing SHAs (fast and cheap) */ - GIT_DIFF_FIND_EXACT_MATCH_ONLY = (1 << 14), + GIT_DIFF_FIND_EXACT_MATCH_ONLY = (1u << 14), /** do not break rewrites unless they contribute to a rename */ - GIT_DIFF_BREAK_REWRITES_FOR_RENAMES_ONLY = (1 << 15), + GIT_DIFF_BREAK_REWRITES_FOR_RENAMES_ONLY = (1u << 15), } git_diff_find_t; /** @@ -517,7 +546,7 @@ typedef struct { #define GIT_DIFF_FIND_OPTIONS_VERSION 1 #define GIT_DIFF_FIND_OPTIONS_INIT {GIT_DIFF_FIND_OPTIONS_VERSION} -/** @name Diff List Generator Functions +/** @name Diff Generator Functions * * These are the functions you would use to create (or destroy) a * git_diff from various objects in a repository. @@ -525,14 +554,14 @@ typedef struct { /**@{*/ /** - * Deallocate a diff list. + * Deallocate a diff. * - * @param diff The previously created diff list; cannot be used after free. + * @param diff The previously created diff; cannot be used after free. */ GIT_EXTERN(void) git_diff_free(git_diff *diff); /** - * Create a diff list with the difference between two tree objects. + * Create a diff with the difference between two tree objects. * * This is equivalent to `git diff ` * @@ -555,7 +584,7 @@ GIT_EXTERN(int) git_diff_tree_to_tree( const git_diff_options *opts); /**< can be NULL for defaults */ /** - * Create a diff list between a tree and repository index. + * Create a diff between a tree and repository index. * * This is equivalent to `git diff --cached ` or if you pass * the HEAD tree, then like `git diff --cached`. @@ -577,7 +606,7 @@ GIT_EXTERN(int) git_diff_tree_to_index( const git_diff_options *opts); /**< can be NULL for defaults */ /** - * Create a diff list between the repository index and the workdir directory. + * Create a diff between the repository index and the workdir directory. * * This matches the `git diff` command. See the note below on * `git_diff_tree_to_workdir` for a discussion of the difference between @@ -599,7 +628,7 @@ GIT_EXTERN(int) git_diff_index_to_workdir( const git_diff_options *opts); /**< can be NULL for defaults */ /** - * Create a diff list between a tree and the working directory. + * Create a diff between a tree and the working directory. * * The tree you provide will be used for the "old_file" side of the delta, * and the working directory will be used for the "new_file" side. @@ -613,9 +642,10 @@ GIT_EXTERN(int) git_diff_index_to_workdir( * files in the index. It may come as a surprise, but there is no direct * equivalent in core git. * - * To emulate `git diff `, call both `git_diff_tree_to_index` and - * `git_diff_index_to_workdir`, then call `git_diff_merge` on the results. - * That will yield a `git_diff` that matches the git output. + * To emulate `git diff `, use `git_diff_tree_to_workdir_with_index` + * (or `git_diff_tree_to_index` and `git_diff_index_to_workdir`, then call + * `git_diff_merge` on the results). That will yield a `git_diff` that + * matches the git output. * * If this seems confusing, take the case of a file with a staged deletion * where the file has then been put back into the working dir and modified. @@ -634,10 +664,29 @@ GIT_EXTERN(int) git_diff_tree_to_workdir( const git_diff_options *opts); /**< can be NULL for defaults */ /** - * Merge one diff list into another. + * Create a diff between a tree and the working directory using index data + * to account for staged deletes, tracked files, etc. + * + * This emulates `git diff ` by diffing the tree to the index and + * the index to the working directory and blending the results into a + * single diff that includes staged deleted, etc. + * + * @param diff A pointer to a git_diff pointer that will be allocated. + * @param repo The repository containing the tree. + * @param old_tree A git_tree object to diff from, or NULL for empty tree. + * @param opts Structure with options to influence diff or NULL for defaults. + */ +GIT_EXTERN(int) git_diff_tree_to_workdir_with_index( + git_diff **diff, + git_repository *repo, + git_tree *old_tree, + const git_diff_options *opts); /**< can be NULL for defaults */ + +/** + * Merge one diff into another. * * This merges items from the "from" list into the "onto" list. The - * resulting diff list will have all items that appear in either list. + * resulting diff will have all items that appear in either list. * If an item appears in both lists, then it will be "merged" to appear * as if the old version was from the "onto" list and the new version * is from the "from" list (with the exception that if the item has a @@ -651,40 +700,90 @@ GIT_EXTERN(int) git_diff_merge( const git_diff *from); /** - * Transform a diff list marking file renames, copies, etc. + * Transform a diff marking file renames, copies, etc. * - * This modifies a diff list in place, replacing old entries that look + * This modifies a diff in place, replacing old entries that look * like renames or copies with new entries reflecting those changes. * This also will, if requested, break modified files into add/remove * pairs if the amount of change is above a threshold. * - * @param diff Diff list to run detection algorithms on + * @param diff diff to run detection algorithms on * @param options Control how detection should be run, NULL for defaults * @return 0 on success, -1 on failure */ GIT_EXTERN(int) git_diff_find_similar( git_diff *diff, - git_diff_find_options *options); + const git_diff_find_options *options); /**@}*/ -/** @name Diff List Processor Functions +/** @name Diff Processor Functions * - * These are the functions you apply to a diff list to process it + * These are the functions you apply to a diff to process it * or read it in some way. */ /**@{*/ /** - * Loop over all deltas in a diff list issuing callbacks. + * Query how many diff records are there in a diff. + * + * @param diff A git_diff generated by one of the above functions + * @return Count of number of deltas in the list + */ +GIT_EXTERN(size_t) git_diff_num_deltas(const git_diff *diff); + +/** + * Query how many diff deltas are there in a diff filtered by type. + * + * This works just like `git_diff_entrycount()` with an extra parameter + * that is a `git_delta_t` and returns just the count of how many deltas + * match that particular type. + * + * @param diff A git_diff generated by one of the above functions + * @param type A git_delta_t value to filter the count + * @return Count of number of deltas matching delta_t type + */ +GIT_EXTERN(size_t) git_diff_num_deltas_of_type( + const git_diff *diff, git_delta_t type); + +/** + * Return the diff delta for an entry in the diff list. + * + * The `git_delta` pointer points to internal data and you do not have + * to release it when you are done with it. It will go away when the + * `git_diff` (or any associated `git_patch`) goes away. + * + * Note that the flags on the delta related to whether it has binary + * content or not may not be set if there are no attributes set for the + * file and there has been no reason to load the file data at this point. + * For now, if you need those flags to be up to date, your only option is + * to either use `git_diff_foreach` or create a `git_patch`. + * + * @param diff Diff list object + * @param idx Index into diff list + * @return Pointer to git_diff_delta (or NULL if `idx` out of range) + */ +GIT_EXTERN(const git_diff_delta *) git_diff_get_delta( + const git_diff *diff, size_t idx); + +/** + * Check if deltas are sorted case sensitively or insensitively. + * + * @param diff diff to check + * @return 0 if case sensitive, 1 if case is ignored + */ +GIT_EXTERN(int) git_diff_is_sorted_icase(const git_diff *diff); + +/** + * Loop over all deltas in a diff issuing callbacks. * * This will iterate through all of the files described in a diff. You * should provide a file callback to learn about each file. * * The "hunk" and "line" callbacks are optional, and the text diff of the * files will only be calculated if they are not NULL. Of course, these - * callbacks will not be invoked for binary files on the diff list or for + * callbacks will not be invoked for binary files on the diff or for * files whose only changed is a file mode change. * * Returning a non-zero value from any of the callbacks will terminate @@ -708,46 +807,13 @@ GIT_EXTERN(int) git_diff_foreach( git_diff_line_cb line_cb, void *payload); -/** - * Iterate over a diff generating text output like "git diff --name-status". - * - * Returning a non-zero value from the callbacks will terminate the - * iteration and cause this return `GIT_EUSER`. - * - * @param diff A git_diff generated by one of the above functions. - * @param print_cb Callback to make per line of diff text. - * @param payload Reference pointer that will be passed to your callback. - * @return 0 on success, GIT_EUSER on non-zero callback, or error code - */ -GIT_EXTERN(int) git_diff_print_compact( - git_diff *diff, - git_diff_line_cb print_cb, - void *payload); - -/** - * Iterate over a diff generating text output like "git diff --raw". - * - * Returning a non-zero value from the callbacks will terminate the - * iteration and cause this return `GIT_EUSER`. - * - * @param diff A git_diff generated by one of the above functions. - * @param print_cb Callback to make per line of diff text. - * @param payload Reference pointer that will be passed to your callback. - * @return 0 on success, GIT_EUSER on non-zero callback, or error code - */ -GIT_EXTERN(int) git_diff_print_raw( - git_diff *diff, - git_diff_line_cb print_cb, - void *payload); - /** * Look up the single character abbreviation for a delta status code. * - * When you call `git_diff_print_compact` it prints single letter codes into - * the output such as 'A' for added, 'D' for deleted, 'M' for modified, etc. - * It is sometimes convenient to convert a git_delta_t value into these - * letters for your own purposes. This function does just that. By the - * way, unmodified will return a space (i.e. ' '). + * When you run `git diff --name-status` it uses single letter codes in + * the output such as 'A' for added, 'D' for deleted, 'M' for modified, + * etc. This function converts a git_delta_t value into these letters for + * your own purposes. GIT_DELTA_UNTRACKED will return a space (i.e. ' '). * * @param status The git_delta_t value to look up * @return The single character label for that code @@ -755,58 +821,34 @@ GIT_EXTERN(int) git_diff_print_raw( GIT_EXTERN(char) git_diff_status_char(git_delta_t status); /** - * Iterate over a diff generating text output like "git diff". - * - * This is a super easy way to generate a patch from a diff. + * Possible output formats for diff data + */ +typedef enum { + GIT_DIFF_FORMAT_PATCH = 1u, /**< full git diff */ + GIT_DIFF_FORMAT_PATCH_HEADER = 2u, /**< just the file headers of patch */ + GIT_DIFF_FORMAT_RAW = 3u, /**< like git diff --raw */ + GIT_DIFF_FORMAT_NAME_ONLY = 4u, /**< like git diff --name-only */ + GIT_DIFF_FORMAT_NAME_STATUS = 5u, /**< like git diff --name-status */ +} git_diff_format_t; + +/** + * Iterate over a diff generating formatted text output. * * Returning a non-zero value from the callbacks will terminate the * iteration and cause this return `GIT_EUSER`. * * @param diff A git_diff generated by one of the above functions. - * @param payload Reference pointer that will be passed to your callbacks. - * @param print_cb Callback function to output lines of the diff. This - * same function will be called for file headers, hunk - * headers, and diff lines. Fortunately, you can probably - * use various GIT_DIFF_LINE constants to determine what - * text you are given. + * @param format A git_diff_forrmat_t value to pick the text format. + * @param print_cb Callback to make per line of diff text. + * @param payload Reference pointer that will be passed to your callback. * @return 0 on success, GIT_EUSER on non-zero callback, or error code */ -GIT_EXTERN(int) git_diff_print_patch( +GIT_EXTERN(int) git_diff_print( git_diff *diff, + git_diff_format_t format, git_diff_line_cb print_cb, void *payload); -/** - * Query how many diff records are there in a diff list. - * - * @param diff A git_diff generated by one of the above functions - * @return Count of number of deltas in the list - */ -GIT_EXTERN(size_t) git_diff_num_deltas(git_diff *diff); - -/** - * Query how many diff deltas are there in a diff list filtered by type. - * - * This works just like `git_diff_entrycount()` with an extra parameter - * that is a `git_delta_t` and returns just the count of how many deltas - * match that particular type. - * - * @param diff A git_diff generated by one of the above functions - * @param type A git_delta_t value to filter the count - * @return Count of number of deltas matching delta_t type - */ -GIT_EXTERN(size_t) git_diff_num_deltas_of_type( - git_diff *diff, - git_delta_t type); - -/** - * Check if deltas are sorted case sensitively or insensitively. - * - * @param diff Diff list to check - * @return 0 if case sensitive, 1 if case is ignored - */ -GIT_EXTERN(int) git_diff_is_sorted_icase(const git_diff *diff); - /**@}*/ diff --git a/include/git2/patch.h b/include/git2/patch.h index ad9be7516..9836245de 100644 --- a/include/git2/patch.h +++ b/include/git2/patch.h @@ -47,17 +47,13 @@ typedef struct git_patch git_patch; * It is okay to pass NULL for either of the output parameters; if you pass * NULL for the `git_patch`, then the text diff will not be calculated. * - * @param patch_out Output parameter for the delta patch object - * @param delta_out Output parameter for the delta object + * @param out Output parameter for the delta patch object * @param diff Diff list object * @param idx Index into diff list * @return 0 on success, other value < 0 on error */ GIT_EXTERN(int) git_patch_from_diff( - git_patch **patch_out, - const git_diff_delta **delta_out, - git_diff *diff, - size_t idx); + git_patch **out, git_diff *diff, size_t idx); /** * Directly generate a patch from the difference between two blobs. @@ -112,20 +108,17 @@ GIT_EXTERN(int) git_patch_from_blob_and_buffer( /** * Free a git_patch object. */ -GIT_EXTERN(void) git_patch_free( - git_patch *patch); +GIT_EXTERN(void) git_patch_free(git_patch *patch); /** * Get the delta associated with a patch */ -GIT_EXTERN(const git_diff_delta *) git_patch_delta( - git_patch *patch); +GIT_EXTERN(const git_diff_delta *) git_patch_get_delta(git_patch *patch); /** * Get the number of hunks in a patch */ -GIT_EXTERN(size_t) git_patch_num_hunks( - git_patch *patch); +GIT_EXTERN(size_t) git_patch_num_hunks(git_patch *patch); /** * Get line counts of each type in a patch. diff --git a/src/diff.c b/src/diff.c index 1efde98e4..8dc704d58 100644 --- a/src/diff.c +++ b/src/diff.c @@ -81,7 +81,7 @@ static int diff_delta__from_one( if (!git_pathspec__match( &diff->pathspec, entry->path, DIFF_FLAG_IS_SET(diff, GIT_DIFF_DISABLE_PATHSPEC_MATCH), - DIFF_FLAG_IS_SET(diff, GIT_DIFF_DELTAS_ARE_ICASE), + DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_CASE), &matched_pathspec, NULL)) return 0; @@ -368,14 +368,14 @@ static git_diff *diff_list_alloc( * the ignore_case bit set */ if (!git_iterator_ignore_case(old_iter) && !git_iterator_ignore_case(new_iter)) { - diff->opts.flags &= ~GIT_DIFF_DELTAS_ARE_ICASE; + diff->opts.flags &= ~GIT_DIFF_IGNORE_CASE; diff->strcomp = git__strcmp; diff->strncomp = git__strncmp; diff->pfxcomp = git__prefixcmp; diff->entrycomp = git_index_entry__cmp; } else { - diff->opts.flags |= GIT_DIFF_DELTAS_ARE_ICASE; + diff->opts.flags |= GIT_DIFF_IGNORE_CASE; diff->strcomp = git__strcasecmp; diff->strncomp = git__strncasecmp; @@ -399,9 +399,9 @@ static int diff_list_apply_options( if (opts) { /* copy user options (except case sensitivity info from iterators) */ - bool icase = DIFF_FLAG_IS_SET(diff, GIT_DIFF_DELTAS_ARE_ICASE); + bool icase = DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_CASE); memcpy(&diff->opts, opts, sizeof(diff->opts)); - DIFF_FLAG_SET(diff, GIT_DIFF_DELTAS_ARE_ICASE, icase); + DIFF_FLAG_SET(diff, GIT_DIFF_IGNORE_CASE, icase); /* initialize pathspec from options */ if (git_pathspec__vinit(&diff->pathspec, &opts->pathspec, pool) < 0) @@ -413,7 +413,7 @@ static int diff_list_apply_options( diff->opts.flags |= GIT_DIFF_INCLUDE_TYPECHANGE; /* flag INCLUDE_UNTRACKED_CONTENT implies INCLUDE_UNTRACKED */ - if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_UNTRACKED_CONTENT)) + if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_SHOW_UNTRACKED_CONTENT)) diff->opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED; /* load config values that affect diff behavior */ @@ -674,7 +674,7 @@ static int maybe_modified( if (!git_pathspec__match( &diff->pathspec, oitem->path, DIFF_FLAG_IS_SET(diff, GIT_DIFF_DISABLE_PATHSPEC_MATCH), - DIFF_FLAG_IS_SET(diff, GIT_DIFF_DELTAS_ARE_ICASE), + DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_CASE), &matched_pathspec, NULL)) return 0; @@ -910,7 +910,7 @@ static int handle_unmatched_new_item( */ if (!recurse_into_dir && delta_type == GIT_DELTA_UNTRACKED && - DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_FAST_UNTRACKED_DIRS)) + DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_ENABLE_FAST_UNTRACKED_DIRS)) { git_diff_delta *last; @@ -1084,7 +1084,7 @@ int git_diff__from_iterators( git_buf_init(&info.ignore_prefix, 0); /* make iterators have matching icase behavior */ - if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_DELTAS_ARE_ICASE)) { + if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_CASE)) { if ((error = git_iterator_set_ignore_case(old_iter, true)) < 0 || (error = git_iterator_set_ignore_case(new_iter, true)) < 0) goto cleanup; @@ -1164,7 +1164,7 @@ int git_diff_tree_to_tree( * currently case insensitive, unless the user explicitly asked * for case insensitivity */ - if (opts && (opts->flags & GIT_DIFF_DELTAS_ARE_ICASE) != 0) + if (opts && (opts->flags & GIT_DIFF_IGNORE_CASE) != 0) iflag = GIT_ITERATOR_IGNORE_CASE; DIFF_FROM_ITERATORS( @@ -1206,7 +1206,7 @@ int git_diff_tree_to_index( if (!error) { git_diff *d = *diff; - d->opts.flags |= GIT_DIFF_DELTAS_ARE_ICASE; + d->opts.flags |= GIT_DIFF_IGNORE_CASE; d->strcomp = git__strcasecmp; d->strncomp = git__strncasecmp; d->pfxcomp = git__prefixcmp_icase; @@ -1242,7 +1242,6 @@ int git_diff_index_to_workdir( return error; } - int git_diff_tree_to_workdir( git_diff **diff, git_repository *repo, @@ -1262,16 +1261,43 @@ int git_diff_tree_to_workdir( return error; } -size_t git_diff_num_deltas(git_diff *diff) +int git_diff_tree_to_workdir_with_index( + git_diff **diff, + git_repository *repo, + git_tree *old_tree, + const git_diff_options *opts) { - assert(diff); - return (size_t)diff->deltas.length; + int error = 0; + git_diff *d1 = NULL, *d2 = NULL; + + assert(diff && repo); + + if (!(error = git_diff_tree_to_index(&d1, repo, old_tree, NULL, opts)) && + !(error = git_diff_index_to_workdir(&d2, repo, NULL, opts))) + error = git_diff_merge(d1, d2); + + git_diff_free(d2); + + if (error) { + git_diff_free(d1); + d1 = NULL; + } + + *diff = d1; + return error; } -size_t git_diff_num_deltas_of_type(git_diff *diff, git_delta_t type) + +size_t git_diff_num_deltas(const git_diff *diff) +{ + assert(diff); + return diff->deltas.length; +} + +size_t git_diff_num_deltas_of_type(const git_diff *diff, git_delta_t type) { size_t i, count = 0; - git_diff_delta *delta; + const git_diff_delta *delta; assert(diff); @@ -1282,9 +1308,15 @@ size_t git_diff_num_deltas_of_type(git_diff *diff, git_delta_t type) return count; } +const git_diff_delta *git_diff_get_delta(const git_diff *diff, size_t idx) +{ + assert(diff); + return git_vector_get(&diff->deltas, idx); +} + int git_diff_is_sorted_icase(const git_diff *diff) { - return (diff->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) != 0; + return (diff->opts.flags & GIT_DIFF_IGNORE_CASE) != 0; } int git_diff__paired_foreach( @@ -1318,10 +1350,10 @@ int git_diff__paired_foreach( * always sort by the old name in the i2w list. */ h2i_icase = head2idx != NULL && - (head2idx->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) != 0; + (head2idx->opts.flags & GIT_DIFF_IGNORE_CASE) != 0; i2w_icase = idx2wd != NULL && - (idx2wd->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) != 0; + (idx2wd->opts.flags & GIT_DIFF_IGNORE_CASE) != 0; icase_mismatch = (head2idx != NULL && idx2wd != NULL && h2i_icase != i2w_icase); diff --git a/src/diff_file.c b/src/diff_file.c index a7bca4dca..a4c8641bc 100644 --- a/src/diff_file.c +++ b/src/diff_file.c @@ -110,7 +110,7 @@ int git_diff_file_content__init_from_diff( has_data = use_old; break; case GIT_DELTA_UNTRACKED: has_data = !use_old && - (diff->opts.flags & GIT_DIFF_INCLUDE_UNTRACKED_CONTENT) != 0; + (diff->opts.flags & GIT_DIFF_SHOW_UNTRACKED_CONTENT) != 0; break; case GIT_DELTA_MODIFIED: case GIT_DELTA_COPIED: diff --git a/src/diff_patch.c b/src/diff_patch.c index a106944c7..951368200 100644 --- a/src/diff_patch.c +++ b/src/diff_patch.c @@ -17,7 +17,9 @@ typedef struct diff_patch_line diff_patch_line; struct diff_patch_line { const char *ptr; size_t len; - size_t lines, oldno, newno; + size_t lines; + size_t oldno; + size_t newno; char origin; }; @@ -109,9 +111,7 @@ static int diff_patch_init_from_diff( } static int diff_patch_alloc_from_diff( - git_patch **out, - git_diff *diff, - size_t delta_index) + git_patch **out, git_diff *diff, size_t delta_index) { int error; git_patch *patch = git__calloc(1, sizeof(git_patch)); @@ -605,10 +605,7 @@ int git_patch_from_blob_and_buffer( } int git_patch_from_diff( - git_patch **patch_ptr, - const git_diff_delta **delta_ptr, - git_diff *diff, - size_t idx) + git_patch **patch_ptr, git_diff *diff, size_t idx) { int error = 0; git_xdiff_output xo; @@ -616,7 +613,6 @@ int git_patch_from_diff( git_patch *patch = NULL; if (patch_ptr) *patch_ptr = NULL; - if (delta_ptr) *delta_ptr = NULL; if (diff_required(diff, "git_patch_from_diff") < 0) return -1; @@ -627,9 +623,6 @@ int git_patch_from_diff( return GIT_ENOTFOUND; } - if (delta_ptr) - *delta_ptr = delta; - if (git_diff_delta__should_skip(&diff->opts, delta)) return 0; @@ -671,7 +664,7 @@ void git_patch_free(git_patch *patch) GIT_REFCOUNT_DEC(patch, diff_patch_free); } -const git_diff_delta *git_patch_delta(git_patch *patch) +const git_diff_delta *git_patch_get_delta(git_patch *patch) { assert(patch); return patch->delta; diff --git a/src/diff_print.c b/src/diff_print.c index 1cf6da2e3..a6423d92b 100644 --- a/src/diff_print.c +++ b/src/diff_print.c @@ -11,22 +11,34 @@ typedef struct { git_diff *diff; + git_diff_format_t format; git_diff_line_cb print_cb; void *payload; git_buf *buf; + uint32_t flags; int oid_strlen; } diff_print_info; static int diff_print_info_init( diff_print_info *pi, - git_buf *out, git_diff *diff, git_diff_line_cb cb, void *payload) + git_buf *out, + git_diff *diff, + git_diff_format_t format, + git_diff_line_cb cb, + void *payload) { pi->diff = diff; + pi->format = format; pi->print_cb = cb; pi->payload = payload; pi->buf = out; - if (!diff || !diff->repo) + if (diff) + pi->flags = diff->opts.flags; + + if (diff && diff->opts.oid_abbrev != 0) + pi->oid_strlen = diff->opts.oid_abbrev; + else if (!diff || !diff->repo) pi->oid_strlen = GIT_ABBREV_DEFAULT; else if (git_repository__cvar( &pi->oid_strlen, diff->repo, GIT_CVAR_ABBREV) < 0) @@ -77,7 +89,32 @@ static int callback_error(void) return GIT_EUSER; } -static int diff_print_one_compact( +static int diff_print_one_name_only( + const git_diff_delta *delta, float progress, void *data) +{ + diff_print_info *pi = data; + git_buf *out = pi->buf; + + GIT_UNUSED(progress); + + if ((pi->flags & GIT_DIFF_SHOW_UNMODIFIED) == 0 && + delta->status == GIT_DELTA_UNMODIFIED) + return 0; + + git_buf_clear(out); + + if (git_buf_puts(out, delta->new_file.path) < 0 || + git_buf_putc(out, '\n')) + return -1; + + if (pi->print_cb(delta, NULL, GIT_DIFF_LINE_FILE_HDR, + git_buf_cstr(out), git_buf_len(out), pi->payload)) + return callback_error(); + + return 0; +} + +static int diff_print_one_name_status( const git_diff_delta *delta, float progress, void *data) { diff_print_info *pi = data; @@ -88,7 +125,7 @@ static int diff_print_one_compact( GIT_UNUSED(progress); - if (code == ' ') + if ((pi->flags & GIT_DIFF_SHOW_UNMODIFIED) == 0 && code == ' ') return 0; old_suffix = diff_pick_suffix(delta->old_file.mode); @@ -119,24 +156,6 @@ static int diff_print_one_compact( return 0; } -/* print a git_diff to a print callback in compact format */ -int git_diff_print_compact( - git_diff *diff, - git_diff_line_cb print_cb, - void *payload) -{ - int error; - git_buf buf = GIT_BUF_INIT; - diff_print_info pi; - - if (!(error = diff_print_info_init(&pi, &buf, diff, print_cb, payload))) - error = git_diff_foreach(diff, diff_print_one_compact, NULL, NULL, &pi); - - git_buf_free(&buf); - - return error; -} - static int diff_print_one_raw( const git_diff_delta *delta, float progress, void *data) { @@ -147,7 +166,7 @@ static int diff_print_one_raw( GIT_UNUSED(progress); - if (code == ' ') + if ((pi->flags & GIT_DIFF_SHOW_UNMODIFIED) == 0 && code == ' ') return 0; git_buf_clear(out); @@ -180,24 +199,6 @@ static int diff_print_one_raw( return 0; } -/* print a git_diff to a print callback in raw output format */ -int git_diff_print_raw( - git_diff *diff, - git_diff_line_cb print_cb, - void *payload) -{ - int error; - git_buf buf = GIT_BUF_INIT; - diff_print_info pi; - - if (!(error = diff_print_info_init(&pi, &buf, diff, print_cb, payload))) - error = git_diff_foreach(diff, diff_print_one_raw, NULL, NULL, &pi); - - git_buf_free(&buf); - - return error; -} - static int diff_print_oid_range( git_buf *out, const git_diff_delta *delta, int oid_strlen) { @@ -287,7 +288,6 @@ static int diff_print_patch_file( pi->diff ? pi->diff->opts.old_prefix : DIFF_OLD_PREFIX_DEFAULT; const char *newpfx = pi->diff ? pi->diff->opts.new_prefix : DIFF_NEW_PREFIX_DEFAULT; - uint32_t opts_flags = pi->diff ? pi->diff->opts.flags : GIT_DIFF_NORMAL; GIT_UNUSED(progress); @@ -295,7 +295,7 @@ static int diff_print_patch_file( delta->status == GIT_DELTA_UNMODIFIED || delta->status == GIT_DELTA_IGNORED || (delta->status == GIT_DELTA_UNTRACKED && - (opts_flags & GIT_DIFF_INCLUDE_UNTRACKED_CONTENT) == 0)) + (pi->flags & GIT_DIFF_SHOW_UNTRACKED_CONTENT) == 0)) return 0; if (git_diff_delta__format_file_header( @@ -379,20 +379,47 @@ static int diff_print_patch_line( return 0; } -/* print a git_diff to an output callback in patch format */ -int git_diff_print_patch( +/* print a git_diff to an output callback */ +int git_diff_print( git_diff *diff, + git_diff_format_t format, git_diff_line_cb print_cb, void *payload) { int error; git_buf buf = GIT_BUF_INIT; diff_print_info pi; + git_diff_file_cb print_file = NULL; + git_diff_hunk_cb print_hunk = NULL; + git_diff_line_cb print_line = NULL; - if (!(error = diff_print_info_init(&pi, &buf, diff, print_cb, payload))) + switch (format) { + case GIT_DIFF_FORMAT_PATCH: + print_file = diff_print_patch_file; + print_hunk = diff_print_patch_hunk; + print_line = diff_print_patch_line; + break; + case GIT_DIFF_FORMAT_PATCH_HEADER: + print_file = diff_print_patch_file; + break; + case GIT_DIFF_FORMAT_RAW: + print_file = diff_print_one_raw; + break; + case GIT_DIFF_FORMAT_NAME_ONLY: + print_file = diff_print_one_name_only; + break; + case GIT_DIFF_FORMAT_NAME_STATUS: + print_file = diff_print_one_name_status; + break; + default: + giterr_set(GITERR_INVALID, "Unknown diff output format (%d)", format); + return -1; + } + + if (!(error = diff_print_info_init( + &pi, &buf, diff, format, print_cb, payload))) error = git_diff_foreach( - diff, diff_print_patch_file, diff_print_patch_hunk, - diff_print_patch_line, &pi); + diff, print_file, print_hunk, print_line, &pi); git_buf_free(&buf); @@ -412,7 +439,8 @@ int git_patch_print( assert(patch && print_cb); if (!(error = diff_print_info_init( - &pi, &temp, git_patch__diff(patch), print_cb, payload))) + &pi, &temp, git_patch__diff(patch), + GIT_DIFF_FORMAT_PATCH, print_cb, payload))) error = git_patch__invoke_callbacks( patch, diff_print_patch_file, diff_print_patch_hunk, diff_print_patch_line, &pi); diff --git a/src/diff_tform.c b/src/diff_tform.c index c0a60672c..92771847c 100644 --- a/src/diff_tform.c +++ b/src/diff_tform.c @@ -117,15 +117,15 @@ int git_diff_merge( git_pool_init(&onto_pool, 1, 0) < 0) return -1; - if ((onto->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) != 0 || - (from->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) != 0) + if ((onto->opts.flags & GIT_DIFF_IGNORE_CASE) != 0 || + (from->opts.flags & GIT_DIFF_IGNORE_CASE) != 0) { ignore_case = true; /* This function currently only supports merging diff lists that * are sorted identically. */ - assert((onto->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) != 0 && - (from->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) != 0); + assert((onto->opts.flags & GIT_DIFF_IGNORE_CASE) != 0 && + (from->opts.flags & GIT_DIFF_IGNORE_CASE) != 0); } for (i = 0, j = 0; i < onto->deltas.length || j < from->deltas.length; ) { @@ -232,7 +232,7 @@ int git_diff_find_similar__calc_similarity( static int normalize_find_opts( git_diff *diff, git_diff_find_options *opts, - git_diff_find_options *given) + const git_diff_find_options *given) { git_config *cfg = NULL; @@ -760,7 +760,7 @@ typedef struct { int git_diff_find_similar( git_diff *diff, - git_diff_find_options *given_opts) + const git_diff_find_options *given_opts) { size_t s, t; int error = 0, similarity; diff --git a/src/diff_xdiff.c b/src/diff_xdiff.c index e3aa8f3e1..972039753 100644 --- a/src/diff_xdiff.c +++ b/src/diff_xdiff.c @@ -59,7 +59,7 @@ static int git_xdiff_cb(void *priv, mmbuffer_t *bufs, int len) { git_xdiff_info *info = priv; git_patch *patch = info->patch; - const git_diff_delta *delta = git_patch_delta(patch); + const git_diff_delta *delta = git_patch_get_delta(patch); git_diff_output *output = &info->xo->output; if (len == 1) { @@ -145,7 +145,7 @@ static int git_xdiff(git_diff_output *output, git_patch *patch) void git_xdiff_init(git_xdiff_output *xo, const git_diff_options *opts) { - uint32_t flags = opts ? opts->flags : GIT_DIFF_NORMAL; + uint32_t flags = opts ? opts->flags : 0; xo->output.diff_cb = git_xdiff; diff --git a/src/reset.c b/src/reset.c index 3fd4b9165..194bca6f1 100644 --- a/src/reset.c +++ b/src/reset.c @@ -26,8 +26,7 @@ int git_reset_default( git_tree *tree = NULL; git_diff *diff = NULL; git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - size_t i; - git_diff_delta *delta; + size_t i, max_i; git_index_entry entry; int error; git_index *index = NULL; @@ -58,7 +57,9 @@ int git_reset_default( &diff, repo, tree, index, &opts)) < 0) goto cleanup; - git_vector_foreach(&diff->deltas, i, delta) { + for (i = 0, max_i = git_diff_num_deltas(diff); i < max_i; ++i) { + const git_diff_delta *delta = git_diff_get_delta(diff, i); + if ((error = git_index_conflict_remove(index, delta->old_file.path)) < 0) goto cleanup; diff --git a/src/stash.c b/src/stash.c index 6cf26f711..6fa74ac0a 100644 --- a/src/stash.c +++ b/src/stash.c @@ -153,65 +153,61 @@ cleanup: return error; } -struct cb_data { - git_index *index; - - int error; - +struct stash_update_rules { bool include_changed; bool include_untracked; bool include_ignored; }; -static int update_index_cb( - const git_diff_delta *delta, - float progress, - void *payload) +static int stash_update_index_from_diff( + git_index *index, + const git_diff *diff, + struct stash_update_rules *data) { - struct cb_data *data = (struct cb_data *)payload; - const char *add_path = NULL; + int error = 0; + size_t d, max_d = git_diff_num_deltas(diff); - GIT_UNUSED(progress); + for (d = 0; !error && d < max_d; ++d) { + const char *add_path = NULL; + const git_diff_delta *delta = git_diff_get_delta(diff, d); - switch (delta->status) { - case GIT_DELTA_IGNORED: - if (data->include_ignored) - add_path = delta->new_file.path; - break; - - case GIT_DELTA_UNTRACKED: - if (data->include_untracked) - add_path = delta->new_file.path; - break; - - case GIT_DELTA_ADDED: - case GIT_DELTA_MODIFIED: - if (data->include_changed) - add_path = delta->new_file.path; - break; - - case GIT_DELTA_DELETED: - if (!data->include_changed) + switch (delta->status) { + case GIT_DELTA_IGNORED: + if (data->include_ignored) + add_path = delta->new_file.path; break; - if (git_index_find(NULL, data->index, delta->old_file.path) == 0) - data->error = git_index_remove( - data->index, delta->old_file.path, 0); - break; - default: - /* Unimplemented */ - giterr_set( - GITERR_INVALID, - "Cannot update index. Unimplemented status (%d)", - delta->status); - data->error = -1; - break; + case GIT_DELTA_UNTRACKED: + if (data->include_untracked) + add_path = delta->new_file.path; + break; + + case GIT_DELTA_ADDED: + case GIT_DELTA_MODIFIED: + if (data->include_changed) + add_path = delta->new_file.path; + break; + + case GIT_DELTA_DELETED: + if (data->include_changed && + !git_index_find(NULL, index, delta->old_file.path)) + error = git_index_remove(index, delta->old_file.path, 0); + break; + + default: + /* Unimplemented */ + giterr_set( + GITERR_INVALID, + "Cannot update index. Unimplemented status (%d)", + delta->status); + return -1; + } + + if (add_path != NULL) + error = git_index_add_bypath(index, add_path); } - if (add_path != NULL) - data->error = git_index_add_bypath(data->index, add_path); - - return data->error; + return error; } static int build_untracked_tree( @@ -223,13 +219,11 @@ static int build_untracked_tree( git_tree *i_tree = NULL; git_diff *diff = NULL; git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - struct cb_data data = {0}; + struct stash_update_rules data = {0}; int error; git_index_clear(index); - data.index = index; - if (flags & GIT_STASH_INCLUDE_UNTRACKED) { opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED | GIT_DIFF_RECURSE_UNTRACKED_DIRS; @@ -248,13 +242,8 @@ static int build_untracked_tree( &diff, git_index_owner(index), i_tree, &opts)) < 0) goto cleanup; - if ((error = git_diff_foreach( - diff, update_index_cb, NULL, NULL, &data)) < 0) - { - if (error == GIT_EUSER) - error = data.error; + if ((error = stash_update_index_from_diff(index, diff, &data)) < 0) goto cleanup; - } error = build_tree_from_index(tree_out, index); @@ -311,9 +300,9 @@ static int build_workdir_tree( { git_repository *repo = git_index_owner(index); git_tree *b_tree = NULL; - git_diff *diff = NULL, *diff2 = NULL; + git_diff *diff = NULL; git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - struct cb_data data = {0}; + struct stash_update_rules data = {0}; int error; opts.flags = GIT_DIFF_IGNORE_SUBMODULES; @@ -321,33 +310,19 @@ static int build_workdir_tree( if ((error = git_commit_tree(&b_tree, b_commit)) < 0) goto cleanup; - if ((error = git_diff_tree_to_index(&diff, repo, b_tree, NULL, &opts)) < 0) + if ((error = git_diff_tree_to_workdir_with_index( + &diff, repo, b_tree, &opts)) < 0) goto cleanup; - if ((error = git_diff_index_to_workdir(&diff2, repo, NULL, &opts)) < 0) - goto cleanup; - - if ((error = git_diff_merge(diff, diff2)) < 0) - goto cleanup; - - data.index = index; data.include_changed = true; - if ((error = git_diff_foreach( - diff, update_index_cb, NULL, NULL, &data)) < 0) - { - if (error == GIT_EUSER) - error = data.error; + if ((error = stash_update_index_from_diff(index, diff, &data)) < 0) goto cleanup; - } - - if ((error = build_tree_from_index(tree_out, index)) < 0) - goto cleanup; + error = build_tree_from_index(tree_out, index); cleanup: git_diff_free(diff); - git_diff_free(diff2); git_tree_free(b_tree); return error; diff --git a/tests-clar/diff/blob.c b/tests-clar/diff/blob.c index bed0da0bf..898c037b5 100644 --- a/tests-clar/diff/blob.c +++ b/tests-clar/diff/blob.c @@ -165,7 +165,7 @@ void test_diff_blob__can_compare_text_blobs_with_patch(void) cl_assert(p != NULL); - delta = git_patch_delta(p); + delta = git_patch_get_delta(p); cl_assert(delta != NULL); cl_assert_equal_i(GIT_DELTA_MODIFIED, delta->status); cl_assert(git_oid_equal(git_blob_id(a), &delta->old_file.oid)); @@ -188,7 +188,7 @@ void test_diff_blob__can_compare_text_blobs_with_patch(void) cl_assert(p != NULL); - delta = git_patch_delta(p); + delta = git_patch_get_delta(p); cl_assert(delta != NULL); cl_assert_equal_i(GIT_DELTA_MODIFIED, delta->status); cl_assert(git_oid_equal(git_blob_id(b), &delta->old_file.oid)); @@ -211,7 +211,7 @@ void test_diff_blob__can_compare_text_blobs_with_patch(void) cl_assert(p != NULL); - delta = git_patch_delta(p); + delta = git_patch_get_delta(p); cl_assert(delta != NULL); cl_assert_equal_i(GIT_DELTA_MODIFIED, delta->status); cl_assert(git_oid_equal(git_blob_id(a), &delta->old_file.oid)); @@ -231,7 +231,7 @@ void test_diff_blob__can_compare_text_blobs_with_patch(void) cl_assert(p != NULL); - delta = git_patch_delta(p); + delta = git_patch_get_delta(p); cl_assert(delta != NULL); cl_assert_equal_i(GIT_DELTA_MODIFIED, delta->status); cl_assert(git_oid_equal(git_blob_id(c), &delta->old_file.oid)); @@ -326,7 +326,7 @@ void test_diff_blob__can_compare_against_null_blobs_with_patch(void) cl_assert(p != NULL); - delta = git_patch_delta(p); + delta = git_patch_get_delta(p); cl_assert(delta != NULL); cl_assert_equal_i(GIT_DELTA_DELETED, delta->status); cl_assert(git_oid_equal(git_blob_id(d), &delta->old_file.oid)); @@ -351,7 +351,7 @@ void test_diff_blob__can_compare_against_null_blobs_with_patch(void) cl_assert(p != NULL); - delta = git_patch_delta(p); + delta = git_patch_get_delta(p); cl_assert(delta != NULL); cl_assert_equal_i(GIT_DELTA_ADDED, delta->status); cl_assert(git_oid_iszero(&delta->old_file.oid)); @@ -376,7 +376,7 @@ void test_diff_blob__can_compare_against_null_blobs_with_patch(void) cl_assert(p != NULL); - delta = git_patch_delta(p); + delta = git_patch_get_delta(p); cl_assert(delta != NULL); cl_assert_equal_i(GIT_DELTA_DELETED, delta->status); cl_assert((delta->flags & GIT_DIFF_FLAG_BINARY) != 0); @@ -389,7 +389,7 @@ void test_diff_blob__can_compare_against_null_blobs_with_patch(void) cl_assert(p != NULL); - delta = git_patch_delta(p); + delta = git_patch_get_delta(p); cl_assert(delta != NULL); cl_assert_equal_i(GIT_DELTA_ADDED, delta->status); cl_assert((delta->flags & GIT_DIFF_FLAG_BINARY) != 0); @@ -443,7 +443,7 @@ void test_diff_blob__can_compare_identical_blobs_with_patch(void) cl_git_pass(git_patch_from_blobs(&p, d, NULL, d, NULL, &opts)); cl_assert(p != NULL); - delta = git_patch_delta(p); + delta = git_patch_get_delta(p); cl_assert(delta != NULL); cl_assert_equal_i(GIT_DELTA_UNMODIFIED, delta->status); cl_assert_equal_sz(delta->old_file.size, git_blob_rawsize(d)); @@ -457,7 +457,7 @@ void test_diff_blob__can_compare_identical_blobs_with_patch(void) cl_git_pass(git_patch_from_blobs(&p, NULL, NULL, NULL, NULL, &opts)); cl_assert(p != NULL); - delta = git_patch_delta(p); + delta = git_patch_get_delta(p); cl_assert(delta != NULL); cl_assert_equal_i(GIT_DELTA_UNMODIFIED, delta->status); cl_assert_equal_sz(0, delta->old_file.size); @@ -470,7 +470,7 @@ void test_diff_blob__can_compare_identical_blobs_with_patch(void) cl_git_pass(git_patch_from_blobs(&p, alien, NULL, alien, NULL, &opts)); cl_assert(p != NULL); - cl_assert_equal_i(GIT_DELTA_UNMODIFIED, git_patch_delta(p)->status); + cl_assert_equal_i(GIT_DELTA_UNMODIFIED, git_patch_get_delta(p)->status); cl_assert_equal_i(0, (int)git_patch_num_hunks(p)); git_patch_free(p); } @@ -709,7 +709,7 @@ void test_diff_blob__can_compare_blob_to_buffer_with_patch(void) &p, a, NULL, b_content, strlen(b_content), NULL, &opts)); cl_assert(p != NULL); - cl_assert_equal_i(GIT_DELTA_MODIFIED, git_patch_delta(p)->status); + cl_assert_equal_i(GIT_DELTA_MODIFIED, git_patch_get_delta(p)->status); cl_assert_equal_i(1, (int)git_patch_num_hunks(p)); cl_assert_equal_i(6, git_patch_num_lines_in_hunk(p, 0)); @@ -725,7 +725,7 @@ void test_diff_blob__can_compare_blob_to_buffer_with_patch(void) cl_git_pass(git_patch_from_blob_and_buffer( &p, a, NULL, a_content, strlen(a_content), NULL, &opts)); cl_assert(p != NULL); - cl_assert_equal_i(GIT_DELTA_UNMODIFIED, git_patch_delta(p)->status); + cl_assert_equal_i(GIT_DELTA_UNMODIFIED, git_patch_get_delta(p)->status); cl_assert_equal_i(0, (int)git_patch_num_hunks(p)); git_patch_free(p); @@ -733,7 +733,7 @@ void test_diff_blob__can_compare_blob_to_buffer_with_patch(void) cl_git_pass(git_patch_from_blob_and_buffer( &p, NULL, NULL, a_content, strlen(a_content), NULL, &opts)); cl_assert(p != NULL); - cl_assert_equal_i(GIT_DELTA_ADDED, git_patch_delta(p)->status); + cl_assert_equal_i(GIT_DELTA_ADDED, git_patch_get_delta(p)->status); cl_assert_equal_i(1, (int)git_patch_num_hunks(p)); cl_assert_equal_i(1, git_patch_num_lines_in_hunk(p, 0)); git_patch_free(p); @@ -742,7 +742,7 @@ void test_diff_blob__can_compare_blob_to_buffer_with_patch(void) cl_git_pass(git_patch_from_blob_and_buffer( &p, a, NULL, NULL, 0, NULL, &opts)); cl_assert(p != NULL); - cl_assert_equal_i(GIT_DELTA_DELETED, git_patch_delta(p)->status); + cl_assert_equal_i(GIT_DELTA_DELETED, git_patch_get_delta(p)->status); cl_assert_equal_i(1, (int)git_patch_num_hunks(p)); cl_assert_equal_i(1, git_patch_num_lines_in_hunk(p, 0)); git_patch_free(p); @@ -753,7 +753,7 @@ void test_diff_blob__can_compare_blob_to_buffer_with_patch(void) cl_git_pass(git_patch_from_blob_and_buffer( &p, a, NULL, NULL, 0, NULL, &opts)); cl_assert(p != NULL); - cl_assert_equal_i(GIT_DELTA_ADDED, git_patch_delta(p)->status); + cl_assert_equal_i(GIT_DELTA_ADDED, git_patch_get_delta(p)->status); cl_assert_equal_i(1, (int)git_patch_num_hunks(p)); cl_assert_equal_i(1, git_patch_num_lines_in_hunk(p, 0)); git_patch_free(p); diff --git a/tests-clar/diff/diff_helpers.c b/tests-clar/diff/diff_helpers.c index 34ef1df5a..cf768865e 100644 --- a/tests-clar/diff/diff_helpers.c +++ b/tests-clar/diff/diff_helpers.c @@ -162,8 +162,8 @@ int diff_foreach_via_iterator( const git_diff_delta *delta; size_t h, num_h; - cl_git_pass(git_patch_from_diff(&patch, &delta, diff, d)); - cl_assert(delta); + cl_git_pass(git_patch_from_diff(&patch, diff, d)); + cl_assert((delta = git_patch_get_delta(patch)) != NULL); /* call file_cb for this file */ if (file_cb != NULL && file_cb(delta, (float)d / num_d, data) != 0) { @@ -245,10 +245,12 @@ static int diff_print_cb( void diff_print(FILE *fp, git_diff *diff) { - cl_git_pass(git_diff_print_patch(diff, diff_print_cb, fp ? fp : stderr)); + cl_git_pass(git_diff_print( + diff, GIT_DIFF_FORMAT_PATCH, diff_print_cb, fp ? fp : stderr)); } void diff_print_raw(FILE *fp, git_diff *diff) { - cl_git_pass(git_diff_print_raw(diff, diff_print_cb, fp ? fp : stderr)); + cl_git_pass(git_diff_print( + diff, GIT_DIFF_FORMAT_RAW, diff_print_cb, fp ? fp : stderr)); } diff --git a/tests-clar/diff/diffiter.c b/tests-clar/diff/diffiter.c index 48b56e20e..1a1e5dfc3 100644 --- a/tests-clar/diff/diffiter.c +++ b/tests-clar/diff/diffiter.c @@ -20,10 +20,12 @@ void test_diff_diffiter__create(void) num_d = git_diff_num_deltas(diff); for (d = 0; d < num_d; ++d) { - const git_diff_delta *delta; - cl_git_pass(git_patch_from_diff(NULL, &delta, diff, d)); + const git_diff_delta *delta = git_diff_get_delta(diff, d); + cl_assert(delta != NULL); } + cl_assert(!git_diff_get_delta(diff, num_d)); + git_diff_free(diff); } @@ -39,8 +41,7 @@ void test_diff_diffiter__iterate_files_1(void) num_d = git_diff_num_deltas(diff); for (d = 0; d < num_d; ++d) { - const git_diff_delta *delta; - cl_git_pass(git_patch_from_diff(NULL, &delta, diff, d)); + const git_diff_delta *delta = git_diff_get_delta(diff, d); cl_assert(delta != NULL); diff_file_cb(delta, (float)d / (float)num_d, &exp); @@ -63,8 +64,7 @@ void test_diff_diffiter__iterate_files_2(void) cl_assert_equal_i(8, (int)num_d); for (d = 0; d < num_d; ++d) { - const git_diff_delta *delta; - cl_git_pass(git_patch_from_diff(NULL, &delta, diff, d)); + const git_diff_delta *delta = git_diff_get_delta(diff, d); cl_assert(delta != NULL); count++; } @@ -91,12 +91,9 @@ void test_diff_diffiter__iterate_files_and_hunks(void) for (d = 0; d < num_d; ++d) { git_patch *patch; - const git_diff_delta *delta; size_t h, num_h; - cl_git_pass(git_patch_from_diff(&patch, &delta, diff, d)); - - cl_assert(delta); + cl_git_pass(git_patch_from_diff(&patch, diff, d)); cl_assert(patch); file_count++; @@ -145,9 +142,10 @@ void test_diff_diffiter__max_size_threshold(void) git_patch *patch; const git_diff_delta *delta; - cl_git_pass(git_patch_from_diff(&patch, &delta, diff, d)); - cl_assert(delta); + cl_git_pass(git_patch_from_diff(&patch, diff, d)); cl_assert(patch); + delta = git_patch_get_delta(patch); + cl_assert(delta); file_count++; hunk_count += (int)git_patch_num_hunks(patch); @@ -180,7 +178,8 @@ void test_diff_diffiter__max_size_threshold(void) git_patch *patch; const git_diff_delta *delta; - cl_git_pass(git_patch_from_diff(&patch, &delta, diff, d)); + cl_git_pass(git_patch_from_diff(&patch, diff, d)); + delta = git_patch_get_delta(patch); file_count++; hunk_count += (int)git_patch_num_hunks(patch); @@ -221,11 +220,10 @@ void test_diff_diffiter__iterate_all(void) num_d = git_diff_num_deltas(diff); for (d = 0; d < num_d; ++d) { git_patch *patch; - const git_diff_delta *delta; size_t h, num_h; - cl_git_pass(git_patch_from_diff(&patch, &delta, diff, d)); - cl_assert(patch && delta); + cl_git_pass(git_patch_from_diff(&patch, diff, d)); + cl_assert(patch); exp.files++; num_h = git_patch_num_hunks(patch); @@ -312,7 +310,7 @@ void test_diff_diffiter__iterate_randomly_while_saving_state(void) patches[p] = NULL; /* cache new patch */ - cl_git_pass(git_patch_from_diff(&patches[p], NULL, diff, d)); + cl_git_pass(git_patch_from_diff(&patches[p], diff, d)); cl_assert(patches[p] != NULL); /* process old patch if non-NULL */ @@ -428,7 +426,7 @@ void test_diff_diffiter__iterate_and_generate_patch_text(void) git_patch *patch; char *text; - cl_git_pass(git_patch_from_diff(&patch, NULL, diff, d)); + cl_git_pass(git_patch_from_diff(&patch, diff, d)); cl_assert(patch != NULL); cl_git_pass(git_patch_to_str(&text, patch)); diff --git a/tests-clar/diff/drivers.c b/tests-clar/diff/drivers.c index 518f24e7b..fbd1dff81 100644 --- a/tests-clar/diff/drivers.c +++ b/tests-clar/diff/drivers.c @@ -44,7 +44,7 @@ void test_diff_drivers__patterns(void) cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, one, NULL)); cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); - cl_git_pass(git_patch_from_diff(&patch, NULL, diff, 0)); + cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_git_pass(git_patch_to_str(&text, patch)); cl_assert_equal_s(expected0, text); @@ -59,7 +59,7 @@ void test_diff_drivers__patterns(void) cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, one, NULL)); cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); - cl_git_pass(git_patch_from_diff(&patch, NULL, diff, 0)); + cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_git_pass(git_patch_to_str(&text, patch)); cl_assert_equal_s(expected1, text); @@ -74,7 +74,7 @@ void test_diff_drivers__patterns(void) cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, one, NULL)); cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); - cl_git_pass(git_patch_from_diff(&patch, NULL, diff, 0)); + cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_git_pass(git_patch_to_str(&text, patch)); cl_assert_equal_s(expected0, text); @@ -91,7 +91,7 @@ void test_diff_drivers__patterns(void) cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, one, NULL)); cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); - cl_git_pass(git_patch_from_diff(&patch, NULL, diff, 0)); + cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_git_pass(git_patch_to_str(&text, patch)); cl_assert_equal_s(expected1, text); @@ -112,7 +112,7 @@ void test_diff_drivers__patterns(void) cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, one, NULL)); cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); - cl_git_pass(git_patch_from_diff(&patch, NULL, diff, 0)); + cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_git_pass(git_patch_to_str(&text, patch)); cl_assert_equal_s(expected2, text); @@ -144,7 +144,7 @@ void test_diff_drivers__long_lines(void) cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, NULL)); cl_assert_equal_sz(1, git_diff_num_deltas(diff)); - cl_git_pass(git_patch_from_diff(&patch, NULL, diff, 0)); + cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_git_pass(git_patch_to_str(&actual, patch)); /* if chmod not supported, overwrite mode bits since anything is possible */ diff --git a/tests-clar/diff/patch.c b/tests-clar/diff/patch.c index e5d8fca9a..9c01c3b4e 100644 --- a/tests-clar/diff/patch.c +++ b/tests-clar/diff/patch.c @@ -95,7 +95,8 @@ void test_diff_patch__can_properly_display_the_removal_of_a_file(void) cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, one, another, NULL)); - cl_git_pass(git_diff_print_patch(diff, check_removal_cb, NULL)); + cl_git_pass(git_diff_print( + diff, GIT_DIFF_FORMAT_PATCH, check_removal_cb, NULL)); git_diff_free(diff); @@ -122,7 +123,7 @@ void test_diff_patch__to_string(void) cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); - cl_git_pass(git_patch_from_diff(&patch, NULL, diff, 0)); + cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_git_pass(git_patch_to_str(&text, patch)); @@ -167,7 +168,7 @@ void test_diff_patch__config_options(void) cl_git_pass(git_diff_tree_to_index(&diff, g_repo, one, NULL, &opts)); cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); - cl_git_pass(git_patch_from_diff(&patch, NULL, diff, 0)); + cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_git_pass(git_patch_to_str(&text, patch)); cl_assert_equal_s(expected1, text); @@ -178,7 +179,7 @@ void test_diff_patch__config_options(void) cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); - cl_git_pass(git_patch_from_diff(&patch, NULL, diff, 0)); + cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_git_pass(git_patch_to_str(&text, patch)); cl_assert_equal_s(expected2, text); @@ -192,7 +193,7 @@ void test_diff_patch__config_options(void) cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); - cl_git_pass(git_patch_from_diff(&patch, NULL, diff, 0)); + cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_git_pass(git_patch_to_str(&text, patch)); cl_assert_equal_s(expected3, text); @@ -206,7 +207,7 @@ void test_diff_patch__config_options(void) cl_git_pass(git_diff_tree_to_index(&diff, g_repo, one, NULL, &opts)); cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); - cl_git_pass(git_patch_from_diff(&patch, NULL, diff, 0)); + cl_git_pass(git_patch_from_diff(&patch, diff, 0)); cl_git_pass(git_patch_to_str(&text, patch)); cl_assert_equal_s(expected4, text); @@ -253,7 +254,8 @@ void test_diff_patch__hunks_have_correct_line_numbers(void) cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); - cl_git_pass(git_patch_from_diff(&patch, &delta, diff, 0)); + cl_git_pass(git_patch_from_diff(&patch, diff, 0)); + cl_assert((delta = git_patch_get_delta(patch)) != NULL); cl_assert_equal_i(GIT_DELTA_MODIFIED, (int)delta->status); cl_assert_equal_i(2, (int)git_patch_num_hunks(patch)); @@ -346,7 +348,8 @@ void test_diff_patch__hunks_have_correct_line_numbers(void) cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); - cl_git_pass(git_patch_from_diff(&patch, &delta, diff, 0)); + cl_git_pass(git_patch_from_diff(&patch, diff, 0)); + cl_assert((delta = git_patch_get_delta(patch)) != NULL); cl_assert_equal_i(GIT_DELTA_MODIFIED, (int)delta->status); cl_assert_equal_i(1, (int)git_patch_num_hunks(patch)); @@ -427,7 +430,8 @@ static void check_single_patch_stats( cl_assert_equal_i(1, (int)git_diff_num_deltas(diff)); - cl_git_pass(git_patch_from_diff(&patch, &delta, diff, 0)); + cl_git_pass(git_patch_from_diff(&patch, diff, 0)); + cl_assert((delta = git_patch_get_delta(patch)) != NULL); cl_assert_equal_i(GIT_DELTA_MODIFIED, (int)delta->status); cl_assert_equal_i((int)hunks, (int)git_patch_num_hunks(patch)); diff --git a/tests-clar/diff/rename.c b/tests-clar/diff/rename.c index 45706fb47..42bb65aa8 100644 --- a/tests-clar/diff/rename.c +++ b/tests-clar/diff/rename.c @@ -584,7 +584,8 @@ void test_diff_rename__patch(void) cl_assert_equal_i(4, (int)git_diff_num_deltas(diff)); - cl_git_pass(git_patch_from_diff(&patch, &delta, diff, 0)); + cl_git_pass(git_patch_from_diff(&patch, diff, 0)); + cl_assert((delta = git_patch_get_delta(patch)) != NULL); cl_assert_equal_i(GIT_DELTA_COPIED, (int)delta->status); cl_git_pass(git_patch_to_str(&text, patch)); @@ -593,13 +594,13 @@ void test_diff_rename__patch(void) git_patch_free(patch); - cl_git_pass(git_patch_from_diff(NULL, &delta, diff, 1)); + cl_assert((delta = git_diff_get_delta(diff, 1)) != NULL); cl_assert_equal_i(GIT_DELTA_UNMODIFIED, (int)delta->status); - cl_git_pass(git_patch_from_diff(NULL, &delta, diff, 2)); + cl_assert((delta = git_diff_get_delta(diff, 2)) != NULL); cl_assert_equal_i(GIT_DELTA_MODIFIED, (int)delta->status); - cl_git_pass(git_patch_from_diff(NULL, &delta, diff, 3)); + cl_assert((delta = git_diff_get_delta(diff, 3)) != NULL); cl_assert_equal_i(GIT_DELTA_MODIFIED, (int)delta->status); git_diff_free(diff); diff --git a/tests-clar/diff/submodules.c b/tests-clar/diff/submodules.c index 24283e2fa..24545b2c7 100644 --- a/tests-clar/diff/submodules.c +++ b/tests-clar/diff/submodules.c @@ -22,7 +22,8 @@ static void check_diff_patches_at_line( char *patch_text; for (d = 0; d < num_d; ++d, git_patch_free(patch)) { - cl_git_pass(git_patch_from_diff(&patch, &delta, diff, d)); + cl_git_pass(git_patch_from_diff(&patch, diff, d)); + cl_assert((delta = git_patch_get_delta(patch)) != NULL); if (delta->status == GIT_DELTA_UNMODIFIED) { cl_assert_at_line(expected[d] == NULL, file, line); @@ -123,7 +124,7 @@ void test_diff_submodules__dirty_submodule_2(void) cl_git_pass(git_submodule_reload_all(g_repo)); opts.flags = GIT_DIFF_INCLUDE_UNTRACKED | - GIT_DIFF_INCLUDE_UNTRACKED_CONTENT | + GIT_DIFF_SHOW_UNTRACKED_CONTENT | GIT_DIFF_RECURSE_UNTRACKED_DIRS | GIT_DIFF_DISABLE_PATHSPEC_MATCH; opts.old_prefix = "a"; opts.new_prefix = "b"; diff --git a/tests-clar/diff/tree.c b/tests-clar/diff/tree.c index 7286ee121..8231d3632 100644 --- a/tests-clar/diff/tree.c +++ b/tests-clar/diff/tree.c @@ -90,6 +90,9 @@ void test_diff_tree__0(void) git_tree_free(c); } +#define DIFF_OPTS(FLAGS, CTXT) \ + {GIT_DIFF_OPTIONS_VERSION, (FLAGS), 0, {NULL,0}, NULL, NULL, (CTXT), 1} + void test_diff_tree__options(void) { /* grabbed a couple of commit oids from the history of the attr repo */ @@ -102,16 +105,16 @@ void test_diff_tree__options(void) int test_ab_or_cd[] = { 0, 0, 0, 0, 1, 1, 1, 1, 1 }; git_diff_options test_options[] = { /* a vs b tests */ - { 1, GIT_DIFF_NORMAL, 1, 1, NULL, NULL, {0} }, - { 1, GIT_DIFF_NORMAL, 3, 1, NULL, NULL, {0} }, - { 1, GIT_DIFF_REVERSE, 2, 1, NULL, NULL, {0} }, - { 1, GIT_DIFF_FORCE_TEXT, 2, 1, NULL, NULL, {0} }, + DIFF_OPTS(GIT_DIFF_NORMAL, 1), + DIFF_OPTS(GIT_DIFF_NORMAL, 3), + DIFF_OPTS(GIT_DIFF_REVERSE, 2), + DIFF_OPTS(GIT_DIFF_FORCE_TEXT, 2), /* c vs d tests */ - { 1, GIT_DIFF_NORMAL, 3, 1, NULL, NULL, {0} }, - { 1, GIT_DIFF_IGNORE_WHITESPACE, 3, 1, NULL, NULL, {0} }, - { 1, GIT_DIFF_IGNORE_WHITESPACE_CHANGE, 3, 1, NULL, NULL, {0} }, - { 1, GIT_DIFF_IGNORE_WHITESPACE_EOL, 3, 1, NULL, NULL, {0} }, - { 1, GIT_DIFF_IGNORE_WHITESPACE | GIT_DIFF_REVERSE, 1, 1, NULL, NULL, {0} }, + DIFF_OPTS(GIT_DIFF_NORMAL, 3), + DIFF_OPTS(GIT_DIFF_IGNORE_WHITESPACE, 3), + DIFF_OPTS(GIT_DIFF_IGNORE_WHITESPACE_CHANGE, 3), + DIFF_OPTS(GIT_DIFF_IGNORE_WHITESPACE_EOL, 3), + DIFF_OPTS(GIT_DIFF_IGNORE_WHITESPACE | GIT_DIFF_REVERSE, 1), }; /* to generate these values: @@ -255,7 +258,6 @@ void test_diff_tree__larger_hunks(void) const char *a_commit = "d70d245ed97ed2aa596dd1af6536e4bfdb047b69"; const char *b_commit = "7a9e0b02e63179929fed24f0a3e0f19168114d10"; size_t d, num_d, h, num_h, l, num_l, header_len, line_len; - const git_diff_delta *delta; git_patch *patch; const git_diff_hunk *range; const char *header, *line; @@ -273,8 +275,8 @@ void test_diff_tree__larger_hunks(void) num_d = git_diff_num_deltas(diff); for (d = 0; d < num_d; ++d) { - cl_git_pass(git_patch_from_diff(&patch, &delta, diff, d)); - cl_assert(patch && delta); + cl_git_pass(git_patch_from_diff(&patch, diff, d)); + cl_assert(patch); num_h = git_patch_num_hunks(patch); for (h = 0; h < num_h; h++) { @@ -297,7 +299,7 @@ void test_diff_tree__larger_hunks(void) git_patch_free(patch); } - cl_git_fail(git_patch_from_diff(&patch, &delta, diff, num_d)); + cl_git_fail(git_patch_from_diff(&patch, diff, num_d)); cl_assert_equal_i(2, (int)num_d); } diff --git a/tests-clar/diff/workdir.c b/tests-clar/diff/workdir.c index 4af667196..ee63a700d 100644 --- a/tests-clar/diff/workdir.c +++ b/tests-clar/diff/workdir.c @@ -707,7 +707,7 @@ void test_diff_workdir__larger_hunks(void) cl_assert_equal_i(2, (int)num_d); for (d = 0; d < num_d; ++d) { - cl_git_pass(git_patch_from_diff(&patch, NULL, diff, d)); + cl_git_pass(git_patch_from_diff(&patch, diff, d)); cl_assert(patch); num_h = git_patch_num_hunks(patch); @@ -769,7 +769,7 @@ void test_diff_workdir__submodules(void) GIT_DIFF_INCLUDE_UNTRACKED | GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_RECURSE_UNTRACKED_DIRS | - GIT_DIFF_INCLUDE_UNTRACKED_CONTENT; + GIT_DIFF_SHOW_UNTRACKED_CONTENT; cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, a, &opts)); @@ -912,7 +912,7 @@ void test_diff_workdir__can_diff_empty_file(void) cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, tree, &opts)); cl_assert_equal_i(3, (int)git_diff_num_deltas(diff)); /* diffs are: .gitattributes, README.txt, sub/sub/.gitattributes */ - cl_git_pass(git_patch_from_diff(&patch, NULL, diff, 1)); + cl_git_pass(git_patch_from_diff(&patch, diff, 1)); git_patch_free(patch); git_diff_free(diff); @@ -923,7 +923,7 @@ void test_diff_workdir__can_diff_empty_file(void) cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, tree, &opts)); cl_assert_equal_i(3, (int)git_diff_num_deltas(diff)); - cl_git_pass(git_patch_from_diff(&patch, NULL, diff, 1)); + cl_git_pass(git_patch_from_diff(&patch, diff, 1)); git_patch_free(patch); git_diff_free(diff); @@ -1170,7 +1170,7 @@ void test_diff_workdir__untracked_directory_scenarios(void) /* quick version avoids directory scan */ - opts.flags = opts.flags | GIT_DIFF_FAST_UNTRACKED_DIRS; + opts.flags = opts.flags | GIT_DIFF_ENABLE_FAST_UNTRACKED_DIRS; memset(&exp, 0, sizeof(exp)); exp.names = files1; @@ -1190,7 +1190,7 @@ void test_diff_workdir__untracked_directory_scenarios(void) /* directory with nested non-ignored content */ - opts.flags = opts.flags & ~GIT_DIFF_FAST_UNTRACKED_DIRS; + opts.flags = opts.flags & ~GIT_DIFF_ENABLE_FAST_UNTRACKED_DIRS; cl_git_mkfile("status/subdir/directory/more/notignored", "not ignored deep under untracked\n"); @@ -1271,14 +1271,17 @@ void test_diff_workdir__untracked_with_bom(void) "\xFF\xFE\x31\x00\x32\x00\x33\x00\x34\x00", 10, O_WRONLY|O_CREAT, 0664); opts.flags = - GIT_DIFF_INCLUDE_UNTRACKED | GIT_DIFF_INCLUDE_UNTRACKED_CONTENT; + GIT_DIFF_INCLUDE_UNTRACKED | GIT_DIFF_SHOW_UNTRACKED_CONTENT; cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); cl_assert_equal_i(1, git_diff_num_deltas(diff)); - cl_git_pass(git_patch_from_diff(NULL, &delta, diff, 0)); + cl_assert((delta = git_diff_get_delta(diff, 0)) != NULL); cl_assert_equal_i(GIT_DELTA_UNTRACKED, delta->status); - cl_assert((delta->flags & GIT_DIFF_FLAG_BINARY) != 0); + + /* not known at this point + * cl_assert((delta->flags & GIT_DIFF_FLAG_BINARY) != 0); + */ git_diff_free(diff); } From 74a627f04528f7e02f69d8d7947820582ce7ca15 Mon Sep 17 00:00:00 2001 From: Russell Belfer Date: Mon, 21 Oct 2013 09:07:19 -0700 Subject: [PATCH 3/7] Tweak to git_diff_delta structure for nfiles While the base git_diff_delta structure always contains two files, when we introduce conflict data, it will be helpful to have an indicator when an additional file is involved. --- include/git2/diff.h | 5 +++-- src/diff.c | 2 ++ src/diff_tform.c | 47 ++++++++++++++++++++++++++------------------- 3 files changed, 32 insertions(+), 22 deletions(-) diff --git a/include/git2/diff.h b/include/git2/diff.h index fd79ccf04..f1572cbd5 100644 --- a/include/git2/diff.h +++ b/include/git2/diff.h @@ -292,8 +292,9 @@ typedef struct { */ typedef struct { git_delta_t status; - uint32_t similarity; /**< for RENAMED and COPIED, value 0-100 */ - uint32_t flags; + uint32_t flags; /**< git_diff_flag_t values */ + uint16_t similarity; /**< for RENAMED and COPIED, value 0-100 */ + uint16_t nfiles; /**< number of files in this delta */ git_diff_file old_file; git_diff_file new_file; } git_diff_delta; diff --git a/src/diff.c b/src/diff.c index 8dc704d58..37bc737d6 100644 --- a/src/diff.c +++ b/src/diff.c @@ -90,6 +90,7 @@ static int diff_delta__from_one( /* This fn is just for single-sided diffs */ assert(status != GIT_DELTA_MODIFIED); + delta->nfiles = 1; if (delta->status == GIT_DELTA_DELETED) { delta->old_file.mode = entry->mode; @@ -148,6 +149,7 @@ static int diff_delta__from_two( delta = diff_delta__alloc(diff, status, canonical_path); GITERR_CHECK_ALLOC(delta); + delta->nfiles = 2; git_oid_cpy(&delta->old_file.oid, &old_entry->oid); delta->old_file.size = old_entry->file_size; diff --git a/src/diff_tform.c b/src/diff_tform.c index 92771847c..9461ca2c8 100644 --- a/src/diff_tform.c +++ b/src/diff_tform.c @@ -350,6 +350,7 @@ static int apply_splits_and_deletes( goto on_error; deleted->status = GIT_DELTA_DELETED; + deleted->nfiles = 1; memset(&deleted->new_file, 0, sizeof(deleted->new_file)); deleted->new_file.path = deleted->old_file.path; deleted->new_file.flags |= GIT_DIFF_FLAG_VALID_OID; @@ -361,6 +362,7 @@ static int apply_splits_and_deletes( delta->status = GIT_DELTA_UNTRACKED; else delta->status = GIT_DELTA_ADDED; + delta->nfiles = 1; memset(&delta->old_file, 0, sizeof(delta->old_file)); delta->old_file.path = delta->new_file.path; delta->old_file.flags |= GIT_DIFF_FLAG_VALID_OID; @@ -612,7 +614,7 @@ static int calc_self_similarity( return error; if (similarity >= 0) { - delta->similarity = (uint32_t)similarity; + delta->similarity = (uint16_t)similarity; delta->flags |= GIT_DIFF_FLAG__HAS_SELF_SIMILARITY; } @@ -745,17 +747,18 @@ GIT_INLINE(bool) delta_is_new_only(git_diff_delta *delta) } GIT_INLINE(void) delta_make_rename( - git_diff_delta *to, const git_diff_delta *from, uint32_t similarity) + git_diff_delta *to, const git_diff_delta *from, uint16_t similarity) { to->status = GIT_DELTA_RENAMED; to->similarity = similarity; + to->nfiles = 2; memcpy(&to->old_file, &from->old_file, sizeof(to->old_file)); to->flags &= ~GIT_DIFF_FLAG__TO_SPLIT; } typedef struct { - uint32_t idx; - uint32_t similarity; + size_t idx; + uint16_t similarity; } diff_find_match; int git_diff_find_similar( @@ -763,7 +766,8 @@ int git_diff_find_similar( const git_diff_find_options *given_opts) { size_t s, t; - int error = 0, similarity; + int error = 0, result; + uint16_t similarity; git_diff_delta *src, *tgt; git_diff_find_options opts; size_t num_deltas, num_srcs = 0, num_tgts = 0; @@ -839,17 +843,18 @@ find_best_matches: /* calculate similarity for this pair and find best match */ if (s == t) - similarity = -1; /* don't measure self-similarity here */ + result = -1; /* don't measure self-similarity here */ else if ((error = similarity_measure( - &similarity, diff, &opts, sigcache, 2 * s, 2 * t + 1)) < 0) + &result, diff, &opts, sigcache, 2 * s, 2 * t + 1)) < 0) goto cleanup; - if (similarity < 0) + if (result < 0) continue; + similarity = (uint16_t)result; /* is this a better rename? */ - if (tgt2src[t].similarity < (uint32_t)similarity && - src2tgt[s].similarity < (uint32_t)similarity) + if (tgt2src[t].similarity < similarity && + src2tgt[s].similarity < similarity) { /* eject old mapping */ if (src2tgt[s].similarity > 0) { @@ -862,18 +867,18 @@ find_best_matches: } /* write new mapping */ - tgt2src[t].idx = (uint32_t)s; - tgt2src[t].similarity = (uint32_t)similarity; - src2tgt[s].idx = (uint32_t)t; - src2tgt[s].similarity = (uint32_t)similarity; + tgt2src[t].idx = s; + tgt2src[t].similarity = similarity; + src2tgt[s].idx = t; + src2tgt[s].similarity = similarity; } /* keep best absolute match for copies */ if (tgt2src_copy != NULL && - tgt2src_copy[t].similarity < (uint32_t)similarity) + tgt2src_copy[t].similarity < similarity) { - tgt2src_copy[t].idx = (uint32_t)s; - tgt2src_copy[t].similarity = (uint32_t)similarity; + tgt2src_copy[t].idx = s; + tgt2src_copy[t].similarity = similarity; } if (++tried_srcs >= num_srcs) @@ -943,7 +948,7 @@ find_best_matches: delta_make_rename(tgt, src, best_match->similarity); num_rewrites--; - src->status = GIT_DELTA_DELETED; + assert(src->status == GIT_DELTA_DELETED); memcpy(&src->old_file, &swap, sizeof(src->old_file)); memset(&src->new_file, 0, sizeof(src->new_file)); src->new_file.path = src->old_file.path; @@ -953,7 +958,7 @@ find_best_matches: if (src2tgt[t].similarity > 0 && src2tgt[t].idx > t) { /* what used to be at src t is now at src s */ - tgt2src[src2tgt[t].idx].idx = (uint32_t)s; + tgt2src[src2tgt[t].idx].idx = s; } } } @@ -969,6 +974,7 @@ find_best_matches: src->status = (diff->new_src == GIT_ITERATOR_TYPE_WORKDIR) ? GIT_DELTA_UNTRACKED : GIT_DELTA_ADDED; + src->nfiles = 1; memset(&src->old_file, 0, sizeof(src->old_file)); src->old_file.path = src->new_file.path; src->old_file.flags |= GIT_DIFF_FLAG_VALID_OID; @@ -1006,7 +1012,7 @@ find_best_matches: /* otherwise, if we just overwrote a source, update mapping */ else if (src2tgt[t].similarity > 0 && src2tgt[t].idx > t) { /* what used to be at src t is now at src s */ - tgt2src[src2tgt[t].idx].idx = (uint32_t)s; + tgt2src[src2tgt[t].idx].idx = s; } num_updates++; @@ -1026,6 +1032,7 @@ find_best_matches: tgt->status = GIT_DELTA_COPIED; tgt->similarity = best_match->similarity; + tgt->nfiles = 2; memcpy(&tgt->old_file, &src->old_file, sizeof(tgt->old_file)); num_updates++; From 3b5f795446601868d52d09ebac70ae3b7aee157a Mon Sep 17 00:00:00 2001 From: Russell Belfer Date: Mon, 21 Oct 2013 13:42:42 -0700 Subject: [PATCH 4/7] Create git_diff_line and extend git_diff_hunk Instead of having functions with so very many parameters to pass hunk and line data, this takes the existing git_diff_hunk struct and extends it with more hunk data, plus adds a git_diff_line. Those structs are used to pass back hunk and line data instead of the old APIs that took tons of parameters. Some work that was previously only being done for git_diff_patch creation (scanning the diff content for exact line counts) is now done for all callbacks, but the performance difference should not be noticable. --- examples/diff.c | 28 +++-- examples/log.c | 16 ++- include/git2/diff.h | 31 +++-- include/git2/patch.h | 17 +-- src/diff_patch.c | 133 ++++---------------- src/diff_print.c | 93 +++++++------- src/diff_xdiff.c | 81 ++++++++++-- tests-clar/diff/blob.c | 20 +-- tests-clar/diff/diff_helpers.c | 58 ++++----- tests-clar/diff/diff_helpers.h | 10 +- tests-clar/diff/diffiter.c | 28 ++--- tests-clar/diff/patch.c | 223 +++++++++++++++------------------ tests-clar/diff/tree.c | 19 ++- tests-clar/diff/workdir.c | 21 ++-- 14 files changed, 360 insertions(+), 418 deletions(-) diff --git a/examples/diff.c b/examples/diff.c index 2542f4269..694621f1e 100644 --- a/examples/diff.c +++ b/examples/diff.c @@ -45,25 +45,23 @@ char *colors[] = { static int printer( const git_diff_delta *delta, - const git_diff_hunk *range, - char usage, - const char *line, - size_t line_len, + const git_diff_hunk *hunk, + const git_diff_line *line, void *data) { int *last_color = data, color = 0; - (void)delta; (void)range; (void)line_len; + (void)delta; (void)hunk; if (*last_color >= 0) { - switch (usage) { - case GIT_DIFF_LINE_ADDITION: color = 3; break; - case GIT_DIFF_LINE_DELETION: color = 2; break; + switch (line->origin) { + case GIT_DIFF_LINE_ADDITION: color = 3; break; + case GIT_DIFF_LINE_DELETION: color = 2; break; case GIT_DIFF_LINE_ADD_EOFNL: color = 3; break; case GIT_DIFF_LINE_DEL_EOFNL: color = 2; break; - case GIT_DIFF_LINE_FILE_HDR: color = 1; break; - case GIT_DIFF_LINE_HUNK_HDR: color = 4; break; - default: color = 0; + case GIT_DIFF_LINE_FILE_HDR: color = 1; break; + case GIT_DIFF_LINE_HUNK_HDR: color = 4; break; + default: break; } if (color != *last_color) { if (*last_color == 1 || color == 1) @@ -73,7 +71,13 @@ static int printer( } } - fputs(line, stdout); + if (line->origin == GIT_DIFF_LINE_CONTEXT || + line->origin == GIT_DIFF_LINE_ADDITION || + line->origin == GIT_DIFF_LINE_DELETION) + fputc(line->origin, stdout); + + fwrite(line->content, 1, line->content_len, stdout); + return 0; } diff --git a/examples/log.c b/examples/log.c index 30de16ae9..4c2df07c9 100644 --- a/examples/log.c +++ b/examples/log.c @@ -184,14 +184,18 @@ static void print_commit(git_commit *commit) static int print_diff( const git_diff_delta *delta, - const git_diff_hunk *range, - char usage, - const char *line, - size_t line_len, + const git_diff_hunk *hunk, + const git_diff_line *line, void *data) { - (void)delta; (void)range; (void)usage; (void)line_len; (void)data; - fputs(line, stdout); + (void)delta; (void)hunk; (void)data; + + if (line->origin == GIT_DIFF_LINE_CONTEXT || + line->origin == GIT_DIFF_LINE_ADDITION || + line->origin == GIT_DIFF_LINE_DELETION) + fputc(line->origin, stdout); + + fwrite(line->content, 1, line->content_len, stdout); return 0; } diff --git a/include/git2/diff.h b/include/git2/diff.h index f1572cbd5..ed9f71355 100644 --- a/include/git2/diff.h +++ b/include/git2/diff.h @@ -385,10 +385,12 @@ typedef int (*git_diff_file_cb)( */ typedef struct git_diff_hunk git_diff_hunk; struct git_diff_hunk { - int old_start; /** Starting line number in old_file */ - int old_lines; /** Number of lines in old_file */ - int new_start; /** Starting line number in new_file */ - int new_lines; /** Number of lines in new_file */ + int old_start; /** Starting line number in old_file */ + int old_lines; /** Number of lines in old_file */ + int new_start; /** Starting line number in new_file */ + int new_lines; /** Number of lines in new_file */ + size_t header_len; /** Number of bytes in header text */ + char header[128]; /** Header text, NUL-byte terminated */ }; /** @@ -397,8 +399,6 @@ struct git_diff_hunk { typedef int (*git_diff_hunk_cb)( const git_diff_delta *delta, const git_diff_hunk *hunk, - const char *header, - size_t header_len, void *payload); /** @@ -428,6 +428,19 @@ typedef enum { GIT_DIFF_LINE_BINARY = 'B' /**< For "Binary files x and y differ" */ } git_diff_line_t; +/** + * Structure describing a line (or data span) of a diff. + */ +typedef struct git_diff_line git_diff_line; +struct git_diff_line { + char origin; /** A git_diff_line_t value */ + int old_lineno; /** Line number in old file or -1 for added line */ + int new_lineno; /** Line number in new file or -1 for deleted line */ + int num_lines; /** Number of newline characters in content */ + size_t content_len; /** Number of bytes of data */ + const char *content; /** Pointer to diff text, not NUL-byte terminated */ +}; + /** * When iterating over a diff, callback that will be made per text diff * line. In this context, the provided range will be NULL. @@ -438,10 +451,8 @@ typedef enum { */ typedef int (*git_diff_line_cb)( const git_diff_delta *delta, /** delta that contains this data */ - const git_diff_hunk *hunk, /** range of lines containing this data */ - char line_origin, /** git_diff_t value from above */ - const char *content, /** diff data - not NUL terminated */ - size_t content_len, /** number of bytes of diff data */ + const git_diff_hunk *hunk, /** hunk containing this data */ + const git_diff_line *line, /** line data */ void *payload); /** user reference data */ /** diff --git a/include/git2/patch.h b/include/git2/patch.h index 9836245de..6a6ad92d7 100644 --- a/include/git2/patch.h +++ b/include/git2/patch.h @@ -150,9 +150,6 @@ GIT_EXTERN(int) git_patch_line_stats( * as NULL if you don't care about that particular piece of information. * * @param out Output pointer to git_diff_hunk of hunk - * @param header Output pointer to header string for hunk. Unlike the - * content pointer for each line, this will be NUL-terminated - * @param header_len Output value of characters in header string * @param lines_in_hunk Output count of total lines in this hunk * @param patch Input pointer to patch object * @param hunk_idx Input index of hunk to get information about @@ -160,8 +157,6 @@ GIT_EXTERN(int) git_patch_line_stats( */ GIT_EXTERN(int) git_patch_get_hunk( const git_diff_hunk **out, - const char **header, - size_t *header_len, size_t *lines_in_hunk, git_patch *patch, size_t hunk_idx); @@ -185,22 +180,14 @@ GIT_EXTERN(int) git_patch_num_lines_in_hunk( * index larger than the number of hunks or a line index larger than * the number of lines in the hunk, this will return -1. * - * @param line_origin A GIT_DIFF_LINE constant from above - * @param content Pointer to content of diff line, not NUL-terminated - * @param content_len Number of characters in content - * @param old_lineno Line number in old file or -1 if line is added - * @param new_lineno Line number in new file or -1 if line is deleted + * @param out The git_diff_line data for this line * @param patch The patch to look in * @param hunk_idx The index of the hunk * @param line_of_hunk The index of the line in the hunk * @return 0 on success, <0 on failure */ GIT_EXTERN(int) git_patch_get_line_in_hunk( - char *line_origin, - const char **content, - size_t *content_len, - int *old_lineno, - int *new_lineno, + const git_diff_line **out, git_patch *patch, size_t hunk_idx, size_t line_of_hunk); diff --git a/src/diff_patch.c b/src/diff_patch.c index 951368200..cc49d68eb 100644 --- a/src/diff_patch.c +++ b/src/diff_patch.c @@ -12,23 +12,10 @@ #include "diff_xdiff.h" #include "fileops.h" -/* cached information about a single span in a diff */ -typedef struct diff_patch_line diff_patch_line; -struct diff_patch_line { - const char *ptr; - size_t len; - size_t lines; - size_t oldno; - size_t newno; - char origin; -}; - /* cached information about a hunk in a diff */ typedef struct diff_patch_hunk diff_patch_hunk; struct diff_patch_hunk { git_diff_hunk hunk; - char header[128]; - size_t header_len; size_t line_start; size_t line_count; }; @@ -42,8 +29,7 @@ struct git_patch { git_diff_file_content nfile; uint32_t flags; git_array_t(diff_patch_hunk) hunks; - git_array_t(diff_patch_line) lines; - size_t oldno, newno; + git_array_t(git_diff_line) lines; size_t content_size, context_size, header_size; git_pool flattened; }; @@ -57,7 +43,8 @@ enum { GIT_DIFF_PATCH_FLATTENED = (1 << 5), }; -static void diff_output_init(git_diff_output*, const git_diff_options*, +static void diff_output_init( + git_diff_output*, const git_diff_options*, git_diff_file_cb, git_diff_hunk_cb, git_diff_line_cb, void*); static void diff_output_to_patch(git_diff_output *, git_patch *); @@ -81,7 +68,7 @@ static void diff_patch_init_common(git_patch *patch) diff_patch_update_binary(patch); if ((patch->delta->flags & GIT_DIFF_FLAG_BINARY) != 0) - patch->flags |= GIT_DIFF_PATCH_LOADED; /* set LOADED but not DIFFABLE */ + patch->flags |= GIT_DIFF_PATCH_LOADED; /* LOADED but not DIFFABLE */ patch->flags |= GIT_DIFF_PATCH_INITIALIZED; @@ -687,7 +674,7 @@ int git_patch_line_stats( memset(totals, 0, sizeof(totals)); for (idx = 0; idx < git_array_size(patch->lines); ++idx) { - diff_patch_line *line = git_array_get(patch->lines, idx); + git_diff_line *line = git_array_get(patch->lines, idx); if (!line) continue; @@ -721,8 +708,6 @@ static int diff_error_outofrange(const char *thing) int git_patch_get_hunk( const git_diff_hunk **out, - const char **header, - size_t *header_len, size_t *lines_in_hunk, git_patch *patch, size_t hunk_idx) @@ -734,15 +719,11 @@ int git_patch_get_hunk( if (!hunk) { if (out) *out = NULL; - if (header) *header = NULL; - if (header_len) *header_len = 0; if (lines_in_hunk) *lines_in_hunk = 0; return diff_error_outofrange("hunk"); } if (out) *out = &hunk->hunk; - if (header) *header = hunk->header; - if (header_len) *header_len = hunk->header_len; if (lines_in_hunk) *lines_in_hunk = hunk->line_count; return 0; } @@ -758,49 +739,30 @@ int git_patch_num_lines_in_hunk(git_patch *patch, size_t hunk_idx) } int git_patch_get_line_in_hunk( - char *line_origin, - const char **content, - size_t *content_len, - int *old_lineno, - int *new_lineno, + const git_diff_line **out, git_patch *patch, size_t hunk_idx, size_t line_of_hunk) { diff_patch_hunk *hunk; - diff_patch_line *line; - const char *thing; + git_diff_line *line; assert(patch); if (!(hunk = git_array_get(patch->hunks, hunk_idx))) { - thing = "hunk"; - goto notfound; + if (out) *out = NULL; + return diff_error_outofrange("hunk"); } if (line_of_hunk >= hunk->line_count || !(line = git_array_get( patch->lines, hunk->line_start + line_of_hunk))) { - thing = "line"; - goto notfound; + if (out) *out = NULL; + return diff_error_outofrange("line"); } - if (line_origin) *line_origin = line->origin; - if (content) *content = line->ptr; - if (content_len) *content_len = line->len; - if (old_lineno) *old_lineno = (int)line->oldno; - if (new_lineno) *new_lineno = (int)line->newno; - + if (out) *out = line; return 0; - -notfound: - if (line_origin) *line_origin = GIT_DIFF_LINE_CONTEXT; - if (content) *content = NULL; - if (content_len) *content_len = 0; - if (old_lineno) *old_lineno = -1; - if (new_lineno) *new_lineno = -1; - - return diff_error_outofrange(thing); } size_t git_patch_size( @@ -880,18 +842,16 @@ int git_patch__invoke_callbacks( for (i = 0; !error && i < git_array_size(patch->hunks); ++i) { diff_patch_hunk *h = git_array_get(patch->hunks, i); - error = hunk_cb( - patch->delta, &h->hunk, h->header, h->header_len, payload); + error = hunk_cb(patch->delta, &h->hunk, payload); if (!line_cb) continue; for (j = 0; !error && j < h->line_count; ++j) { - diff_patch_line *l = + git_diff_line *l = git_array_get(patch->lines, h->line_start + j); - error = line_cb( - patch->delta, &h->hunk, l->origin, l->ptr, l->len, payload); + error = line_cb(patch->delta, &h->hunk, l, payload); } } @@ -911,8 +871,6 @@ static int diff_patch_file_cb( static int diff_patch_hunk_cb( const git_diff_delta *delta, const git_diff_hunk *hunk_, - const char *header, - size_t header_len, void *payload) { git_patch *patch = payload; @@ -925,34 +883,23 @@ static int diff_patch_hunk_cb( memcpy(&hunk->hunk, hunk_, sizeof(hunk->hunk)); - assert(header_len + 1 < sizeof(hunk->header)); - memcpy(&hunk->header, header, header_len); - hunk->header[header_len] = '\0'; - hunk->header_len = header_len; - - patch->header_size += header_len; + patch->header_size += hunk_->header_len; hunk->line_start = git_array_size(patch->lines); hunk->line_count = 0; - patch->oldno = hunk_->old_start; - patch->newno = hunk_->new_start; - return 0; } static int diff_patch_line_cb( const git_diff_delta *delta, const git_diff_hunk *hunk_, - char line_origin, - const char *content, - size_t content_len, + const git_diff_line *line_, void *payload) { git_patch *patch = payload; diff_patch_hunk *hunk; - diff_patch_line *line; - const char *content_end = content + content_len; + git_diff_line *line; GIT_UNUSED(delta); GIT_UNUSED(hunk_); @@ -963,48 +910,20 @@ static int diff_patch_line_cb( line = git_array_alloc(patch->lines); GITERR_CHECK_ALLOC(line); - line->ptr = content; - line->len = content_len; - line->origin = line_origin; + memcpy(line, line_, sizeof(*line)); /* do some bookkeeping so we can provide old/new line numbers */ - line->lines = 0; - while (content < content_end) - if (*content++ == '\n') - ++line->lines; + patch->content_size += line->content_len; - patch->content_size += content_len; - - switch (line_origin) { - case GIT_DIFF_LINE_ADDITION: + if (line->origin == GIT_DIFF_LINE_ADDITION || + line->origin == GIT_DIFF_LINE_DELETION) patch->content_size += 1; - case GIT_DIFF_LINE_DEL_EOFNL: - line->oldno = -1; - line->newno = patch->newno; - patch->newno += line->lines; - break; - case GIT_DIFF_LINE_DELETION: + else if (line->origin == GIT_DIFF_LINE_CONTEXT) { patch->content_size += 1; - case GIT_DIFF_LINE_ADD_EOFNL: - line->oldno = patch->oldno; - line->newno = -1; - patch->oldno += line->lines; - break; - case GIT_DIFF_LINE_CONTEXT: - patch->content_size += 1; - patch->context_size += 1; - case GIT_DIFF_LINE_CONTEXT_EOFNL: - patch->context_size += content_len; - line->oldno = patch->oldno; - line->newno = patch->newno; - patch->oldno += line->lines; - patch->newno += line->lines; - break; - default: - assert(false); - break; - } + patch->context_size += line->content_len + 1; + } else if (line->origin == GIT_DIFF_LINE_CONTEXT_EOFNL) + patch->context_size += line->content_len; hunk->line_count++; diff --git a/src/diff_print.c b/src/diff_print.c index a6423d92b..b04b11515 100644 --- a/src/diff_print.c +++ b/src/diff_print.c @@ -17,6 +17,7 @@ typedef struct { git_buf *buf; uint32_t flags; int oid_strlen; + git_diff_line line; } diff_print_info; static int diff_print_info_init( @@ -51,6 +52,11 @@ static int diff_print_info_init( else if (pi->oid_strlen > GIT_OID_HEXSZ + 1) pi->oid_strlen = GIT_OID_HEXSZ + 1; + memset(&pi->line, 0, sizeof(pi->line)); + pi->line.old_lineno = -1; + pi->line.new_lineno = -1; + pi->line.num_lines = 1; + return 0; } @@ -107,8 +113,11 @@ static int diff_print_one_name_only( git_buf_putc(out, '\n')) return -1; - if (pi->print_cb(delta, NULL, GIT_DIFF_LINE_FILE_HDR, - git_buf_cstr(out), git_buf_len(out), pi->payload)) + pi->line.origin = GIT_DIFF_LINE_FILE_HDR; + pi->line.content = git_buf_cstr(out); + pi->line.content_len = git_buf_len(out); + + if (pi->print_cb(delta, NULL, &pi->line, pi->payload)) return callback_error(); return 0; @@ -149,8 +158,11 @@ static int diff_print_one_name_status( if (git_buf_oom(out)) return -1; - if (pi->print_cb(delta, NULL, GIT_DIFF_LINE_FILE_HDR, - git_buf_cstr(out), git_buf_len(out), pi->payload)) + pi->line.origin = GIT_DIFF_LINE_FILE_HDR; + pi->line.content = git_buf_cstr(out); + pi->line.content_len = git_buf_len(out); + + if (pi->print_cb(delta, NULL, &pi->line, pi->payload)) return callback_error(); return 0; @@ -192,8 +204,11 @@ static int diff_print_one_raw( if (git_buf_oom(out)) return -1; - if (pi->print_cb(delta, NULL, GIT_DIFF_LINE_FILE_HDR, - git_buf_cstr(out), git_buf_len(out), pi->payload)) + pi->line.origin = GIT_DIFF_LINE_FILE_HDR; + pi->line.content = git_buf_cstr(out); + pi->line.content_len = git_buf_len(out); + + if (pi->print_cb(delta, NULL, &pi->line, pi->payload)) return callback_error(); return 0; @@ -302,8 +317,11 @@ static int diff_print_patch_file( pi->buf, delta, oldpfx, newpfx, pi->oid_strlen) < 0) return -1; - if (pi->print_cb(delta, NULL, GIT_DIFF_LINE_FILE_HDR, - git_buf_cstr(pi->buf), git_buf_len(pi->buf), pi->payload)) + pi->line.origin = GIT_DIFF_LINE_FILE_HDR; + pi->line.content = git_buf_cstr(pi->buf); + pi->line.content_len = git_buf_len(pi->buf); + + if (pi->print_cb(delta, NULL, &pi->line, pi->payload)) return callback_error(); if ((delta->flags & GIT_DIFF_FLAG_BINARY) == 0) @@ -316,8 +334,12 @@ static int diff_print_patch_file( "Binary files %s%s and %s%s differ\n") < 0) return -1; - if (pi->print_cb(delta, NULL, GIT_DIFF_LINE_BINARY, - git_buf_cstr(pi->buf), git_buf_len(pi->buf), pi->payload)) + pi->line.origin = GIT_DIFF_LINE_BINARY; + pi->line.content = git_buf_cstr(pi->buf); + pi->line.content_len = git_buf_len(pi->buf); + pi->line.num_lines = 1; + + if (pi->print_cb(delta, NULL, &pi->line, pi->payload)) return callback_error(); return 0; @@ -325,9 +347,7 @@ static int diff_print_patch_file( static int diff_print_patch_hunk( const git_diff_delta *d, - const git_diff_hunk *r, - const char *header, - size_t header_len, + const git_diff_hunk *h, void *data) { diff_print_info *pi = data; @@ -335,12 +355,11 @@ static int diff_print_patch_hunk( if (S_ISDIR(d->new_file.mode)) return 0; - git_buf_clear(pi->buf); - if (git_buf_put(pi->buf, header, header_len) < 0) - return -1; + pi->line.origin = GIT_DIFF_LINE_HUNK_HDR; + pi->line.content = h->header; + pi->line.content_len = h->header_len; - if (pi->print_cb(d, r, GIT_DIFF_LINE_HUNK_HDR, - git_buf_cstr(pi->buf), git_buf_len(pi->buf), pi->payload)) + if (pi->print_cb(d, h, &pi->line, pi->payload)) return callback_error(); return 0; @@ -348,10 +367,8 @@ static int diff_print_patch_hunk( static int diff_print_patch_line( const git_diff_delta *delta, - const git_diff_hunk *range, - char line_origin, /* GIT_DIFF_LINE value from above */ - const char *content, - size_t content_len, + const git_diff_hunk *hunk, + const git_diff_line *line, void *data) { diff_print_info *pi = data; @@ -359,21 +376,7 @@ static int diff_print_patch_line( if (S_ISDIR(delta->new_file.mode)) return 0; - git_buf_clear(pi->buf); - git_buf_grow(pi->buf, content_len + 2); - - if (line_origin == GIT_DIFF_LINE_ADDITION || - line_origin == GIT_DIFF_LINE_DELETION || - line_origin == GIT_DIFF_LINE_CONTEXT) - git_buf_putc(pi->buf, line_origin); - - git_buf_put(pi->buf, content, content_len); - - if (git_buf_oom(pi->buf)) - return -1; - - if (pi->print_cb(delta, range, line_origin, - git_buf_cstr(pi->buf), git_buf_len(pi->buf), pi->payload)) + if (pi->print_cb(delta, hunk, line, pi->payload)) return callback_error(); return 0; @@ -452,15 +455,19 @@ int git_patch_print( static int diff_print_to_buffer_cb( const git_diff_delta *delta, - const git_diff_hunk *range, - char line_origin, - const char *content, - size_t content_len, + const git_diff_hunk *hunk, + const git_diff_line *line, void *payload) { git_buf *output = payload; - GIT_UNUSED(delta); GIT_UNUSED(range); GIT_UNUSED(line_origin); - return git_buf_put(output, content, content_len); + GIT_UNUSED(delta); GIT_UNUSED(hunk); + + if (line->origin == GIT_DIFF_LINE_ADDITION || + line->origin == GIT_DIFF_LINE_DELETION || + line->origin == GIT_DIFF_LINE_CONTEXT) + git_buf_putc(output, line->origin); + + return git_buf_put(output, line->content, line->content_len); } /* print a git_patch to a string buffer */ diff --git a/src/diff_xdiff.c b/src/diff_xdiff.c index 972039753..a90f8d5d1 100644 --- a/src/diff_xdiff.c +++ b/src/diff_xdiff.c @@ -53,36 +53,94 @@ typedef struct { git_xdiff_output *xo; git_patch *patch; git_diff_hunk hunk; + size_t old_lineno, new_lineno; } git_xdiff_info; +static int diff_update_lines( + git_xdiff_info *info, + git_diff_line *line, + const char *content, + size_t content_len) +{ + const char *scan = content, *scan_end = content + content_len; + + for (line->num_lines = 0; scan < scan_end; ++scan) + if (*scan == '\n') + ++line->num_lines; + + line->content = content; + line->content_len = content_len; + + /* expect " "/"-"/"+", then data */ + switch (line->origin) { + case GIT_DIFF_LINE_ADDITION: + case GIT_DIFF_LINE_DEL_EOFNL: + line->old_lineno = -1; + line->new_lineno = info->new_lineno; + info->new_lineno += line->num_lines; + break; + case GIT_DIFF_LINE_DELETION: + case GIT_DIFF_LINE_ADD_EOFNL: + line->old_lineno = info->old_lineno; + line->new_lineno = -1; + info->old_lineno += line->num_lines; + break; + case GIT_DIFF_LINE_CONTEXT: + case GIT_DIFF_LINE_CONTEXT_EOFNL: + line->old_lineno = info->old_lineno; + line->new_lineno = info->new_lineno; + info->old_lineno += line->num_lines; + info->new_lineno += line->num_lines; + break; + default: + giterr_set(GITERR_INVALID, "Unknown diff line origin %02x", + (unsigned int)line->origin); + return -1; + } + + return 0; +} + static int git_xdiff_cb(void *priv, mmbuffer_t *bufs, int len) { git_xdiff_info *info = priv; git_patch *patch = info->patch; const git_diff_delta *delta = git_patch_get_delta(patch); git_diff_output *output = &info->xo->output; + git_diff_line line; if (len == 1) { output->error = git_xdiff_parse_hunk(&info->hunk, bufs[0].ptr); if (output->error < 0) return output->error; + info->hunk.header_len = bufs[0].size; + if (info->hunk.header_len >= sizeof(info->hunk.header)) + info->hunk.header_len = sizeof(info->hunk.header) - 1; + memcpy(info->hunk.header, bufs[0].ptr, info->hunk.header_len); + info->hunk.header[info->hunk.header_len] = '\0'; + if (output->hunk_cb != NULL && - output->hunk_cb(delta, &info->hunk, - bufs[0].ptr, bufs[0].size, output->payload)) + output->hunk_cb(delta, &info->hunk, output->payload)) output->error = GIT_EUSER; + + info->old_lineno = info->hunk.old_start; + info->new_lineno = info->hunk.new_start; } if (len == 2 || len == 3) { /* expect " "/"-"/"+", then data */ - char origin = + line.origin = (*bufs[0].ptr == '+') ? GIT_DIFF_LINE_ADDITION : (*bufs[0].ptr == '-') ? GIT_DIFF_LINE_DELETION : GIT_DIFF_LINE_CONTEXT; - if (output->data_cb != NULL && - output->data_cb(delta, &info->hunk, - origin, bufs[1].ptr, bufs[1].size, output->payload)) + output->error = diff_update_lines( + info, &line, bufs[1].ptr, bufs[1].size); + + if (!output->error && + output->data_cb != NULL && + output->data_cb(delta, &info->hunk, &line, output->payload)) output->error = GIT_EUSER; } @@ -92,14 +150,17 @@ static int git_xdiff_cb(void *priv, mmbuffer_t *bufs, int len) * If we have a '-' and a third buf, then we have removed a line * with out a newline but added a blank line, so ADD_EOFNL. */ - char origin = + line.origin = (*bufs[0].ptr == '+') ? GIT_DIFF_LINE_DEL_EOFNL : (*bufs[0].ptr == '-') ? GIT_DIFF_LINE_ADD_EOFNL : GIT_DIFF_LINE_CONTEXT_EOFNL; - if (output->data_cb != NULL && - output->data_cb(delta, &info->hunk, - origin, bufs[2].ptr, bufs[2].size, output->payload)) + output->error = diff_update_lines( + info, &line, bufs[2].ptr, bufs[2].size); + + if (!output->error && + output->data_cb != NULL && + output->data_cb(delta, &info->hunk, &line, output->payload)) output->error = GIT_EUSER; } diff --git a/tests-clar/diff/blob.c b/tests-clar/diff/blob.c index 898c037b5..b51bc0f38 100644 --- a/tests-clar/diff/blob.c +++ b/tests-clar/diff/blob.c @@ -319,8 +319,8 @@ void test_diff_blob__can_compare_against_null_blobs_with_patch(void) git_blob *e = NULL; git_patch *p; const git_diff_delta *delta; - int line; - char origin; + const git_diff_line *line; + int l, max_l; cl_git_pass(git_patch_from_blobs(&p, d, NULL, e, NULL, &opts)); @@ -337,10 +337,10 @@ void test_diff_blob__can_compare_against_null_blobs_with_patch(void) cl_assert_equal_i(1, (int)git_patch_num_hunks(p)); cl_assert_equal_i(14, git_patch_num_lines_in_hunk(p, 0)); - for (line = 0; line < git_patch_num_lines_in_hunk(p, 0); ++line) { - cl_git_pass(git_patch_get_line_in_hunk( - &origin, NULL, NULL, NULL, NULL, p, 0, line)); - cl_assert_equal_i(GIT_DIFF_LINE_DELETION, (int)origin); + max_l = git_patch_num_lines_in_hunk(p, 0); + for (l = 0; l < max_l; ++l) { + cl_git_pass(git_patch_get_line_in_hunk(&line, p, 0, l)); + cl_assert_equal_i(GIT_DIFF_LINE_DELETION, (int)line->origin); } git_patch_free(p); @@ -362,10 +362,10 @@ void test_diff_blob__can_compare_against_null_blobs_with_patch(void) cl_assert_equal_i(1, (int)git_patch_num_hunks(p)); cl_assert_equal_i(14, git_patch_num_lines_in_hunk(p, 0)); - for (line = 0; line < git_patch_num_lines_in_hunk(p, 0); ++line) { - cl_git_pass(git_patch_get_line_in_hunk( - &origin, NULL, NULL, NULL, NULL, p, 0, line)); - cl_assert_equal_i(GIT_DIFF_LINE_ADDITION, (int)origin); + max_l = git_patch_num_lines_in_hunk(p, 0); + for (l = 0; l < max_l; ++l) { + cl_git_pass(git_patch_get_line_in_hunk(&line, p, 0, l)); + cl_assert_equal_i(GIT_DIFF_LINE_ADDITION, (int)line->origin); } git_patch_free(p); diff --git a/tests-clar/diff/diff_helpers.c b/tests-clar/diff/diff_helpers.c index cf768865e..466d0ef54 100644 --- a/tests-clar/diff/diff_helpers.c +++ b/tests-clar/diff/diff_helpers.c @@ -95,41 +95,37 @@ int diff_print_file_cb( int diff_hunk_cb( const git_diff_delta *delta, - const git_diff_hunk *range, - const char *header, - size_t header_len, + const git_diff_hunk *hunk, void *payload) { diff_expects *e = payload; + const char *scan = hunk->header, *scan_end = scan + hunk->header_len; GIT_UNUSED(delta); /* confirm no NUL bytes in header text */ - while (header_len--) cl_assert('\0' != *header++); + while (scan < scan_end) + cl_assert('\0' != *scan++); e->hunks++; - e->hunk_old_lines += range->old_lines; - e->hunk_new_lines += range->new_lines; + e->hunk_old_lines += hunk->old_lines; + e->hunk_new_lines += hunk->new_lines; return 0; } int diff_line_cb( const git_diff_delta *delta, - const git_diff_hunk *range, - char line_origin, - const char *content, - size_t content_len, + const git_diff_hunk *hunk, + const git_diff_line *line, void *payload) { diff_expects *e = payload; GIT_UNUSED(delta); - GIT_UNUSED(range); - GIT_UNUSED(content); - GIT_UNUSED(content_len); + GIT_UNUSED(hunk); e->lines++; - switch (line_origin) { + switch (line->origin) { case GIT_DIFF_LINE_CONTEXT: case GIT_DIFF_LINE_CONTEXT_EOFNL: /* techically not a line */ e->line_ctxt++; @@ -186,30 +182,23 @@ int diff_foreach_via_iterator( num_h = git_patch_num_hunks(patch); for (h = 0; h < num_h; h++) { - const git_diff_hunk *range; - const char *hdr; - size_t hdr_len, l, num_l; + const git_diff_hunk *hunk; + size_t l, num_l; - cl_git_pass(git_patch_get_hunk( - &range, &hdr, &hdr_len, &num_l, patch, h)); + cl_git_pass(git_patch_get_hunk(&hunk, &num_l, patch, h)); - if (hunk_cb && hunk_cb(delta, range, hdr, hdr_len, data) != 0) { + if (hunk_cb && hunk_cb(delta, hunk, data) != 0) { git_patch_free(patch); goto abort; } for (l = 0; l < num_l; ++l) { - char origin; - const char *line; - size_t line_len; - int old_lineno, new_lineno; + const git_diff_line *line; - cl_git_pass(git_patch_get_line_in_hunk( - &origin, &line, &line_len, &old_lineno, &new_lineno, - patch, h, l)); + cl_git_pass(git_patch_get_line_in_hunk(&line, patch, h, l)); if (line_cb && - line_cb(delta, range, origin, line, line_len, data) != 0) { + line_cb(delta, hunk, line, data) != 0) { git_patch_free(patch); goto abort; } @@ -228,18 +217,15 @@ abort: static int diff_print_cb( const git_diff_delta *delta, - const git_diff_hunk *range, - char line_origin, /**< GIT_DIFF_LINE_... value from above */ - const char *content, - size_t content_len, + const git_diff_hunk *hunk, + const git_diff_line *line, void *payload) { GIT_UNUSED(payload); GIT_UNUSED(delta); - GIT_UNUSED(range); - GIT_UNUSED(line_origin); - GIT_UNUSED(content_len); - fputs(content, (FILE *)payload); + GIT_UNUSED(hunk); + fprintf((FILE *)payload, "%c%.*s", + line->origin, (int)line->content_len, line->content); return 0; } diff --git a/tests-clar/diff/diff_helpers.h b/tests-clar/diff/diff_helpers.h index e3ad61f29..bf21f4b1f 100644 --- a/tests-clar/diff/diff_helpers.h +++ b/tests-clar/diff/diff_helpers.h @@ -44,17 +44,13 @@ extern int diff_print_file_cb( extern int diff_hunk_cb( const git_diff_delta *delta, - const git_diff_hunk *range, - const char *header, - size_t header_len, + const git_diff_hunk *hunk, void *cb_data); extern int diff_line_cb( const git_diff_delta *delta, - const git_diff_hunk *range, - char line_origin, - const char *content, - size_t content_len, + const git_diff_hunk *hunk, + const git_diff_line *line, void *cb_data); extern int diff_foreach_via_iterator( diff --git a/tests-clar/diff/diffiter.c b/tests-clar/diff/diffiter.c index 1a1e5dfc3..f886e1baa 100644 --- a/tests-clar/diff/diffiter.c +++ b/tests-clar/diff/diffiter.c @@ -101,15 +101,10 @@ void test_diff_diffiter__iterate_files_and_hunks(void) num_h = git_patch_num_hunks(patch); for (h = 0; h < num_h; h++) { - const git_diff_hunk *range; - const char *header; - size_t header_len, num_l; + const git_diff_hunk *hunk; - cl_git_pass(git_patch_get_hunk( - &range, &header, &header_len, &num_l, patch, h)); - - cl_assert(range); - cl_assert(header); + cl_git_pass(git_patch_get_hunk(&hunk, NULL, patch, h)); + cl_assert(hunk); hunk_count++; } @@ -229,22 +224,17 @@ void test_diff_diffiter__iterate_all(void) num_h = git_patch_num_hunks(patch); for (h = 0; h < num_h; h++) { const git_diff_hunk *range; - const char *header; - size_t header_len, l, num_l; + size_t l, num_l; - cl_git_pass(git_patch_get_hunk( - &range, &header, &header_len, &num_l, patch, h)); - cl_assert(range && header); + cl_git_pass(git_patch_get_hunk(&range, &num_l, patch, h)); + cl_assert(range); exp.hunks++; for (l = 0; l < num_l; ++l) { - char origin; - const char *content; - size_t content_len; + const git_diff_line *line; - cl_git_pass(git_patch_get_line_in_hunk( - &origin, &content, &content_len, NULL, NULL, patch, h, l)); - cl_assert(content); + cl_git_pass(git_patch_get_line_in_hunk(&line, patch, h, l)); + cl_assert(line && line->content); exp.lines++; } } diff --git a/tests-clar/diff/patch.c b/tests-clar/diff/patch.c index 9c01c3b4e..366e5da58 100644 --- a/tests-clar/diff/patch.c +++ b/tests-clar/diff/patch.c @@ -26,40 +26,37 @@ void test_diff_patch__cleanup(void) static int check_removal_cb( const git_diff_delta *delta, - const git_diff_hunk *range, - char line_origin, - const char *formatted_output, - size_t output_len, + const git_diff_hunk *hunk, + const git_diff_line *line, void *payload) { GIT_UNUSED(payload); - GIT_UNUSED(output_len); - switch (line_origin) { + switch (line->origin) { case GIT_DIFF_LINE_FILE_HDR: - cl_assert_equal_s(EXPECTED_HEADER, formatted_output); - cl_assert(range == NULL); + cl_assert_equal_s(EXPECTED_HEADER, line->content); + cl_assert(hunk == NULL); goto check_delta; case GIT_DIFF_LINE_HUNK_HDR: - cl_assert_equal_s(EXPECTED_HUNK, formatted_output); + cl_assert_equal_s(EXPECTED_HUNK, line->content); /* Fall through */ case GIT_DIFF_LINE_CONTEXT: case GIT_DIFF_LINE_DELETION: - goto check_range; + goto check_hunk; default: /* unexpected code path */ return -1; } -check_range: - cl_assert(range != NULL); - cl_assert_equal_i(1, range->old_start); - cl_assert_equal_i(2, range->old_lines); - cl_assert_equal_i(0, range->new_start); - cl_assert_equal_i(0, range->new_lines); +check_hunk: + cl_assert(hunk != NULL); + cl_assert_equal_i(1, hunk->old_start); + cl_assert_equal_i(2, hunk->old_lines); + cl_assert_equal_i(0, hunk->new_start); + cl_assert_equal_i(0, hunk->new_lines); check_delta: cl_assert_equal_s("subdir.txt", delta->old_file.path); @@ -227,11 +224,9 @@ void test_diff_patch__hunks_have_correct_line_numbers(void) git_diff *diff; git_patch *patch; const git_diff_delta *delta; - const git_diff_hunk *range; - const char *hdr, *text; - size_t hdrlen, hunklen, textlen; - char origin; - int oldno, newno; + const git_diff_hunk *hunk; + const git_diff_line *line; + size_t hunklen; git_buf old_content = GIT_BUF_INIT, actual = GIT_BUF_INIT; const char *new_content = "The Song of Seven Cities\n------------------------\n\nI WAS Lord of Cities very sumptuously builded.\nSeven roaring Cities paid me tribute from afar.\nIvory their outposts were--the guardrooms of them gilded,\nAnd garrisoned with Amazons invincible in war.\n\nThis is some new text;\nNot as good as the old text;\nBut here it is.\n\nSo they warred and trafficked only yesterday, my Cities.\nTo-day there is no mark or mound of where my Cities stood.\nFor the River rose at midnight and it washed away my Cities.\nThey are evened with Atlantis and the towns before the Flood.\n\nRain on rain-gorged channels raised the water-levels round them,\nFreshet backed on freshet swelled and swept their world from sight,\nTill the emboldened floods linked arms and, flashing forward, drowned them--\nDrowned my Seven Cities and their peoples in one night!\n\nLow among the alders lie their derelict foundations,\nThe beams wherein they trusted and the plinths whereon they built--\nMy rulers and their treasure and their unborn populations,\nDead, destroyed, aborted, and defiled with mud and silt!\n\nAnother replacement;\nBreaking up the poem;\nGenerating some hunks.\n\nTo the sound of trumpets shall their seed restore my Cities\nWealthy and well-weaponed, that once more may I behold\nAll the world go softly when it walks before my Cities,\nAnd the horses and the chariots fleeing from them as of old!\n\n -- Rudyard Kipling\n"; @@ -263,78 +258,71 @@ void test_diff_patch__hunks_have_correct_line_numbers(void) /* check hunk 0 */ cl_git_pass( - git_patch_get_hunk(&range, &hdr, &hdrlen, &hunklen, patch, 0)); + git_patch_get_hunk(&hunk, &hunklen, patch, 0)); cl_assert_equal_i(18, (int)hunklen); - cl_assert_equal_i(6, (int)range->old_start); - cl_assert_equal_i(15, (int)range->old_lines); - cl_assert_equal_i(6, (int)range->new_start); - cl_assert_equal_i(9, (int)range->new_lines); + cl_assert_equal_i(6, (int)hunk->old_start); + cl_assert_equal_i(15, (int)hunk->old_lines); + cl_assert_equal_i(6, (int)hunk->new_start); + cl_assert_equal_i(9, (int)hunk->new_lines); cl_assert_equal_i(18, (int)git_patch_num_lines_in_hunk(patch, 0)); - cl_git_pass(git_patch_get_line_in_hunk( - &origin, &text, &textlen, &oldno, &newno, patch, 0, 0)); - cl_assert_equal_i(GIT_DIFF_LINE_CONTEXT, (int)origin); - cl_git_pass(git_buf_set(&actual, text, textlen)); + cl_git_pass(git_patch_get_line_in_hunk(&line, patch, 0, 0)); + cl_assert_equal_i(GIT_DIFF_LINE_CONTEXT, (int)line->origin); + cl_git_pass(git_buf_set(&actual, line->content, line->content_len)); cl_assert_equal_s("Ivory their outposts were--the guardrooms of them gilded,\n", actual.ptr); - cl_assert_equal_i(6, oldno); - cl_assert_equal_i(6, newno); + cl_assert_equal_i(6, line->old_lineno); + cl_assert_equal_i(6, line->new_lineno); - cl_git_pass(git_patch_get_line_in_hunk( - &origin, &text, &textlen, &oldno, &newno, patch, 0, 3)); - cl_assert_equal_i(GIT_DIFF_LINE_DELETION, (int)origin); - cl_git_pass(git_buf_set(&actual, text, textlen)); + cl_git_pass(git_patch_get_line_in_hunk(&line, patch, 0, 3)); + cl_assert_equal_i(GIT_DIFF_LINE_DELETION, (int)line->origin); + cl_git_pass(git_buf_set(&actual, line->content, line->content_len)); cl_assert_equal_s("All the world went softly when it walked before my Cities--\n", actual.ptr); - cl_assert_equal_i(9, oldno); - cl_assert_equal_i(-1, newno); + cl_assert_equal_i(9, line->old_lineno); + cl_assert_equal_i(-1, line->new_lineno); - cl_git_pass(git_patch_get_line_in_hunk( - &origin, &text, &textlen, &oldno, &newno, patch, 0, 12)); - cl_assert_equal_i(GIT_DIFF_LINE_ADDITION, (int)origin); - cl_git_pass(git_buf_set(&actual, text, textlen)); + cl_git_pass(git_patch_get_line_in_hunk(&line, patch, 0, 12)); + cl_assert_equal_i(GIT_DIFF_LINE_ADDITION, (int)line->origin); + cl_git_pass(git_buf_set(&actual, line->content, line->content_len)); cl_assert_equal_s("This is some new text;\n", actual.ptr); - cl_assert_equal_i(-1, oldno); - cl_assert_equal_i(9, newno); + cl_assert_equal_i(-1, line->old_lineno); + cl_assert_equal_i(9, line->new_lineno); /* check hunk 1 */ - cl_git_pass( - git_patch_get_hunk(&range, &hdr, &hdrlen, &hunklen, patch, 1)); + cl_git_pass(git_patch_get_hunk(&hunk, &hunklen, patch, 1)); cl_assert_equal_i(18, (int)hunklen); - cl_assert_equal_i(31, (int)range->old_start); - cl_assert_equal_i(15, (int)range->old_lines); - cl_assert_equal_i(25, (int)range->new_start); - cl_assert_equal_i(9, (int)range->new_lines); + cl_assert_equal_i(31, (int)hunk->old_start); + cl_assert_equal_i(15, (int)hunk->old_lines); + cl_assert_equal_i(25, (int)hunk->new_start); + cl_assert_equal_i(9, (int)hunk->new_lines); cl_assert_equal_i(18, (int)git_patch_num_lines_in_hunk(patch, 1)); - cl_git_pass(git_patch_get_line_in_hunk( - &origin, &text, &textlen, &oldno, &newno, patch, 1, 0)); - cl_assert_equal_i(GIT_DIFF_LINE_CONTEXT, (int)origin); - cl_git_pass(git_buf_set(&actual, text, textlen)); + cl_git_pass(git_patch_get_line_in_hunk(&line, patch, 1, 0)); + cl_assert_equal_i(GIT_DIFF_LINE_CONTEXT, (int)line->origin); + cl_git_pass(git_buf_set(&actual, line->content, line->content_len)); cl_assert_equal_s("My rulers and their treasure and their unborn populations,\n", actual.ptr); - cl_assert_equal_i(31, oldno); - cl_assert_equal_i(25, newno); + cl_assert_equal_i(31, line->old_lineno); + cl_assert_equal_i(25, line->new_lineno); - cl_git_pass(git_patch_get_line_in_hunk( - &origin, &text, &textlen, &oldno, &newno, patch, 1, 3)); - cl_assert_equal_i(GIT_DIFF_LINE_DELETION, (int)origin); - cl_git_pass(git_buf_set(&actual, text, textlen)); + cl_git_pass(git_patch_get_line_in_hunk(&line, patch, 1, 3)); + cl_assert_equal_i(GIT_DIFF_LINE_DELETION, (int)line->origin); + cl_git_pass(git_buf_set(&actual, line->content, line->content_len)); cl_assert_equal_s("The Daughters of the Palace whom they cherished in my Cities,\n", actual.ptr); - cl_assert_equal_i(34, oldno); - cl_assert_equal_i(-1, newno); + cl_assert_equal_i(34, line->old_lineno); + cl_assert_equal_i(-1, line->new_lineno); - cl_git_pass(git_patch_get_line_in_hunk( - &origin, &text, &textlen, &oldno, &newno, patch, 1, 12)); - cl_assert_equal_i(GIT_DIFF_LINE_ADDITION, (int)origin); - cl_git_pass(git_buf_set(&actual, text, textlen)); + cl_git_pass(git_patch_get_line_in_hunk(&line, patch, 1, 12)); + cl_assert_equal_i(GIT_DIFF_LINE_ADDITION, (int)line->origin); + cl_git_pass(git_buf_set(&actual, line->content, line->content_len)); cl_assert_equal_s("Another replacement;\n", actual.ptr); - cl_assert_equal_i(-1, oldno); - cl_assert_equal_i(28, newno); + cl_assert_equal_i(-1, line->old_lineno); + cl_assert_equal_i(28, line->new_lineno); git_patch_free(patch); git_diff_free(diff); @@ -356,57 +344,51 @@ void test_diff_patch__hunks_have_correct_line_numbers(void) /* check hunk 0 */ - cl_git_pass( - git_patch_get_hunk(&range, &hdr, &hdrlen, &hunklen, patch, 0)); + cl_git_pass(git_patch_get_hunk(&hunk, &hunklen, patch, 0)); cl_assert_equal_i(6, (int)hunklen); - cl_assert_equal_i(46, (int)range->old_start); - cl_assert_equal_i(4, (int)range->old_lines); - cl_assert_equal_i(46, (int)range->new_start); - cl_assert_equal_i(4, (int)range->new_lines); + cl_assert_equal_i(46, (int)hunk->old_start); + cl_assert_equal_i(4, (int)hunk->old_lines); + cl_assert_equal_i(46, (int)hunk->new_start); + cl_assert_equal_i(4, (int)hunk->new_lines); cl_assert_equal_i(6, (int)git_patch_num_lines_in_hunk(patch, 0)); - cl_git_pass(git_patch_get_line_in_hunk( - &origin, &text, &textlen, &oldno, &newno, patch, 0, 1)); - cl_assert_equal_i(GIT_DIFF_LINE_CONTEXT, (int)origin); - cl_git_pass(git_buf_set(&actual, text, textlen)); + cl_git_pass(git_patch_get_line_in_hunk(&line, patch, 0, 1)); + cl_assert_equal_i(GIT_DIFF_LINE_CONTEXT, (int)line->origin); + cl_git_pass(git_buf_set(&actual, line->content, line->content_len)); cl_assert_equal_s("And the horses and the chariots fleeing from them as of old!\n", actual.ptr); - cl_assert_equal_i(47, oldno); - cl_assert_equal_i(47, newno); + cl_assert_equal_i(47, line->old_lineno); + cl_assert_equal_i(47, line->new_lineno); - cl_git_pass(git_patch_get_line_in_hunk( - &origin, &text, &textlen, &oldno, &newno, patch, 0, 2)); - cl_assert_equal_i(GIT_DIFF_LINE_CONTEXT, (int)origin); - cl_git_pass(git_buf_set(&actual, text, textlen)); + cl_git_pass(git_patch_get_line_in_hunk(&line, patch, 0, 2)); + cl_assert_equal_i(GIT_DIFF_LINE_CONTEXT, (int)line->origin); + cl_git_pass(git_buf_set(&actual, line->content, line->content_len)); cl_assert_equal_s("\n", actual.ptr); - cl_assert_equal_i(48, oldno); - cl_assert_equal_i(48, newno); + cl_assert_equal_i(48, line->old_lineno); + cl_assert_equal_i(48, line->new_lineno); - cl_git_pass(git_patch_get_line_in_hunk( - &origin, &text, &textlen, &oldno, &newno, patch, 0, 3)); - cl_assert_equal_i(GIT_DIFF_LINE_DELETION, (int)origin); - cl_git_pass(git_buf_set(&actual, text, textlen)); + cl_git_pass(git_patch_get_line_in_hunk(&line, patch, 0, 3)); + cl_assert_equal_i(GIT_DIFF_LINE_DELETION, (int)line->origin); + cl_git_pass(git_buf_set(&actual, line->content, line->content_len)); cl_assert_equal_s(" -- Rudyard Kipling\n", actual.ptr); - cl_assert_equal_i(49, oldno); - cl_assert_equal_i(-1, newno); + cl_assert_equal_i(49, line->old_lineno); + cl_assert_equal_i(-1, line->new_lineno); - cl_git_pass(git_patch_get_line_in_hunk( - &origin, &text, &textlen, &oldno, &newno, patch, 0, 4)); - cl_assert_equal_i(GIT_DIFF_LINE_ADDITION, (int)origin); - cl_git_pass(git_buf_set(&actual, text, textlen)); + cl_git_pass(git_patch_get_line_in_hunk(&line, patch, 0, 4)); + cl_assert_equal_i(GIT_DIFF_LINE_ADDITION, (int)line->origin); + cl_git_pass(git_buf_set(&actual, line->content, line->content_len)); cl_assert_equal_s(" -- Rudyard Kipling", actual.ptr); - cl_assert_equal_i(-1, oldno); - cl_assert_equal_i(49, newno); + cl_assert_equal_i(-1, line->old_lineno); + cl_assert_equal_i(49, line->new_lineno); - cl_git_pass(git_patch_get_line_in_hunk( - &origin, &text, &textlen, &oldno, &newno, patch, 0, 5)); - cl_assert_equal_i(GIT_DIFF_LINE_DEL_EOFNL, (int)origin); - cl_git_pass(git_buf_set(&actual, text, textlen)); + cl_git_pass(git_patch_get_line_in_hunk(&line, patch, 0, 5)); + cl_assert_equal_i(GIT_DIFF_LINE_DEL_EOFNL, (int)line->origin); + cl_git_pass(git_buf_set(&actual, line->content, line->content_len)); cl_assert_equal_s("\n\\ No newline at end of file\n", actual.ptr); - cl_assert_equal_i(-1, oldno); - cl_assert_equal_i(49, newno); + cl_assert_equal_i(-1, line->old_lineno); + cl_assert_equal_i(49, line->new_lineno); git_patch_free(patch); git_diff_free(diff); @@ -465,31 +447,34 @@ static void check_single_patch_stats( /* walk lines in hunk with basic sanity checks */ for (; hunks > 0; --hunks) { size_t i, max_i; - int lastoldno = -1, oldno, lastnewno = -1, newno; - char origin; + const git_diff_line *line; + int last_new_lineno = -1, last_old_lineno = -1; max_i = git_patch_num_lines_in_hunk(patch, hunks - 1); for (i = 0; i < max_i; ++i) { int expected = 1; - cl_git_pass(git_patch_get_line_in_hunk( - &origin, NULL, NULL, &oldno, &newno, patch, hunks - 1, i)); + cl_git_pass( + git_patch_get_line_in_hunk(&line, patch, hunks - 1, i)); - if (origin == GIT_DIFF_LINE_ADD_EOFNL || - origin == GIT_DIFF_LINE_DEL_EOFNL || - origin == GIT_DIFF_LINE_CONTEXT_EOFNL) + if (line->origin == GIT_DIFF_LINE_ADD_EOFNL || + line->origin == GIT_DIFF_LINE_DEL_EOFNL || + line->origin == GIT_DIFF_LINE_CONTEXT_EOFNL) expected = 0; - if (oldno >= 0) { - if (lastoldno >= 0) - cl_assert_equal_i(expected, oldno - lastoldno); - lastoldno = oldno; + if (line->old_lineno >= 0) { + if (last_old_lineno >= 0) + cl_assert_equal_i( + expected, line->old_lineno - last_old_lineno); + last_old_lineno = line->old_lineno; } - if (newno >= 0) { - if (lastnewno >= 0) - cl_assert_equal_i(expected, newno - lastnewno); - lastnewno = newno; + + if (line->new_lineno >= 0) { + if (last_new_lineno >= 0) + cl_assert_equal_i( + expected, line->new_lineno - last_new_lineno); + last_new_lineno = line->new_lineno; } } } diff --git a/tests-clar/diff/tree.c b/tests-clar/diff/tree.c index 8231d3632..ca2daf5fb 100644 --- a/tests-clar/diff/tree.c +++ b/tests-clar/diff/tree.c @@ -257,11 +257,10 @@ void test_diff_tree__larger_hunks(void) { const char *a_commit = "d70d245ed97ed2aa596dd1af6536e4bfdb047b69"; const char *b_commit = "7a9e0b02e63179929fed24f0a3e0f19168114d10"; - size_t d, num_d, h, num_h, l, num_l, header_len, line_len; + size_t d, num_d, h, num_h, l, num_l; git_patch *patch; - const git_diff_hunk *range; - const char *header, *line; - char origin; + const git_diff_hunk *hunk; + const git_diff_line *line; g_repo = cl_git_sandbox_init("diff"); @@ -280,21 +279,17 @@ void test_diff_tree__larger_hunks(void) num_h = git_patch_num_hunks(patch); for (h = 0; h < num_h; h++) { - cl_git_pass(git_patch_get_hunk( - &range, &header, &header_len, &num_l, patch, h)); + cl_git_pass(git_patch_get_hunk(&hunk, &num_l, patch, h)); for (l = 0; l < num_l; ++l) { - cl_git_pass(git_patch_get_line_in_hunk( - &origin, &line, &line_len, NULL, NULL, patch, h, l)); + cl_git_pass(git_patch_get_line_in_hunk(&line, patch, h, l)); cl_assert(line); } - cl_git_fail(git_patch_get_line_in_hunk( - &origin, &line, &line_len, NULL, NULL, patch, h, num_l)); + cl_git_fail(git_patch_get_line_in_hunk(&line, patch, h, num_l)); } - cl_git_fail(git_patch_get_hunk( - &range, &header, &header_len, &num_l, patch, num_h)); + cl_git_fail(git_patch_get_hunk(&hunk, &num_l, patch, num_h)); git_patch_free(patch); } diff --git a/tests-clar/diff/workdir.c b/tests-clar/diff/workdir.c index ee63a700d..474891c16 100644 --- a/tests-clar/diff/workdir.c +++ b/tests-clar/diff/workdir.c @@ -673,7 +673,7 @@ void test_diff_workdir__larger_hunks(void) const char *b_commit = "7a9e0b02e63179929fed24f0a3e0f19168114d10"; git_tree *a, *b; git_diff_options opts = GIT_DIFF_OPTIONS_INIT; - size_t i, d, num_d, h, num_h, l, num_l, header_len, line_len; + size_t i, d, num_d, h, num_h, l, num_l; g_repo = cl_git_sandbox_init("diff"); @@ -686,9 +686,8 @@ void test_diff_workdir__larger_hunks(void) for (i = 0; i <= 2; ++i) { git_diff *diff = NULL; git_patch *patch; - const git_diff_hunk *range; - const char *header, *line; - char origin; + const git_diff_hunk *hunk; + const git_diff_line *line; /* okay, this is a bit silly, but oh well */ switch (i) { @@ -712,23 +711,21 @@ void test_diff_workdir__larger_hunks(void) num_h = git_patch_num_hunks(patch); for (h = 0; h < num_h; h++) { - cl_git_pass(git_patch_get_hunk( - &range, &header, &header_len, &num_l, patch, h)); + cl_git_pass(git_patch_get_hunk(&hunk, &num_l, patch, h)); for (l = 0; l < num_l; ++l) { - cl_git_pass(git_patch_get_line_in_hunk( - &origin, &line, &line_len, NULL, NULL, patch, h, l)); + cl_git_pass( + git_patch_get_line_in_hunk(&line, patch, h, l)); cl_assert(line); } /* confirm fail after the last item */ - cl_git_fail(git_patch_get_line_in_hunk( - &origin, &line, &line_len, NULL, NULL, patch, h, num_l)); + cl_git_fail( + git_patch_get_line_in_hunk(&line, patch, h, num_l)); } /* confirm fail after the last item */ - cl_git_fail(git_patch_get_hunk( - &range, &header, &header_len, &num_l, patch, num_h)); + cl_git_fail(git_patch_get_hunk(&hunk, &num_l, patch, num_h)); git_patch_free(patch); } From 623460ab139bbb0360f51b26ac27fb6932569278 Mon Sep 17 00:00:00 2001 From: Russell Belfer Date: Mon, 21 Oct 2013 14:16:53 -0700 Subject: [PATCH 5/7] Fix warnings for win64 --- src/diff_xdiff.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/diff_xdiff.c b/src/diff_xdiff.c index a90f8d5d1..d07a33221 100644 --- a/src/diff_xdiff.c +++ b/src/diff_xdiff.c @@ -53,7 +53,7 @@ typedef struct { git_xdiff_output *xo; git_patch *patch; git_diff_hunk hunk; - size_t old_lineno, new_lineno; + int old_lineno, new_lineno; } git_xdiff_info; static int diff_update_lines( @@ -77,20 +77,20 @@ static int diff_update_lines( case GIT_DIFF_LINE_DEL_EOFNL: line->old_lineno = -1; line->new_lineno = info->new_lineno; - info->new_lineno += line->num_lines; + info->new_lineno += (int)line->num_lines; break; case GIT_DIFF_LINE_DELETION: case GIT_DIFF_LINE_ADD_EOFNL: line->old_lineno = info->old_lineno; line->new_lineno = -1; - info->old_lineno += line->num_lines; + info->old_lineno += (int)line->num_lines; break; case GIT_DIFF_LINE_CONTEXT: case GIT_DIFF_LINE_CONTEXT_EOFNL: line->old_lineno = info->old_lineno; line->new_lineno = info->new_lineno; - info->old_lineno += line->num_lines; - info->new_lineno += line->num_lines; + info->old_lineno += (int)line->num_lines; + info->new_lineno += (int)line->num_lines; break; default: giterr_set(GITERR_INVALID, "Unknown diff line origin %02x", From 5de4ec810492178095897f726bfaf879994aaf11 Mon Sep 17 00:00:00 2001 From: Russell Belfer Date: Mon, 21 Oct 2013 15:36:38 -0700 Subject: [PATCH 6/7] Implement patience and minimal diff flags It seems that to implement these options, we just have to pass the appropriate flags through to the libxdiff code taken from core git. So let's do it (and add a test). --- include/git2/diff.h | 5 ++++- src/diff_xdiff.c | 5 +++++ tests-clar/diff/workdir.c | 47 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/include/git2/diff.h b/include/git2/diff.h index ed9f71355..505c3e793 100644 --- a/include/git2/diff.h +++ b/include/git2/diff.h @@ -175,8 +175,11 @@ typedef enum { */ GIT_DIFF_SHOW_UNMODIFIED = (1u << 26), - /** Use the "patience diff" algorithm (currently unimplemented) */ + /** Use the "patience diff" algorithm */ GIT_DIFF_PATIENCE = (1u << 28), + /** Take extra time to find minimal diff */ + GIT_DIFF_MINIMAL = (1 << 29), + } git_diff_option_t; /** diff --git a/src/diff_xdiff.c b/src/diff_xdiff.c index d07a33221..2aca76f87 100644 --- a/src/diff_xdiff.c +++ b/src/diff_xdiff.c @@ -222,6 +222,11 @@ void git_xdiff_init(git_xdiff_output *xo, const git_diff_options *opts) if (flags & GIT_DIFF_IGNORE_WHITESPACE_EOL) xo->params.flags |= XDF_IGNORE_WHITESPACE_AT_EOL; + if (flags & GIT_DIFF_PATIENCE) + xo->params.flags |= XDF_PATIENCE_DIFF; + if (flags & GIT_DIFF_MINIMAL) + xo->params.flags |= XDF_NEED_MINIMAL; + memset(&xo->callback, 0, sizeof(xo->callback)); xo->callback.outf = git_xdiff_cb; } diff --git a/tests-clar/diff/workdir.c b/tests-clar/diff/workdir.c index 474891c16..e72acdb03 100644 --- a/tests-clar/diff/workdir.c +++ b/tests-clar/diff/workdir.c @@ -1282,3 +1282,50 @@ void test_diff_workdir__untracked_with_bom(void) git_diff_free(diff); } + +void test_diff_workdir__patience_diff(void) +{ + git_index *index; + git_diff_options opts = GIT_DIFF_OPTIONS_INIT; + git_diff *diff = NULL; + git_patch *patch = NULL; + char *as_str = NULL; + const char *expected_normal = "diff --git a/test.txt b/test.txt\nindex 34a5acc..d52725f 100644\n--- a/test.txt\n+++ b/test.txt\n@@ -1,10 +1,7 @@\n When I wrote this\n I did not know\n-how to create\n-a patience diff\n I did not know\n how to create\n+a patience diff\n another problem\n-I did not know\n-how to create\n a minimal diff\n"; + const char *expected_patience = "diff --git a/test.txt b/test.txt\nindex 34a5acc..d52725f 100644\n--- a/test.txt\n+++ b/test.txt\n@@ -1,10 +1,7 @@\n When I wrote this\n I did not know\n+I did not know\n how to create\n a patience diff\n-I did not know\n-how to create\n another problem\n-I did not know\n-how to create\n a minimal diff\n"; + + g_repo = cl_git_sandbox_init("empty_standard_repo"); + cl_repo_set_bool(g_repo, "core.autocrlf", true); + cl_git_pass(git_repository_index(&index, g_repo)); + + cl_git_mkfile( + "empty_standard_repo/test.txt", + "When I wrote this\nI did not know\nhow to create\na patience diff\nI did not know\nhow to create\nanother problem\nI did not know\nhow to create\na minimal diff\n"); + cl_git_pass(git_index_add_bypath(index, "test.txt")); + cl_repo_commit_from_index(NULL, g_repo, NULL, 1372350000, "Base"); + + cl_git_rewritefile( + "empty_standard_repo/test.txt", + "When I wrote this\nI did not know\nI did not know\nhow to create\na patience diff\nanother problem\na minimal diff\n"); + + cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); + cl_assert_equal_i(1, git_diff_num_deltas(diff)); + cl_git_pass(git_patch_from_diff(&patch, diff, 0)); + cl_git_pass(git_patch_to_str(&as_str, patch)); + + cl_assert_equal_s(expected_normal, as_str); + git__free(as_str); + git_patch_free(patch); + git_diff_free(diff); + + opts.flags |= GIT_DIFF_PATIENCE; + + cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); + cl_assert_equal_i(1, git_diff_num_deltas(diff)); + cl_git_pass(git_patch_from_diff(&patch, diff, 0)); + cl_git_pass(git_patch_to_str(&as_str, patch)); + + cl_assert_equal_s(expected_patience, as_str); + git__free(as_str); + git_patch_free(patch); + git_diff_free(diff); +} From 7ce60099eec2442cc5428a830b88d51e8f9c1d75 Mon Sep 17 00:00:00 2001 From: Russell Belfer Date: Tue, 22 Oct 2013 11:12:44 -0700 Subject: [PATCH 7/7] Fix typo --- include/git2/diff.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/git2/diff.h b/include/git2/diff.h index 505c3e793..5d360bedb 100644 --- a/include/git2/diff.h +++ b/include/git2/diff.h @@ -853,7 +853,7 @@ typedef enum { * iteration and cause this return `GIT_EUSER`. * * @param diff A git_diff generated by one of the above functions. - * @param format A git_diff_forrmat_t value to pick the text format. + * @param format A git_diff_format_t value to pick the text format. * @param print_cb Callback to make per line of diff text. * @param payload Reference pointer that will be passed to your callback. * @return 0 on success, GIT_EUSER on non-zero callback, or error code