summaryrefslogtreecommitdiff
path: root/tests-clar/diff/diff_helpers.c
diff options
context:
space:
mode:
authorBen Straub <bs@github.com>2013-10-28 11:04:58 -0700
committerBen Straub <bs@github.com>2013-10-28 11:04:58 -0700
commit42c8f8f807fe986534e0cbabbfabc32cb4eb9077 (patch)
treefa76cb66e6d49855be344026b1cbe31d03962916 /tests-clar/diff/diff_helpers.c
parenta7d28f40a2a01382b76c55ca0a0672c177adaf69 (diff)
parent5c50f22a93c78190fb7d81802199ff9defc8cf55 (diff)
downloadlibgit2-42c8f8f807fe986534e0cbabbfabc32cb4eb9077.tar.gz
Merge remote-tracking branch 'libgit2/development' into blame
Diffstat (limited to 'tests-clar/diff/diff_helpers.c')
-rw-r--r--tests-clar/diff/diff_helpers.c133
1 files changed, 77 insertions, 56 deletions
diff --git a/tests-clar/diff/diff_helpers.c b/tests-clar/diff/diff_helpers.c
index a5c322b4d..466d0ef54 100644
--- a/tests-clar/diff/diff_helpers.c
+++ b/tests-clar/diff/diff_helpers.c
@@ -21,6 +21,35 @@ git_tree *resolve_commit_oid_to_tree(
return tree;
}
+static char diff_pick_suffix(int mode)
+{
+ if (S_ISDIR(mode))
+ return '/';
+ else if (GIT_PERMS_IS_EXEC(mode))
+ return '*';
+ else
+ return ' ';
+}
+
+static void fprintf_delta(FILE *fp, const git_diff_delta *delta, float progress)
+{
+ char code = git_diff_status_char(delta->status);
+ char old_suffix = diff_pick_suffix(delta->old_file.mode);
+ char new_suffix = diff_pick_suffix(delta->new_file.mode);
+
+ fprintf(fp, "%c\t%s", code, delta->old_file.path);
+
+ if ((delta->old_file.path != delta->new_file.path &&
+ strcmp(delta->old_file.path, delta->new_file.path) != 0) ||
+ (delta->old_file.mode != delta->new_file.mode &&
+ delta->old_file.mode != 0 && delta->new_file.mode != 0))
+ fprintf(fp, "%c %s%c", old_suffix, delta->new_file.path, new_suffix);
+ else if (old_suffix != ' ')
+ fprintf(fp, "%c", old_suffix);
+
+ fprintf(fp, "\t[%.2f]\n", progress);
+}
+
int diff_file_cb(
const git_diff_delta *delta,
float progress,
@@ -29,9 +58,7 @@ int diff_file_cb(
diff_expects *e = payload;
if (e->debug)
- fprintf(stderr, "%c %s (%.3f)\n",
- git_diff_status_char(delta->status),
- delta->old_file.path, progress);
+ fprintf_delta(stderr, delta, progress);
if (e->names)
cl_assert_equal_s(e->names[e->files], delta->old_file.path);
@@ -55,48 +82,50 @@ int diff_print_file_cb(
float progress,
void *payload)
{
- fprintf(stderr, "%c %s\n",
- git_diff_status_char(delta->status), delta->old_file.path);
+ if (!payload) {
+ fprintf_delta(stderr, delta, progress);
+ return 0;
+ }
+
+ if (!((diff_expects *)payload)->debug)
+ fprintf_delta(stderr, delta, progress);
+
return diff_file_cb(delta, progress, payload);
}
int diff_hunk_cb(
const git_diff_delta *delta,
- const git_diff_range *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_range *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++;
@@ -116,25 +145,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_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) {
- git_diff_patch_free(patch);
+ git_patch_free(patch);
goto abort;
}
@@ -146,44 +175,37 @@ 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 char *hdr;
- size_t hdr_len, l, num_l;
+ const git_diff_hunk *hunk;
+ size_t l, num_l;
- cl_git_pass(git_diff_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) {
- git_diff_patch_free(patch);
+ 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_diff_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) {
- git_diff_patch_free(patch);
+ line_cb(delta, hunk, line, data) != 0) {
+ git_patch_free(patch);
goto abort;
}
}
}
- git_diff_patch_free(patch);
+ git_patch_free(patch);
}
return 0;
@@ -195,27 +217,26 @@ abort:
static int diff_print_cb(
const git_diff_delta *delta,
- const git_diff_range *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;
}
-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));
+ 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_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));
+ cl_git_pass(git_diff_print(
+ diff, GIT_DIFF_FORMAT_RAW, diff_print_cb, fp ? fp : stderr));
}