Skip to contentSkip to conversation

branch: add support for --dry-run option

PATCH In review Alexander Kuleshov 4 participants 5 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 branch: add support for --dry-run option

This patch adds support -d/--dry-run option for branch(es) deletion. If -d/--dry-run option passed to git branch -d branch..., branch(es) will not be removed, instead just print list of branches that are to be removed.

For example:

    $ git branch
    a
    b
    c
    * master
    $ git branch -d -n a b c
    delete branch 'a' (261c0d1)
    delete branch 'b' (261c0d1)
    delete branch 'c' (261c0d1)
---
 Documentation/git-branch.txt | 11 +++++++++--
 builtin/branch.c             | 13 +++++++++++++
 2 files changed, 22 insertions(+), 2 deletions(-)
Documentation/git-branch.txt +9 -2
index 311b336..32ea581 100644
@@ -16,7 +16,7 @@ SYNOPSIS
 'git branch' (--set-upstream-to=<upstream> | -u <upstream>) [<branchname>]
 'git branch' --unset-upstream [<branchname>]
 'git branch' (-m | -M) [<oldbranch>] <newbranch>
-'git branch' (-d | -D) [-r] <branchname>...
+'git branch' (-d | -D) [--dry-run | -n] [-r] <branchname>...
 'git branch' --edit-description [<branchname>]
 
 DESCRIPTION
@@ -63,7 +63,9 @@ to happen.
 
 With a `-d` or `-D` option, `<branchname>` will be deleted.  You may
 specify more than one branch for deletion.  If the branch currently
-has a reflog then the reflog will also be deleted.
+has a reflog then the reflog will also be deleted. If passed `-n` or
+`--dry-run` option, branch(es) will be not removed, but show a list of
+branches that are to be removed.
 
 Use `-r` together with `-d` to delete remote-tracking branches. Note, that it
 only makes sense to delete remote-tracking branches if they no longer exist
@@ -83,6 +85,11 @@ OPTIONS
 -D::
 	Delete a branch irrespective of its merged status.
 
+-n::
+--dry-run::
+	Don't remove the branch(es), but show a list of branches that are
+	to be removed.
+
 -l::
 --create-reflog::
 	Create the branch's reflog.  This activates recording of
builtin/branch.c +13 -0
index d8949cb..4a35a2f 100644
@@ -61,6 +61,7 @@ static unsigned char merge_filter_ref[20];
 
 static struct string_list output = STRING_LIST_INIT_DUP;
 static unsigned int colopts;
+static int branch_delete_show_only;
 
 static int parse_branch_color_slot(const char *slot)
 {
@@ -255,6 +256,17 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
 			continue;
 		}
 
