]> git.ipfire.org Git - thirdparty/git.git/blame - checkout.c
Eleventh batch
[thirdparty/git.git] / checkout.c
CommitLineData
7c85a87c
TG
1#include "cache.h"
2#include "remote.h"
ec0cb496 3#include "refspec.h"
7c85a87c 4#include "checkout.h"
8d7b558b 5#include "config.h"
7c85a87c
TG
6
7struct tracking_name_data {
8 /* const */ char *src_ref;
9 char *dst_ref;
10 struct object_id *dst_oid;
e4d2d55a 11 int num_matches;
8d7b558b
ÆAB
12 const char *default_remote;
13 char *default_dst_ref;
14 struct object_id *default_dst_oid;
7c85a87c
TG
15};
16
8d7b558b 17#define TRACKING_NAME_DATA_INIT { NULL, NULL, NULL, 0, NULL, NULL, NULL }
e417151b 18
7c85a87c
TG
19static int check_tracking_name(struct remote *remote, void *cb_data)
20{
21 struct tracking_name_data *cb = cb_data;
0ad4a5ff
BW
22 struct refspec_item query;
23 memset(&query, 0, sizeof(struct refspec_item));
7c85a87c
TG
24 query.src = cb->src_ref;
25 if (remote_find_tracking(remote, &query) ||
26 get_oid(query.dst, cb->dst_oid)) {
27 free(query.dst);
28 return 0;
29 }
e4d2d55a 30 cb->num_matches++;
8d7b558b
ÆAB
31 if (cb->default_remote && !strcmp(remote->name, cb->default_remote)) {
32 struct object_id *dst = xmalloc(sizeof(*cb->default_dst_oid));
33 cb->default_dst_ref = xstrdup(query.dst);
34 oidcpy(dst, cb->dst_oid);
35 cb->default_dst_oid = dst;
36 }
7c85a87c
TG
37 if (cb->dst_ref) {
38 free(query.dst);
7c85a87c
TG
39 return 0;
40 }
41 cb->dst_ref = query.dst;
42 return 0;
43}
44
3c87aa94
ÆAB
45const char *unique_tracking_name(const char *name, struct object_id *oid,
46 int *dwim_remotes_matched)
7c85a87c 47{
e417151b 48 struct tracking_name_data cb_data = TRACKING_NAME_DATA_INIT;
8d7b558b
ÆAB
49 const char *default_remote = NULL;
50 if (!git_config_get_string_const("checkout.defaultremote", &default_remote))
51 cb_data.default_remote = default_remote;
7c85a87c
TG
52 cb_data.src_ref = xstrfmt("refs/heads/%s", name);
53 cb_data.dst_oid = oid;
54 for_each_remote(check_tracking_name, &cb_data);
3c87aa94
ÆAB
55 if (dwim_remotes_matched)
56 *dwim_remotes_matched = cb_data.num_matches;
7c85a87c 57 free(cb_data.src_ref);
8d7b558b
ÆAB
58 free((char *)default_remote);
59 if (cb_data.num_matches == 1) {
60 free(cb_data.default_dst_ref);
61 free(cb_data.default_dst_oid);
7c85a87c 62 return cb_data.dst_ref;
8d7b558b 63 }
7c85a87c 64 free(cb_data.dst_ref);
8d7b558b
ÆAB
65 if (cb_data.default_dst_ref) {
66 oidcpy(oid, cb_data.default_dst_oid);
67 free(cb_data.default_dst_oid);
68 return cb_data.default_dst_ref;
69 }
7c85a87c
TG
70 return NULL;
71}