Add for_each_remote() function, and extend remote_find_tracking()
The function for_each_remote() does exactly what the name suggests. The function remote_find_tracking() was extended to be able to search remote refs for a given local ref. The caller sets either src or dst (but not both) in the refspec parameter, and remote_find_tracking() will fill in the other and return 0. Both changes are required for the next step: simplification of git-branch's --track functionality. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>maint
parent
5206d13091
commit
b42f69273b
56
remote.c
56
remote.c
|
@ -279,6 +279,25 @@ struct remote *remote_get(const char *name)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int for_each_remote(each_remote_fn fn, void *priv)
|
||||||
|
{
|
||||||
|
int i, result = 0;
|
||||||
|
read_config();
|
||||||
|
for (i = 0; i < allocated_remotes && !result; i++) {
|
||||||
|
struct remote *r = remotes[i];
|
||||||
|
if (!r)
|
||||||
|
continue;
|
||||||
|
if (!r->fetch)
|
||||||
|
r->fetch = parse_ref_spec(r->fetch_refspec_nr,
|
||||||
|
r->fetch_refspec);
|
||||||
|
if (!r->push)
|
||||||
|
r->push = parse_ref_spec(r->push_refspec_nr,
|
||||||
|
r->push_refspec);
|
||||||
|
result = fn(r, priv);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
int remote_has_uri(struct remote *remote, const char *uri)
|
int remote_has_uri(struct remote *remote, const char *uri)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
@ -291,32 +310,43 @@ int remote_has_uri(struct remote *remote, const char *uri)
|
||||||
|
|
||||||
int remote_find_tracking(struct remote *remote, struct refspec *refspec)
|
int remote_find_tracking(struct remote *remote, struct refspec *refspec)
|
||||||
{
|
{
|
||||||
|
int find_src = refspec->src == NULL;
|
||||||
|
char *needle, **result;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
if (find_src) {
|
||||||
|
if (refspec->dst == NULL)
|
||||||
|
return error("find_tracking: need either src or dst");
|
||||||
|
needle = refspec->dst;
|
||||||
|
result = &refspec->src;
|
||||||
|
} else {
|
||||||
|
needle = refspec->src;
|
||||||
|
result = &refspec->dst;
|
||||||
|
}
|
||||||
|
|
||||||
for (i = 0; i < remote->fetch_refspec_nr; i++) {
|
for (i = 0; i < remote->fetch_refspec_nr; i++) {
|
||||||
struct refspec *fetch = &remote->fetch[i];
|
struct refspec *fetch = &remote->fetch[i];
|
||||||
|
const char *key = find_src ? fetch->dst : fetch->src;
|
||||||
|
const char *value = find_src ? fetch->src : fetch->dst;
|
||||||
if (!fetch->dst)
|
if (!fetch->dst)
|
||||||
continue;
|
continue;
|
||||||
if (fetch->pattern) {
|
if (fetch->pattern) {
|
||||||
if (!prefixcmp(refspec->src, fetch->src)) {
|
if (!prefixcmp(needle, key)) {
|
||||||
refspec->dst =
|
*result = xmalloc(strlen(value) +
|
||||||
xmalloc(strlen(fetch->dst) +
|
strlen(needle) -
|
||||||
strlen(refspec->src) -
|
strlen(key) + 1);
|
||||||
strlen(fetch->src) + 1);
|
strcpy(*result, value);
|
||||||
strcpy(refspec->dst, fetch->dst);
|
strcpy(*result + strlen(value),
|
||||||
strcpy(refspec->dst + strlen(fetch->dst),
|
needle + strlen(key));
|
||||||
refspec->src + strlen(fetch->src));
|
|
||||||
refspec->force = fetch->force;
|
refspec->force = fetch->force;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
} else {
|
} else if (!strcmp(needle, key)) {
|
||||||
if (!strcmp(refspec->src, fetch->src)) {
|
*result = xstrdup(value);
|
||||||
refspec->dst = xstrdup(fetch->dst);
|
|
||||||
refspec->force = fetch->force;
|
refspec->force = fetch->force;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
refspec->dst = NULL;
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
5
remote.h
5
remote.h
|
@ -20,13 +20,16 @@ struct remote {
|
||||||
|
|
||||||
struct remote *remote_get(const char *name);
|
struct remote *remote_get(const char *name);
|
||||||
|
|
||||||
|
typedef int each_remote_fn(struct remote *remote, void *priv);
|
||||||
|
int for_each_remote(each_remote_fn fn, void *priv);
|
||||||
|
|
||||||
int remote_has_uri(struct remote *remote, const char *uri);
|
int remote_has_uri(struct remote *remote, const char *uri);
|
||||||
|
|
||||||
struct refspec {
|
struct refspec {
|
||||||
unsigned force : 1;
|
unsigned force : 1;
|
||||||
unsigned pattern : 1;
|
unsigned pattern : 1;
|
||||||
|
|
||||||
const char *src;
|
char *src;
|
||||||
char *dst;
|
char *dst;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -305,8 +305,8 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, cha
|
||||||
if (remote) {
|
if (remote) {
|
||||||
struct refspec rs;
|
struct refspec rs;
|
||||||
rs.src = ref->name;
|
rs.src = ref->name;
|
||||||
remote_find_tracking(remote, &rs);
|
rs.dst = NULL;
|
||||||
if (rs.dst) {
|
if (!remote_find_tracking(remote, &rs)) {
|
||||||
struct ref_lock *lock;
|
struct ref_lock *lock;
|
||||||
fprintf(stderr, " Also local %s\n", rs.dst);
|
fprintf(stderr, " Also local %s\n", rs.dst);
|
||||||
if (will_delete_ref) {
|
if (will_delete_ref) {
|
||||||
|
|
Loading…
Reference in New Issue