Skip to content

Commit 8fcf377

Browse files
stash: add --ours-label, --theirs-label, --base-label 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>
1 parent b15384c commit 8fcf377

4 files changed

Lines changed: 51 additions & 5 deletions

File tree

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] [--ours-label=<label>] [--theirs-label=<label>] [--base-label=<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+
`--ours-label=<label>`::
199+
`--theirs-label=<label>`::
200+
`--base-label=<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+
`--base-label` only has an effect with merge.conflictStyle=diff3.
206+
198207
`-k`::
199208
`--keep-index`::
200209
`--no-keep-index`::

builtin/stash.c

Lines changed: 1 addition & 1 deletion
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] [--ours-label=<label>] [--theirs-label=<label>] [--base-label=<label>] [<stash>]")
4848
#define BUILTIN_STASH_BRANCH_USAGE \
4949
N_("git stash branch <branchname> [<stash>]")
5050
#define BUILTIN_STASH_STORE_USAGE \

t/t3903-stash.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,6 +1666,43 @@ 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+
echo base >file &&
1674+
git add file &&
1675+
git commit -m base &&
1676+
echo stashed >file &&
1677+
git stash push -m "stashed" &&
1678+
echo upstream >file &&
1679+
git add file &&
1680+
git commit -m upstream &&
1681+
test_must_fail git -c merge.conflictStyle=diff3 stash apply --ours-label=UP --theirs-label=STASH &&
1682+
test_grep "^<<<<<<< UP" file &&
1683+
test_grep "^||||||| Stash base" file &&
1684+
test_grep "^>>>>>>> STASH" file
1685+
)
1686+
'
1687+
1688+
test_expect_success 'apply with empty conflict labels' '
1689+
git init empty_labels &&
1690+
(
1691+
cd empty_labels &&
1692+
echo base >file &&
1693+
git add file &&
1694+
git commit -m base &&
1695+
echo stashed >file &&
1696+
git stash push -m "stashed" &&
1697+
echo upstream >file &&
1698+
git add file &&
1699+
git commit -m upstream &&
1700+
test_must_fail git stash apply --ours-label= --theirs-label= &&
1701+
test_grep "^<<<<<<<$" file &&
1702+
test_grep "^>>>>>>>$" file
1703+
)
1704+
'
1705+
16691706
test_expect_success 'stash create reports a locked index' '
16701707
test_when_finished "rm -rf repo" &&
16711708
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)