libgit2/tests-clar/diff/diff_helpers.c
Russell Belfer 145e696b49 Minor fixes, cleanups, and clarifications
There are three actual changes in this commit:

1. When the trailing newline of a file is removed in a diff, the
   change will now be reported with `GIT_DIFF_LINE_DEL_EOFNL` passed
   to the callback.  Previously, the `ADD_EOFNL` constant was given
   which was just an error in my understanding of when the various
   circumstances arose.  `GIT_DIFF_LINE_ADD_EOFNL` is deprecated and
   should never be generated.  A new newline is simply an `ADD`.
2. Rewrote the `diff_delta__merge_like_cgit` function that contains
   the core logic of the `git_diff_merge` implementation.  The new
   version doesn't actually have significantly different behavior,
   but the logic should be much more obvious, I think.
3. Fixed a bug in `git_diff_merge` where it freed a string pool
   while some of the string data was still in use.  This led to
   `git_diff_print_patch` accessing memory that had been freed.

The rest of this commit contains improved documentation in `diff.h`
to make the behavior and the equivalencies with core git clearer,
and a bunch of new tests to cover the various cases, oh and a minor
simplification of `examples/diff.c`.
2012-06-08 12:11:13 -07:00

105 lines
2.1 KiB
C

#include "clar_libgit2.h"
#include "diff_helpers.h"
git_tree *resolve_commit_oid_to_tree(
git_repository *repo,
const char *partial_oid)
{
unsigned int len = (unsigned int)strlen(partial_oid);
git_oid oid;
git_object *obj = NULL;
git_tree *tree = NULL;
if (git_oid_fromstrn(&oid, partial_oid, len) == 0)
git_object_lookup_prefix(&obj, repo, &oid, len, GIT_OBJ_ANY);
cl_assert(obj);
if (git_object_type(obj) == GIT_OBJ_TREE)
return (git_tree *)obj;
cl_assert(git_object_type(obj) == GIT_OBJ_COMMIT);
cl_git_pass(git_commit_tree(&tree, (git_commit *)obj));
git_object_free(obj);
return tree;
}
int diff_file_fn(
void *cb_data,
git_diff_delta *delta,
float progress)
{
diff_expects *e = cb_data;
GIT_UNUSED(progress);
e-> at_least_one_of_them_is_binary = delta->binary;
e->files++;
switch (delta->status) {
case GIT_DELTA_ADDED: e->file_adds++; break;
case GIT_DELTA_DELETED: e->file_dels++; break;
case GIT_DELTA_MODIFIED: e->file_mods++; break;
case GIT_DELTA_IGNORED: e->file_ignored++; break;
case GIT_DELTA_UNTRACKED: e->file_untracked++; break;
case GIT_DELTA_UNMODIFIED: e->file_unmodified++; break;
default: break;
}
return 0;
}
int diff_hunk_fn(
void *cb_data,
git_diff_delta *delta,
git_diff_range *range,
const char *header,
size_t header_len)
{
diff_expects *e = cb_data;
GIT_UNUSED(delta);
GIT_UNUSED(header);
GIT_UNUSED(header_len);
e->hunks++;
e->hunk_old_lines += range->old_lines;
e->hunk_new_lines += range->new_lines;
return 0;
}
int diff_line_fn(
void *cb_data,
git_diff_delta *delta,
git_diff_range *range,
char line_origin,
const char *content,
size_t content_len)
{
diff_expects *e = cb_data;
GIT_UNUSED(delta);
GIT_UNUSED(range);
GIT_UNUSED(content);
GIT_UNUSED(content_len);
e->lines++;
switch (line_origin) {
case GIT_DIFF_LINE_CONTEXT:
e->line_ctxt++;
break;
case GIT_DIFF_LINE_ADDITION:
e->line_adds++;
break;
case GIT_DIFF_LINE_ADD_EOFNL:
assert(0);
break;
case GIT_DIFF_LINE_DELETION:
e->line_dels++;
break;
case GIT_DIFF_LINE_DEL_EOFNL:
/* technically not a line delete, but we'll count it as such */
e->line_dels++;
break;
default:
break;
}
return 0;
}