From cd33323b7251e0bb15c5ee476e918859b661cc5f Mon Sep 17 00:00:00 2001 From: Russell Belfer Date: Fri, 27 Jan 2012 11:29:25 -0800 Subject: [PATCH] Initial implementation of git_diff_blob This gets the basic plumbing in place for git_diff_blob. There is a known issue where additional parameters like the number of lines of context to display on the diff are not working correctly (which leads one of the new unit tests to fail). --- include/git2/diff.h | 103 ++++++++++ src/diff.c | 104 ++++++++++ src/xdiff/xinclude.h | 6 +- tests-clay/diff/blob.c | 181 ++++++++++++++++++ tests/resources/attr/.gitted/index | Bin 1376 -> 1376 bytes tests/resources/attr/.gitted/logs/HEAD | Bin 491 -> 828 bytes .../attr/.gitted/logs/refs/heads/master | Bin 491 -> 828 bytes .../37/0fe9ec224ce33e71f9e5ec2bd1142ce9937a6a | Bin 0 -> 177 bytes .../3a/6df026462ebafe455af9867d27eda20a9e0974 | Bin 0 -> 84 bytes .../4d/713dc48e6b1bd75b0d61ad078ba9ca3a56745d | Bin 0 -> 73 bytes .../71/7fc31f6b84f9d6fc3a4edbca259d7fc92beee2 | Bin 0 -> 422 bytes .../96/089fd31ce1d3ee2afb0ba09ba063066932f027 | Bin 0 -> 422 bytes .../c9/6bbb2c2557a8325ae1559e3ba79cdcecb23076 | Bin 0 -> 124 bytes .../f5/b0af1fb4f5c0cd7aad880711d368a07333c307 | Bin 0 -> 165 bytes .../fe/773770c5a6cc7185580c9204b1ff18a33ff3fc | Bin 0 -> 151 bytes .../resources/attr/.gitted/refs/heads/master | Bin 41 -> 41 bytes tests/resources/attr/root_test2 | Bin 20 -> 61 bytes tests/resources/attr/root_test3 | Bin 20 -> 143 bytes tests/resources/attr/root_test4.txt | Bin 12 -> 204 bytes 19 files changed, 393 insertions(+), 1 deletion(-) create mode 100644 include/git2/diff.h create mode 100644 src/diff.c create mode 100644 tests-clay/diff/blob.c create mode 100644 tests/resources/attr/.gitted/objects/37/0fe9ec224ce33e71f9e5ec2bd1142ce9937a6a create mode 100644 tests/resources/attr/.gitted/objects/3a/6df026462ebafe455af9867d27eda20a9e0974 create mode 100644 tests/resources/attr/.gitted/objects/4d/713dc48e6b1bd75b0d61ad078ba9ca3a56745d create mode 100644 tests/resources/attr/.gitted/objects/71/7fc31f6b84f9d6fc3a4edbca259d7fc92beee2 create mode 100644 tests/resources/attr/.gitted/objects/96/089fd31ce1d3ee2afb0ba09ba063066932f027 create mode 100644 tests/resources/attr/.gitted/objects/c9/6bbb2c2557a8325ae1559e3ba79cdcecb23076 create mode 100644 tests/resources/attr/.gitted/objects/f5/b0af1fb4f5c0cd7aad880711d368a07333c307 create mode 100644 tests/resources/attr/.gitted/objects/fe/773770c5a6cc7185580c9204b1ff18a33ff3fc diff --git a/include/git2/diff.h b/include/git2/diff.h new file mode 100644 index 000000000..1d3a8d408 --- /dev/null +++ b/include/git2/diff.h @@ -0,0 +1,103 @@ +/* + * Copyright (C) 2009-2012 the libgit2 contributors + * + * 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_diff_h__ +#define INCLUDE_git_diff_h__ + +#include "common.h" +#include "types.h" +#include "oid.h" +#include "tree.h" +#include "refs.h" + +/** + * @file git2/diff.h + * @brief Git tree and file differencing routines. + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +typedef int (*git_diff_file_fn)( + void *cb_data, + const git_oid *old, + const char *old_path, + int old_mode, + const git_oid *new, /* hashed object if from work tree */ + const char *new_path, + int new_mode); + +typedef int (*git_diff_hunk_fn)( + void *cb_data, + int old_start, + int old_lines, + int new_start, + int new_lines); + +#define GIT_DIFF_LINE_CONTEXT 0 +#define GIT_DIFF_LINE_ADDITION 1 +#define GIT_DIFF_LINE_DELETION 2 + +typedef int (*git_diff_line_fn)( + void *cb_data, + int origin, /* GIT_DIFF_LINE value from above */ + const char *content, + size_t content_len); + +typedef struct { + int context_lines; + int interhunk_lines; + int ignore_whitespace; + + git_diff_file_fn file_cb; + git_diff_hunk_fn hunk_cb; + git_diff_line_fn line_cb; + void *cb_data; +} git_diff_opts; + + +GIT_EXTERN(int) git_diff_blobs( + git_repository *repo, + git_blob *old, + git_blob *new, + git_diff_opts *options); + +GIT_EXTERN(int) git_diff_trees( + git_repository *repo, + git_tree *old, + git_tree *new, + git_diff_opts *options); + +GIT_EXTERN(int) git_diff_index( + git_repository *repo, + git_tree *old, + git_diff_opts *options); + +/* pass NULL for the git_tree to diff workdir against index */ +GIT_EXTERN(int) git_diff_workdir( + git_repository *repo, + git_tree *old, + git_diff_opts *options); + +GIT_EXTERN(int) git_diff_workdir_file( + git_repository *repo, + git_blob *old, + const char *path, + git_diff_opts *options); + +/* pass git_objects to diff against or NULL for index. + * can handle: blob->blob, tree->index, tree->tree + * it will be an error if object types don't match + */ +/* pass git_object to diff WT against or NULL for index + * can handle: index->wt, tree->wt, blob->wt with path + */ + +GIT_END_DECL + +/** @} */ + +#endif diff --git a/src/diff.c b/src/diff.c new file mode 100644 index 000000000..7128b7c76 --- /dev/null +++ b/src/diff.c @@ -0,0 +1,104 @@ +/* + * Copyright (C) 2009-2011 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "common.h" +#include "git2/diff.h" +#include "xdiff/xdiff.h" +#include "blob.h" +#include + +static int read_next_int(const char **str, int *value) +{ + const char *scan = *str; + int v = 0, digits = 0; + /* find next digit */ + for (scan = *str; *scan && !isdigit(*scan); scan++); + /* parse next number */ + for (; isdigit(*scan); scan++, digits++) + v = (v * 10) + (*scan - '0'); + *str = scan; + *value = v; + return (digits > 0) ? GIT_SUCCESS : GIT_ENOTFOUND; +} + +static int diff_output_cb(void *priv, mmbuffer_t *bufs, int len) +{ + int err = GIT_SUCCESS; + git_diff_opts *opts = priv; + + if (len == 1) { + int ostart = -1, olen = 0, nstart = -1, nlen = 0; + /* expect something of the form "@@ -%d[,%d] +%d[,%d] @@" */ + if (opts->hunk_cb && bufs[0].ptr[0] == '@') { + const char *scan = bufs[0].ptr; + if (!(err = read_next_int(&scan, &ostart)) && *scan == ',') + err = read_next_int(&scan, &olen); + if (!err && !(err = read_next_int(&scan, &nstart)) && *scan == ',') + err = read_next_int(&scan, &nlen); + if (!err && ostart >= 0 && nstart >= 0) + err = opts->hunk_cb( + opts->cb_data, ostart, olen, nstart, nlen); + } + } + else if (len == 2 || len == 3) { + int origin; + /* expect " "/"-"/"+", then data, then maybe newline */ + origin = + (*bufs[0].ptr == '+') ? GIT_DIFF_LINE_ADDITION : + (*bufs[0].ptr == '-') ? GIT_DIFF_LINE_DELETION : + GIT_DIFF_LINE_CONTEXT; + + if (opts->line_cb) + err = opts->line_cb( + opts->cb_data, origin, bufs[1].ptr, bufs[1].size); + } + + return err; +} + +int git_diff_blobs( + git_repository *repo, + git_blob *old_blob, + git_blob *new_blob, + git_diff_opts *options) +{ + mmfile_t old, new; + xpparam_t params; + xdemitconf_t cfg; + xdemitcb_t callback; + + assert(repo && old_blob && new_blob && options); + + old.ptr = (char *)git_blob_rawcontent(old_blob); + old.size = git_blob_rawsize(old_blob); + + new.ptr = (char *)git_blob_rawcontent(new_blob); + new.size = git_blob_rawsize(new_blob); + + memset(¶ms, 0, sizeof(params)); + + memset(&cfg, 0, sizeof(cfg)); + cfg.ctxlen = options->context_lines || 3; + cfg.interhunkctxlen = options->interhunk_lines || 3; + if (options->ignore_whitespace) + cfg.flags |= XDF_WHITESPACE_FLAGS; + + memset(&callback, 0, sizeof(callback)); + callback.outf = diff_output_cb; + callback.priv = options; + + if (options->file_cb) + options->file_cb( + options->cb_data, + git_object_id((const git_object *)old_blob), NULL, 010644, + git_object_id((const git_object *)new_blob), NULL, 010644); + + xdl_diff(&old, &new, ¶ms, &cfg, &callback); + + return GIT_SUCCESS; +} + diff --git a/src/xdiff/xinclude.h b/src/xdiff/xinclude.h index 526ccb344..2928d329b 100644 --- a/src/xdiff/xinclude.h +++ b/src/xdiff/xinclude.h @@ -26,10 +26,14 @@ #include #include #include -#include #include #include +#ifdef WIN32 +#else +#include +#endif + #include "xmacros.h" #include "xdiff.h" #include "xtypes.h" diff --git a/tests-clay/diff/blob.c b/tests-clay/diff/blob.c new file mode 100644 index 000000000..2fb3e7740 --- /dev/null +++ b/tests-clay/diff/blob.c @@ -0,0 +1,181 @@ +#include "clay_libgit2.h" +#include "fileops.h" +#include "git2/diff.h" + +static git_repository *g_repo = NULL; + +void test_diff_blob__initialize(void) +{ + cl_fixture_sandbox("attr"); + cl_git_pass(p_rename("attr/.gitted", "attr/.git")); + cl_git_pass(p_rename("attr/gitattributes", "attr/.gitattributes")); + cl_git_pass(git_repository_open(&g_repo, "attr/.git")); +} + +void test_diff_blob__cleanup(void) +{ + git_repository_free(g_repo); + g_repo = NULL; + cl_fixture_cleanup("attr"); +} + +typedef struct { + int files; + int hunks; + int hunk_new_lines; + int hunk_old_lines; + int lines; + int line_ctxt; + int line_adds; + int line_dels; +} diff_expects; + +static void log(const char *str, int n) +{ + FILE *fp = fopen("/Users/rb/tmp/diff.log", "a"); + if (n > 0) + fprintf(fp, "%.*s", n, str); + else + fputs(str, fp); + fclose(fp); +} + +static int diff_file_fn( + void *cb_data, + const git_oid *old, + const char *old_path, + int old_mode, + const git_oid *new, + const char *new_path, + int new_mode) +{ + diff_expects *e = cb_data; + e->files++; + log("-- file --\n", 0); + return 0; +} + +static int diff_hunk_fn( + void *cb_data, + int old_start, + int old_lines, + int new_start, + int new_lines) +{ + diff_expects *e = cb_data; + e->hunks++; + e->hunk_old_lines += old_lines; + e->hunk_new_lines += new_lines; + log("-- hunk --\n", 0); + return 0; +} + +static int diff_line_fn( + void *cb_data, + int origin, + const char *content, + size_t content_len) +{ + diff_expects *e = cb_data; + e->lines++; + switch (origin) { + case GIT_DIFF_LINE_CONTEXT: + log("[ ]", 3); + e->line_ctxt++; + break; + case GIT_DIFF_LINE_ADDITION: + log("[+]", 3); + e->line_adds++; + break; + case GIT_DIFF_LINE_DELETION: + log("[-]", 3); + e->line_dels++; + break; + default: + cl_assert("Unknown diff line origin" == 0); + } + log(content, content_len); + return 0; +} + +void test_diff_blob__0(void) +{ + int err; + git_blob *a, *b, *c, *d; + git_oid a_oid, b_oid, c_oid, d_oid; + git_diff_opts opts; + diff_expects exp; + + /* tests/resources/attr/root_test1 */ + cl_git_pass(git_oid_fromstrn(&a_oid, "45141a79", 8)); + cl_git_pass(git_blob_lookup_prefix(&a, g_repo, &a_oid, 4)); + + /* tests/resources/attr/root_test2 */ + cl_git_pass(git_oid_fromstrn(&b_oid, "4d713dc4", 8)); + cl_git_pass(git_blob_lookup_prefix(&b, g_repo, &b_oid, 4)); + + /* tests/resources/attr/root_test3 */ + cl_git_pass(git_oid_fromstrn(&c_oid, "c96bbb2c2557a832", 16)); + cl_git_pass(git_blob_lookup_prefix(&c, g_repo, &c_oid, 8)); + + /* tests/resources/attr/root_test4.txt */ + cl_git_pass(git_oid_fromstrn(&d_oid, "fe773770c5a6", 12)); + cl_git_pass(git_blob_lookup_prefix(&d, g_repo, &d_oid, 6)); + + /* Doing the equivalent of a `diff -U 2` on these files */ + + opts.context_lines = 2; + opts.interhunk_lines = 0; + opts.ignore_whitespace = 0; + opts.file_cb = diff_file_fn; + opts.hunk_cb = diff_hunk_fn; + opts.line_cb = diff_line_fn; + opts.cb_data = &exp; + + memset(&exp, 0, sizeof(exp)); + cl_git_pass(git_diff_blobs(g_repo, a, b, &opts)); + + cl_assert(exp.files == 1); + cl_assert(exp.hunks == 1); + cl_assert(exp.lines == 6); + cl_assert(exp.line_ctxt == 1); + cl_assert(exp.line_adds == 5); + cl_assert(exp.line_dels == 0); + + memset(&exp, 0, sizeof(exp)); + cl_git_pass(git_diff_blobs(g_repo, b, c, &opts)); + + cl_assert(exp.files == 1); + cl_assert(exp.hunks == 1); + cl_assert(exp.lines == 15); + cl_assert(exp.line_ctxt == 3); + cl_assert(exp.line_adds == 9); + cl_assert(exp.line_dels == 3); + + memset(&exp, 0, sizeof(exp)); + cl_git_pass(git_diff_blobs(g_repo, a, c, &opts)); + + cl_assert(exp.files == 1); + cl_assert(exp.hunks == 1); + cl_assert(exp.lines == 13); + cl_assert(exp.line_ctxt == 0); + cl_assert(exp.line_adds == 12); + cl_assert(exp.line_dels == 1); + + opts.context_lines = 2; + + memset(&exp, 0, sizeof(exp)); + cl_git_pass(git_diff_blobs(g_repo, c, d, &opts)); + + cl_assert(exp.files == 1); + cl_assert(exp.hunks == 2); + cl_assert(exp.lines == 16); + cl_assert(exp.line_ctxt == 6); + cl_assert(exp.line_adds == 6); + cl_assert(exp.line_dels == 4); + + git_blob_free(a); + git_blob_free(b); + git_blob_free(c); +} + diff --git a/tests/resources/attr/.gitted/index b/tests/resources/attr/.gitted/index index c52747e0b6c9a457a28c9b9fea098f0fa2e59c48..f35d3005eede3416685c62c857e9664fb533d5a4 100644 GIT binary patch delta 492 zcmaFB^?*yo#WTp6fq{Vuhz0${%anjLgFoZTG!V_lz`(-59rbFWih+c`czGOHj=SkL zLN48RVh&ht9as)UUuOEmEg-ovH%z(seG~tH<*?azYqASt14ttl$!M|3cNlT%ygONi z$pWNP8Pm$>=E)^Y7WMv$`%S@;P#Ww)p42??|7#P0-Od<>!~gm!uY#7=g?H0e{7Zg~&9-yib{E=JlV<-mRk=z9QBr>S5?S z>*aIqyxC+>hGd{I*ffZNQBVWHG}ORoGy~84D>pAVy6jA0YXr|EmW}@<7TbURv$=!$ c2&2fR?cc*rcFD~6cS>RVSv`qrLD!$X0JB7P00000 delta 209 zcmaFB^?*yo#WTp6fq{Vuhz0%r$Zi4B3^1AxByMRqX`+h3WEU23#>o?NAdD#!w?G(E zC;l;*{DoPZk$U%#1P1E4h604uiBlkYGZNcjDc6M~oyp&_Q52u@aEvH%-D zZE^{eH66&(XEd5@!y?Z(1I#uCvu94;!lJsllld^CNYs&OOEN#t=&Z6+#1vKU#ker%U_sfnbFu*L>)p8C_N8#l1O zWF5Qn0lYif07DNqgQRE3J><@PD1KZhc-5jhd0TpwFlABAO@l&|xo!$YVdlgrkKJM= Qbs!A#O*7gs!bhgX8+o!uiU0rr delta 7 OcmdnP_L_OaYeoPK*#k`g diff --git a/tests/resources/attr/.gitted/logs/refs/heads/master b/tests/resources/attr/.gitted/logs/refs/heads/master index f518a465a7fde4ce500074c48c87c6ffeacf036f..6d096351c674a5120c97a424fe209c7ec03ab504 100644 GIT binary patch delta 238 zcma*h!KuPP5P)Gs5KI+wQxC$-PLj!5g&r(mvO6OIH9{gDtwCNRuK+u;32i{|_@Dmo zHGR(hUz5M=&Z6+#1vKU#ker%U_sfnbFu*L>)p8C_N8#l1O zWF5Qn0lYif07DNqgQRE3J><@PD1KZhc-5jhd0TpwFlABAO@l&|xo!$YVdlgrkKJM= Qbs!A#O*7gs!bhgX8+o!uiU0rr delta 7 OcmdnP_L_OaYeoPK*#k`g diff --git a/tests/resources/attr/.gitted/objects/37/0fe9ec224ce33e71f9e5ec2bd1142ce9937a6a b/tests/resources/attr/.gitted/objects/37/0fe9ec224ce33e71f9e5ec2bd1142ce9937a6a new file mode 100644 index 0000000000000000000000000000000000000000..9c37c5946cf33f0dae9266d1e87d926d546010b7 GIT binary patch literal 177 zcmV;i08amS0i90UX#+73%(tr81`_l*NhdHNq=bMASZQ~YgS(TFWZb?5>A+vZJeg9D zV;f+-ejAM$qKQ(Rk&xF#5`{@{Vaf%qPc*$`Gc#Lt!^YBzV`W)@rKrke literal 0 HcmV?d00001 diff --git a/tests/resources/attr/.gitted/objects/3a/6df026462ebafe455af9867d27eda20a9e0974 b/tests/resources/attr/.gitted/objects/3a/6df026462ebafe455af9867d27eda20a9e0974 new file mode 100644 index 0000000000000000000000000000000000000000..c74add8265383a1c4d303e844383353cac3c4db5 GIT binary patch literal 84 zcmV-a0IUCa0X51o3V<*S1yJXn;-A6E19$@m=aznGAWfhKdwoT4dDAQE1>3qD-ichR qgt%peit$Qm_i1PxM4|dbG{RrTA5Mt|-ZXT7SB|gHYI*>84<5l5-zBF2 literal 0 HcmV?d00001 diff --git a/tests/resources/attr/.gitted/objects/4d/713dc48e6b1bd75b0d61ad078ba9ca3a56745d b/tests/resources/attr/.gitted/objects/4d/713dc48e6b1bd75b0d61ad078ba9ca3a56745d new file mode 100644 index 0000000000000000000000000000000000000000..eb1e8d0c569d9892fef46a9d08595ce6aa0912a7 GIT binary patch literal 73 zcmV-P0Ji^l0R_Rq4S*mJ1VG8Fk>(@FfcPQQAjK)DKcOP&F^Wd?(mk!9N4*WOR^9COxTOtMRg|A! z5)V>j$lxj>Rk^&v>FBIvr76rxTz=lq{cO=y88P@4+8*i4mcAa%o4A&}d*vysu##AG zRmKcfxgXTr^mhGojr!SEtNwNo*F4S=bX6vLB^4zMzXQ@YuKT=3>Xx_p+&$Yb7KPrg ztuQeF0)^tzq?F7ehWT5#(@O6?;|`m?{@l8W+j1xLFx^B}V8o!Qc~3*<<;<6X&FgL* QbCor237z8&01I2sMPj(!-T(jq literal 0 HcmV?d00001 diff --git a/tests/resources/attr/.gitted/objects/96/089fd31ce1d3ee2afb0ba09ba063066932f027 b/tests/resources/attr/.gitted/objects/96/089fd31ce1d3ee2afb0ba09ba063066932f027 new file mode 100644 index 0000000000000000000000000000000000000000..efa62f9126094e49dd42e2c035cbc2e1cc53926f GIT binary patch literal 422 zcmV;X0a^Zd0V^p=O;s>8Fk>(@FfcPQQAjK)DKcOP&F^Wd?(mk!9N4*WOR^9COxTOtMRg|A! z5)V>j$lxj>Rk^&v>FBIvr76rxTz=lq{cO=y88P@4+8*i4mcAa%o4A&}d*vysu##AG zRmKb_vv=#LhOaP+dKfy-dik6?Z#Ef}p{p{{E2$`9_*ZUTaCF(3!qy0$Nh}-xODwkk z{Kv!q2o#D-lTtE^80K%`PAk3pj5}=l`g7|hZp)p}!*ml_ff0kI<~xMJev{rT#+JKye}vk}aqArFn(fsqH4zL%va+=C1nxK=@UaTX diff --git a/tests/resources/attr/root_test2 b/tests/resources/attr/root_test2 index 45141a79a77842c59a63229403220a4e4be74e3d..4d713dc48e6b1bd75b0d61ad078ba9ca3a56745d 100644 GIT binary patch delta 46 zcmWf!ogkvg6`Y@&s*sqHl39|OpO=`Ukdv90TFk}el3$*ukda!Hs*seLlV8ro1pr!S B4(9(elpDUZe<8MfF)w+ZrQ6L(_4D z(ui(4zUpgcJRQm4CeYPhMArcY=G2l~$jHg05}kZA=qR2f`?j}vuPjI4o|;15VwkIK>z>% literal 12 TcmeZB&B@7ENK8-6%;N$891R2?