Commit 6789b7a7 by Russell Belfer

Add buffer to buffer diff and patch APIs

This adds `git_diff_buffers` and `git_patch_from_buffers`.  This
also includes a bunch of internal refactoring to increase the
shared code between these functions and the blob-to-blob and
blob-to-buffer APIs, as well as some higher level assert helpers
in the tests to also remove redundancy.
parent d8839992
...@@ -1013,6 +1013,39 @@ GIT_EXTERN(int) git_diff_blob_to_buffer( ...@@ -1013,6 +1013,39 @@ GIT_EXTERN(int) git_diff_blob_to_buffer(
git_diff_line_cb line_cb, git_diff_line_cb line_cb,
void *payload); void *payload);
/**
* Directly run a diff between two buffers.
*
* Even more than with `git_diff_blobs`, comparing two buffer lacks
* context, so the `git_diff_file` parameters to the callbacks will be
* faked a la the rules for `git_diff_blobs()`.
*
* @param old_buffer Raw data for old side of diff, or NULL for empty
* @param old_len Length of the raw data for old side of the diff
* @param old_as_path Treat old buffer as if it had this filename; can be NULL
* @param new_buffer Raw data for new side of diff, or NULL for empty
* @param new_len Length of raw data for new side of diff
* @param new_as_path Treat buffer as if it had this filename; can be NULL
* @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 line_cb Callback for each line in diff; can be NULL
* @param payload Payload passed to each callback function
* @return 0 on success, non-zero callback return value, or error code
*/
GIT_EXTERN(int) git_diff_buffers(
const void *old_buffer,
size_t old_len,
const char *old_as_path,
const void *new_buffer,
size_t new_len,
const char *new_as_path,
const git_diff_options *options,
git_diff_file_cb file_cb,
git_diff_hunk_cb hunk_cb,
git_diff_line_cb line_cb,
void *payload);
GIT_END_DECL GIT_END_DECL
......
...@@ -106,6 +106,34 @@ GIT_EXTERN(int) git_patch_from_blob_and_buffer( ...@@ -106,6 +106,34 @@ GIT_EXTERN(int) git_patch_from_blob_and_buffer(
const git_diff_options *opts); const git_diff_options *opts);
/** /**
* Directly generate a patch from the difference between two buffers.
*
* This is just like `git_diff_buffers()` 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_buffer Raw data for old side of diff, or NULL for empty
* @param old_len Length of the raw data for old side of the diff
* @param old_as_path Treat old buffer as if it had this filename; can be NULL
* @param new_buffer Raw data for new side of diff, or NULL for empty
* @param new_len Length of raw data for new side of diff
* @param new_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_buffers(
git_patch **out,
const void *old_buffer,
size_t old_len,
const char *old_as_path,
const char *new_buffer,
size_t new_len,
const char *new_as_path,
const git_diff_options *opts);
/**
* Free a git_patch object. * Free a git_patch object.
*/ */
GIT_EXTERN(void) git_patch_free(git_patch *patch); GIT_EXTERN(void) git_patch_free(git_patch *patch);
......
...@@ -127,57 +127,38 @@ int git_diff_file_content__init_from_diff( ...@@ -127,57 +127,38 @@ int git_diff_file_content__init_from_diff(
return diff_file_content_init_common(fc, &diff->opts); return diff_file_content_init_common(fc, &diff->opts);
} }
int git_diff_file_content__init_from_blob( int git_diff_file_content__init_from_src(
git_diff_file_content *fc, git_diff_file_content *fc,
git_repository *repo, git_repository *repo,
const git_diff_options *opts, const git_diff_options *opts,
const git_blob *blob, const git_diff_file_content_src *src,
git_diff_file *as_file) git_diff_file *as_file)
{ {
memset(fc, 0, sizeof(*fc)); memset(fc, 0, sizeof(*fc));
fc->repo = repo; fc->repo = repo;
fc->file = as_file; fc->file = as_file;
fc->blob = blob; fc->blob = src->blob;
if (!blob) { if (!src->blob && !src->buf) {
fc->flags |= GIT_DIFF_FLAG__NO_DATA; fc->flags |= GIT_DIFF_FLAG__NO_DATA;
} else { } else {
fc->flags |= GIT_DIFF_FLAG__LOADED; fc->flags |= GIT_DIFF_FLAG__LOADED;
fc->file->flags |= GIT_DIFF_FLAG_VALID_ID; fc->file->flags |= GIT_DIFF_FLAG_VALID_ID;
fc->file->size = git_blob_rawsize(blob);
fc->file->mode = GIT_FILEMODE_BLOB; fc->file->mode = GIT_FILEMODE_BLOB;
git_oid_cpy(&fc->file->id, git_blob_id(blob));
fc->map.len = (size_t)fc->file->size; if (src->blob) {
fc->map.data = (char *)git_blob_rawcontent(blob); fc->file->size = git_blob_rawsize(src->blob);
} git_oid_cpy(&fc->file->id, git_blob_id(src->blob));
return diff_file_content_init_common(fc, opts);
}
int git_diff_file_content__init_from_raw(
git_diff_file_content *fc,
git_repository *repo,
const git_diff_options *opts,
const char *buf,
size_t buflen,
git_diff_file *as_file)
{
memset(fc, 0, sizeof(*fc));
fc->repo = repo;
fc->file = as_file;
if (!buf) { fc->map.len = (size_t)fc->file->size;
fc->flags |= GIT_DIFF_FLAG__NO_DATA; fc->map.data = (char *)git_blob_rawcontent(src->blob);
} else { } else {
fc->flags |= GIT_DIFF_FLAG__LOADED; fc->file->size = src->buflen;
fc->file->flags |= GIT_DIFF_FLAG_VALID_ID; git_odb_hash(&fc->file->id, src->buf, src->buflen, GIT_OBJ_BLOB);
fc->file->size = buflen;
fc->file->mode = GIT_FILEMODE_BLOB;
git_odb_hash(&fc->file->id, buf, buflen, GIT_OBJ_BLOB);
fc->map.len = buflen; fc->map.len = src->buflen;
fc->map.data = (char *)buf; fc->map.data = (char *)src->buf;
}
} }
return diff_file_content_init_common(fc, opts); return diff_file_content_init_common(fc, opts);
......
...@@ -31,19 +31,21 @@ extern int git_diff_file_content__init_from_diff( ...@@ -31,19 +31,21 @@ extern int git_diff_file_content__init_from_diff(
size_t delta_index, size_t delta_index,
bool use_old); bool use_old);
extern int git_diff_file_content__init_from_blob( typedef struct {
git_diff_file_content *fc, const git_blob *blob;
git_repository *repo, const void *buf;
const git_diff_options *opts, size_t buflen;
const git_blob *blob, const char *as_path;
git_diff_file *as_file); } git_diff_file_content_src;
#define GIT_DIFF_FILE_CONTENT_SRC__BLOB(BLOB,PATH) { (BLOB),NULL,0,(PATH) }
#define GIT_DIFF_FILE_CONTENT_SRC__BUF(BUF,LEN,PATH) { NULL,(BUF),(LEN),(PATH) }
extern int git_diff_file_content__init_from_raw( extern int git_diff_file_content__init_from_src(
git_diff_file_content *fc, git_diff_file_content *fc,
git_repository *repo, git_repository *repo,
const git_diff_options *opts, const git_diff_options *opts,
const char *buf, const git_diff_file_content_src *src,
size_t buflen,
git_diff_file *as_file); git_diff_file *as_file);
/* this loads the blob/file-on-disk as needed */ /* this loads the blob/file-on-disk as needed */
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
* a Linking Exception. For full terms see the included COPYING file. * a Linking Exception. For full terms see the included COPYING file.
*/ */
#include "common.h" #include "common.h"
#include "git2/blob.h"
#include "diff.h" #include "diff.h"
#include "diff_file.h" #include "diff_file.h"
#include "diff_driver.h" #include "diff_driver.h"
...@@ -334,38 +335,45 @@ static int diff_single_generate(diff_patch_with_delta *pd, git_xdiff_output *xo) ...@@ -334,38 +335,45 @@ static int diff_single_generate(diff_patch_with_delta *pd, git_xdiff_output *xo)
return error; return error;
} }
static int diff_patch_from_blobs( static int diff_patch_from_sources(
diff_patch_with_delta *pd, diff_patch_with_delta *pd,
git_xdiff_output *xo, git_xdiff_output *xo,
const git_blob *old_blob, git_diff_file_content_src *oldsrc,
const char *old_path, git_diff_file_content_src *newsrc,
const git_blob *new_blob,
const char *new_path,
const git_diff_options *opts) const git_diff_options *opts)
{ {
int error = 0; int error = 0;
git_repository *repo = git_repository *repo =
new_blob ? git_object_owner((const git_object *)new_blob) : oldsrc->blob ? git_blob_owner(oldsrc->blob) :
old_blob ? git_object_owner((const git_object *)old_blob) : NULL; newsrc->blob ? git_blob_owner(newsrc->blob) : NULL;
git_diff_file *lfile = &pd->delta.old_file, *rfile = &pd->delta.new_file;
git_diff_file_content *ldata = &pd->patch.ofile, *rdata = &pd->patch.nfile;
GITERR_CHECK_VERSION(opts, GIT_DIFF_OPTIONS_VERSION, "git_diff_options"); GITERR_CHECK_VERSION(opts, GIT_DIFF_OPTIONS_VERSION, "git_diff_options");
if (opts && (opts->flags & GIT_DIFF_REVERSE) != 0) { if (opts && (opts->flags & GIT_DIFF_REVERSE) != 0) {
const git_blob *tmp_blob; void *tmp = lfile; lfile = rfile; rfile = tmp;
const char *tmp_path; tmp = ldata; ldata = rdata; rdata = tmp;
tmp_blob = old_blob; old_blob = new_blob; new_blob = tmp_blob;
tmp_path = old_path; old_path = new_path; new_path = tmp_path;
} }
pd->patch.delta = &pd->delta; pd->patch.delta = &pd->delta;
pd->delta.old_file.path = old_path; if (!oldsrc->as_path) {
pd->delta.new_file.path = new_path; if (newsrc->as_path)
oldsrc->as_path = newsrc->as_path;
else
oldsrc->as_path = newsrc->as_path = "file";
}
else if (!newsrc->as_path)
newsrc->as_path = oldsrc->as_path;
lfile->path = oldsrc->as_path;
rfile->path = newsrc->as_path;
if ((error = git_diff_file_content__init_from_blob( if ((error = git_diff_file_content__init_from_src(
&pd->patch.ofile, repo, opts, old_blob, &pd->delta.old_file)) < 0 || ldata, repo, opts, oldsrc, lfile)) < 0 ||
(error = git_diff_file_content__init_from_blob( (error = git_diff_file_content__init_from_src(
&pd->patch.nfile, repo, opts, new_blob, &pd->delta.new_file)) < 0) rdata, repo, opts, newsrc, rfile)) < 0)
return error; return error;
return diff_single_generate(pd, xo); return diff_single_generate(pd, xo);
...@@ -400,11 +408,9 @@ static int diff_patch_with_delta_alloc( ...@@ -400,11 +408,9 @@ static int diff_patch_with_delta_alloc(
return 0; return 0;
} }
int git_diff_blobs( static int diff_from_sources(
const git_blob *old_blob, git_diff_file_content_src *oldsrc,
const char *old_path, git_diff_file_content_src *newsrc,
const git_blob *new_blob,
const char *new_path,
const git_diff_options *opts, const git_diff_options *opts,
git_diff_file_cb file_cb, git_diff_file_cb file_cb,
git_diff_hunk_cb hunk_cb, git_diff_hunk_cb hunk_cb,
...@@ -420,26 +426,19 @@ int git_diff_blobs( ...@@ -420,26 +426,19 @@ int git_diff_blobs(
&xo.output, opts, file_cb, hunk_cb, data_cb, payload); &xo.output, opts, file_cb, hunk_cb, data_cb, payload);
git_xdiff_init(&xo, opts); git_xdiff_init(&xo, opts);
if (!old_path && new_path)
old_path = new_path;
else if (!new_path && old_path)
new_path = old_path;
memset(&pd, 0, sizeof(pd)); memset(&pd, 0, sizeof(pd));
error = diff_patch_from_blobs(
&pd, &xo, old_blob, old_path, new_blob, new_path, opts); error = diff_patch_from_sources(&pd, &xo, oldsrc, newsrc, opts);
git_patch_free(&pd.patch); git_patch_free(&pd.patch);
return error; return error;
} }
int git_patch_from_blobs( static int patch_from_sources(
git_patch **out, git_patch **out,
const git_blob *old_blob, git_diff_file_content_src *oldsrc,
const char *old_path, git_diff_file_content_src *newsrc,
const git_blob *new_blob,
const char *new_path,
const git_diff_options *opts) const git_diff_options *opts)
{ {
int error = 0; int error = 0;
...@@ -449,17 +448,15 @@ int git_patch_from_blobs( ...@@ -449,17 +448,15 @@ int git_patch_from_blobs(
assert(out); assert(out);
*out = NULL; *out = NULL;
if (diff_patch_with_delta_alloc(&pd, &old_path, &new_path) < 0) if ((error = diff_patch_with_delta_alloc(
return -1; &pd, &oldsrc->as_path, &newsrc->as_path)) < 0)
return error;
memset(&xo, 0, sizeof(xo)); memset(&xo, 0, sizeof(xo));
diff_output_to_patch(&xo.output, &pd->patch); diff_output_to_patch(&xo.output, &pd->patch);
git_xdiff_init(&xo, opts); git_xdiff_init(&xo, opts);
error = diff_patch_from_blobs( if (!(error = diff_patch_from_sources(pd, &xo, oldsrc, newsrc, opts)))
pd, &xo, old_blob, old_path, new_blob, new_path, opts);
if (!error)
*out = (git_patch *)pd; *out = (git_patch *)pd;
else else
git_patch_free((git_patch *)pd); git_patch_free((git_patch *)pd);
...@@ -467,46 +464,38 @@ int git_patch_from_blobs( ...@@ -467,46 +464,38 @@ int git_patch_from_blobs(
return error; return error;
} }
static int diff_patch_from_blob_and_buffer( int git_diff_blobs(
diff_patch_with_delta *pd,
git_xdiff_output *xo,
const git_blob *old_blob, const git_blob *old_blob,
const char *old_path, const char *old_path,
const char *buf, const git_blob *new_blob,
size_t buflen, const char *new_path,
const char *buf_path, const git_diff_options *opts,
const git_diff_options *opts) git_diff_file_cb file_cb,
git_diff_hunk_cb hunk_cb,
git_diff_line_cb data_cb,
void *payload)
{ {
int error = 0; git_diff_file_content_src osrc =
git_repository *repo = GIT_DIFF_FILE_CONTENT_SRC__BLOB(old_blob, old_path);
old_blob ? git_object_owner((const git_object *)old_blob) : NULL; git_diff_file_content_src nsrc =
GIT_DIFF_FILE_CONTENT_SRC__BLOB(new_blob, new_path);
GITERR_CHECK_VERSION(opts, GIT_DIFF_OPTIONS_VERSION, "git_diff_options"); return diff_from_sources(
&osrc, &nsrc, opts, file_cb, hunk_cb, data_cb, payload);
pd->patch.delta = &pd->delta; }
if (opts && (opts->flags & GIT_DIFF_REVERSE) != 0) {
pd->delta.old_file.path = buf_path;
pd->delta.new_file.path = old_path;
if (!(error = git_diff_file_content__init_from_raw(
&pd->patch.ofile, repo, opts, buf, buflen, &pd->delta.old_file)))
error = git_diff_file_content__init_from_blob(
&pd->patch.nfile, repo, opts, old_blob, &pd->delta.new_file);
} else {
pd->delta.old_file.path = old_path;
pd->delta.new_file.path = buf_path;
if (!(error = git_diff_file_content__init_from_blob(
&pd->patch.ofile, repo, opts, old_blob, &pd->delta.old_file)))
error = git_diff_file_content__init_from_raw(
&pd->patch.nfile, repo, opts, buf, buflen, &pd->delta.new_file);
}
if (error < 0)
return error;
return diff_single_generate(pd, xo); int git_patch_from_blobs(
git_patch **out,
const git_blob *old_blob,
const char *old_path,
const git_blob *new_blob,
const char *new_path,
const git_diff_options *opts)
{
git_diff_file_content_src osrc =
GIT_DIFF_FILE_CONTENT_SRC__BLOB(old_blob, old_path);
git_diff_file_content_src nsrc =
GIT_DIFF_FILE_CONTENT_SRC__BLOB(new_blob, new_path);
return patch_from_sources(out, &osrc, &nsrc, opts);
} }
int git_diff_blob_to_buffer( int git_diff_blob_to_buffer(
...@@ -521,27 +510,12 @@ int git_diff_blob_to_buffer( ...@@ -521,27 +510,12 @@ int git_diff_blob_to_buffer(
git_diff_line_cb data_cb, git_diff_line_cb data_cb,
void *payload) void *payload)
{ {
int error = 0; git_diff_file_content_src osrc =
diff_patch_with_delta pd; GIT_DIFF_FILE_CONTENT_SRC__BLOB(old_blob, old_path);
git_xdiff_output xo; git_diff_file_content_src nsrc =
GIT_DIFF_FILE_CONTENT_SRC__BUF(buf, buflen, buf_path);
memset(&xo, 0, sizeof(xo)); return diff_from_sources(
diff_output_init( &osrc, &nsrc, opts, file_cb, hunk_cb, data_cb, payload);
&xo.output, opts, file_cb, hunk_cb, data_cb, payload);
git_xdiff_init(&xo, opts);
if (!old_path && buf_path)
old_path = buf_path;
else if (!buf_path && old_path)
buf_path = old_path;
memset(&pd, 0, sizeof(pd));
error = diff_patch_from_blob_and_buffer(
&pd, &xo, old_blob, old_path, buf, buflen, buf_path, opts);
git_patch_free(&pd.patch);
return error;
} }
int git_patch_from_blob_and_buffer( int git_patch_from_blob_and_buffer(
...@@ -553,29 +527,49 @@ int git_patch_from_blob_and_buffer( ...@@ -553,29 +527,49 @@ int git_patch_from_blob_and_buffer(
const char *buf_path, const char *buf_path,
const git_diff_options *opts) const git_diff_options *opts)
{ {
int error = 0; git_diff_file_content_src osrc =
diff_patch_with_delta *pd; GIT_DIFF_FILE_CONTENT_SRC__BLOB(old_blob, old_path);
git_xdiff_output xo; git_diff_file_content_src nsrc =
GIT_DIFF_FILE_CONTENT_SRC__BUF(buf, buflen, buf_path);
assert(out); return patch_from_sources(out, &osrc, &nsrc, opts);
*out = NULL; }
if (diff_patch_with_delta_alloc(&pd, &old_path, &buf_path) < 0)
return -1;
memset(&xo, 0, sizeof(xo));
diff_output_to_patch(&xo.output, &pd->patch);
git_xdiff_init(&xo, opts);
error = diff_patch_from_blob_and_buffer(
pd, &xo, old_blob, old_path, buf, buflen, buf_path, opts);
if (!error) int git_diff_buffers(
*out = (git_patch *)pd; const void *old_buf,
else size_t old_len,
git_patch_free((git_patch *)pd); const char *old_path,
const void *new_buf,
size_t new_len,
const char *new_path,
const git_diff_options *opts,
git_diff_file_cb file_cb,
git_diff_hunk_cb hunk_cb,
git_diff_line_cb data_cb,
void *payload)
{
git_diff_file_content_src osrc =
GIT_DIFF_FILE_CONTENT_SRC__BUF(old_buf, old_len, old_path);
git_diff_file_content_src nsrc =
GIT_DIFF_FILE_CONTENT_SRC__BUF(new_buf, new_len, new_path);
return diff_from_sources(
&osrc, &nsrc, opts, file_cb, hunk_cb, data_cb, payload);
}
return error; int git_patch_from_buffers(
git_patch **out,
const void *old_buf,
size_t old_len,
const char *old_path,
const char *new_buf,
size_t new_len,
const char *new_path,
const git_diff_options *opts)
{
git_diff_file_content_src osrc =
GIT_DIFF_FILE_CONTENT_SRC__BUF(old_buf, old_len, old_path);
git_diff_file_content_src nsrc =
GIT_DIFF_FILE_CONTENT_SRC__BUF(new_buf, new_len, new_path);
return patch_from_sources(out, &osrc, &nsrc, opts);
} }
int git_patch_from_diff( int git_patch_from_diff(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment