Merge branch 'jk/push-to-delete'
authorJunio C Hamano <gitster@pobox.com>
Sun, 10 Jan 2010 16:52:45 +0000 (08:52 -0800)
committerJunio C Hamano <gitster@pobox.com>
Sun, 10 Jan 2010 16:52:45 +0000 (08:52 -0800)
* jk/push-to-delete:
builtin-push: add --delete as syntactic sugar for :foo

1  2 
builtin-push.c
t/t5516-fetch-push.sh
diff --combined builtin-push.c
index dcfb53f1884d2648993214b4378afbd1f83760b0,ce6b0df464a3bf9074357252005143748af0f946..f7661d245e6abb7be3bddee5141403c911c9ec43
@@@ -15,6 -15,7 +15,7 @@@ static const char * const push_usage[] 
  };
  
  static int thin;
+ static int deleterefs;
  static const char *receivepack;
  
  static const char **refspec;
@@@ -39,11 -40,24 +40,24 @@@ static void set_refspecs(const char **r
                        if (nr <= ++i)
                                die("tag shorthand without <tag>");
                        len = strlen(refs[i]) + 11;
-                       tag = xmalloc(len);
-                       strcpy(tag, "refs/tags/");
+                       if (deleterefs) {
+                               tag = xmalloc(len+1);
+                               strcpy(tag, ":refs/tags/");
+                       } else {
+                               tag = xmalloc(len);
+                               strcpy(tag, "refs/tags/");
+                       }
                        strcat(tag, refs[i]);
                        ref = tag;
-               }
+               } else if (deleterefs && !strchr(ref, ':')) {
+                       char *delref;
+                       int len = strlen(ref)+1;
+                       delref = xmalloc(len);
+                       strcpy(delref, ":");
+                       strcat(delref, ref);
+                       ref = delref;
+               } else if (deleterefs)
+                       die("--delete only accepts plain target ref names");
                add_refspec(ref);
        }
  }
