threads / patch / 14037

patchgit-push: add a --delete flag to allow branch deletion

Subject: [PATCH] git-push: add a --delete flag to allow branch deletion

## tl;dr

5 messages between Jun 19, 2008 and Jun 20, 2008. Diffs are folded; open one to read it.

replies: 4people: 4as markdown or json

Pieter de Bie· Jun 19, 2008, 14:13 UTC · lore

While this is already possible with the :<dst> refspec syntax, a flag specifically for this action is more userfriendly.

Signed-off-by: Pieter de Bie <pdebie@ai.rug.nl>
---

The question on how to delete remote branches appears frequently in the irc channel. If we add an explicit option to do this to git push, hopefully it will be more clear.

 Documentation/git-push.txt |   13 ++++++++++++-
 builtin-push.c             |   37 +++++++++++++++++++++++++++++++++++--
 t/t5516-fetch-push.sh      |   13 +++++++++++++
 3 files changed, 60 insertions(+), 3 deletions(-)
Show changes to 3 files +60 −3

Documentation/git-push.txt, builtin-push.c, t/t5516-fetch-push.sh

diff --git a/Documentation/git-push.txt b/Documentation/git-push.txt
index 89e0049..6a772a5 100644
--- a/Documentation/git-push.txt
+++ b/Documentation/git-push.txt
@@ -11,6 +11,8 @@ SYNOPSIS
 [verse]
 'git-push' [--all] [--dry-run] [--tags] [--receive-pack=<git-receive-pack>]
            [--repo=all] [-f | --force] [-v | --verbose] [<repository> <refspec>...]
+'git push' --delete <repository> <branchname> [<branch2> ...]
+
 
 DESCRIPTION
 -----------
@@ -18,6 +20,8 @@ DESCRIPTION
 Updates remote refs using local refs, while sending objects
 necessary to complete the given refs.
 
+In the second form, deletes one or more branches on a given repository.
+
 You can make interesting things happen to a repository
 every time you push into it, by setting up 'hooks' there.  See
 documentation for linkgit:git-receive-pack[1].
@@ -52,7 +56,7 @@ A parameter <ref> without a colon pushes the <ref> from the source
 repository to the destination repository under the same name.
 +
 Pushing an empty <src> allows you to delete the <dst> ref from
-the remote repository.
+the remote repository. The same can be done by using the --delete flag.
 +
 The special refspec `:` (or `+:` to allow non-fast forward updates)
 directs git to push "matching" heads: for every head that exists on
@@ -75,6 +79,10 @@ nor in any Push line of the corresponding remotes file---see below).
 	if the configuration option `remote.<remote>.mirror` is
 	set.
 
+--delete::
+	Does not push anything, but instead deletes <branchname> on the
+	remote repository.
+
 --dry-run::
 	Do everything except actually send the updates.
 
@@ -179,6 +187,9 @@ git push origin :experimental::
 	Find a ref that matches `experimental` in the `origin` repository
 	(e.g. `refs/heads/experimental`), and delete it.
 
+git push --delete origin experimental::
+	Does the same as the above, but is more friendly.
+
 git push origin master:satellite/master::
 	Find a ref that matches `master` in the source repository
 	(most likely, it would find `refs/heads/master`), and update
diff --git a/builtin-push.c b/builtin-push.c
index b35aad6..81f9a58 100644
--- a/builtin-push.c
+++ b/builtin-push.c
@@ -10,7 +10,8 @@
 #include "parse-options.h"
 
 static const char * const push_usage[] = {
-	"git-push [--all | --mirror] [--dry-run] [--tags] [--receive-pack=<git-receive-pack>] [--repo=all] [-f | --force] [-v] [<repository> <refspec>...]",
+	"git push [--all | --mirror] [--dry-run] [--tags] [--receive-pack=<git-receive-pack>] [--repo=all] [-f | --force] [-v] [<repository> <refspec>...]",
+	"git push --delete <repository> <branchname> [<branch2> ...]",
 	NULL,
 };
 
