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