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

[PATCH v5 1/6] checkout: factor out functions to new lib file

From
Thomas Gummerer <t.gummerer@gmail.com>
Date
Nov 26, 2017, 19:43 UTC
Message-ID
<20171126194356.16187-2-t.gummerer@gmail.com>
In-Reply-To
<20171126194356.16187-1-t.gummerer@gmail.com>

Factor the functions out, so they can be re-used from other places. In particular these functions will be re-used in builtin/worktree.c to make git worktree add dwim more.

While there add some docs to the function.
Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 Makefile           |  1 +
 builtin/checkout.c | 41 +----------------------------------------
 checkout.c         | 43 +++++++++++++++++++++++++++++++++++++++++++
 checkout.h         | 13 +++++++++++++
 4 files changed, 58 insertions(+), 40 deletions(-)
 create mode 100644 checkout.c
 create mode 100644 checkout.h
diff --git a/Makefile b/Makefile
index e53750ca01..a80a8fcca9 100644
--- a/Makefile
+++ b/Makefile
@@ -759,6 +759,7 @@ LIB_OBJS += branch.o
 LIB_OBJS += bulk-checkin.o
 LIB_OBJS += bundle.o
 LIB_OBJS += cache-tree.o
+LIB_OBJS += checkout.o
 LIB_OBJS += color.o
 LIB_OBJS += column.o
 LIB_OBJS += combine-diff.o
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 7d8bcc3833..ad8f94044c 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -1,5 +1,6 @@
 #include "builtin.h"
 #include "config.h"
+#include "checkout.h"
 #include "lockfile.h"
 #include "parse-options.h"
 #include "refs.h"
@@ -872,46 +873,6 @@ static int git_checkout_config(const char *var, const char *value, void *cb)
 	return git_xmerge_config(var, value, NULL);
 }
 
