Skip to content

Commit 1fcc199

Browse files
HaraldNordgrengitster
authored andcommitted
stash: add --label-ours, --label-theirs, --label-base for apply
Allow callers of "git stash apply" to pass custom labels for conflict markers instead of the default "Updated upstream" and "Stashed changes". Document the new options and add a test. Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent b15384c commit 1fcc199

File tree

4 files changed

+67
-11
lines changed

4 files changed

+67
-11
lines changed

Documentation/git-stash.adoc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ git stash list [<log-options>]
1212
git stash show [-u | --include-untracked | --only-untracked] [<diff-options>] [<stash>]
1313
git stash drop [-q | --quiet] [<stash>]
1414
git stash pop [--index] [-q | --quiet] [<stash>]
15-
git stash apply [--index] [-q | --quiet] [<stash>]
15+
git stash apply [--index] [-q | --quiet] [--label-ours=<label>] [--label-theirs=<label>] [--label-base=<label>] [<stash>]
1616
git stash branch <branchname> [<stash>]
1717
git stash [push] [-p | --patch] [-S | --staged] [-k | --[no-]keep-index] [-q | --quiet]
1818
[-u | --include-untracked] [-a | --all] [(-m | --message) <message>]
@@ -195,6 +195,15 @@ the index's ones. However, this can fail, when you have conflicts
195195
(which are stored in the index, where you therefore can no longer
196196
apply the changes as they were originally).
197197
198+
`--label-ours=<label>`::
199+
`--label-theirs=<label>`::
200+
`--label-base=<label>`::
201+
These options are only valid for the `apply` command.
202+
+
203+
Use the given labels in conflict markers instead of the default
204+
"Updated upstream", "Stashed changes", and "Stash base".
205+
`--label-base` only has an effect with merge.conflictStyle=diff3.
206+
198207
`-k`::
199208
`--keep-index`::
200209
`--no-keep-index`::

builtin/stash.c

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
#define BUILTIN_STASH_POP_USAGE \
4545
N_("git stash pop [--index] [-q | --quiet] [<stash>]")
4646
#define BUILTIN_STASH_APPLY_USAGE \
47-
N_("git stash apply [--index] [-q | --quiet] [<stash>]")
47+
N_("git stash apply [--index] [-q | --quiet] [--label-ours=<label>] [--label-theirs=<label>] [--label-base=<label>] [<stash>]")
4848
#define BUILTIN_STASH_BRANCH_USAGE \
4949
N_("git stash branch <branchname> [<stash>]")
5050
#define BUILTIN_STASH_STORE_USAGE \
@@ -590,8 +590,11 @@ static void unstage_changes_unless_new(struct object_id *orig_tree)
590590
die(_("could not write index"));
591591
}
592592

593-
static int do_apply_stash(const char *prefix, struct stash_info *info,
594-
int index, int quiet)
593+
static int do_apply_stash_with_labels(const char *prefix,
594+
struct stash_info *info,
595+
int index, int quiet,
596+
const char *label_ours, const char *label_theirs,
597+
const char *label_base)
595598
{
596599
int clean, ret;
597600
int has_index = index;
@@ -643,9 +646,9 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,
643646

644647
init_ui_merge_options(&o, the_repository);
645648

646-
o.branch1 = "Updated upstream";
647-
o.branch2 = "Stashed changes";
648-
o.ancestor = "Stash base";
649+
o.branch1 = label_ours ? label_ours : "Updated upstream";
650+
o.branch2 = label_theirs ? label_theirs : "Stashed changes";
651+
o.ancestor = label_base ? label_base : "Stash base";
649652

650653
if (oideq(&info->b_tree, &c_tree))
651654
o.branch1 = "Version stash was based on";
@@ -717,17 +720,31 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,
717720
return ret;
718721
}
719722