@@ -48,6 +49,24 @@ static void set_refspecs(const char **refs, int nr)
 	}
 }
 
+static void set_delete_refspecs(const char **branches, int nr)
+{
+	int i;
+	for (i = 0; i < nr; i++) {
+		const char *branch = branches[i];
+		int len;
+		char *delete_refspec;
+
+		if (strchr(branch, ':'))
+			die("%s is not a valid branchname", branch);
+		len = strlen(branch) + 1;
+		delete_refspec = xmalloc(len);
+		*delete_refspec = ':';
+		strcpy(delete_refspec + 1, branch);
+		add_refspec(delete_refspec);
+	}
+}
+
 static int do_push(const char *repo, int flags)
 {
 	int i, errs;
@@ -108,6 +127,7 @@ int cmd_push(int argc, const char **argv, const char *prefix)
 	int tags = 0;
 	int rc;
 	const char *repo = NULL;	/* default repository */
+	int delete = 0;
 
 	struct option options[] = {
 		OPT__VERBOSE(&verbose),
@@ -118,6 +138,7 @@ int cmd_push(int argc, const char **argv, const char *prefix)
 		OPT_BOOLEAN( 0 , "dry-run", &dry_run, "dry run"),
 		OPT_BOOLEAN('f', "force", &force, "force updates"),
 		OPT_BOOLEAN( 0 , "thin", &thin, "use thin pack"),
+		OPT_BOOLEAN('d', "delete", &delete, "delete remote branch"),
 		OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", "receive pack program"),
 		OPT_STRING( 0 , "exec", &receivepack, "receive-pack", "receive pack program"),
 		OPT_END()
@@ -125,6 +146,18 @@ int cmd_push(int argc, const char **argv, const char *prefix)
 
 	argc = parse_options(argc, argv, options, push_usage, 0);
 
+	if (delete) {
+		if (all || mirror || tags || force || repo)
+			die("--delete cannot be used with --all, --mirror, --tags, --farce or --repo");
+
+		if (argc == 0)
+			die("Need a remote on which to delete a branch");
+		if (argc == 1)
+			die("Need a branch on the remote to delete");
+		repo = argv[0];
+		set_delete_refspecs(argv + 1, argc - 1);
+	}
+
 	if (force)
 		flags |= TRANSPORT_PUSH_FORCE;
 	if (dry_run)
@@ -138,7 +171,7 @@ int cmd_push(int argc, const char **argv, const char *prefix)
 	if (mirror)
 		flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
 
-	if (argc > 0) {
+	if (!delete && argc > 0) {
 		repo = argv[0];
 		set_refspecs(argv + 1, argc - 1);
 	}
diff --git a/t/t5516-fetch-push.sh b/t/t5516-fetch-push.sh
index 6805032..42fe89c 100755
--- a/t/t5516-fetch-push.sh
+++ b/t/t5516-fetch-push.sh
@@ -361,6 +361,19 @@ test_expect_success 'push with HEAD nonexisting at remote' '
 	git push testrepo HEAD &&
 	check_push_result $the_commit heads/local
 '
+test_expect_success 'delete with --delete syntax' '
+
+	git push testrepo master:test1 &&
+	git push --delete testrepo test1 &&
+	git push testrepo master:test1 master:test2 &&
+	check_push_result $the_commit heads/test1 &&
+	check_push_result $the_commit heads/test2 &&
+	git push --delete testrepo test1 test2 &&
+	!(check_push_result $the_commit heads/test1) &&
+	!(check_push_result $the_commit heads/test2) &&
+	test_must_fail git push --delete testrepo test1
+
+'
 
 test_expect_success 'push with +HEAD' '
 
-- 
1.5.6.rc1.153.gc1d96
Andreas Ericsson· Jun 19, 2008, 22:30 UTC · re: Pieter de Bie · lore

Re: [PATCH] git-push: add a --delete flag to allow branch deletion

Pieter de Bie wrote:
> While this is already possible with the :<dst> refspec syntax,
> a flag specifically for this action is more userfriendly.
> 

I like it. I always have to tell my co-workers which side of the colon the branch-name to delete should be on. Since they want the remote to lose the branch, it seems logical to some of them to make that side empty. This would make one (very small) headache of mine go away.

Question though; does it delete the currently checked out branch if no branch-name is given? I'd prefer if one has to explicitly name the branch to delete.

-- 
Andreas Ericsson                   andreas.ericsson@op5.se
OP5 AB                             www.op5.se
Tel: +46 8-230225                  Fax: +46 8-230231
Pieter de Bie· Jun 19, 2008, 22:55 UTC · re: Andreas Ericsson · lore

Re: [PATCH] git-push: add a --delete flag to allow branch deletion

On 20 jun 2008, at 00:30, Andreas Ericsson wrote:
> Question though; does it delete the currently checked out
> branch if no branch-name is given? I'd prefer if one has
> to explicitly name the branch to delete.

You have to explicitly give it a remote and one or more branches to delete. I thought it would be best to be unambiguous in cases where deletion is concerned :)

- Pieter
Sverre Rabbelier· Jun 19, 2008, 23:20 UTC · re: Pieter de Bie · lore

Re: [PATCH] git-push: add a --delete flag to allow branch deletion

On Fri, Jun 20, 2008 at 12:55 AM, Pieter de Bie <pdebie@ai.rug.nl> wrote:
Show 9 quoted lines
> On 20 jun 2008, at 00:30, Andreas Ericsson wrote:
>
>> Question though; does it delete the currently checked out
>> branch if no branch-name is given? I'd prefer if one has
>> to explicitly name the branch to delete.
>
> You have to explicitly give it a remote and one or more branches
> to delete. I thought it would be best to be unambiguous in cases
> where deletion is concerned :)

I like it too, it's one of the most unintuitive parts of the git interface imo. I am guessing there is no "-d" switch because deletion is involved? Then again, there is a "git branch -d/D"?

-- 
Cheers,

Sverre Rabbelier
Junio C Hamano· Jun 20, 2008, 00:51 UTC · re: Pieter de Bie · lore

Re: [PATCH] git-push: add a --delete flag to allow branch deletion

Pieter de Bie <pdebie@ai.rug.nl> writes:
Show 19 quoted lines
> diff --git a/Documentation/git-push.txt b/Documentation/git-push.txt
> index 89e0049..6a772a5 100644
> --- a/Documentation/git-push.txt
> +++ b/Documentation/git-push.txt
> @@ -11,6 +11,8 @@ SYNOPSIS
>  [verse]
>  'git-push' [--all] [--dry-run] [--tags] [--receive-pack=<git-receive-pack>]
>             [--repo=all] [-f | --force] [-v | --verbose] [<repository> <refspec>...]
> +'git push' --delete <repository> <branchname> [<branch2> ...]
> +
>  
>  DESCRIPTION
>  -----------
> @@ -18,6 +20,8 @@ DESCRIPTION
>  Updates remote refs using local refs, while sending objects
>  necessary to complete the given refs.
>  
> +In the second form, deletes one or more branches on a given repository.
> +

Is this really about branches? Is it usable to somehow delete tags, and if so how?

What happens if you have a branch 'foo' and a tag 'foo' in the remote repository and you say "git push --delete $there foo"?

Show 7 quoted lines
> @@ -125,6 +146,18 @@ int cmd_push(int argc, const char **argv, const char *prefix)
>  
>  	argc = parse_options(argc, argv, options, push_usage, 0);
>  
> +	if (delete) {
> +		if (all || mirror || tags || force || repo)
> +			die("--delete cannot be used with --all, --mirror, --tags, --farce or --repo");
Nice try, but it is not so amusing.

← back to recent threads