-struct tracking_name_data {
-	/* const */ char *src_ref;
-	char *dst_ref;
-	struct object_id *dst_oid;
-	int unique;
-};
-
-static int check_tracking_name(struct remote *remote, void *cb_data)
-{
-	struct tracking_name_data *cb = cb_data;
-	struct refspec query;
-	memset(&query, 0, sizeof(struct refspec));
-	query.src = cb->src_ref;
-	if (remote_find_tracking(remote, &query) ||
-	    get_oid(query.dst, cb->dst_oid)) {
-		free(query.dst);
-		return 0;
-	}
-	if (cb->dst_ref) {
-		free(query.dst);
-		cb->unique = 0;
-		return 0;
-	}
-	cb->dst_ref = query.dst;
-	return 0;
-}
-
-static const char *unique_tracking_name(const char *name, struct object_id *oid)
-{
-	struct tracking_name_data cb_data = { NULL, NULL, NULL, 1 };
-	cb_data.src_ref = xstrfmt("refs/heads/%s", name);
-	cb_data.dst_oid = oid;
-	for_each_remote(check_tracking_name, &cb_data);
-	free(cb_data.src_ref);
-	if (cb_data.unique)
-		return cb_data.dst_ref;
-	free(cb_data.dst_ref);
-	return NULL;
-}
-
 static int parse_branchname_arg(int argc, const char **argv,
 				int dwim_new_local_branch_ok,
 				struct branch_info *new,
diff --git a/checkout.c b/checkout.c
new file mode 100644
index 0000000000..ac42630f74
--- /dev/null
+++ b/checkout.c
@@ -0,0 +1,43 @@
+#include "cache.h"
+#include "remote.h"
+#include "checkout.h"
+
+struct tracking_name_data {
+	/* const */ char *src_ref;
+	char *dst_ref;
+	struct object_id *dst_oid;
+	int unique;
+};
+
+static int check_tracking_name(struct remote *remote, void *cb_data)
+{
+	struct tracking_name_data *cb = cb_data;
+	struct refspec query;
+	memset(&query, 0, sizeof(struct refspec));
+	query.src = cb->src_ref;
+	if (remote_find_tracking(remote, &query) ||
+	    get_oid(query.dst, cb->dst_oid)) {
+		free(query.dst);
+		return 0;
+	}
+	if (cb->dst_ref) {
+		free(query.dst);
+		cb->unique = 0;
+		return 0;
+	}
+	cb->dst_ref = query.dst;
+	return 0;
+}
+
+const char *unique_tracking_name(const char *name, struct object_id *oid)
+{
+	struct tracking_name_data cb_data = { NULL, NULL, NULL, 1 };
+	cb_data.src_ref = xstrfmt("refs/heads/%s", name);
+	cb_data.dst_oid = oid;
+	for_each_remote(check_tracking_name, &cb_data);
+	free(cb_data.src_ref);
+	if (cb_data.unique)
+		return cb_data.dst_ref;
+	free(cb_data.dst_ref);
+	return NULL;
+}
diff --git a/checkout.h b/checkout.h
new file mode 100644
index 0000000000..9980711179
--- /dev/null
+++ b/checkout.h
@@ -0,0 +1,13 @@
+#ifndef CHECKOUT_H
+#define CHECKOUT_H
+
+#include "cache.h"
+
+/*
+ * Check if the branch name uniquely matches a branch name on a remote
+ * tracking branch.  Return the name of the remote if such a branch
+ * exists, NULL otherwise.
+ */
+extern const char *unique_tracking_name(const char *name, struct object_id *oid);
+
+#endif /* CHECKOUT_H */
-- 
2.15.0.426.gb06021eeb
Previous: Thomas GummererNext: Thomas Gummerer
Message 20 of 36 in “make git worktree add dwim more”
  1. 0/4 make git worktree add dwim moreThomas Gummerer, Nov 22, 2017
  2. 4/4 worktree: make add <path> dwimThomas Gummerer, Nov 22, 2017
  3. Junio C HamanoNov 24, 2017
  4. Thomas GummererNov 25, 2017
  5. Paul SmithNov 25, 2017
  6. Thomas GummererNov 25, 2017
  7. Randall S. BeckerNov 25, 2017
  8. Thomas GummererNov 25, 2017
  9. Paul SmithNov 25, 2017
  10. Junio C HamanoNov 26, 2017
  11. Thomas GummererNov 26, 2017
  12. 3/4 worktree: make add <path> <branch> dwimThomas Gummerer, Nov 22, 2017
  13. Junio C HamanoNov 24, 2017
  14. 1/4 checkout: factor out functions to new lib fileThomas Gummerer, Nov 22, 2017
  15. Junio C HamanoNov 24, 2017
  16. 2/4 worktree: add --[no-]track option to the add subcommandThomas Gummerer, Nov 22, 2017
  17. Junio C HamanoNov 24, 2017
  18. Thomas GummererNov 25, 2017
  19. 0/6 make git worktree add dwim moreThomas Gummerer, Nov 26, 2017
  20. 1/6 checkout: factor out functions to new lib fileThomas Gummerer, Nov 26, 2017
  21. 2/6 worktree: add can be created from any commit-ishThomas Gummerer, Nov 26, 2017
  22. 3/6 worktree: add --[no-]track option to the add subcommandThomas Gummerer, Nov 26, 2017
  23. 4/6 worktree: make add <path> <branch> dwimThomas Gummerer, Nov 26, 2017
  24. 6/6 add worktree.guessRemote config optionThomas Gummerer, Nov 26, 2017
  25. Junio C HamanoNov 27, 2017
  26. Thomas GummererNov 27, 2017
  27. 5/6 worktree: add --guess-remote flag to add subcommandThomas Gummerer, Nov 26, 2017
  28. Junio C HamanoNov 27, 2017
  29. Thomas GummererNov 27, 2017
  30. 0/6 make git worktree add dwim moreThomas Gummerer, Nov 29, 2017
  31. 1/6 checkout: factor out functions to new lib fileThomas Gummerer, Nov 29, 2017
  32. 2/6 worktree: add can be created from any commit-ishThomas Gummerer, Nov 29, 2017
  33. 3/6 worktree: add --[no-]track option to the add subcommandThomas Gummerer, Nov 29, 2017
  34. 5/6 worktree: add --guess-remote flag to add subcommandThomas Gummerer, Nov 29, 2017
  35. 6/6 add worktree.guessRemote config optionThomas Gummerer, Nov 29, 2017
  36. 4/6 worktree: make add <path> <branch> dwimThomas Gummerer, Nov 29, 2017

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.