summaryrefslogtreecommitdiff
path: root/src/diff_output.c
diff options
context:
space:
mode:
authorRussell Belfer <rb@github.com>2013-01-07 15:44:22 -0800
committerRussell Belfer <rb@github.com>2013-01-07 15:44:22 -0800
commitf2b7f7a6cb8678763ada70e75d5b38407770e269 (patch)
tree0fca2a3ed081c1aa3e98679468f1ca273d1a3280 /src/diff_output.c
parentf6234cd994ad01fb3aa8c2f0fd8e3d2cf89cf3f2 (diff)
downloadlibgit2-f2b7f7a6cb8678763ada70e75d5b38407770e269.tar.gz
Share git_diff_blobs/git_diff_blob_to_buffer code
This moves the implementation of these two APIs into common code that will be shared between the two. Also, this adds tests for the `git_diff_blob_to_buffer` API. Lastly, this adds some extra `const` to a few places that can use it.
Diffstat (limited to 'src/diff_output.c')
-rw-r--r--src/diff_output.c222
1 files changed, 103 insertions, 119 deletions
diff --git a/src/diff_output.c b/src/diff_output.c
index eeb6f2d16..c586c371d 100644
--- a/src/diff_output.c
+++ b/src/diff_output.c
@@ -132,16 +132,16 @@ static int diff_delta_is_binary_by_attr(
}
static int diff_delta_is_binary_by_content(
- diff_context *ctxt, git_diff_delta *delta, git_diff_file *file, git_map *map)
+ diff_context *ctxt,
+ git_diff_delta *delta,
+ git_diff_file *file,
+ const git_map *map)
{
- git_buf search;
+ const git_buf search = { map->data, 0, min(map->len, 4000) };
GIT_UNUSED(ctxt);
if ((file->flags & KNOWN_BINARY_FLAGS) == 0) {
- search.ptr = map->data;
- search.size = min(map->len, 4000);
-
if (git_buf_text_is_binary(&search))
file->flags |= GIT_DIFF_FILE_BINARY;
else
@@ -1232,7 +1232,7 @@ int git_diff_print_patch(
}
static void set_data_from_blob(
- git_blob *blob, git_map *map, git_diff_file *file)
+ const git_blob *blob, git_map *map, git_diff_file *file)
{
if (blob) {
file->size = git_blob_rawsize(blob);
@@ -1250,97 +1250,95 @@ static void set_data_from_blob(
}
}
-int git_diff_blobs(
- git_blob *old_blob,
- git_blob *new_blob,
- const git_diff_options *options,
+static void set_data_from_buffer(
+ const char *buffer, size_t buffer_len, git_map *map, git_diff_file *file)
+{
+ file->size = (git_off_t)buffer_len;
+ file->mode = 0644;
+
+ if (!buffer)
+ file->flags |= GIT_DIFF_FILE_NO_DATA;
+ else
+ git_odb_hash(&file->oid, buffer, buffer_len, GIT_OBJ_BLOB);
+
+ map->len = buffer_len;
+ map->data = (char *)buffer;
+}
+
+typedef struct {
+ diff_context ctxt;
+ git_diff_delta delta;
+ git_diff_patch patch;
+} diff_single_data;
+
+static int diff_single_init(
+ diff_single_data *data,
+ git_repository *repo,
+ const git_diff_options *opts,
git_diff_file_cb file_cb,
git_diff_hunk_cb hunk_cb,
git_diff_data_cb data_cb,
void *payload)
{
- int error;
- git_repository *repo;
- diff_context ctxt;
- git_diff_delta delta;
- git_diff_patch patch;
-
- GITERR_CHECK_VERSION(options, GIT_DIFF_OPTIONS_VERSION, "git_diff_options");
-
- if (options && (options->flags & GIT_DIFF_REVERSE)) {
- git_blob *swap = old_blob;
- old_blob = new_blob;
- new_blob = swap;
- }
+ GITERR_CHECK_VERSION(opts, GIT_DIFF_OPTIONS_VERSION, "git_diff_options");
- if (new_blob)
- repo = git_object_owner((git_object *)new_blob);
- else if (old_blob)
- repo = git_object_owner((git_object *)old_blob);
- else
- repo = NULL;
+ memset(data, 0, sizeof(*data));
diff_context_init(
- &ctxt, NULL, repo, options,
- file_cb, hunk_cb, data_cb, payload);
+ &data->ctxt, NULL, repo, opts, file_cb, hunk_cb, data_cb, payload);
- diff_patch_init(&ctxt, &patch);
+ diff_patch_init(&data->ctxt, &data->patch);
- /* create a fake delta record and simulate diff_patch_load */
+ return 0;
+}
- memset(&delta, 0, sizeof(delta));
- delta.binary = -1;
+static int diff_single_apply(diff_single_data *data)
+{
+ int error;
+ git_diff_delta *delta = &data->delta;
+ bool has_old = ((delta->old_file.flags & GIT_DIFF_FILE_NO_DATA) == 0);
+ bool has_new = ((delta->new_file.flags & GIT_DIFF_FILE_NO_DATA) == 0);
- set_data_from_blob(old_blob, &patch.old_data, &delta.old_file);
- set_data_from_blob(new_blob, &patch.new_data, &delta.new_file);
+ /* finish setting up fake git_diff_delta record and loaded data */
- delta.status = new_blob ?
- (old_blob ? GIT_DELTA_MODIFIED : GIT_DELTA_ADDED) :
- (old_blob ? GIT_DELTA_DELETED : GIT_DELTA_UNTRACKED);
+ data->patch.delta = delta;
+ delta->binary = -1;
- if (git_oid_cmp(&delta.new_file.oid, &delta.old_file.oid) == 0)
- delta.status = GIT_DELTA_UNMODIFIED;
+ delta->status = has_new ?
+ (has_old ? GIT_DELTA_MODIFIED : GIT_DELTA_ADDED) :
+ (has_old ? GIT_DELTA_DELETED : GIT_DELTA_UNTRACKED);
- patch.delta = &delta;
+ if (git_oid_cmp(&delta->new_file.oid, &delta->old_file.oid) == 0)
+ delta->status = GIT_DELTA_UNMODIFIED;
if ((error = diff_delta_is_binary_by_content(
- &ctxt, &delta, &delta.old_file, &patch.old_data)) < 0 ||
+ &data->ctxt, delta, &delta->old_file, &data->patch.old_data)) < 0 ||
(error = diff_delta_is_binary_by_content(
- &ctxt, &delta, &delta.new_file, &patch.new_data)) < 0)
+ &data->ctxt, delta, &delta->new_file, &data->patch.new_data)) < 0)
goto cleanup;
- patch.flags |= GIT_DIFF_PATCH_LOADED;
- if (delta.binary != 1 && delta.status != GIT_DELTA_UNMODIFIED)
- patch.flags |= GIT_DIFF_PATCH_DIFFABLE;
+ data->patch.flags |= GIT_DIFF_PATCH_LOADED;
+
+ if (delta->binary != 1 && delta->status != GIT_DELTA_UNMODIFIED)
+ data->patch.flags |= GIT_DIFF_PATCH_DIFFABLE;
/* do diffs */
- if (!(error = diff_delta_file_callback(&ctxt, patch.delta, 1)))
- error = diff_patch_generate(&ctxt, &patch);
+ if (!(error = diff_delta_file_callback(&data->ctxt, delta, 1)))
+ error = diff_patch_generate(&data->ctxt, &data->patch);
cleanup:
- diff_patch_unload(&patch);
-
if (error == GIT_EUSER)
giterr_clear();
- return error;
-}
+ diff_patch_unload(&data->patch);
-static void set_data_from_buffer(
- char *buffer, size_t buffer_len, git_map *map, git_diff_file *file)
-{
- file->size = buffer_len;
- file->mode = 0644;
-
- map->len = (size_t)file->size;
- map->data = (char *)buffer;
+ return error;
}
-int git_diff_blob_to_buffer(
- git_blob *old_blob,
- char *buffer,
- size_t buffer_len,
+int git_diff_blobs(
+ const git_blob *old_blob,
+ const git_blob *new_blob,
const git_diff_options *options,
git_diff_file_cb file_cb,
git_diff_hunk_cb hunk_cb,
@@ -1348,71 +1346,57 @@ int git_diff_blob_to_buffer(
void *payload)
{
int error;
- git_repository *repo;
- diff_context ctxt;
- git_diff_delta delta;
- git_diff_patch patch;
+ diff_single_data d;
+ git_repository *repo =
+ new_blob ? git_object_owner((const git_object *)new_blob) :
+ old_blob ? git_object_owner((const git_object *)old_blob) : NULL;
- GITERR_CHECK_VERSION(options, GIT_DIFF_OPTIONS_VERSION, "git_diff_options");
+ if ((error = diff_single_init(
+ &d, repo, options, file_cb, hunk_cb, data_cb, payload)) < 0)
+ return error;
- if (old_blob)
- repo = git_object_owner((git_object *)old_blob);
- else
- repo = NULL;
+ if (options && (options->flags & GIT_DIFF_REVERSE) != 0) {
+ const git_blob *swap = old_blob;
+ old_blob = new_blob;
+ new_blob = swap;
+ }
- diff_context_init(
- &ctxt, NULL, repo, options,
- file_cb, hunk_cb, data_cb, payload);
+ set_data_from_blob(old_blob, &d.patch.old_data, &d.delta.old_file);
+ set_data_from_blob(new_blob, &d.patch.new_data, &d.delta.new_file);
- diff_patch_init(&ctxt, &patch);
+ return diff_single_apply(&d);
+}
- /* create a fake delta record and simulate diff_patch_load */
+int git_diff_blob_to_buffer(
+ const git_blob *old_blob,
+ const char *buf,
+ size_t buflen,
+ const git_diff_options *options,
+ git_diff_file_cb file_cb,
+ git_diff_hunk_cb hunk_cb,
+ git_diff_data_cb data_cb,
+ void *payload)
+{
+ int error;
+ diff_single_data d;
+ git_repository *repo =
+ old_blob ? git_object_owner((const git_object *)old_blob) : NULL;
- memset(&delta, 0, sizeof(delta));
- delta.binary = -1;
+ if ((error = diff_single_init(
+ &d, repo, options, file_cb, hunk_cb, data_cb, payload)) < 0)
+ return error;
- if (options && (options->flags & GIT_DIFF_REVERSE)) {
- set_data_from_blob(old_blob, &patch.new_data, &delta.new_file);
- set_data_from_buffer(buffer, buffer_len, &patch.old_data, &delta.old_file);
+ if (options && (options->flags & GIT_DIFF_REVERSE) != 0) {
+ set_data_from_buffer(buf, buflen, &d.patch.old_data, &d.delta.old_file);
+ set_data_from_blob(old_blob, &d.patch.new_data, &d.delta.new_file);
} else {
- set_data_from_blob(old_blob, &patch.old_data, &delta.old_file);
- set_data_from_buffer(buffer, buffer_len, &patch.new_data, &delta.new_file);
+ set_data_from_blob(old_blob, &d.patch.old_data, &d.delta.old_file);
+ set_data_from_buffer(buf, buflen, &d.patch.new_data, &d.delta.new_file);
}
- delta.status = buffer ?
- (old_blob ? GIT_DELTA_MODIFIED : GIT_DELTA_ADDED) :
- (old_blob ? GIT_DELTA_DELETED : GIT_DELTA_UNTRACKED);
-
- if (git_oid_cmp(&delta.new_file.oid, &delta.old_file.oid) == 0)
- delta.status = GIT_DELTA_UNMODIFIED;
-
- patch.delta = &delta;
-
- if ((error = diff_delta_is_binary_by_content(
- &ctxt, &delta, &delta.old_file, &patch.old_data)) < 0 ||
- (error = diff_delta_is_binary_by_content(
- &ctxt, &delta, &delta.new_file, &patch.new_data)) < 0)
- goto cleanup;
-
- patch.flags |= GIT_DIFF_PATCH_LOADED;
- if (delta.binary != 1 && delta.status != GIT_DELTA_UNMODIFIED)
- patch.flags |= GIT_DIFF_PATCH_DIFFABLE;
-
- /* do diffs */
-
- if (!(error = diff_delta_file_callback(&ctxt, patch.delta, 1)))
- error = diff_patch_generate(&ctxt, &patch);
-
-cleanup:
- diff_patch_unload(&patch);
-
- if (error == GIT_EUSER)
- giterr_clear();
-
- return error;
+ return diff_single_apply(&d);
}
-
size_t git_diff_num_deltas(git_diff_list *diff)
{
assert(diff);