checkout.c: change "unique" member to "num_matches"
[git/git.git] / checkout.c
CommitLineData
7c85a87c
TG
1#include "cache.h"
2#include "remote.h"
ec0cb496 3#include "refspec.h"
7c85a87c
TG
4#include "checkout.h"
5
6struct tracking_name_data {
7 /* const */ char *src_ref;
8 char *dst_ref;
9 struct object_id *dst_oid;
e4d2d55a 10 int num_matches;
7c85a87c
TG
11};
12
e4d2d55a 13#define TRACKING_NAME_DATA_INIT { NULL, NULL, NULL, 0 }
e417151b 14
7c85a87c
TG
15static int check_tracking_name(struct remote *remote, void *cb_data)
16{
17 struct tracking_name_data *cb = cb_data;
0ad4a5ff
BW
18 struct refspec_item query;
19 memset(&query, 0, sizeof(struct refspec_item));
7c85a87c
TG
20 query.src = cb->src_ref;
21 if (remote_find_tracking(remote, &query) ||
22 get_oid(query.dst, cb->dst_oid)) {
23 free(query.dst);
24 return 0;
25 }
e4d2d55a 26 cb->num_matches++;
7c85a87c
TG
27 if (cb->dst_ref) {
28 free(query.dst);
7c85a87c
TG
29 return 0;
30 }
31 cb->dst_ref = query.dst;
32 return 0;
33}
34
35const char *unique_tracking_name(const char *name, struct object_id *oid)
36{
e417151b 37 struct tracking_name_data cb_data = TRACKING_NAME_DATA_INIT;
7c85a87c
TG
38 cb_data.src_ref = xstrfmt("refs/heads/%s", name);
39 cb_data.dst_oid = oid;
40 for_each_remote(check_tracking_name, &cb_data);
41 free(cb_data.src_ref);
e4d2d55a 42 if (cb_data.num_matches == 1)
7c85a87c
TG
43 return cb_data.dst_ref;
44 free(cb_data.dst_ref);
45 return NULL;
46}