All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] teach the new "@{-1} syntax to "git branch"
@ 2009-02-14  7:08 Junio C Hamano
  2009-02-14  7:11 ` Jay Soffian
  0 siblings, 1 reply; 21+ messages in thread
From: Junio C Hamano @ 2009-02-14  7:08 UTC (permalink / raw)
  To: git

This teaches the new "@{-1} syntax to refer to the previous branch to "git
branch".  After looking at somebody's faulty patch series on a topic
branch too long, if you decide it is not worth merging, you can just say:

    $ git checkout master
    $ git branch -D @{-1}

to get rid of it without having to type the name of the topic you now hate
so much for wasting a lot of your time.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 branch.c         |   19 ++++++++++++++-----
 builtin-branch.c |   31 +++++++++++++++++++------------
 2 files changed, 33 insertions(+), 17 deletions(-)

diff --git a/branch.c b/branch.c
index b1ac837..1f00e44 100644
--- a/branch.c
+++ b/branch.c
@@ -103,14 +103,22 @@ void create_branch(const char *head,
 	struct ref_lock *lock;
 	struct commit *commit;
 	unsigned char sha1[20];
-	char *real_ref, ref[PATH_MAX], msg[PATH_MAX + 20];
+	char *real_ref, msg[PATH_MAX + 20];
+	struct strbuf ref = STRBUF_INIT;
 	int forcing = 0;
+	int len;
 
-	snprintf(ref, sizeof ref, "refs/heads/%s", name);
-	if (check_ref_format(ref))
+	len = strlen(name);
+	if (interpret_nth_last_branch(name, &ref) != len) {
+		strbuf_reset(&ref);
+		strbuf_add(&ref, name, len);
+	}
+	strbuf_splice(&ref, 0, 0, "refs/heads/", 11);
+
+	if (check_ref_format(ref.buf))
 		die("'%s' is not a valid branch name.", name);
 
-	if (resolve_ref(ref, sha1, 1, NULL)) {
+	if (resolve_ref(ref.buf, sha1, 1, NULL)) {
 		if (!force)
 			die("A branch named '%s' already exists.", name);
 		else if (!is_bare_repository() && !strcmp(head, name))
@@ -142,7 +150,7 @@ void create_branch(const char *head,
 		die("Not a valid branch point: '%s'.", start_name);
 	hashcpy(sha1, commit->object.sha1);
 
-	lock = lock_any_ref_for_update(ref, NULL, 0);
+	lock = lock_any_ref_for_update(ref.buf, NULL, 0);
 	if (!lock)
 		die("Failed to lock ref for update: %s.", strerror(errno));
 
@@ -162,6 +170,7 @@ void create_branch(const char *head,
 	if (write_ref_sha1(lock, sha1, msg) < 0)
 		die("Failed to write ref: %s.", strerror(errno));
 
+	strbuf_release(&ref);
 	free(real_ref);
 }
 
diff --git a/builtin-branch.c b/builtin-branch.c
index 56a1971..504a981 100644
--- a/builtin-branch.c
+++ b/builtin-branch.c
@@ -99,6 +99,7 @@ static int delete_branches(int argc, const char **argv, int force, int kinds)
 	const char *fmt, *remote;
 	int i;
 	int ret = 0;
+	struct strbuf bname = STRBUF_INIT;
 
 	switch (kinds) {
 	case REF_REMOTE_BRANCH:
@@ -119,20 +120,25 @@ static int delete_branches(int argc, const char **argv, int force, int kinds)
 		if (!head_rev)
 			die("Couldn't look up commit object for HEAD");
 	}
-	for (i = 0; i < argc; i++) {
-		if (kinds == REF_LOCAL_BRANCH && !strcmp(head, argv[i])) {
+	for (i = 0; i < argc; i++, strbuf_release(&bname)) {
+		int len = strlen(argv[i]);
+
+		if (interpret_nth_last_branch(argv[i], &bname) != len)
+			strbuf_add(&bname, argv[i], len);
+
+		if (kinds == REF_LOCAL_BRANCH && !strcmp(head, bname.buf)) {
 			error("Cannot delete the branch '%s' "
-				"which you are currently on.", argv[i]);
+			      "which you are currently on.", bname.buf);
 			ret = 1;
 			continue;
 		}
 
 		free(name);
 
-		name = xstrdup(mkpath(fmt, argv[i]));
+		name = xstrdup(mkpath(fmt, bname.buf));
 		if (!resolve_ref(name, sha1, 1, NULL)) {
 			error("%sbranch '%s' not found.",
-					remote, argv[i]);
+					remote, bname.buf);
 			ret = 1;
 			continue;
 		}
@@ -152,22 +158,23 @@ static int delete_branches(int argc, const char **argv, int force, int kinds)
 		if (!force &&
 		    !in_merge_bases(rev, &head_rev, 1)) {
 			error("The branch '%s' is not an ancestor of "
-				"your current HEAD.\n"
-				"If you are sure you want to delete it, "
-				"run 'git branch -D %s'.", argv[i], argv[i]);
+			      "your current HEAD.\n"
+			      "If you are sure you want to delete it, "
+			      "run 'git branch -D %s'.", bname.buf, bname.buf);
 			ret = 1;
 			continue;
 		}
 
 		if (delete_ref(name, sha1, 0)) {
 			error("Error deleting %sbranch '%s'", remote,
-			       argv[i]);
+			      bname.buf);
 			ret = 1;
 		} else {
 			struct strbuf buf = STRBUF_INIT;
-			printf("Deleted %sbranch %s (%s).\n", remote, argv[i],
-				find_unique_abbrev(sha1, DEFAULT_ABBREV));
-			strbuf_addf(&buf, "branch.%s", argv[i]);
+			printf("Deleted %sbranch %s (%s).\n", remote,
+			       bname.buf,
+			       find_unique_abbrev(sha1, DEFAULT_ABBREV));
+			strbuf_addf(&buf, "branch.%s", bname.buf);
 			if (git_config_rename_section(buf.buf, NULL) < 0)
 				warning("Update of config-file failed");
 			strbuf_release(&buf);
-- 
1.6.2.rc0.80.ge11dd

^ permalink raw reply related	[flat|nested] 21+ messages in thread

end of thread, other threads:[~2009-02-27  6:51 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-02-14  7:08 [PATCH] teach the new "@{-1} syntax to "git branch" Junio C Hamano
2009-02-14  7:11 ` Jay Soffian
2009-02-14  7:21   ` Junio C Hamano
2009-02-14  9:37     ` [PATCH v2 1/2] Teach the " Junio C Hamano
2009-02-14  9:39     ` [PATCH v2 2/2] Teach @{-1} to git merge Junio C Hamano
2009-02-14 22:52       ` Johannes Schindelin
2009-02-15  2:05         ` Junio C Hamano
2009-02-15  2:32           ` Junio C Hamano
2009-02-15  9:24           ` Nanako Shiraishi
2009-02-15 10:02             ` Junio C Hamano
2009-02-15 11:21           ` Johannes Schindelin
2009-02-16  4:16             ` Junio C Hamano
2009-02-16 10:41               ` Johannes Schindelin
2009-02-16 16:40                 ` Jay Soffian
2009-02-26  0:11                 ` Junio C Hamano
2009-02-26 10:18                   ` Johannes Schindelin
2009-02-27  4:27         ` Deskin Miller
2009-02-27  5:24           ` Junio C Hamano
2009-02-27  6:10             ` [PATCH] git-branch: a detached HEAD and branch called HEAD are different Junio C Hamano
2009-02-27  6:12             ` [PATCH] "git branch -M @{-1}" Junio C Hamano
2009-02-27  6:49               ` Junio C Hamano

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.