checkout: factor out functions to new lib file

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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Thomas Gummerer committed Nov 26, 2017 at 19:43 UTC 7c85a87c5449f6495fe0327cfdcd1e0940d8545a
4 files changed +58 -40
Makefile
+1
@@ -759,6 +759,7 @@ LIB_OBJS += branch.o
759 LIB_OBJS += bulk-checkin.o
760 LIB_OBJS += bundle.o
761 LIB_OBJS += cache-tree.o
762 +LIB_OBJS += checkout.o
763 LIB_OBJS += color.o
764 LIB_OBJS += column.o
765 LIB_OBJS += combine-diff.o
builtin/checkout.c
+1 -40
@@ -1,5 +1,6 @@
1 #include "builtin.h"
2 #include "config.h"
3 +#include "checkout.h"
4 #include "lockfile.h"
5 #include "parse-options.h"
6 #include "refs.h"
@@ -872,46 +873,6 @@ static int git_checkout_config(const char *var, const char *value, void *cb)
873 return git_xmerge_config(var, value, NULL);
874 }
875
875 -struct tracking_name_data {
876 - /* const */ char *src_ref;
877 - char *dst_ref;
878 - struct object_id *dst_oid;
879 - int unique;
880 -};
881 -
882 -static int check_tracking_name(struct remote *remote, void *cb_data)
883 -{
884 - struct tracking_name_data *cb = cb_data;
885 - struct refspec query;
886 - memset(&query, 0, sizeof(struct refspec));
887 - query.src = cb->src_ref;
888 - if (remote_find_tracking(remote, &query) ||
889 - get_oid(query.dst, cb->dst_oid)) {
890 - free(query.dst);
891 - return 0;
892 - }
893 - if (cb->dst_ref) {
894 - free(query.dst);
895 - cb->unique = 0;
896 - return 0;
897 - }
898 - cb->dst_ref = query.dst;
899 - return 0;
900 -}
901 -
902 -static const char *unique_tracking_name(const char *name, struct object_id *oid)
903 -{
904 - struct tracking_name_data cb_data = { NULL, NULL, NULL, 1 };
905 - cb_data.src_ref = xstrfmt("refs/heads/%s", name);
906 - cb_data.dst_oid = oid;
907 - for_each_remote(check_tracking_name, &cb_data);
908 - free(cb_data.src_ref);
909 - if (cb_data.unique)
910 - return cb_data.dst_ref;
911 - free(cb_data.dst_ref);
912 - return NULL;
913 -}
914 -
876 static int parse_branchname_arg(int argc, const char **argv,
877 int dwim_new_local_branch_ok,
878 struct branch_info *new,
checkout.c new
+43
@@ -0,0 +1,43 @@
1 +#include "cache.h"
2 +#include "remote.h"
3 +#include "checkout.h"
4 +
5 +struct tracking_name_data {
6 + /* const */ char *src_ref;
7 + char *dst_ref;
8 + struct object_id *dst_oid;
9 + int unique;
10 +};
11 +
12 +static int check_tracking_name(struct remote *remote, void *cb_data)
13 +{
14 + struct tracking_name_data *cb = cb_data;
15 + struct refspec query;
16 + memset(&query, 0, sizeof(struct refspec));
17 + query.src = cb->src_ref;
18 + if (remote_find_tracking(remote, &query) ||
19 + get_oid(query.dst, cb->dst_oid)) {
20 + free(query.dst);
21 + return 0;
22 + }
23 + if (cb->dst_ref) {
24 + free(query.dst);
25 + cb->unique = 0;
26 + return 0;
27 + }
28 + cb->dst_ref = query.dst;
29 + return 0;
30 +}
31 +
32 +const char *unique_tracking_name(const char *name, struct object_id *oid)
33 +{
34 + struct tracking_name_data cb_data = { NULL, NULL, NULL, 1 };
35 + cb_data.src_ref = xstrfmt("refs/heads/%s", name);
36 + cb_data.dst_oid = oid;
37 + for_each_remote(check_tracking_name, &cb_data);
38 + free(cb_data.src_ref);
39 + if (cb_data.unique)
40 + return cb_data.dst_ref;
41 + free(cb_data.dst_ref);
42 + return NULL;
43 +}
checkout.h new
+13
@@ -0,0 +1,13 @@
1 +#ifndef CHECKOUT_H
2 +#define CHECKOUT_H
3 +
4 +#include "cache.h"
5 +
6 +/*
7 + * Check if the branch name uniquely matches a branch name on a remote
8 + * tracking branch. Return the name of the remote if such a branch
9 + * exists, NULL otherwise.
10 + */
11 +extern const char *unique_tracking_name(const char *name, struct object_id *oid);
12 +
13 +#endif /* CHECKOUT_H */