All of lore.kernel.org
 help / color / mirror / Atom feed
From: Thomas Gummerer <t.gummerer@gmail.com>
To: git@vger.kernel.org
Cc: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>,
	"Junio C Hamano" <gitster@pobox.com>,
	"Eric Sunshine" <sunshine@sunshineco.com>,
	"Thomas Gummerer" <t.gummerer@gmail.com>
Subject: [PATCH v4 1/4] checkout: factor out functions to new lib file
Date: Wed, 22 Nov 2017 22:30:17 +0000	[thread overview]
Message-ID: <20171122223020.2780-2-t.gummerer@gmail.com> (raw)
In-Reply-To: <20171122223020.2780-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         | 42 ++++++++++++++++++++++++++++++++++++++++++
 checkout.h         | 13 +++++++++++++
 4 files changed, 57 insertions(+), 40 deletions(-)
 create mode 100644 checkout.c
 create mode 100644 checkout.h

diff --git a/Makefile b/Makefile
index cd75985991..8d603c7443 100644
--- a/Makefile
+++ b/Makefile
@@ -757,6 +757,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 fc4f8fd2ea..9e1cfd10b3 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"
@@ -874,46 +875,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..b0c744d37a
--- /dev/null
+++ b/checkout.c
@@ -0,0 +1,42 @@
+#include "cache.h"
+#include "remote.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.345.gf926f18f3


  reply	other threads:[~2017-11-22 22:30 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailto:20171118224706.13810-1-t.gummerer@gmail.com>
2017-11-22 22:30 ` [PATCH v4 0/4] make git worktree add dwim more Thomas Gummerer
2017-11-22 22:30   ` Thomas Gummerer [this message]
2017-11-24  6:47     ` [PATCH v4 1/4] checkout: factor out functions to new lib file Junio C Hamano
2017-11-22 22:30   ` [PATCH v4 2/4] worktree: add --[no-]track option to the add subcommand Thomas Gummerer
2017-11-24  6:57     ` Junio C Hamano
2017-11-25 16:58       ` Thomas Gummerer
2017-11-22 22:30   ` [PATCH v4 3/4] worktree: make add <path> <branch> dwim Thomas Gummerer
2017-11-24  6:59     ` Junio C Hamano
2017-11-22 22:30   ` [PATCH v4 4/4] worktree: make add <path> dwim Thomas Gummerer
2017-11-24  7:11     ` Junio C Hamano
2017-11-25 17:50       ` Thomas Gummerer
2017-11-25 18:26         ` Paul Smith
2017-11-25 20:06           ` Thomas Gummerer
2017-11-25 20:39             ` Randall S. Becker
2017-11-25 21:48               ` Thomas Gummerer
2017-11-25 23:11             ` Paul Smith
2017-11-26  3:35         ` Junio C Hamano
2017-11-26 11:37           ` Thomas Gummerer
2017-11-26 19:43   ` [PATCH v5 0/6] make git worktree add dwim more Thomas Gummerer
2017-11-26 19:43     ` [PATCH v5 1/6] checkout: factor out functions to new lib file Thomas Gummerer
2017-11-26 19:43     ` [PATCH v5 2/6] worktree: add can be created from any commit-ish Thomas Gummerer
2017-11-26 19:43     ` [PATCH v5 3/6] worktree: add --[no-]track option to the add subcommand Thomas Gummerer
2017-11-26 19:43     ` [PATCH v5 4/6] worktree: make add <path> <branch> dwim Thomas Gummerer
2017-11-26 19:43     ` [PATCH v5 5/6] worktree: add --guess-remote flag to add subcommand Thomas Gummerer
2017-11-27  6:36       ` Junio C Hamano
2017-11-27 20:56         ` Thomas Gummerer
2017-11-26 19:43     ` [PATCH v5 6/6] add worktree.guessRemote config option Thomas Gummerer
2017-11-27  6:45       ` Junio C Hamano
2017-11-27 20:59         ` Thomas Gummerer
2017-11-29 20:04     ` [PATCH v6 0/6] make git worktree add dwim more Thomas Gummerer
2017-11-29 20:04       ` [PATCH v6 1/6] checkout: factor out functions to new lib file Thomas Gummerer
2017-11-29 20:04       ` [PATCH v6 2/6] worktree: add can be created from any commit-ish Thomas Gummerer
2017-11-29 20:04       ` [PATCH v6 3/6] worktree: add --[no-]track option to the add subcommand Thomas Gummerer
2017-11-29 20:04       ` [PATCH v6 4/6] worktree: make add <path> <branch> dwim Thomas Gummerer
2017-11-29 20:04       ` [PATCH v6 5/6] worktree: add --guess-remote flag to add subcommand Thomas Gummerer
2017-11-29 20:04       ` [PATCH v6 6/6] add worktree.guessRemote config option Thomas Gummerer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20171122223020.2780-2-t.gummerer@gmail.com \
    --to=t.gummerer@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=pclouds@gmail.com \
    --cc=sunshine@sunshineco.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.