diff --git a/Documentation/line-range-options.adoc b/Documentation/line-range-options.adoc index 72f639b5e79ea4..744582185706eb 100644 --- a/Documentation/line-range-options.adoc +++ b/Documentation/line-range-options.adoc @@ -9,10 +9,14 @@ __ and __ (or __) must exist in the starting revision. You can specify this option more than once. Implies `--patch`. Patch output can be suppressed using `--no-patch`. - Non-patch diff formats `--raw`, `--name-only`, `--name-status`, - and `--summary` are supported. Diff stat formats - (`--stat`, `--numstat`, `--shortstat`, `--dirstat`) are not - currently implemented. + Other diff formats are supported, including `--raw`, + `--name-only`, `--name-status`, `--summary`, `--check`, + `--stat`, `--numstat`, and `--shortstat`. + The stat formats show range-scoped counts: only lines within + the tracked range are counted. `--dirstat` is not supported: + by default it measures whole-file change rather than the + line-level diff, so it cannot be confined to the tracked range. + Use `--numstat` for exact per-file counts within the range. + Patch formatting options such as `--word-diff`, `--color-moved`, `--no-prefix`, and whitespace options (`-w`, `-b`) are supported, diff --git a/diff.c b/diff.c index 5a584fa1d569e7..a81b35f53bb730 100644 --- a/diff.c +++ b/diff.c @@ -610,28 +610,64 @@ struct emit_callback { }; /* - * State for the line-range callback wrappers that sit between - * xdi_diff_outf() and fn_out_consume(). xdiff produces a normal, - * unfiltered diff; the wrappers intercept each hunk header and line, - * track post-image position, and forward only lines that fall within - * the requested ranges. Contiguous in-range lines are collected into - * range hunks and flushed with a synthetic @@ header so that - * fn_out_consume() sees well-formed unified-diff fragments. + * Line-range filter: scopes "git log -L" output to the tracked ranges. * - * Removal lines ('-') cannot be classified by post-image position, so - * they are buffered in pending_rm until the next '+' or ' ' line - * reveals whether they precede an in-range line (flush into range hunk) or - * an out-of-range line (discard). + * It sits between xdi_diff_outf() and an output callback (fn_out_consume, + * diffstat_consume, checkdiff_consume). xdiff produces a normal diff; the + * filter forwards only the lines inside the requested ranges, collecting + * contiguous in-range lines into a "range hunk" emitted with a synthetic + * @@ header so the callback sees well-formed unified-diff fragments. + * + * A diff turns a pre-image (old file) into a post-image (new file). Each + * line is context (' ', in both), a removal ('-', pre-image only), or an + * addition ('+', post-image only). -L tracks ranges in the post-image, so + * a line is in range by its post-image position. + * + * Two 1-based cursors track the next line in each image, seeded from the + * xdiff hunk header: + * + * new_lineno advances on '+' and ' ' (lines present in the new file) + * old_lineno advances on '-' and ' ' (lines present in the old file) + * + * Ranges are 0-based half-open [start, end), so a line is tested at the + * 0-based index new_idx = new_lineno - 1. + * + * A '-' occupies no new-file line; it sits between two. Because it does + * not advance new_lineno, it is classified at the new_idx that the + * following '+'/' ' will occupy, and xdiff emits removals before additions + * within a change, so that index is already known. + * + * Example, tracking post-image line 2 (range [1, 2)) of: + * + * old new + * 1 a 1 a + * 2 b 2 X (b -> X) + * 3 c 3 c + * + * classify each line by new_idx (cursors shown before they advance): + * ' a' old_lineno 1 new_lineno 1 new_idx 0 -> skip + * '-b' old_lineno 2 new_lineno 2 new_idx 1 -> keep (removal) + * '+X' old_lineno 3 new_lineno 2 new_idx 1 -> keep (addition) + * ' c' old_lineno 3 new_lineno 3 new_idx 2 -> past end, flush + * + * -b and +X share new_idx = 1 because -b did not advance new_lineno; both land + * in the range hunk, which is flushed when ' c' crosses end. */ -struct line_range_callback { +struct line_range_filter { xdiff_emit_line_fn orig_line_fn; + /* + * Optional; consumers that report file line numbers (e.g. + * checkdiff) need the synthetic hunk header to set their + * post-image position before in-range lines are replayed. + */ + xdiff_emit_hunk_fn orig_hunk_fn; void *orig_cb_data; const struct range_set *ranges; /* 0-based [start, end) */ unsigned int cur_range; /* index into the range_set */ /* Post/pre-image line counters (1-based, set from hunk headers) */ - long lno_post; - long lno_pre; + long new_lineno; + long old_lineno; /* * Function name from most recent xdiff hunk header; @@ -640,17 +676,14 @@ struct line_range_callback { char func[80]; long funclen; - /* Range hunk being accumulated for the current range */ - struct strbuf rhunk; - long rhunk_old_begin, rhunk_old_count; - long rhunk_new_begin, rhunk_new_count; - int rhunk_active; - int rhunk_has_changes; /* any '+' or '-' lines? */ - - /* Removal lines not yet known to be in-range */ - struct strbuf pending_rm; - int pending_rm_count; - long pending_rm_pre_begin; /* pre-image line of first pending */ + /* The range hunk being accumulated for the current range. */ + struct { + struct strbuf lines; /* buffered in-range diff lines */ + long old_begin, old_count; + long new_begin, new_count; + int active; + int has_changes; /* any '+' or '-' line? */ + } hunk; int ret; /* latched error from orig_line_fn */ }; @@ -2540,50 +2573,100 @@ static int quick_consume(void *priv, char *line UNUSED, unsigned long len UNUSED return 1; } -static void discard_pending_rm(struct line_range_callback *s) +static void line_range_filter_init(struct line_range_filter *filter, + const struct range_set *ranges, + xdiff_emit_line_fn line_fn, + void *cb_data) { - strbuf_reset(&s->pending_rm); - s->pending_rm_count = 0; + memset(filter, 0, sizeof(*filter)); + filter->orig_line_fn = line_fn; + filter->orig_cb_data = cb_data; + filter->ranges = ranges; + strbuf_init(&filter->hunk.lines, 0); } -static void flush_rhunk(struct line_range_callback *s) +/* + * Unified-diff hunk begins: a side with no lines names the line just + * before the change (xdl_emit_hunk_hdr() does the same). The begins are + * seeded from xdiff's hunk header, which already applied this for a pure + * creation or deletion (begin 0), so leave a 0 alone. + */ +static long unified_begin(long begin, long count) +{ + return (count || begin <= 0) ? begin : begin - 1; +} + +/* + * The range hunk's old/new counts are maintained as lines are appended; + * cross-check them against the buffer so a miscount surfaces as a BUG + * rather than a silently malformed @@ header. + */ +static void assert_hunk_counts(const struct line_range_filter *filter) +{ + const char *p = filter->hunk.lines.buf; + const char *end = p + filter->hunk.lines.len; + long n_old = 0, n_new = 0; + + while (p < end) { + const char *eol = memchr(p, '\n', end - p); + if (*p == ' ' || *p == '-') + n_old++; + if (*p == ' ' || *p == '+') + n_new++; + p = eol ? eol + 1 : end; + } + if (n_old != filter->hunk.old_count || n_new != filter->hunk.new_count) + BUG("line-range hunk counts (%ld,%ld) disagree with buffer (%ld,%ld)", + filter->hunk.old_count, filter->hunk.new_count, n_old, n_new); +} + +static void flush_hunk(struct line_range_filter *filter) { struct strbuf hdr = STRBUF_INIT; const char *p, *end; + long old_begin, new_begin; - if (!s->rhunk_active || s->ret) + if (!filter->hunk.active || filter->ret) return; - /* Drain any pending removal lines into the range hunk */ - if (s->pending_rm_count) { - strbuf_addbuf(&s->rhunk, &s->pending_rm); - s->rhunk_old_count += s->pending_rm_count; - s->rhunk_has_changes = 1; - discard_pending_rm(s); - } - /* * Suppress context-only hunks: they contain no actual changes * and would just be noise. This can happen when the inflated * ctxlen causes xdiff to emit context covering a range that * has no changes in this commit. */ - if (!s->rhunk_has_changes) { - s->rhunk_active = 0; - strbuf_reset(&s->rhunk); + if (!filter->hunk.has_changes) { + filter->hunk.active = 0; + strbuf_reset(&filter->hunk.lines); return; } + assert_hunk_counts(filter); + + old_begin = unified_begin(filter->hunk.old_begin, filter->hunk.old_count); + new_begin = unified_begin(filter->hunk.new_begin, filter->hunk.new_count); + strbuf_addf(&hdr, "@@ -%ld,%ld +%ld,%ld @@", - s->rhunk_old_begin, s->rhunk_old_count, - s->rhunk_new_begin, s->rhunk_new_count); - if (s->funclen > 0) { + old_begin, filter->hunk.old_count, + new_begin, filter->hunk.new_count); + if (filter->funclen > 0) { strbuf_addch(&hdr, ' '); - strbuf_add(&hdr, s->func, s->funclen); + strbuf_add(&hdr, filter->func, filter->funclen); } strbuf_addch(&hdr, '\n'); - s->ret = s->orig_line_fn(s->orig_cb_data, hdr.buf, hdr.len); + /* + * Inform a line-numbering consumer of the post-image position + * before replaying lines, mirroring the hunk callback xdiff + * would have issued for a non-scoped diff. + */ + if (filter->orig_hunk_fn) + filter->orig_hunk_fn(filter->orig_cb_data, + old_begin, filter->hunk.old_count, + new_begin, filter->hunk.new_count, + filter->func, filter->funclen); + + filter->ret = filter->orig_line_fn(filter->orig_cb_data, hdr.buf, hdr.len); strbuf_release(&hdr); /* @@ -2591,18 +2674,18 @@ static void flush_rhunk(struct line_range_callback *s) * The cast discards const because xdiff_emit_line_fn takes * char *, though fn_out_consume does not modify the buffer. */ - p = s->rhunk.buf; - end = p + s->rhunk.len; - while (!s->ret && p < end) { + p = filter->hunk.lines.buf; + end = p + filter->hunk.lines.len; + while (!filter->ret && p < end) { const char *eol = memchr(p, '\n', end - p); unsigned long line_len = eol ? (unsigned long)(eol - p + 1) : (unsigned long)(end - p); - s->ret = s->orig_line_fn(s->orig_cb_data, (char *)p, line_len); + filter->ret = filter->orig_line_fn(filter->orig_cb_data, (char *)p, line_len); p += line_len; } - s->rhunk_active = 0; - strbuf_reset(&s->rhunk); + filter->hunk.active = 0; + strbuf_reset(&filter->hunk.lines); } static void line_range_hunk_fn(void *data, @@ -2610,116 +2693,124 @@ static void line_range_hunk_fn(void *data, long new_begin, long new_nr UNUSED, const char *func, long funclen) { - struct line_range_callback *s = data; + struct line_range_filter *filter = data; /* * When count > 0, begin is 1-based. When count == 0, begin is * adjusted down by 1 by xdl_emit_hunk_hdr(), but no lines of * that type will arrive, so the value is unused. - * - * Any pending removal lines from the previous xdiff hunk are - * intentionally left in pending_rm: the line callback will - * flush or discard them when the next content line reveals - * whether the removals precede in-range content. */ - s->lno_post = new_begin; - s->lno_pre = old_begin; + filter->new_lineno = new_begin; + filter->old_lineno = old_begin; if (funclen > 0) { - if (funclen > (long)sizeof(s->func)) - funclen = sizeof(s->func); - memcpy(s->func, func, funclen); + if (funclen > (long)sizeof(filter->func)) + funclen = sizeof(filter->func); + memcpy(filter->func, func, funclen); } - s->funclen = funclen; + filter->funclen = funclen; } static int line_range_line_fn(void *priv, char *line, unsigned long len) { - struct line_range_callback *s = priv; - const struct range *cur; - long lno_0, cur_pre; + struct line_range_filter *filter = priv; + long new_idx; + int in_range; - if (s->ret) - return s->ret; - - if (line[0] == '-') { - if (!s->pending_rm_count) - s->pending_rm_pre_begin = s->lno_pre; - s->lno_pre++; - strbuf_add(&s->pending_rm, line, len); - s->pending_rm_count++; - return s->ret; - } + if (filter->ret) + return filter->ret; if (line[0] == '\\') { - if (s->pending_rm_count) - strbuf_add(&s->pending_rm, line, len); - else if (s->rhunk_active) - strbuf_add(&s->rhunk, line, len); - /* otherwise outside tracked range; drop silently */ - return s->ret; + if (filter->hunk.active) + strbuf_add(&filter->hunk.lines, line, len); + return filter->ret; } - if (line[0] != '+' && line[0] != ' ') + if (line[0] != '+' && line[0] != ' ' && line[0] != '-') BUG("unexpected diff line type '%c'", line[0]); - lno_0 = s->lno_post - 1; - cur_pre = s->lno_pre; /* save before advancing for context lines */ - s->lno_post++; - if (line[0] == ' ') - s->lno_pre++; - - /* Advance past ranges we've passed */ - while (s->cur_range < s->ranges->nr && - lno_0 >= s->ranges->ranges[s->cur_range].end) { - if (s->rhunk_active) - flush_rhunk(s); - discard_pending_rm(s); - s->cur_range++; - } + /* + * new_idx is this line's 0-based post-image index (see the model on + * struct line_range_filter). The cursors are advanced only after + * the line is classified, so a '-' is tested at the same new_idx as + * the '+'/' ' that follows it. + */ + new_idx = filter->new_lineno - 1; - /* Past all ranges */ - if (s->cur_range >= s->ranges->nr) { - discard_pending_rm(s); - return s->ret; + /* Retire ranges we have passed, flushing the one we leave. */ + while (filter->cur_range < filter->ranges->nr && + new_idx >= filter->ranges->ranges[filter->cur_range].end) { + if (filter->hunk.active) + flush_hunk(filter); + filter->cur_range++; } - cur = &s->ranges->ranges[s->cur_range]; + in_range = filter->cur_range < filter->ranges->nr && + new_idx >= filter->ranges->ranges[filter->cur_range].start && + new_idx < filter->ranges->ranges[filter->cur_range].end; - /* Before current range */ - if (lno_0 < cur->start) { - discard_pending_rm(s); - return s->ret; - } + if (in_range) { + /* + * The first in-range line opens a range hunk; its position + * fixes the hunk's old/new begins, read before the cursors + * advance below. + */ + if (!filter->hunk.active) { + filter->hunk.active = 1; + filter->hunk.has_changes = 0; + filter->hunk.new_begin = new_idx + 1; + filter->hunk.old_begin = filter->old_lineno; + filter->hunk.old_count = 0; + filter->hunk.new_count = 0; + strbuf_reset(&filter->hunk.lines); + } - /* In range so start a new range hunk if needed */ - if (!s->rhunk_active) { - s->rhunk_active = 1; - s->rhunk_has_changes = 0; - s->rhunk_new_begin = lno_0 + 1; - s->rhunk_old_begin = s->pending_rm_count - ? s->pending_rm_pre_begin : cur_pre; - s->rhunk_old_count = 0; - s->rhunk_new_count = 0; - strbuf_reset(&s->rhunk); + strbuf_add(&filter->hunk.lines, line, len); + switch (line[0]) { + case '-': + filter->hunk.old_count++; + filter->hunk.has_changes = 1; + break; + case '+': + filter->hunk.new_count++; + filter->hunk.has_changes = 1; + break; + default: /* ' ', context */ + filter->hunk.old_count++; + filter->hunk.new_count++; + break; + } } - /* Flush pending removals into range hunk */ - if (s->pending_rm_count) { - strbuf_addbuf(&s->rhunk, &s->pending_rm); - s->rhunk_old_count += s->pending_rm_count; - s->rhunk_has_changes = 1; - discard_pending_rm(s); - } + /* + * Advance each image's cursor: a line present in that image (see + * the model) consumes one of its line numbers. + */ + if (line[0] != '-') + filter->new_lineno++; + if (line[0] != '+') + filter->old_lineno++; - strbuf_add(&s->rhunk, line, len); - s->rhunk_new_count++; - if (line[0] == '+') - s->rhunk_has_changes = 1; - else - s->rhunk_old_count++; + return filter->ret; +} - return s->ret; +/* + * Run an xdiff pass through an initialized line-range filter, flush the + * final range hunk, and release the filter. Returns non-zero if xdiff or + * any forwarded callback failed. + */ +static int line_range_filter_diff(struct line_range_filter *filter, + mmfile_t *mf1, mmfile_t *mf2, + xpparam_t *xpp, xdemitconf_t *xecfg) +{ + int ret = xdi_diff_outf(mf1, mf2, line_range_hunk_fn, + line_range_line_fn, filter, xpp, xecfg); + if (!ret) { + flush_hunk(filter); + ret = filter->ret; + } + strbuf_release(&filter->hunk.lines); + return ret; } static void pprint_rename(struct strbuf *name, const char *a, const char *b) @@ -4086,16 +4177,12 @@ static void builtin_diff(const char *name_a, xdi_diff_outf(&mf1, &mf2, NULL, quick_consume, &ecbdata, &xpp, &xecfg); } else if (line_ranges) { - struct line_range_callback lr_state; + struct line_range_filter lr_state; unsigned int i; long max_span = 0; - memset(&lr_state, 0, sizeof(lr_state)); - lr_state.orig_line_fn = fn_out_consume; - lr_state.orig_cb_data = &ecbdata; - lr_state.ranges = line_ranges; - strbuf_init(&lr_state.rhunk, 0); - strbuf_init(&lr_state.pending_rm, 0); + line_range_filter_init(&lr_state, line_ranges, + fn_out_consume, &ecbdata); /* * Inflate ctxlen so that all changes within @@ -4118,19 +4205,10 @@ static void builtin_diff(const char *name_a, if (max_span > xecfg.ctxlen) xecfg.ctxlen = max_span; - if (xdi_diff_outf(&mf1, &mf2, - line_range_hunk_fn, - line_range_line_fn, - &lr_state, &xpp, &xecfg)) + if (line_range_filter_diff(&lr_state, &mf1, &mf2, + &xpp, &xecfg)) die("unable to generate diff for %s", one->path); - - flush_rhunk(&lr_state); - if (lr_state.ret) - die("unable to generate diff for %s", - one->path); - strbuf_release(&lr_state.rhunk); - strbuf_release(&lr_state.pending_rm); } else if (xdi_diff_outf(&mf1, &mf2, NULL, fn_out_consume, &ecbdata, &xpp, &xecfg)) die("unable to generate diff for %s", one->path); @@ -4247,7 +4325,21 @@ static void builtin_diffstat(const char *name_a, const char *name_b, xecfg.ctxlen = o->context; xecfg.interhunkctxlen = o->interhunkcontext; xecfg.flags = XDL_EMIT_NO_HUNK_HDR; - if (xdi_diff_outf(&mf1, &mf2, NULL, + + if (p->line_ranges) { + struct line_range_filter lr_filter; + + line_range_filter_init(&lr_filter, p->line_ranges, + diffstat_consume, diffstat); + + /* Need hunk headers for post-image position tracking */ + xecfg.flags &= ~XDL_EMIT_NO_HUNK_HDR; + + if (line_range_filter_diff(&lr_filter, &mf1, &mf2, + &xpp, &xecfg)) + die("unable to generate diffstat for %s", + one->path); + } else if (xdi_diff_outf(&mf1, &mf2, NULL, diffstat_consume, diffstat, &xpp, &xecfg)) die("unable to generate diffstat for %s", one->path); @@ -4281,7 +4373,8 @@ static void builtin_checkdiff(const char *name_a, const char *name_b, const char *attr_path, struct diff_filespec *one, struct diff_filespec *two, - struct diff_options *o) + struct diff_options *o, + const struct range_set *line_ranges) { mmfile_t mf1, mf2; struct checkdiff_t data; @@ -4321,7 +4414,19 @@ static void builtin_checkdiff(const char *name_a, const char *name_b, memset(&xecfg, 0, sizeof(xecfg)); xecfg.ctxlen = 1; /* at least one context line */ xpp.flags = 0; - if (xdi_diff_outf(&mf1, &mf2, checkdiff_consume_hunk, + + if (line_ranges) { + struct line_range_filter lr_filter; + + line_range_filter_init(&lr_filter, line_ranges, + checkdiff_consume, &data); + lr_filter.orig_hunk_fn = checkdiff_consume_hunk; + + if (line_range_filter_diff(&lr_filter, &mf1, &mf2, + &xpp, &xecfg)) + die("unable to generate checkdiff for %s", + one->path); + } else if (xdi_diff_outf(&mf1, &mf2, checkdiff_consume_hunk, checkdiff_consume, &data, &xpp, &xecfg)) die("unable to generate checkdiff for %s", one->path); @@ -5126,7 +5231,8 @@ static void run_checkdiff(struct diff_filepair *p, struct diff_options *o) diff_fill_oid_info(p->one, o->repo->index); diff_fill_oid_info(p->two, o->repo->index); - builtin_checkdiff(name, other, attr_path, p->one, p->two, o); + builtin_checkdiff(name, other, attr_path, p->one, p->two, o, + p->line_ranges); } void repo_diff_setup(struct repository *r, struct diff_options *options) diff --git a/revision.c b/revision.c index 6a8101e8b7ef5f..7abb287451ba14 100644 --- a/revision.c +++ b/revision.c @@ -3193,8 +3193,10 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s (revs->diffopt.output_format & ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT | DIFF_FORMAT_RAW | DIFF_FORMAT_NAME | - DIFF_FORMAT_NAME_STATUS | DIFF_FORMAT_SUMMARY)))) - die(_("-L does not yet support the requested diff format")); + DIFF_FORMAT_NAME_STATUS | DIFF_FORMAT_SUMMARY | + DIFF_FORMAT_NUMSTAT | DIFF_FORMAT_DIFFSTAT | + DIFF_FORMAT_SHORTSTAT | DIFF_FORMAT_CHECKDIFF)))) + die(_("-L does not support the requested diff format")); if (revs->expand_tabs_in_log < 0) revs->expand_tabs_in_log = revs->expand_tabs_in_log_default; diff --git a/t/t4211-line-log.sh b/t/t4211-line-log.sh index ca4eb7bbc713ef..70d297e8030fc0 100755 --- a/t/t4211-line-log.sh +++ b/t/t4211-line-log.sh @@ -176,24 +176,22 @@ test_expect_success '--name-status shows status and path' ' test_grep ! "^@@" actual ' -test_expect_success '--stat is not yet supported with -L' ' - test_must_fail git log -L1,24:b.c --stat 2>err && - test_grep "does not yet support" err -' - -test_expect_success '--numstat is not yet supported with -L' ' - test_must_fail git log -L1,24:b.c --numstat 2>err && - test_grep "does not yet support" err -' - -test_expect_success '--shortstat is not yet supported with -L' ' - test_must_fail git log -L1,24:b.c --shortstat 2>err && - test_grep "does not yet support" err -' - -test_expect_success '--dirstat is not yet supported with -L' ' +test_expect_success '--stat works with -L' ' + git log -L1,24:b.c --stat --format= >actual && + test_grep "b.c |" actual && + test_grep "file changed" actual && + test_grep ! "^diff --git" actual +' + +test_expect_success '--dirstat is not supported with -L' ' + # --dirstat is degenerate for line ranges (a single tracked + # file is always 100% of its directory) and its default mode + # is not range-scoped, so it is rejected like any other + # unsupported diff format. test_must_fail git log -L1,24:b.c --dirstat 2>err && - test_grep "does not yet support" err + test_grep "does not support" err && + test_must_fail git log -L1,24:b.c --dirstat=lines 2>err && + test_grep "does not support" err ' test_expect_success 'setup for checking fancy rename following' ' @@ -738,6 +736,143 @@ test_expect_success '-L with -G filters to diff-text matches' ' grep "F2 + 2" actual ' +test_expect_success 'setup for trailing deletion test' ' + git checkout --orphan trailing-del && + git reset --hard && + cat >file.c <<-\EOF && + void tracked() + { + return 1; + } + // trailing comment + EOF + git add file.c && + test_tick && + git commit -m "add file with trailing comment" && + # Modify tracked() AND delete the trailing comment in + # one commit, so the commit touches the tracked range + # and is not filtered out by the revision walker. + cat >file.c <<-\EOF && + void tracked() + { + return 2; + } + EOF + git commit -a -m "modify tracked and delete trailing comment" +' + +test_expect_success '-L does not include deletions past end of tracked range' ' + git log -L:tracked:file.c --format= -1 -p >actual && + # The trailing comment deletion is outside the tracked + # range and should not appear in the patch output. + test_grep "return 2" actual && + test_grep ! "trailing comment" actual +' + +test_expect_success '-L includes leading deletions resolved by in-range line' ' + git checkout --orphan leading-del && + git reset --hard && + cat >file.c <<-\EOF && + // leading comment + void tracked() + { + return 1; + } + EOF + git add file.c && + test_tick && + git commit -m "add file with leading comment" && + cat >file.c <<-\EOF && + void tracked() + { + return 2; + } + EOF + git commit -a -m "modify tracked and delete leading comment" && + git log -L:tracked:file.c --format= -1 -p >actual && + # The leading comment deletion is resolved by the next + # non-removal line (void tracked), which is in range: a + # removal is classified by the position of the following + # line, so it joins the range that line falls in. + test_grep "return 2" actual && + test_grep "leading comment" actual +' + +test_expect_success 'setup for line-range filter edge cases' ' + git checkout --orphan filter-edge && + git reset --hard && + cat >file.c <<-\EOF && + void before() + { + return 0; + } + + void tracked() + { + int a = 1; + int b = 2; + int c = 3; + return a + b + c; + } + + void after() + { + return 9; + } + EOF + git add file.c && + test_tick && + git commit -m "initial" +' + +test_expect_success '-L change at exact first line of range' ' + git checkout filter-edge && + # Change the function signature (first line of range) + sed "s/void tracked/int tracked/" file.c >tmp && + mv tmp file.c && + git commit -a -m "change first line" && + git log -L:tracked:file.c -p --format=%s -1 >actual && + test_grep "change first line" actual && + test_grep "+int tracked" actual && + test_grep "\\-void tracked" actual +' + +test_expect_success '-L change at exact last line of range' ' + git checkout filter-edge && + git reset --hard HEAD~1 && + # Change the closing brace line (last line of range) + sed "s/^}$/} \/\/ end tracked/" file.c >tmp && + mv tmp file.c && + git commit -a -m "change last line" && + git log -L:tracked:file.c -p --format=%s -1 >actual && + test_grep "change last line" actual && + test_grep "end tracked" actual +' + +test_expect_success '-L pure deletion in range (no additions)' ' + git checkout filter-edge && + git reset --hard HEAD~1 && + # Delete a line inside tracked() without adding anything + sed "/int c/d" file.c >tmp && + mv tmp file.c && + git commit -a -m "pure deletion" && + git log -L:tracked:file.c -p --format=%s -1 >actual && + test_grep "pure deletion" actual && + test_grep "\\-.*int c" actual +' + +test_expect_success '-L adjacent change outside range excluded' ' + git checkout filter-edge && + git reset --hard HEAD~1 && + # Change only before() - adjacent to tracked() but outside range + sed "s/return 0/return 42/" file.c >tmp && + mv tmp file.c && + git commit -a -m "change before only" && + # Commit should not appear since tracked() is unchanged + git log -L:tracked:file.c --format=%s --no-patch >actual && + test_grep ! "change before only" actual +' + test_expect_success '-L with --diff-filter=M excludes root commit' ' git checkout parent-oids && git log -L:func2:file.c --diff-filter=M --format=%s --no-patch >actual && @@ -762,9 +897,9 @@ test_expect_success '-L with -S suppresses non-matching commits' ' test_cmp expect actual ' -test_expect_success '--full-diff is not yet supported with -L' ' +test_expect_success '--full-diff is not supported with -L' ' test_must_fail git log -L1,24:b.c --full-diff 2>err && - test_grep "does not yet support" err + test_grep "does not support" err ' test_expect_success '-L --oneline has no extra blank line before diff' ' @@ -775,10 +910,136 @@ test_expect_success '-L --oneline has no extra blank line before diff' ' test_grep "^diff --git" line2 ' +test_expect_success 'setup for stat range-scoping tests' ' + git checkout --orphan stat-scoping && + git reset --hard && + cat >file.c <<-\EOF && + int func1() + { + return F1; + } + + int func2() + { + return F2; + } + EOF + git add file.c && + test_tick && + git commit -m "Add func1() and func2()" && + + # Modify both functions in a single commit so that + # whole-file stats differ from range-scoped stats. + sed -e "s/F1/F1 + 1/" -e "s/F2/F2 + 2/" file.c >tmp && + mv tmp file.c && + git commit -a -m "Modify both functions" +' + +test_expect_success '--numstat counts only lines in tracked range' ' + # "Modify both functions" changes one line in func1 and one in + # func2. Whole-file numstat would show 2 added, 2 deleted. + # Range-scoped numstat for func2 should show only 1 and 1. + git log -L:func2:file.c --numstat --format=%s -1 >actual && + test_grep "Modify both functions" actual && + test_grep "^1 1 file.c$" actual +' + +test_expect_success '--numstat counts only additions for root commit' ' + # Root commit creates both func1 (4 lines) and func2 (4 lines). + # Whole-file numstat would show 9 lines added. Range-scoped + # numstat for func2 should show only 4. + git log -L:func2:file.c --numstat --format=%s >actual && + test_grep "Add func1() and func2()" actual && + test_grep "^4 0 file.c$" actual +' + +test_expect_success '--stat counts only lines in tracked range' ' + git log -L:func2:file.c --stat --format=%s -1 >actual && + test_grep "Modify both functions" actual && + test_grep "1 insertion" actual && + test_grep "1 deletion" actual +' + +test_expect_success '--shortstat counts only lines in tracked range' ' + # Same range-scoped counting as --numstat above. + git log -L:func2:file.c --shortstat --format=%s -1 >actual && + test_grep "Modify both functions" actual && + test_grep "1 insertion" actual && + test_grep "1 deletion" actual +' + +test_expect_success '--numstat matches patch line counts' ' + # Cross-check: count +/- lines in patch output and verify + # --numstat reports the same numbers. Uses the parallel-change + # branch which has renames and multiple commits, exercising the + # filter across nontrivial history. + git checkout parallel-change && + git log -M -L ":f:b.c" --format= -p >patch_out && + adds=$(grep "^+" patch_out | grep -vc "^+++") && + dels=$(grep "^-" patch_out | grep -vc "^---") && + git log -M -L ":f:b.c" --numstat --format= >stat_out && + awk "{a+=\$1; d+=\$2} END {print a, d}" stat_out >stat_counts && + echo "$adds $dels" >patch_counts && + test_cmp patch_counts stat_counts +' + +test_expect_success '-L multiple ranges with --numstat' ' + git checkout stat-scoping && + # Both func1 and func2 were modified by "Modify both functions". + # Track both with separate -L flags; stats should sum. + git log -L:func1:file.c -L:func2:file.c --numstat --format=%s -1 >actual && + test_grep "Modify both functions" actual && + test_grep "^2 2 file.c$" actual +' + test_expect_success '--summary shows new file on root commit' ' git checkout parent-oids && git log -L:func2:file.c --summary --format= >actual && test_grep "create mode 100644 file.c" actual ' +test_expect_success 'setup for --check test' ' + git checkout --orphan check-test && + git reset --hard && + cat >check.c <<-\EOF && + void tracked() + { + return; + } + + void other() + { + return; + } + EOF + git add check.c && + test_tick && + git commit -m "add check.c" && + # Introduce trailing whitespace errors in both functions + sed "s/return;/return; /" check.c >check.c.tmp && + mv check.c.tmp check.c && + git commit -a -m "introduce trailing whitespace" +' + +test_expect_success '--check reports whitespace errors in tracked range' ' + test_must_fail git log -L:tracked:check.c --check --format= >actual && + test_grep "trailing whitespace" actual +' + +test_expect_success '--check scoped to tracked range' ' + test_must_fail git log -L:tracked:check.c --check --format= >actual && + # The error is within tracked(); other() errors are excluded + test_grep "trailing whitespace" actual && + # Only one error reported (tracked function only, not other()) + test $(grep -c "trailing whitespace" actual) = 1 +' + +test_expect_success '--check reports the file line number, not a range offset' ' + # The trailing whitespace is on the third line of check.c. + # Report the real file line number, not a count from the start + # of the range hunk. + test_must_fail git log -L:tracked:check.c --check --format= >actual && + test_grep "check.c:3: trailing whitespace" actual +' + test_done diff --git a/t/t4211/sha1/expect.no-assertion-error b/t/t4211/sha1/expect.no-assertion-error index 54c568f273a6d2..95faf51a7b46d9 100644 --- a/t/t4211/sha1/expect.no-assertion-error +++ b/t/t4211/sha1/expect.no-assertion-error @@ -8,7 +8,7 @@ diff --git a/b.c b/b.c index bf79c2f..27c829c 100644 --- a/b.c +++ b/b.c -@@ -25,0 +18,9 @@ +@@ -24,0 +18,9 @@ +long f(long x) +{ + int s = 0; diff --git a/t/t4211/sha1/expect.vanishes-early b/t/t4211/sha1/expect.vanishes-early index a413ad36598ddf..bf0fb6d858c8b2 100644 --- a/t/t4211/sha1/expect.vanishes-early +++ b/t/t4211/sha1/expect.vanishes-early @@ -8,7 +8,7 @@ diff --git a/a.c b/a.c index 0b9cae5..5de3ea4 100644 --- a/a.c +++ b/a.c -@@ -23,0 +24,1 @@ int main () +@@ -22,0 +24,1 @@ int main () +/* incomplete lines are bad! */ commit 100b61a6f2f720f812620a9d10afb3a960ccb73c diff --git a/t/t4211/sha256/expect.no-assertion-error b/t/t4211/sha256/expect.no-assertion-error index c25f2ce19c05d9..815d27f7f17b7e 100644 --- a/t/t4211/sha256/expect.no-assertion-error +++ b/t/t4211/sha256/expect.no-assertion-error @@ -8,7 +8,7 @@ diff --git a/b.c b/b.c index 69cb69c..a0d566e 100644 --- a/b.c +++ b/b.c -@@ -25,0 +18,9 @@ +@@ -24,0 +18,9 @@ +long f(long x) +{ + int s = 0; diff --git a/t/t4211/sha256/expect.vanishes-early b/t/t4211/sha256/expect.vanishes-early index bc33b963dc8570..fce3d7bd38af42 100644 --- a/t/t4211/sha256/expect.vanishes-early +++ b/t/t4211/sha256/expect.vanishes-early @@ -8,7 +8,7 @@ diff --git a/a.c b/a.c index e4fa1d8..62c1fc2 100644 --- a/a.c +++ b/a.c -@@ -23,0 +24,1 @@ int main () +@@ -22,0 +24,1 @@ int main () +/* incomplete lines are bad! */ commit 29f32ac3141c48b22803e5c4127b719917b67d0f8ca8c5248bebfa2a19f7da10