Skip to content

Commit 861705f

Browse files
diff: add --slide-down option to shift ambiguous hunks downward
When an inserted block is bounded by lines identical to the surrounding context (e.g., "}") the diff slider can place the hunk at any position within the slidable range. Add --slide-down (and diff.slideDown config) to leave ambiguous hunks at the furthest-down position, so that insertions appear appended after the existing context rather than prepended before it. This results in a more readable git diff, without adding any extra lines to the diff. Signed-off-by: Charles G Waldman <cgw@alum.mit.edu>
1 parent 7ff1e8d commit 861705f

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

diff.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555

5656
static int diff_detect_rename_default;
5757
static int diff_indent_heuristic = 1;
58+
static int diff_slide_down;
5859
static int diff_rename_limit_default = 1000;
5960
static int diff_suppress_blank_empty;
6061
static enum git_colorbool diff_use_color_default = GIT_COLOR_UNKNOWN;
@@ -289,6 +290,8 @@ int git_diff_heuristic_config(const char *var, const char *value,
289290
{
290291
if (!strcmp(var, "diff.indentheuristic"))
291292
diff_indent_heuristic = git_config_bool(var, value);
293+
if (!strcmp(var, "diff.slidedown"))
294+
diff_slide_down = git_config_bool(var, value);
292295
return 0;
293296
}
294297

@@ -4861,6 +4864,8 @@ void repo_diff_setup(struct repository *r, struct diff_options *options)
48614864
options->xdl_opts |= diff_algorithm;
48624865
if (diff_indent_heuristic)
48634866
DIFF_XDL_SET(options, INDENT_HEURISTIC);
4867+
if (diff_slide_down)
4868+
DIFF_XDL_SET(options, SLIDE_DOWN);
48644869

48654870
options->orderfile = xstrdup_or_null(diff_order_file_cfg);
48664871

@@ -5910,6 +5915,9 @@ struct option *add_diff_options(const struct option *opts,
59105915
OPT_BIT(0, "indent-heuristic", &options->xdl_opts,
59115916
N_("heuristic to shift diff hunk boundaries for easy reading"),
59125917
XDF_INDENT_HEURISTIC),
5918+
OPT_BIT(0, "slide-down", &options->xdl_opts,
5919+
N_("slide ambiguous diff hunks downward (append after context)"),
5920+
XDF_SLIDE_DOWN),
59135921
OPT_CALLBACK_F(0, "patience", options, NULL,
59145922
N_("generate diff using the \"patience diff\" algorithm"),
59155923
PARSE_OPT_NONEG | PARSE_OPT_NOARG,

xdiff/xdiff.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ extern "C" {
4747
#define XDF_DIFF_ALG(x) ((x) & XDF_DIFF_ALGORITHM_MASK)
4848

4949
#define XDF_INDENT_HEURISTIC (1 << 23)
50+
#define XDF_SLIDE_DOWN (1 << 24)
5051

5152
/* xdemitconf_t.flags */
5253
#define XDL_EMIT_FUNCNAMES (1 << 0)

xdiff/xdiffi.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,15 @@ int xdl_change_compact(xdfile_t *xdf, xdfile_t *xdfo, long flags) {
857857

858858
if (g.end == earliest_end) {
859859
/* no shifting was possible */
860+
} else if (flags & XDF_SLIDE_DOWN) {
861+
/*
862+
* --slide-down: the group is already at the
863+
* furthest-down position. Leave it there.
864+
* This produces diffs where ambiguous hunks
865+
* (e.g., duplicate delimiter lines) appear
866+
* as appended after existing context rather
867+
* than inserted before it.
868+
*/
860869
} else if (end_matching_other != -1) {
861870
/*
862871
* Move the possibly merged group of changes back to

0 commit comments

Comments
 (0)