summaryrefslogtreecommitdiff
path: root/diff.c
diff options
context:
space:
mode:
Diffstat (limited to 'diff.c')
-rw-r--r--diff.c228
1 files changed, 172 insertions, 56 deletions
diff --git a/diff.c b/diff.c
index d1ad6a3..52c7915 100644
--- a/diff.c
+++ b/diff.c
@@ -20,7 +20,7 @@
#include "hashmap.h"
#include "ll-merge.h"
#include "string-list.h"
-#include "argv-array.h"
+#include "strvec.h"
#include "graph.h"
#include "packfile.h"
#include "parse-options.h"
@@ -48,6 +48,7 @@ static const char *diff_order_file_cfg;
int diff_auto_refresh_index = 1;
static int diff_mnemonic_prefix;
static int diff_no_prefix;
+static int diff_relative;
static int diff_stat_graph_width;
static int diff_dirstat_permille_default = 30;
static struct diff_options default_diff_options;
@@ -386,6 +387,10 @@ int git_diff_ui_config(const char *var, const char *value, void *cb)
diff_no_prefix = git_config_bool(var, value);
return 0;
}
+ if (!strcmp(var, "diff.relative")) {
+ diff_relative = git_config_bool(var, value);
+ return 0;
+ }
if (!strcmp(var, "diff.statgraphwidth")) {
diff_stat_graph_width = git_config_int(var, value);
return 0;
@@ -477,14 +482,14 @@ int git_diff_basic_config(const char *var, const char *value, void *cb)
static char *quote_two(const char *one, const char *two)
{
- int need_one = quote_c_style(one, NULL, NULL, 1);
- int need_two = quote_c_style(two, NULL, NULL, 1);
+ int need_one = quote_c_style(one, NULL, NULL, CQUOTE_NODQ);
+ int need_two = quote_c_style(two, NULL, NULL, CQUOTE_NODQ);
struct strbuf res = STRBUF_INIT;
if (need_one + need_two) {
strbuf_addch(&res, '"');
- quote_c_style(one, &res, NULL, 1);
- quote_c_style(two, &res, NULL, 1);
+ quote_c_style(one, &res, NULL, CQUOTE_NODQ);
+ quote_c_style(two, &res, NULL, CQUOTE_NODQ);
strbuf_addch(&res, '"');
} else {
strbuf_addstr(&res, one);
@@ -2048,7 +2053,7 @@ static void fn_out_diff_words_aux(void *priv,
static int find_word_boundaries(mmfile_t *buffer, regex_t *word_regex,
int *begin, int *end)
{
- if (word_regex && *begin < buffer->size) {
+ while (word_regex && *begin < buffer->size) {
regmatch_t match[1];
if (!regexec_buf(word_regex, buffer->ptr + *begin,
buffer->size - *begin, 1, match, 0)) {
@@ -2056,9 +2061,13 @@ static int find_word_boundaries(mmfile_t *buffer, regex_t *word_regex,
'\n', match[0].rm_eo - match[0].rm_so);
*end = p ? p - buffer->ptr : match[0].rm_eo + *begin;
*begin += match[0].rm_so;
- return *begin >= *end;
+ if (*begin == *end)
+ (*begin)++;
+ else
+ return *begin > *end;
+ } else {
+ return -1;
}
- return -1;
}
/* find the next word */
@@ -2228,14 +2237,12 @@ static void init_diff_words_data(struct emit_callback *ecbdata,
struct diff_options *o = xmalloc(sizeof(struct diff_options));
memcpy(o, orig_opts, sizeof(struct diff_options));
- ecbdata->diff_words =
- xcalloc(1, sizeof(struct diff_words_data));
+ CALLOC_ARRAY(ecbdata->diff_words, 1);
ecbdata->diff_words->type = o->word_diff;
ecbdata->diff_words->opt = o;
if (orig_opts->emitted_symbols)
- o->emitted_symbols =
- xcalloc(1, sizeof(struct emitted_diff_symbols));
+ CALLOC_ARRAY(o->emitted_symbols, 1);
if (!o->word_regex)
o->word_regex = userdiff_word_regex(one, o->repo->index);
@@ -2504,7 +2511,7 @@ static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
const char *name_b)
{
struct diffstat_file *x;
- x = xcalloc(1, sizeof(*x));
+ CALLOC_ARRAY(x, 1);
ALLOC_GROW(diffstat->files, diffstat->nr + 1, diffstat->alloc);
diffstat->files[diffstat->nr++] = x;
if (name_b) {
@@ -3148,16 +3155,19 @@ static void show_dirstat_by_line(struct diffstat_t *data, struct diff_options *o
gather_dirstat(options, &dir, changed, "", 0);
}
+static void free_diffstat_file(struct diffstat_file *f)
+{
+ free(f->print_name);
+ free(f->name);
+ free(f->from_name);
+ free(f);
+}
+
void free_diffstat_info(struct diffstat_t *diffstat)
{
int i;
- for (i = 0; i < diffstat->nr; i++) {
- struct diffstat_file *f = diffstat->files[i];
- free(f->print_name);
- free(f->name);
- free(f->from_name);
- free(f);
- }
+ for (i = 0; i < diffstat->nr; i++)
+ free_diffstat_file(diffstat->files[i]);
free(diffstat->files);
}
@@ -3424,7 +3434,7 @@ static void builtin_diff(const char *name_a,
if (o->submodule_format == DIFF_SUBMODULE_LOG &&
(!one->mode || S_ISGITLINK(one->mode)) &&
(!two->mode || S_ISGITLINK(two->mode))) {
- show_submodule_summary(o, one->path ? one->path : two->path,
+ show_submodule_diff_summary(o, one->path ? one->path : two->path,
&one->oid, &two->oid,
two->dirty_submodule);
return;
@@ -3579,6 +3589,8 @@ static void builtin_diff(const char *name_a,
if (header.len && !o->flags.suppress_diff_headers)
ecbdata.header = &header;
xpp.flags = o->xdl_opts;
+ xpp.ignore_regex = o->ignore_regex;
+ xpp.ignore_regex_nr = o->ignore_regex_nr;
xpp.anchors = o->anchors;
xpp.anchors_nr = o->anchors_nr;
xecfg.ctxlen = o->context;
@@ -3655,7 +3667,7 @@ static void builtin_diffstat(const char *name_a, const char *name_b,
{
mmfile_t mf1, mf2;
struct diffstat_file *data;
- int same_contents;
+ int may_differ;
int complete_rewrite = 0;
if (!DIFF_PAIR_UNMERGED(p)) {
@@ -3673,12 +3685,14 @@ static void builtin_diffstat(const char *name_a, const char *name_b,
return;
}
- same_contents = oideq(&one->oid, &two->oid);
+ /* saves some reads if true, not a guarantee of diff outcome */
+ may_differ = !(one->oid_valid && two->oid_valid &&
+ oideq(&one->oid, &two->oid));
if (diff_filespec_is_binary(o->repo, one) ||
diff_filespec_is_binary(o->repo, two)) {
data->is_binary = 1;
- if (same_contents) {
+ if (!may_differ) {
data->added = 0;
data->deleted = 0;
} else {
@@ -3694,7 +3708,7 @@ static void builtin_diffstat(const char *name_a, const char *name_b,
data->added = count_lines(two->data, two->size);
}
- else if (!same_contents) {
+ else if (may_differ) {
/* Crazy xdl interfaces.. */
xpparam_t xpp;
xdemitconf_t xecfg;
@@ -3706,6 +3720,8 @@ static void builtin_diffstat(const char *name_a, const char *name_b,
memset(&xpp, 0, sizeof(xpp));
memset(&xecfg, 0, sizeof(xecfg));
xpp.flags = o->xdl_opts;
+ xpp.ignore_regex = o->ignore_regex;
+ xpp.ignore_regex_nr = o->ignore_regex_nr;
xpp.anchors = o->anchors;
xpp.anchors_nr = o->anchors_nr;
xecfg.ctxlen = o->context;
@@ -3713,6 +3729,27 @@ static void builtin_diffstat(const char *name_a, const char *name_b,
if (xdi_diff_outf(&mf1, &mf2, discard_hunk_line,
diffstat_consume, diffstat, &xpp, &xecfg))
die("unable to generate diffstat for %s", one->path);
+
+ if (DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two)) {
+ struct diffstat_file *file =
+ diffstat->files[diffstat->nr - 1];
+ /*
+ * Omit diffstats of modified files where nothing changed.
+ * Even if may_differ, this might be the case due to
+ * ignoring whitespace changes, etc.
+ *
+ * But note that we special-case additions, deletions,
+ * renames, and mode changes as adding an empty file,
+ * for example is still of interest.
+ */
+ if ((p->status == DIFF_STATUS_MODIFIED)
+ && !file->added
+ && !file->deleted
+ && one->mode == two->mode) {
+ free_diffstat_file(file);
+ diffstat->nr--;
+ }
+ }
}
diff_free_filespec_data(one);
@@ -4080,6 +4117,9 @@ void diff_free_filespec_blob(struct diff_filespec *s)
void diff_free_filespec_data(struct diff_filespec *s)
{
+ if (!s)
+ return;
+
diff_free_filespec_blob(s);
FREE_AND_NULL(s->cnt_data);
}
@@ -4154,7 +4194,7 @@ static struct diff_tempfile *prepare_temp_file(struct repository *r,
die_errno("readlink(%s)", name);
prep_temp_blob(r->index, name, temp, sb.buf, sb.len,
(one->oid_valid ?
- &one->oid : &null_oid),
+ &one->oid : null_oid()),
(one->oid_valid ?
one->mode : S_IFLNK));
strbuf_release(&sb);
@@ -4163,7 +4203,7 @@ static struct diff_tempfile *prepare_temp_file(struct repository *r,
/* we can borrow from the file in the work tree */
temp->name = name;
if (!one->oid_valid)
- oid_to_hex_r(temp->hex, &null_oid);
+ oid_to_hex_r(temp->hex, null_oid());
else
oid_to_hex_r(temp->hex, &one->oid);
/* Even though we may sometimes borrow the
@@ -4187,14 +4227,14 @@ static struct diff_tempfile *prepare_temp_file(struct repository *r,
}
static void add_external_diff_name(struct repository *r,
- struct argv_array *argv,
+ struct strvec *argv,
const char *name,
struct diff_filespec *df)
{
struct diff_tempfile *temp = prepare_temp_file(r, name, df);
- argv_array_push(argv, temp->name);
- argv_array_push(argv, temp->hex);
- argv_array_push(argv, temp->mode);
+ strvec_push(argv, temp->name);
+ strvec_push(argv, temp->hex);
+ strvec_push(argv, temp->mode);
}
/* An external diff command takes:
@@ -4211,12 +4251,12 @@ static void run_external_diff(const char *pgm,
const char *xfrm_msg,
struct diff_options *o)
{
- struct argv_array argv = ARGV_ARRAY_INIT;
- struct argv_array env = ARGV_ARRAY_INIT;
+ struct strvec argv = STRVEC_INIT;
+ struct strvec env = STRVEC_INIT;
struct diff_queue_struct *q = &diff_queued_diff;
- argv_array_push(&argv, pgm);
- argv_array_push(&argv, name);
+ strvec_push(&argv, pgm);
+ strvec_push(&argv, name);
if (one && two) {
add_external_diff_name(o->repo, &argv, name, one);
@@ -4224,22 +4264,22 @@ static void run_external_diff(const char *pgm,
add_external_diff_name(o->repo, &argv, name, two);
else {
add_external_diff_name(o->repo, &argv, other, two);
- argv_array_push(&argv, other);
- argv_array_push(&argv, xfrm_msg);
+ strvec_push(&argv, other);
+ strvec_push(&argv, xfrm_msg);
}
}
- argv_array_pushf(&env, "GIT_DIFF_PATH_COUNTER=%d", ++o->diff_path_counter);
- argv_array_pushf(&env, "GIT_DIFF_PATH_TOTAL=%d", q->nr);
+ strvec_pushf(&env, "GIT_DIFF_PATH_COUNTER=%d", ++o->diff_path_counter);
+ strvec_pushf(&env, "GIT_DIFF_PATH_TOTAL=%d", q->nr);
diff_free_filespec_data(one);
diff_free_filespec_data(two);
- if (run_command_v_opt_cd_env(argv.argv, RUN_USING_SHELL, NULL, env.argv))
+ if (run_command_v_opt_cd_env(argv.v, RUN_USING_SHELL, NULL, env.v))
die(_("external diff died, stopping at %s"), name);
remove_tempfile();
- argv_array_clear(&argv);
- argv_array_clear(&env);
+ strvec_clear(&argv);
+ strvec_clear(&env);
}
static int similarity_index(struct diff_filepair *p)
@@ -4314,7 +4354,10 @@ static void fill_metainfo(struct strbuf *msg,
}
if (one && two && !oideq(&one->oid, &two->oid)) {
const unsigned hexsz = the_hash_algo->hexsz;
- int abbrev = o->flags.full_index ? hexsz : DEFAULT_ABBREV;
+ int abbrev = o->abbrev ? o->abbrev : DEFAULT_ABBREV;
+
+ if (o->flags.full_index)
+ abbrev = hexsz;
if (o->flags.binary) {
mmfile_t mf;
@@ -4538,6 +4581,7 @@ void repo_diff_setup(struct repository *r, struct diff_options *options)
options->interhunkcontext = diff_interhunk_context_default;
options->ws_error_highlight = ws_error_highlight_default;
options->flags.rename_empty = 1;
+ options->flags.relative_name = diff_relative;
options->objfind = NULL;
/* pathchange left =NULL by default */
@@ -4551,6 +4595,9 @@ void repo_diff_setup(struct repository *r, struct diff_options *options)
options->orderfile = diff_order_file_cfg;
+ if (!options->flags.ignore_submodule_set)
+ options->flags.ignore_untracked_in_submodules = 1;
+
if (diff_no_prefix) {
options->a_prefix = options->b_prefix = "";
} else if (!diff_mnemonic_prefix) {
@@ -4592,7 +4639,8 @@ void diff_setup_done(struct diff_options *options)
* inside contents.
*/
- if ((options->xdl_opts & XDF_WHITESPACE_FLAGS))
+ if ((options->xdl_opts & XDF_WHITESPACE_FLAGS) ||
+ options->ignore_regex_nr)
options->flags.diff_from_contents = 1;
else
options->flags.diff_from_contents = 0;
@@ -4872,7 +4920,7 @@ static int diff_opt_find_object(const struct option *option,
return error(_("unable to resolve '%s'"), arg);
if (!opt->objfind)
- opt->objfind = xcalloc(1, sizeof(*opt->objfind));
+ CALLOC_ARRAY(opt->objfind, 1);
opt->pickaxe_opts |= DIFF_PICKAXE_KIND_OBJFIND;
opt->flags.recursive = 1;
@@ -5168,6 +5216,22 @@ static int diff_opt_patience(const struct option *opt,
return 0;
}
+static int diff_opt_ignore_regex(const struct option *opt,
+ const char *arg, int unset)
+{
+ struct diff_options *options = opt->value;
+ regex_t *regex;
+
+ BUG_ON_OPT_NEG(unset);
+ regex = xmalloc(sizeof(*regex));
+ if (regcomp(regex, arg, REG_EXTENDED | REG_NEWLINE))
+ return error(_("invalid regex given to -I: '%s'"), arg);
+ ALLOC_GROW(options->ignore_regex, options->ignore_regex_nr + 1,
+ options->ignore_regex_alloc);
+ options->ignore_regex[options->ignore_regex_nr++] = regex;
+ return 0;
+}
+
static int diff_opt_pickaxe_regex(const struct option *opt,
const char *arg, int unset)
{
@@ -5195,8 +5259,7 @@ static int diff_opt_relative(const struct option *opt,
{
struct diff_options *options = opt->value;
- BUG_ON_OPT_NEG(unset);
- options->flags.relative_name = 1;
+ options->flags.relative_name = !unset;
if (arg)
options->prefix = arg;
return 0;
@@ -5287,6 +5350,19 @@ static int diff_opt_word_diff_regex(const struct option *opt,
return 0;
}
+static int diff_opt_rotate_to(const struct option *opt, const char *arg, int unset)
+{
+ struct diff_options *options = opt->value;
+
+ BUG_ON_OPT_NEG(unset);
+ if (!strcmp(opt->long_name, "skip-to"))
+ options->skip_instead_of_rotate = 1;
+ else
+ options->skip_instead_of_rotate = 0;
+ options->rotate_to = arg;
+ return 0;
+}
+
static void prep_parse_options(struct diff_options *options)
{
struct option parseopts[] = {
@@ -5457,6 +5533,9 @@ static void prep_parse_options(struct diff_options *options)
OPT_BIT_F(0, "ignore-blank-lines", &options->xdl_opts,
N_("ignore changes whose lines are all blank"),
XDF_IGNORE_BLANK_LINES, PARSE_OPT_NONEG),
+ OPT_CALLBACK_F('I', "ignore-matching-lines", options, N_("<regex>"),
+ N_("ignore changes whose all lines match <regex>"),
+ 0, diff_opt_ignore_regex),
OPT_BIT(0, "indent-heuristic", &options->xdl_opts,
N_("heuristic to shift diff hunk boundaries for easy reading"),
XDF_INDENT_HEURISTIC),
@@ -5492,7 +5571,7 @@ static void prep_parse_options(struct diff_options *options)
OPT_GROUP(N_("Other diff options")),
OPT_CALLBACK_F(0, "relative", options, N_("<prefix>"),
N_("when run from subdir, exclude changes outside and show relative paths"),
- PARSE_OPT_NONEG | PARSE_OPT_OPTARG,
+ PARSE_OPT_OPTARG,
diff_opt_relative),
OPT_BOOL('a', "text", &options->flags.text,
N_("treat all files as text")),
@@ -5535,6 +5614,12 @@ static void prep_parse_options(struct diff_options *options)
DIFF_PICKAXE_REGEX, PARSE_OPT_NONEG),
OPT_FILENAME('O', NULL, &options->orderfile,
N_("control the order in which files appear in the output")),
+ OPT_CALLBACK_F(0, "rotate-to", options, N_("<path>"),
+ N_("show the change in the specified path first"),
+ PARSE_OPT_NONEG, diff_opt_rotate_to),
+ OPT_CALLBACK_F(0, "skip-to", options, N_("<path>"),
+ N_("skip the output to the specified path"),
+ PARSE_OPT_NONEG, diff_opt_rotate_to),
OPT_CALLBACK_F(0, "find-object", options, N_("<object-id>"),
N_("look for differences that change the number of occurrences of the specified object"),
PARSE_OPT_NONEG, diff_opt_find_object),
@@ -6039,6 +6124,8 @@ static void patch_id_consume(void *priv, char *line, unsigned long len)
struct patch_id_t *data = priv;
int new_len;
+ if (len > 12 && starts_with(line, "\\ "))
+ return;
new_len = remove_space(line, len);
the_hash_algo->update_fn(data->ctx, line, new_len);
@@ -6151,7 +6238,7 @@ static int diff_get_patch_id(struct diff_options *options, struct object_id *oid
}
if (!stable)
- the_hash_algo->final_fn(oid->hash, &ctx);
+ the_hash_algo->final_oid_fn(oid, &ctx);
return 0;
}
@@ -6253,9 +6340,9 @@ static void diff_flush_patch_all_file_pairs(struct diff_options *o)
if (o->color_moved == COLOR_MOVED_ZEBRA_DIM)
dim_moved_lines(o);
- hashmap_free_entries(&add_lines, struct moved_entry,
+ hashmap_clear_and_free(&add_lines, struct moved_entry,
ent);
- hashmap_free_entries(&del_lines, struct moved_entry,
+ hashmap_clear_and_free(&del_lines, struct moved_entry,
ent);
}
@@ -6270,6 +6357,32 @@ static void diff_flush_patch_all_file_pairs(struct diff_options *o)
}
}
+static void diff_free_file(struct diff_options *options)
+{
+ if (options->close_file)
+ fclose(options->file);
+}
+
+static void diff_free_ignore_regex(struct diff_options *options)
+{
+ int i;
+
+ for (i = 0; i < options->ignore_regex_nr; i++) {
+ regfree(options->ignore_regex[i]);
+ free(options->ignore_regex[i]);
+ }
+ free(options->ignore_regex);
+}
+
+void diff_free(struct diff_options *options)
+{
+ if (options->no_free)
+ return;
+
+ diff_free_file(options);
+ diff_free_ignore_regex(options);
+}
+
void diff_flush(struct diff_options *options)
{
struct diff_queue_struct *q = &diff_queued_diff;
@@ -6333,8 +6446,7 @@ void diff_flush(struct diff_options *options)
* options->file to /dev/null should be safe, because we
* aren't supposed to produce any output anyway.
*/
- if (options->close_file)
- fclose(options->file);
+ diff_free_file(options);
options->file = xfopen("/dev/null", "w");
options->close_file = 1;
options->color_moved = 0;
@@ -6367,8 +6479,7 @@ void diff_flush(struct diff_options *options)
free_queue:
free(q->queue);
DIFF_QUEUE_CLEAR(q);
- if (options->close_file)
- fclose(options->file);
+ diff_free(options);
/*
* Report the content-level differences with HAS_CHANGES;
@@ -6603,6 +6714,8 @@ void diffcore_std(struct diff_options *options)
diffcore_pickaxe(options);
if (options->orderfile)
diffcore_order(options->orderfile);
+ if (options->rotate_to)
+ diffcore_rotate(options);
if (!options->found_follow)
/* See try_to_follow_renames() in tree-diff.c */
diff_resolve_rename_copy();
@@ -6758,8 +6871,11 @@ void diff_change(struct diff_options *options,
return;
if (options->flags.quick && options->skip_stat_unmatch &&
- !diff_filespec_check_stat_unmatch(options->repo, p))
+ !diff_filespec_check_stat_unmatch(options->repo, p)) {
+ diff_free_filespec_data(p->one);
+ diff_free_filespec_data(p->two);
return;
+ }
options->flags.has_changes = 1;
}