Skip to content

Commit eee98e7

Browse files
johnkeepinggitster
authored andcommitted
push: add shorthand for --force-with-lease branch creation
Allow the empty string to stand in for the null SHA-1 when pushing a new branch, like we do when deleting branches. This means that the following command ensures that `new-branch` is created on the remote (that is, is must not already exist): git push --force-with-lease=new-branch: origin new-branch Signed-off-by: John Keeping <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d132b32 commit eee98e7

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

Documentation/git-push.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ if it is going to be updated, by requiring its current value to be
201201
the same as the specified value `<expect>` (which is allowed to be
202202
different from the remote-tracking branch we have for the refname,
203203
or we do not even have to have such a remote-tracking branch when
204-
this form is used).
204+
this form is used). If `<expect>` is the empty string, then the named ref
205+
must not already exist.
205206
+
206207
Note that all forms other than `--force-with-lease=<refname>:<expect>`
207208
that specifies the expected current value of the ref explicitly are

remote.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2304,6 +2304,8 @@ int parse_push_cas_option(struct push_cas_option *cas, const char *arg, int unse
23042304
entry = add_cas_entry(cas, arg, colon - arg);
23052305
if (!*colon)
23062306
entry->use_tracking = 1;
2307+
else if (!colon[1])
2308+
hashclr(entry->expect);
23072309
else if (get_sha1(colon + 1, entry->expect))
23082310
return error("cannot parse expected object name '%s'", colon + 1);
23092311
return 0;

t/t5533-push-cas.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,30 @@ test_expect_success 'cover everything with default force-with-lease (allowed)' '
191191
test_cmp expect actual
192192
'
193193

194+
test_expect_success 'new branch covered by force-with-lease (explicit)' '
195+
setup_srcdst_basic &&
196+
(
197+
cd dst &&
198+
git branch branch master &&
199+
git push --force-with-lease=branch: origin branch
200+
) &&
201+
git ls-remote dst refs/heads/branch >expect &&
202+
git ls-remote src refs/heads/branch >actual &&
203+
test_cmp expect actual
204+
'
205+
206+
test_expect_success 'new branch already exists' '
207+
setup_srcdst_basic &&
208+
(
209+
cd src &&
210+
git checkout -b branch master &&
211+
test_commit c
212+
) &&
213+
(
214+
cd dst &&
215+
git branch branch master &&
216+
test_must_fail git push --force-with-lease=branch: origin branch
217+
)
218+
'
219+
194220
test_done

0 commit comments

Comments
 (0)