Skip to content

Commit 7c85a87

Browse files
tgummerergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 14c63a9 commit 7c85a87

File tree

4 files changed

+58
-40
lines changed

4 files changed

+58
-40
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,7 @@ LIB_OBJS += branch.o
759759
LIB_OBJS += bulk-checkin.o
760760
LIB_OBJS += bundle.o
761761
LIB_OBJS += cache-tree.o
762+
LIB_OBJS += checkout.o
762763
LIB_OBJS += color.o
763764
LIB_OBJS += column.o
764765
LIB_OBJS += combine-diff.o

builtin/checkout.c

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "builtin.h"
22
#include "config.h"
3+
#include "checkout.h"
34
#include "lockfile.h"
45
#include "parse-options.h"
56
#include "refs.h"
@@ -872,46 +873,6 @@ static int git_checkout_config(const char *var, const char *value, void *cb)
872873
return git_xmerge_config(var, value, NULL);
873874
}
874875

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-
915876
static int parse_branchname_arg(int argc, const char **argv,
916877
int dwim_new_local_branch_ok,
917878
struct branch_info *new,

checkout.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 */

0 commit comments

Comments
 (0)