+		if (branch_delete_show_only) {
+			printf(remote_branch
+			       ? _("delete remote branch '%s' (%s)\n")
+			       : _("delete branch '%s' (%s)\n"),
+			       bname.buf,
+			       (flags & REF_ISBROKEN) ? "broken"
+			       : (flags & REF_ISSYMREF) ? target
+			       : find_unique_abbrev(sha1, DEFAULT_ABBREV));
+			continue;
+		}
+
 		if (delete_ref(name, sha1, REF_NODEREF)) {
 			error(remote_branch
 			      ? _("Error deleting remote branch '%s'")
@@ -840,6 +852,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 			REF_REMOTE_BRANCH | REF_LOCAL_BRANCH),
 		OPT_BIT('d', "delete", &delete, N_("delete fully merged branch"), 1),
 		OPT_BIT('D', NULL, &delete, N_("delete branch (even if not merged)"), 2),
+		OPT__DRY_RUN(&branch_delete_show_only, N_("dry run")),
 		OPT_BIT('m', "move", &rename, N_("move/rename a branch and its reflog"), 1),
 		OPT_BIT('M', NULL, &rename, N_("move/rename a branch, even if target exists"), 2),
 		OPT_BOOL(0, "list", &list, N_("list branch names")),
Signature · 1 line
2.3.0.rc0.286.ga3dc223.dirty
#1 lore raw

From: "Alexander Kuleshov" <[email protected]>

Quoted 2 lines

This patch adds support -d/--dry-run option for branch(es) deletion. If -d/--dry-run option passed to git branch -d branch..., branch(es)

surely s|-d/--dry-run|-n/--dry-run|
-n is the short version of --dryrun. -d is already in use.

Quoted 3 lines

will not be removed, instead just print list of branches that are to be removed.

[...]
--
Philip

#2 lore raw
In reply to Alexander Kuleshov

Alexander Kuleshov schrieb am 17.01.2015 um 08:35:

Quoted 13 lines

This patch adds support -d/--dry-run option for branch(es) deletion. If -d/--dry-run option passed to git branch -d branch..., branch(es) will not be removed, instead just print list of branches that are to be removed.

For example:

    $ git branch
    a
    b
    c
    * master
Quoted 4 lines
    $ git branch -d -n a b c
    delete branch 'a' (261c0d1)
    delete branch 'b' (261c0d1)
    delete branch 'c' (261c0d1)

Is there a case where deleting "a b c" would not delete "a b c"?

In other words: What new information does a dry-run give the user, other than what "branch --list -v" would give already? (We could need a shortcut on the latter, but that is a different topic.)

Michael

#3 lore raw
Quoted 14 lines · Michael J Gruber

Alexander Kuleshov schrieb am 17.01.2015 um 08:35:

Quoted 13 lines

This patch adds support -d/--dry-run option for branch(es) deletion. If -d/--dry-run option passed to git branch -d branch..., branch(es) will not be removed, instead just print list of branches that are to be removed.

For example:

    $ git branch
    a
    b
    c
    * master
Quoted 6 lines
Quoted 4 lines
    $ git branch -d -n a b c
    delete branch 'a' (261c0d1)
    delete branch 'b' (261c0d1)
    delete branch 'c' (261c0d1)

Is there a case where deleting "a b c" would not delete "a b c"?

Sure:

$ cd /tmp/
$ git init foo
Initialized empty Git repository in /tmp/foo/.git/
$ cd foo/
$ touch .gitignore
$ git add .gitignore 
$ git commit -m init
[master (root-commit) fde5138] init
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 .gitignore
$ git checkout -b a
Switched to a new branch 'a'
$ git branch -d a
error: Cannot delete the branch 'a' which you are currently on.
$ touch file
$ git add file
$ git commit -m 'add file'
[a e2c2ece] add file
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file
$ git checkout -b b master
Switched to a new branch 'b'
$ git branch -d a
error: The branch 'a' is not fully merged.
If you are sure you want to delete it, run 'git branch -D a'.
Signature · 1 line
Scott Schmit
#4 lore raw

Scott Schmit schrieb am 22.01.2015 um 02:37:

Quoted 40 lines · Michael J Gruber
Quoted 14 lines

Alexander Kuleshov schrieb am 17.01.2015 um 08:35:

Quoted 13 lines

This patch adds support -d/--dry-run option for branch(es) deletion. If -d/--dry-run option passed to git branch -d branch..., branch(es) will not be removed, instead just print list of branches that are to be removed.

For example:

    $ git branch
    a
    b
    c
    * master
Quoted 6 lines
Quoted 4 lines
    $ git branch -d -n a b c
    delete branch 'a' (261c0d1)
    delete branch 'b' (261c0d1)
    delete branch 'c' (261c0d1)

Is there a case where deleting "a b c" would not delete "a b c"?

Sure:

$ cd /tmp/
$ git init foo
Initialized empty Git repository in /tmp/foo/.git/
$ cd foo/
$ touch .gitignore
$ git add .gitignore 
$ git commit -m init
[master (root-commit) fde5138] init
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 .gitignore
$ git checkout -b a
Switched to a new branch 'a'
$ git branch -d a
error: Cannot delete the branch 'a' which you are currently on.
$ touch file
$ git add file
$ git commit -m 'add file'
Quoted 8 lines
[a e2c2ece] add file
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file
$ git checkout -b b master
Switched to a new branch 'b'
$ git branch -d a
error: The branch 'a' is not fully merged.
If you are sure you want to delete it, run 'git branch -D a'.

Yes, and that is something that should go into the commit message. "Why do you want to add --dry-run? Because -d deletes only fully merged branches."

It should have been there in the 1st place, rather than forcing us to ask the question that always needs to answered for a patch: What is the intention? What is it good for?

In this case, we have other means to accomplish the same (--list -v), and they are more natural if you want get information about the state of the branches ("list verbose") than doing "delete dry-run".

Michael