git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/3] builtin/checkout: change -b from an OPTION_STRING to a OPTION_SET_INT

From
Tay Ray Chuan <rctay89@gmail.com>
Date
Jun 21, 2010, 17:19 UTC
Message-ID
<1277140782-4064-3-git-send-email-rctay89@gmail.com>
In-Reply-To
<1277140782-4064-2-git-send-email-rctay89@gmail.com>
This is in preparation for multiple levels of "-b".

To check this change's interaction with --track, the following tests (obtained with grep -l "\-\-track" t/*.sh)were run, and they passed.

  t1507-rev-parse-upstream.sh
  t3200-branch.sh
  t5505-remote.sh
  t5520-pull.sh
  t6040-tracking-info.sh
  t7201-co.sh
(t9114-git-svn-dcommit-merge.sh was excluded as I don't have svn.)
Signed-off-by: Tay Ray Chuan <rctay89@gmail.com>
---
 builtin/checkout.c |   41 ++++++++++++++++++++++++++---------------
 1 files changed, 26 insertions(+), 15 deletions(-)
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 1994be9..e794e1e 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -32,7 +32,8 @@ struct checkout_opts {
 	int writeout_stage;
 	int writeout_error;
 
-	const char *new_branch;
+	int new_branch;
+	const char *new_branch_name;
 	const char *new_orphan_branch;
 	int new_branch_log;
 	enum branch_track track;
@@ -492,7 +493,7 @@ static void update_refs_for_switch(struct checkout_opts *opts,
 {
 	struct strbuf msg = STRBUF_INIT;
 	const char *old_desc;
-	if (opts->new_branch) {
+	if (opts->new_branch_name) {
 		if (opts->new_orphan_branch) {
 			if (opts->new_branch_log && !log_all_ref_updates) {
 				int temp;
@@ -511,9 +512,9 @@ static void update_refs_for_switch(struct checkout_opts *opts,
 			}
 		}
 		else
-			create_branch(old->name, opts->new_branch, new->name, 0,
+			create_branch(old->name, opts->new_branch_name, new->name, 0,
 				      opts->new_branch_log, opts->track);
-		new->name = opts->new_branch;
+		new->name = opts->new_branch_name;
 		setup_branch_path(new);
 	}
 
@@ -531,7 +532,7 @@ static void update_refs_for_switch(struct checkout_opts *opts,
 					new->name);
 			else
 				fprintf(stderr, "Switched to%s branch '%s'\n",
-					opts->new_branch ? " a new" : "",
+					opts->new_branch_name ? " a new" : "",
 					new->name);
 		}
 		if (old->path && old->name) {
@@ -657,7 +658,8 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
 	int dwim_new_local_branch = 1;
 	struct option options[] = {
 		OPT__QUIET(&opts.quiet),
-		OPT_STRING('b', NULL, &opts.new_branch, "new branch", "branch"),
+		OPT_SET_INT('b', NULL, &opts.new_branch,
+			    "create and checkout a new branch", 1),
 		OPT_BOOLEAN('l', NULL, &opts.new_branch_log, "log for new branch"),
 		OPT_SET_INT('t', "track",  &opts.track, "track",
 			BRANCH_TRACK_EXPLICIT),
@@ -692,8 +694,17 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
 			   || opts.new_branch_log || opts.merge || opts.force))
 		die ("--patch is incompatible with all other options");
 
+	if (opts.new_branch > 0) {
+		const char *arg = argv[0];
+		if (!argc || !strcmp(arg, "--"))
+			die ("Missing branch name");
+		opts.new_branch_name = arg;
+		argv++;
+		argc--;
+	}
+
 	/* --track without -b should DWIM */
-	if (0 < opts.track && !opts.new_branch) {
+	if (0 < opts.track && !opts.new_branch_name) {
 		const char *argv0 = argv[0];
 		if (!argc || !strcmp(argv0, "--"))
 			die ("--track needs a branch name");
@@ -704,7 +715,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
 		argv0 = strchr(argv0, '/');
 		if (!argv0 || !argv0[1])
 			die ("Missing branch name; try -b");
-		opts.new_branch = argv0 + 1;
+		opts.new_branch_name = argv0 + 1;
 	}
 
 	if (opts.new_orphan_branch) {
@@ -712,7 +723,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
 			die("--orphan and -b are mutually exclusive");
 		if (opts.track > 0)
 			die("--orphan cannot be used with -t");
-		opts.new_branch = opts.new_orphan_branch;
+		opts.new_branch_name = opts.new_orphan_branch;
 	}
 
 	if (conflict_style) {
@@ -771,13 +782,13 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
 			if (!patch_mode &&
 			    dwim_new_local_branch &&
 			    opts.track == BRANCH_TRACK_UNSPECIFIED &&
-			    !opts.new_branch &&
+			    !opts.new_branch_name &&
 			    !check_filename(NULL, arg) &&
 			    argc == 1) {
 				const char *remote = unique_tracking_name(arg);
 				if (!remote || get_sha1(remote, rev))
 					goto no_reference;
-				opts.new_branch = arg;
+				opts.new_branch_name = arg;
 				arg = remote;
 				/* DWIMmed to create local branch */
 			}
@@ -853,13 +864,13 @@ no_reference:
 	if (patch_mode)
 		return interactive_checkout(new.name, NULL, &opts);
 
-	if (opts.new_branch) {
+	if (opts.new_branch_name) {
 		struct strbuf buf = STRBUF_INIT;
-		if (strbuf_check_branch_ref(&buf, opts.new_branch))
+		if (strbuf_check_branch_ref(&buf, opts.new_branch_name))
 			die("git checkout: we do not like '%s' as a branch name.",
-			    opts.new_branch);
+			    opts.new_branch_name);
 		if (!get_sha1(buf.buf, rev))
-			die("git checkout: branch %s already exists", opts.new_branch);
+			die("git checkout: branch %s already exists", opts.new_branch_name);
 		strbuf_release(&buf);
 	}
 
-- 
1.7.1.513.g4f18
Previous: Tay Ray ChuanNext: Tay Ray Chuan
Message 15 of 29 in “DWIM with "git checkout -f -b"”
  1. 0/3 DWIM with "git checkout -f -b"Tay Ray Chuan, Jun 20, 2010
  2. 1/3 add tests for checkout -bTay Ray Chuan, Jun 20, 2010
  3. 2/3 t2018-checkout-branch.sh: show that checkout -f -b doesn't DWIMTay Ray Chuan, Jun 20, 2010
  4. 3/3 builtin/checkout: DWIM with -f -bTay Ray Chuan, Jun 20, 2010
  5. Junio C HamanoJun 20, 2010
  6. Jeff KingJun 20, 2010
  7. Clemens BuchacherJun 20, 2010
  8. Junio C HamanoJun 20, 2010
  9. Jeff KingJun 20, 2010
  10. Junio C HamanoJun 21, 2010
  11. Tay Ray ChuanJun 21, 2010
  12. Junio C HamanoJun 21, 2010
  13. 0/3 teach git-checkout -B WAS builtin/checkout: DWIM with -f -bTay Ray Chuan, Jun 21, 2010
  14. 1/3 add tests for checkout -bTay Ray Chuan, Jun 21, 2010
  15. 2/3 builtin/checkout: change -b from an OPTION_STRING to a OPTION_SET_INTTay Ray Chuan, Jun 21, 2010
  16. 3/3 builtin/checkout: learn -BTay Ray Chuan, Jun 21, 2010
  17. Junio C HamanoJun 23, 2010
  18. Tay Ray ChuanJun 23, 2010
  19. Erick MattosJun 21, 2010
  20. Tay Ray ChuanJun 23, 2010
  21. Erick MattosJun 23, 2010
  22. Tay Ray ChuanJun 21, 2010
  23. Erick MattosJun 21, 2010
  24. 0/3 teach git-checkout -B WAS builtin/checkout: DWIM with -f -bTay Ray Chuan, Jun 23, 2010
  25. 1/3 add tests for checkout -bTay Ray Chuan, Jun 23, 2010
  26. 2/3 builtin/checkout: reword hint for -bTay Ray Chuan, Jun 23, 2010
  27. 3/3 builtin/checkout: learn -BTay Ray Chuan, Jun 23, 2010
  28. Michel LespinasseJun 21, 2010
  29. Michel LespinasseJun 21, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.