723+
static int do_apply_stash(const char *prefix, struct stash_info *info,
724+
int index, int quiet)
725+
{
726+
return do_apply_stash_with_labels(prefix, info, index, quiet,
727+
NULL, NULL, NULL);
728+
}
729+
720730
static int apply_stash(int argc, const char **argv, const char *prefix,
721731
struct repository *repo UNUSED)
722732
{
723733
int ret = -1;
724734
int quiet = 0;
725735
int index = use_index;
736+
const char *label_ours = NULL, *label_theirs = NULL, *label_base = NULL;
726737
struct stash_info info = STASH_INFO_INIT;
727738
struct option options[] = {
728739
OPT__QUIET(&quiet, N_("be quiet, only report errors")),
729740
OPT_BOOL(0, "index", &index,
730741
N_("attempt to recreate the index")),
742+
OPT_STRING(0, "label-ours", &label_ours, N_("label"),
743+
N_("label for the upstream side in conflict markers")),
744+
OPT_STRING(0, "label-theirs", &label_theirs, N_("label"),
745+
N_("label for the stashed side in conflict markers")),
746+
OPT_STRING(0, "label-base", &label_base, N_("label"),
747+
N_("label for the base in diff3 conflict markers")),
731748
OPT_END()
732749
};
733750

@@ -737,7 +754,8 @@ static int apply_stash(int argc, const char **argv, const char *prefix,
737754
if (get_stash_info(&info, argc, argv))
738755
goto cleanup;
739756

740-
ret = do_apply_stash(prefix, &info, index, quiet);
757+
ret = do_apply_stash_with_labels(prefix, &info, index, quiet,
758+
label_ours, label_theirs, label_base);
741759
cleanup:
742760
free_stash_info(&info);
743761
return ret;

t/t3903-stash.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,6 +1666,35 @@ test_expect_success 'restore untracked files even when we hit conflicts' '
16661666
)
16671667
'
16681668

1669+
test_expect_success 'apply with custom conflict labels' '
1670+
git init conflict_labels &&
1671+
(
1672+
cd conflict_labels &&
1673+
test_commit base file &&
1674+
echo stashed >file &&
1675+
git stash push -m "stashed" &&
1676+
test_commit upstream file &&
1677+
test_must_fail git -c merge.conflictStyle=diff3 stash apply --label-ours=UP --label-theirs=STASH &&
1678+
test_grep "^<<<<<<< UP" file &&
1679+
test_grep "^||||||| Stash base" file &&
1680+
test_grep "^>>>>>>> STASH" file
1681+
)
1682+
'
1683+
1684+
test_expect_success 'apply with empty conflict labels' '
1685+
git init empty_labels &&
1686+
(
1687+
cd empty_labels &&
1688+
test_commit base file &&
1689+
echo stashed >file &&
1690+
git stash push -m "stashed" &&
1691+
test_commit upstream file &&
1692+
test_must_fail git stash apply --label-ours= --label-theirs= &&
1693+
test_grep "^<<<<<<<$" file &&
1694+
test_grep "^>>>>>>>$" file
1695+
)
1696+
'
1697+
16691698
test_expect_success 'stash create reports a locked index' '
16701699
test_when_finished "rm -rf repo" &&
16711700
git init repo &&

xdiff/xmerge.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,9 @@ static int fill_conflict_hunk(xdfenv_t *xe1, const char *name1,
199199
int size, int i, int style,
200200
xdmerge_t *m, char *dest, int marker_size)
201201
{
202-
int marker1_size = (name1 ? strlen(name1) + 1 : 0);
203-
int marker2_size = (name2 ? strlen(name2) + 1 : 0);
204-
int marker3_size = (name3 ? strlen(name3) + 1 : 0);
202+
int marker1_size = (name1 && *name1 ? strlen(name1) + 1 : 0);
203+
int marker2_size = (name2 && *name2 ? strlen(name2) + 1 : 0);
204+
int marker3_size = (name3 && *name3 ? strlen(name3) + 1 : 0);
205205
int needs_cr = is_cr_needed(xe1, xe2, m);
206206

207207
if (marker_size <= 0)

0 commit comments

Comments
 (0)