Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.kernel.org/pub/scm/git/git.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'range-diff.c')
-rw-r--r--range-diff.c321
1 files changed, 219 insertions, 102 deletions
diff --git a/range-diff.c b/range-diff.c
index 48b0e1b4ce..e9479794b4 100644
--- a/range-diff.c
+++ b/range-diff.c
@@ -2,7 +2,7 @@
#include "range-diff.h"
#include "string-list.h"
#include "run-command.h"
-#include "argv-array.h"
+#include "strvec.h"
#include "hashmap.h"
#include "xdiff-interface.h"
#include "linear-assignment.h"
@@ -10,6 +10,8 @@
#include "commit.h"
#include "pretty.h"
#include "userdiff.h"
+#include "apply.h"
+#include "revision.h"
struct patch_util {
/* For the search for an exact match */
@@ -24,60 +26,84 @@ struct patch_util {
struct object_id oid;
};
+static size_t find_end_of_line(char *buffer, unsigned long size)
+{
+ char *eol = memchr(buffer, '\n', size);
+
+ if (!eol)
+ return size;
+
+ *eol = '\0';
+ return eol + 1 - buffer;
+}
+
/*
* Reads the patches into a string list, with the `util` field being populated
* as struct object_id (will need to be free()d).
*/
-static int read_patches(const char *range, struct string_list *list)
+static int read_patches(const char *range, struct string_list *list,
+ const struct strvec *other_arg)
{
struct child_process cp = CHILD_PROCESS_INIT;
- FILE *in;
- struct strbuf buf = STRBUF_INIT, line = STRBUF_INIT;
+ struct strbuf buf = STRBUF_INIT, contents = STRBUF_INIT;
struct patch_util *util = NULL;
int in_header = 1;
-
- argv_array_pushl(&cp.args, "log", "--no-color", "-p", "--no-merges",
- "--reverse", "--date-order", "--decorate=no",
- /*
- * Choose indicators that are not used anywhere
- * else in diffs, but still look reasonable
- * (e.g. will not be confusing when debugging)
- */
- "--output-indicator-new=>",
- "--output-indicator-old=<",
- "--output-indicator-context=#",
- "--no-abbrev-commit", range,
- NULL);
+ char *line, *current_filename = NULL;
+ int offset, len;
+ size_t size;
+
+ strvec_pushl(&cp.args, "log", "--no-color", "-p", "--no-merges",
+ "--reverse", "--date-order", "--decorate=no",
+ "--no-prefix",
+ /*
+ * Choose indicators that are not used anywhere
+ * else in diffs, but still look reasonable
+ * (e.g. will not be confusing when debugging)
+ */
+ "--output-indicator-new=>",
+ "--output-indicator-old=<",
+ "--output-indicator-context=#",
+ "--no-abbrev-commit",
+ "--pretty=medium",
+ "--notes",
+ NULL);
+ if (other_arg)
+ strvec_pushv(&cp.args, other_arg->v);
+ strvec_push(&cp.args, range);
cp.out = -1;
cp.no_stdin = 1;
cp.git_cmd = 1;
if (start_command(&cp))
return error_errno(_("could not start `log`"));
- in = fdopen(cp.out, "r");
- if (!in) {
+ if (strbuf_read(&contents, cp.out, 0) < 0) {
error_errno(_("could not read `log` output"));
finish_command(&cp);
return -1;
}
+ if (finish_command(&cp))
+ return -1;
- while (strbuf_getline(&line, in) != EOF) {
+ line = contents.buf;
+ size = contents.len;
+ for (offset = 0; size > 0; offset += len, size -= len, line += len) {
const char *p;
- if (skip_prefix(line.buf, "commit ", &p)) {
+ len = find_end_of_line(line, size);
+ line[len - 1] = '\0';
+ if (skip_prefix(line, "commit ", &p)) {
if (util) {
string_list_append(list, buf.buf)->util = util;
strbuf_reset(&buf);
}
- util = xcalloc(sizeof(*util), 1);
+ CALLOC_ARRAY(util, 1);
if (get_oid(p, &util->oid)) {
error(_("could not parse commit '%s'"), p);
free(util);
+ free(current_filename);
string_list_clear(list, 1);
strbuf_release(&buf);
- strbuf_release(&line);
- fclose(in);
- finish_command(&cp);
+ strbuf_release(&contents);
return -1;
}
util->matching = -1;
@@ -85,81 +111,137 @@ static int read_patches(const char *range, struct string_list *list)
continue;
}
- if (starts_with(line.buf, "diff --git")) {
+ if (!util) {
+ error(_("could not parse first line of `log` output: "
+ "did not start with 'commit ': '%s'"),
+ line);
+ free(current_filename);
+ string_list_clear(list, 1);
+ strbuf_release(&buf);
+ strbuf_release(&contents);
+ return -1;
+ }
+
+ if (starts_with(line, "diff --git")) {
+ struct patch patch = { 0 };
+ struct strbuf root = STRBUF_INIT;
+ int linenr = 0;
+ int orig_len;
+
in_header = 0;
strbuf_addch(&buf, '\n');
if (!util->diff_offset)
util->diff_offset = buf.len;
- strbuf_addch(&buf, ' ');
- strbuf_addbuf(&buf, &line);
+ line[len - 1] = '\n';
+ orig_len = len;
+ len = parse_git_diff_header(&root, &linenr, 0, line,
+ len, size, &patch);
+ if (len < 0) {
+ error(_("could not parse git header '%.*s'"),
+ orig_len, line);
+ free(util);
+ free(current_filename);
+ string_list_clear(list, 1);
+ strbuf_release(&buf);
+ strbuf_release(&contents);
+ return -1;
+ }
+ strbuf_addstr(&buf, " ## ");
+ if (patch.is_new > 0)
+ strbuf_addf(&buf, "%s (new)", patch.new_name);
+ else if (patch.is_delete > 0)
+ strbuf_addf(&buf, "%s (deleted)", patch.old_name);
+ else if (patch.is_rename)
+ strbuf_addf(&buf, "%s => %s", patch.old_name, patch.new_name);
+ else
+ strbuf_addstr(&buf, patch.new_name);
+
+ free(current_filename);
+ if (patch.is_delete > 0)
+ current_filename = xstrdup(patch.old_name);
+ else
+ current_filename = xstrdup(patch.new_name);
+
+ if (patch.new_mode && patch.old_mode &&
+ patch.old_mode != patch.new_mode)
+ strbuf_addf(&buf, " (mode change %06o => %06o)",
+ patch.old_mode, patch.new_mode);
+
+ strbuf_addstr(&buf, " ##");
} else if (in_header) {
- if (starts_with(line.buf, "Author: ")) {
- strbuf_addbuf(&buf, &line);
+ if (starts_with(line, "Author: ")) {
+ strbuf_addstr(&buf, " ## Metadata ##\n");
+ strbuf_addstr(&buf, line);
strbuf_addstr(&buf, "\n\n");
- } else if (starts_with(line.buf, " ")) {
- strbuf_rtrim(&line);
- strbuf_addbuf(&buf, &line);
+ strbuf_addstr(&buf, " ## Commit message ##\n");
+ } else if (starts_with(line, "Notes") &&
+ line[strlen(line) - 1] == ':') {
+ strbuf_addstr(&buf, "\n\n");
+ /* strip the trailing colon */
+ strbuf_addf(&buf, " ## %.*s ##\n",
+ (int)(strlen(line) - 1), line);
+ } else if (starts_with(line, " ")) {
+ p = line + len - 2;
+ while (isspace(*p) && p >= line)
+ p--;
+ strbuf_add(&buf, line, p - line + 1);
strbuf_addch(&buf, '\n');
}
continue;
- } else if (starts_with(line.buf, "@@ "))
+ } else if (skip_prefix(line, "@@ ", &p)) {
+ p = strstr(p, "@@");
strbuf_addstr(&buf, "@@");
- else if (!line.buf[0] || starts_with(line.buf, "index "))
+ if (current_filename && p[2])
+ strbuf_addf(&buf, " %s:", current_filename);
+ if (p)
+ strbuf_addstr(&buf, p + 2);
+ } else if (!line[0])
/*
* A completely blank (not ' \n', which is context)
* line is not valid in a diff. We skip it
* silently, because this neatly handles the blank
* separator line between commits in git-log
* output.
- *
- * We also want to ignore the diff's `index` lines
- * because they contain exact blob hashes in which
- * we are not interested.
*/
continue;
- else if (line.buf[0] == '>') {
+ else if (line[0] == '>') {
strbuf_addch(&buf, '+');
- strbuf_add(&buf, line.buf + 1, line.len - 1);
- } else if (line.buf[0] == '<') {
+ strbuf_addstr(&buf, line + 1);
+ } else if (line[0] == '<') {
strbuf_addch(&buf, '-');
- strbuf_add(&buf, line.buf + 1, line.len - 1);
- } else if (line.buf[0] == '#') {
+ strbuf_addstr(&buf, line + 1);
+ } else if (line[0] == '#') {
strbuf_addch(&buf, ' ');
- strbuf_add(&buf, line.buf + 1, line.len - 1);
+ strbuf_addstr(&buf, line + 1);
} else {
strbuf_addch(&buf, ' ');
- strbuf_addbuf(&buf, &line);
+ strbuf_addstr(&buf, line);
}
strbuf_addch(&buf, '\n');
util->diffsize++;
}
- fclose(in);
- strbuf_release(&line);
+ strbuf_release(&contents);
if (util)
string_list_append(list, buf.buf)->util = util;
strbuf_release(&buf);
-
- if (finish_command(&cp))
- return -1;
+ free(current_filename);
return 0;
}
static int patch_util_cmp(const void *dummy, const struct patch_util *a,
- const struct patch_util *b, const char *keydata)
+ const struct patch_util *b, const char *keydata)
{
return strcmp(a->diff, keydata ? keydata : b->diff);
}
static void find_exact_matches(struct string_list *a, struct string_list *b)
{
- struct hashmap map;
+ struct hashmap map = HASHMAP_INIT((hashmap_cmp_fn)patch_util_cmp, NULL);
int i;
- hashmap_init(&map, (hashmap_cmp_fn)patch_util_cmp, NULL, 0);
-
/* First, add the patches of a to a hash map */
for (i = 0; i < a->nr; i++) {
struct patch_util *util = a->items[i].util;
@@ -167,8 +249,8 @@ static void find_exact_matches(struct string_list *a, struct string_list *b)
util->i = i;
util->patch = a->items[i].string;
util->diff = util->patch + util->diff_offset;
- hashmap_entry_init(util, strhash(util->diff));
- hashmap_add(&map, util);
+ hashmap_entry_init(&util->e, strhash(util->diff));
+ hashmap_add(&map, &util->e);
}
/* Now try to find exact matches in b */
@@ -178,8 +260,8 @@ static void find_exact_matches(struct string_list *a, struct string_list *b)
util->i = i;
util->patch = b->items[i].string;
util->diff = util->patch + util->diff_offset;
- hashmap_entry_init(util, strhash(util->diff));
- other = hashmap_remove(&map, util, NULL);
+ hashmap_entry_init(&util->e, strhash(util->diff));
+ other = hashmap_remove_entry(&map, util, e, NULL);
if (other) {
if (other->matching >= 0)
BUG("already assigned!");
@@ -189,12 +271,13 @@ static void find_exact_matches(struct string_list *a, struct string_list *b)
}
}
- hashmap_free(&map, 0);
+ hashmap_clear(&map);
}
-static void diffsize_consume(void *data, char *line, unsigned long len)
+static int diffsize_consume(void *data, char *line, unsigned long len)
{
(*(int *)data)++;
+ return 0;
}
static void diffsize_hunk(void *data, long ob, long on, long nb, long nn,
@@ -354,26 +437,27 @@ static void output_pair_header(struct diff_options *diffopt,
fwrite(buf->buf, buf->len, 1, diffopt->file);
}
-static struct userdiff_driver no_func_name = {
- .funcname = { "$^", 0 }
+static struct userdiff_driver section_headers = {
+ .funcname = { "^ ## (.*) ##$\n"
+ "^.?@@ (.*)$", REG_EXTENDED }
};
static struct diff_filespec *get_filespec(const char *name, const char *p)
{
struct diff_filespec *spec = alloc_filespec(name);
- fill_filespec(spec, &null_oid, 0, 0100644);
+ fill_filespec(spec, null_oid(), 0, 0100644);
spec->data = (char *)p;
spec->size = strlen(p);
spec->should_munmap = 0;
spec->is_stdin = 1;
- spec->driver = &no_func_name;
+ spec->driver = &section_headers;
return spec;
}
static void patch_diff(const char *a, const char *b,
- struct diff_options *diffopt)
+ struct diff_options *diffopt)
{
diff_queue(&diff_queued_diff,
get_filespec("a", a), get_filespec("b", b));
@@ -382,12 +466,35 @@ static void patch_diff(const char *a, const char *b,
diff_flush(diffopt);
}
+static struct strbuf *output_prefix_cb(struct diff_options *opt, void *data)
+{
+ return data;
+}
+
static void output(struct string_list *a, struct string_list *b,
- struct diff_options *diffopt)
+ struct range_diff_options *range_diff_opts)
{
struct strbuf buf = STRBUF_INIT, dashes = STRBUF_INIT;
int patch_no_width = decimal_width(1 + (a->nr > b->nr ? a->nr : b->nr));
int i = 0, j = 0;
+ struct diff_options opts;
+ struct strbuf indent = STRBUF_INIT;
+
+ if (range_diff_opts->diffopt)
+ memcpy(&opts, range_diff_opts->diffopt, sizeof(opts));
+ else
+ diff_setup(&opts);
+
+ if (!opts.output_format)
+ opts.output_format = DIFF_FORMAT_PATCH;
+ opts.flags.suppress_diff_headers = 1;
+ opts.flags.dual_color_diffed_diffs =
+ range_diff_opts->dual_color;
+ opts.flags.suppress_hunk_header_line_count = 1;
+ opts.output_prefix = output_prefix_cb;
+ strbuf_addstr(&indent, " ");
+ opts.output_prefix_data = &indent;
+ diff_setup_done(&opts);
/*
* We assume the user is really more interested in the second argument
@@ -408,7 +515,8 @@ static void output(struct string_list *a, struct string_list *b,
/* Show unmatched LHS commit whose predecessors were shown. */
if (i < a->nr && a_util->matching < 0) {
- output_pair_header(diffopt, patch_no_width,
+ if (!range_diff_opts->right_only)
+ output_pair_header(&opts, patch_no_width,
&buf, &dashes, a_util, NULL);
i++;
continue;
@@ -416,7 +524,8 @@ static void output(struct string_list *a, struct string_list *b,
/* Show unmatched RHS commits. */
while (j < b->nr && b_util->matching < 0) {
- output_pair_header(diffopt, patch_no_width,
+ if (!range_diff_opts->left_only)
+ output_pair_header(&opts, patch_no_width,
&buf, &dashes, NULL, b_util);
b_util = ++j < b->nr ? b->items[j].util : NULL;
}
@@ -424,61 +533,41 @@ static void output(struct string_list *a, struct string_list *b,
/* Show matching LHS/RHS pair. */
if (j < b->nr) {
a_util = a->items[b_util->matching].util;
- output_pair_header(diffopt, patch_no_width,
+ output_pair_header(&opts, patch_no_width,
&buf, &dashes, a_util, b_util);
- if (!(diffopt->output_format & DIFF_FORMAT_NO_OUTPUT))
+ if (!(opts.output_format & DIFF_FORMAT_NO_OUTPUT))
patch_diff(a->items[b_util->matching].string,
- b->items[j].string, diffopt);
+ b->items[j].string, &opts);
a_util->shown = 1;
j++;
}
}
strbuf_release(&buf);
strbuf_release(&dashes);
-}
-
-static struct strbuf *output_prefix_cb(struct diff_options *opt, void *data)
-{
- return data;
+ strbuf_release(&indent);
}
int show_range_diff(const char *range1, const char *range2,
- int creation_factor, int dual_color,
- struct diff_options *diffopt)
+ struct range_diff_options *range_diff_opts)
{
int res = 0;
struct string_list branch1 = STRING_LIST_INIT_DUP;
struct string_list branch2 = STRING_LIST_INIT_DUP;
- if (read_patches(range1, &branch1))
+ if (range_diff_opts->left_only && range_diff_opts->right_only)
+ res = error(_("--left-only and --right-only are mutually exclusive"));
+
+ if (!res && read_patches(range1, &branch1, range_diff_opts->other_arg))
res = error(_("could not parse log for '%s'"), range1);
- if (!res && read_patches(range2, &branch2))
+ if (!res && read_patches(range2, &branch2, range_diff_opts->other_arg))
res = error(_("could not parse log for '%s'"), range2);
if (!res) {
- struct diff_options opts;
- struct strbuf indent = STRBUF_INIT;
-
- if (diffopt)
- memcpy(&opts, diffopt, sizeof(opts));
- else
- diff_setup(&opts);
-
- if (!opts.output_format)
- opts.output_format = DIFF_FORMAT_PATCH;
- opts.flags.suppress_diff_headers = 1;
- opts.flags.dual_color_diffed_diffs = dual_color;
- opts.output_prefix = output_prefix_cb;
- strbuf_addstr(&indent, " ");
- opts.output_prefix_data = &indent;
- diff_setup_done(&opts);
-
find_exact_matches(&branch1, &branch2);
- get_correspondences(&branch1, &branch2, creation_factor);
- output(&branch1, &branch2, &opts);
-
- strbuf_release(&indent);
+ get_correspondences(&branch1, &branch2,
+ range_diff_opts->creation_factor);
+ output(&branch1, &branch2, range_diff_opts);
}
string_list_clear(&branch1, 1);
@@ -486,3 +575,31 @@ int show_range_diff(const char *range1, const char *range2,
return res;
}
+
+int is_range_diff_range(const char *arg)
+{
+ char *copy = xstrdup(arg); /* setup_revisions() modifies it */
+ const char *argv[] = { "", copy, "--", NULL };
+ int i, positive = 0, negative = 0;
+ struct rev_info revs;
+
+ init_revisions(&revs, NULL);
+ if (setup_revisions(3, argv, &revs, NULL) == 1) {
+ for (i = 0; i < revs.pending.nr; i++)
+ if (revs.pending.objects[i].item->flags & UNINTERESTING)
+ negative++;
+ else
+ positive++;
+ for (i = 0; i < revs.pending.nr; i++) {
+ struct object *obj = revs.pending.objects[i].item;
+
+ if (obj->type == OBJ_COMMIT)
+ clear_commit_marks((struct commit *)obj,
+ ALL_REV_FLAGS);
+ }
+ }
+
+ free(copy);
+ object_array_clear(&revs.pending);
+ return negative > 0 && positive > 0;
+}