@@@ -87,37 -101,6 +101,37 @@@ static void setup_default_push_refspecs
        }
  }
  
 +static int push_with_options(struct transport *transport, int flags)
 +{
 +      int err;
 +      int nonfastforward;
 +      if (receivepack)
 +              transport_set_option(transport,
 +                                   TRANS_OPT_RECEIVEPACK, receivepack);
 +      if (thin)
 +              transport_set_option(transport, TRANS_OPT_THIN, "yes");
 +
 +      if (flags & TRANSPORT_PUSH_VERBOSE)
 +              fprintf(stderr, "Pushing to %s\n", transport->url);
 +      err = transport_push(transport, refspec_nr, refspec, flags,
 +                           &nonfastforward);
 +      if (err != 0)
 +              error("failed to push some refs to '%s'", transport->url);
 +
 +      err |= transport_disconnect(transport);
 +
 +      if (!err)
 +              return 0;
 +
 +      if (nonfastforward && advice_push_nonfastforward) {
 +              printf("To prevent you from losing history, non-fast-forward updates were rejected\n"
 +                     "Merge the remote changes before pushing again.  See the 'non-fast-forward'\n"
 +                     "section of 'git push --help' for details.\n");
 +      }
 +
 +      return 1;
 +}
 +
  static int do_push(const char *repo, int flags)
  {
        int i, errs;
                url = remote->url;
                url_nr = remote->url_nr;
        }
 -      for (i = 0; i < url_nr; i++) {
 -              struct transport *transport =
 -                      transport_get(remote, url[i]);
 -              int err;
 -              int nonfastforward;
 -              if (receivepack)
 -                      transport_set_option(transport,
 -                                           TRANS_OPT_RECEIVEPACK, receivepack);
 -              if (thin)
 -                      transport_set_option(transport, TRANS_OPT_THIN, "yes");
 -
 -              if (flags & TRANSPORT_PUSH_VERBOSE)
 -                      fprintf(stderr, "Pushing to %s\n", url[i]);
 -              err = transport_push(transport, refspec_nr, refspec, flags,
 -                                   &nonfastforward);
 -              err |= transport_disconnect(transport);
 -
 -              if (!err)
 -                      continue;
 -
 -              error("failed to push some refs to '%s'", url[i]);
 -              if (nonfastforward && advice_push_nonfastforward) {
 -                      printf("To prevent you from losing history, non-fast-forward updates were rejected\n"
 -                             "Merge the remote changes before pushing again.  See the 'non-fast-forward'\n"
 -                             "section of 'git push --help' for details.\n");
 +      if (url_nr) {
 +              for (i = 0; i < url_nr; i++) {
 +                      struct transport *transport =
 +                              transport_get(remote, url[i]);
 +                      if (push_with_options(transport, flags))
 +                              errs++;
                }
 -              errs++;
 +      } else {
 +              struct transport *transport =
 +                      transport_get(remote, NULL);
 +
 +              if (push_with_options(transport, flags))
 +                      errs++;
        }
        return !!errs;
  }
@@@ -196,6 -193,7 +210,7 @@@ int cmd_push(int argc, const char **arg
                OPT_BIT( 0 , "all", &flags, "push all refs", TRANSPORT_PUSH_ALL),
                OPT_BIT( 0 , "mirror", &flags, "mirror all refs",
                            (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
+               OPT_BOOLEAN( 0, "delete", &deleterefs, "delete refs"),
                OPT_BOOLEAN( 0 , "tags", &tags, "push tags (can't be used with --all or --mirror)"),
                OPT_BIT('n' , "dry-run", &flags, "dry run", TRANSPORT_PUSH_DRY_RUN),
                OPT_BIT( 0,  "porcelain", &flags, "machine-readable output", TRANSPORT_PUSH_PORCELAIN),
        git_config(git_default_config, NULL);
        argc = parse_options(argc, argv, prefix, options, push_usage, 0);
  
+       if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
+               die("--delete is incompatible with --all, --mirror and --tags");
+       if (deleterefs && argc < 2)
+               die("--delete doesn't make sense without any refs");
        if (tags)
                add_refspec("refs/tags/*");
  
diff --combined t/t5516-fetch-push.sh
index 516127b5390bafbd725564e91fb24ead224cf3a7,782ddb2ec7009cfb1b1c8aa42a0f8b1863e87b6a..0f04b2e8949dfec46fbc42af8347c9f3e6d302a7
@@@ -12,7 -12,6 +12,7 @@@ mk_empty () 
        (
                cd testrepo &&
                git init &&
 +              git config receive.denyCurrentBranch warn &&
                mv .git/hooks .git/hooks-disabled
        )
  }
@@@ -547,6 -546,32 +547,32 @@@ test_expect_success 'allow deleting an 
  
  '
  
+ test_expect_success 'allow deleting a ref using --delete' '
+       mk_test heads/master &&
+       (cd testrepo && git config receive.denyDeleteCurrent warn) &&
+       git push testrepo --delete master &&
+       (cd testrepo && test_must_fail git rev-parse --verify refs/heads/master)
+ '
+ test_expect_success 'allow deleting a tag using --delete' '
+       mk_test heads/master &&
+       git tag -a -m dummy_message deltag heads/master &&
+       git push testrepo --tags &&
+       (cd testrepo && git rev-parse --verify -q refs/tags/deltag) &&
+       git push testrepo --delete tag deltag &&
+       (cd testrepo && test_must_fail git rev-parse --verify refs/tags/deltag)
+ '
+ test_expect_success 'push --delete without args aborts' '
+       mk_test heads/master &&
+       test_must_fail git push testrepo --delete
+ '
+ test_expect_success 'push --delete refuses src:dest refspecs' '
+       mk_test heads/master &&
+       test_must_fail git push testrepo --delete master:foo
+ '
  test_expect_success 'warn on push to HEAD of non-bare repository' '
        mk_test heads/master
        (cd testrepo &&