Coverage Report

Created: 2024-09-08 06:23

/src/git/checkout.c
Line
Count
Source (jump to first uncovered line)
1
#define USE_THE_REPOSITORY_VARIABLE
2
3
#include "git-compat-util.h"
4
#include "object-name.h"
5
#include "remote.h"
6
#include "refspec.h"
7
#include "repository.h"
8
#include "checkout.h"
9
#include "config.h"
10
#include "strbuf.h"
11
12
struct tracking_name_data {
13
  /* const */ char *src_ref;
14
  char *dst_ref;
15
  struct object_id *dst_oid;
16
  int num_matches;
17
  const char *default_remote;
18
  char *default_dst_ref;
19
  struct object_id *default_dst_oid;
20
};
21
22
0
#define TRACKING_NAME_DATA_INIT { 0 }
23
24
static int check_tracking_name(struct remote *remote, void *cb_data)
25
0
{
26
0
  struct tracking_name_data *cb = cb_data;
27
0
  struct refspec_item query;
28
0
  memset(&query, 0, sizeof(struct refspec_item));
29
0
  query.src = cb->src_ref;
30
0
  if (remote_find_tracking(remote, &query) ||
31
0
      repo_get_oid(the_repository, query.dst, cb->dst_oid)) {
32
0
    free(query.dst);
33
0
    return 0;
34
0
  }
35
0
  cb->num_matches++;
36
0
  if (cb->default_remote && !strcmp(remote->name, cb->default_remote)) {
37
0
    struct object_id *dst = xmalloc(sizeof(*cb->default_dst_oid));
38
0
    cb->default_dst_ref = xstrdup(query.dst);
39
0
    oidcpy(dst, cb->dst_oid);
40
0
    cb->default_dst_oid = dst;
41
0
  }
42
0
  if (cb->dst_ref) {
43
0
    free(query.dst);
44
0
    return 0;
45
0
  }
46
0
  cb->dst_ref = query.dst;
47
0
  return 0;
48
0
}
49
50
char *unique_tracking_name(const char *name, struct object_id *oid,
51
         int *dwim_remotes_matched)
52
0
{
53
0
  struct tracking_name_data cb_data = TRACKING_NAME_DATA_INIT;
54
0
  const char *default_remote = NULL;
55
0
  if (!git_config_get_string_tmp("checkout.defaultremote", &default_remote))
56
0
    cb_data.default_remote = default_remote;
57
0
  cb_data.src_ref = xstrfmt("refs/heads/%s", name);
58
0
  cb_data.dst_oid = oid;
59
0
  for_each_remote(check_tracking_name, &cb_data);
60
0
  if (dwim_remotes_matched)
61
0
    *dwim_remotes_matched = cb_data.num_matches;
62
0
  free(cb_data.src_ref);
63
0
  if (cb_data.num_matches == 1) {
64
0
    free(cb_data.default_dst_ref);
65
0
    free(cb_data.default_dst_oid);
66
0
    return cb_data.dst_ref;
67
0
  }
68
0
  free(cb_data.dst_ref);
69
0
  if (cb_data.default_dst_ref) {
70
0
    oidcpy(oid, cb_data.default_dst_oid);
71
0
    free(cb_data.default_dst_oid);
72
0
    return cb_data.default_dst_ref;
73
0
  }
74
0
  return NULL;
75
0
}