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 <john@keeping.me.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>

John Keeping committed Jul 26, 2016 at 21:44 UTC eee98e74f928a49c310038c77026ebc04e6cf4b2
3 files changed +30 -1
Documentation/git-push.txt
+2 -1
@@ -201,7 +201,8 @@ if it is going to be updated, by requiring its current value to be
201 the same as the specified value `<expect>` (which is allowed to be
202 different from the remote-tracking branch we have for the refname,
203 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.
206 +
207 Note that all forms other than `--force-with-lease=<refname>:<expect>`
208 that specifies the expected current value of the ref explicitly are
remote.c
+2
@@ -2304,6 +2304,8 @@ int parse_push_cas_option(struct push_cas_option *cas, const char *arg, int unse
2304 entry = add_cas_entry(cas, arg, colon - arg);
2305 if (!*colon)
2306 entry->use_tracking = 1;
2307 + else if (!colon[1])
2308 + hashclr(entry->expect);
2309 else if (get_sha1(colon + 1, entry->expect))
2310 return error("cannot parse expected object name '%s'", colon + 1);
2311 return 0;
t/t5533-push-cas.sh
+26
@@ -191,4 +191,30 @@ test_expect_success 'cover everything with default force-with-lease (allowed)' '
191 test_cmp expect actual
192 '
193
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 +
220 test_done