Miscellaneous const changes and utilities

The list of remote refs in struct transport should be const, because
builtin-fetch will get confused if it changes.

The url in git_connect should be const (and work on a copy) instead of
requiring the caller to copy it.

match_refs doesn't modify the refspecs it gets.

get_fetch_map and get_remote_ref don't change the list they get.

Allow transport get_refs_list methods to modify the struct transport.

Add a function to copy a list of refs, when a function needs a mutable
copy of a const list.

Add a function to check the type of a ref, as per the code in connect.c

Signed-off-by: Daniel Barkalow <barkalow@iabervon.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Daniel Barkalow
2007-10-29 21:05:40 -04:00
committed by Junio C Hamano
parent e3d6d56f1c
commit 4577370e9b
9 changed files with 57 additions and 33 deletions

View File

@ -29,7 +29,7 @@ static void unlock_pack_on_signal(int signo)
} }
static void add_merge_config(struct ref **head, static void add_merge_config(struct ref **head,
struct ref *remote_refs, const struct ref *remote_refs,
struct branch *branch, struct branch *branch,
struct ref ***tail) struct ref ***tail)
{ {
@ -77,7 +77,7 @@ static struct ref *get_ref_map(struct transport *transport,
struct ref *ref_map = NULL; struct ref *ref_map = NULL;
struct ref **tail = &ref_map; struct ref **tail = &ref_map;
struct ref *remote_refs = transport_get_remote_refs(transport); const struct ref *remote_refs = transport_get_remote_refs(transport);
if (ref_count || tags) { if (ref_count || tags) {
for (i = 0; i < ref_count; i++) { for (i = 0; i < ref_count; i++) {
@ -345,12 +345,12 @@ static struct ref *find_non_local_tags(struct transport *transport,
struct path_list new_refs = { NULL, 0, 0, 1 }; struct path_list new_refs = { NULL, 0, 0, 1 };
char *ref_name; char *ref_name;
int ref_name_len; int ref_name_len;
unsigned char *ref_sha1; const unsigned char *ref_sha1;
struct ref *tag_ref; const struct ref *tag_ref;
struct ref *rm = NULL; struct ref *rm = NULL;
struct ref *ref_map = NULL; struct ref *ref_map = NULL;
struct ref **tail = &ref_map; struct ref **tail = &ref_map;
struct ref *ref; const struct ref *ref;
for_each_ref(add_existing, &existing_refs); for_each_ref(add_existing, &existing_refs);
for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) { for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {

View File

@ -503,7 +503,7 @@ struct ref {
#define REF_TAGS (1u << 2) #define REF_TAGS (1u << 2)
#define CONNECT_VERBOSE (1u << 0) #define CONNECT_VERBOSE (1u << 0)
extern struct child_process *git_connect(int fd[2], char *url, const char *prog, int flags); extern struct child_process *git_connect(int fd[2], const char *url, const char *prog, int flags);
extern int finish_connect(struct child_process *conn); extern int finish_connect(struct child_process *conn);
extern int path_match(const char *path, int nr, char **match); extern int path_match(const char *path, int nr, char **match);
extern int get_ack(int fd, unsigned char *result_sha1); extern int get_ack(int fd, unsigned char *result_sha1);

View File

@ -36,6 +36,11 @@ static int check_ref(const char *name, int len, unsigned int flags)
return !(flags & ~REF_NORMAL); return !(flags & ~REF_NORMAL);
} }
int check_ref_type(const struct ref *ref, int flags)
{
return check_ref(ref->name, strlen(ref->name), flags);
}
/* /*
* Read all the refs from the other end * Read all the refs from the other end
*/ */
@ -476,9 +481,10 @@ char *get_port(char *host)
* *
* If it returns, the connect is successful; it just dies on errors. * If it returns, the connect is successful; it just dies on errors.
*/ */
struct child_process *git_connect(int fd[2], char *url, struct child_process *git_connect(int fd[2], const char *url_orig,
const char *prog, int flags) const char *prog, int flags)
{ {
char *url = xstrdup(url_orig);
char *host, *path = url; char *host, *path = url;
char *end; char *end;
int c; int c;
@ -568,6 +574,7 @@ struct child_process *git_connect(int fd[2], char *url,
prog, path, 0, prog, path, 0,
target_host, 0); target_host, 0);
free(target_host); free(target_host);
free(url);
if (free_path) if (free_path)
free(path); free(path);
return NULL; return NULL;
@ -619,6 +626,7 @@ struct child_process *git_connect(int fd[2], char *url,
fd[0] = conn->out; /* read from child's stdout */ fd[0] = conn->out; /* read from child's stdout */
fd[1] = conn->in; /* write to child's stdin */ fd[1] = conn->in; /* write to child's stdin */
strbuf_release(&cmd); strbuf_release(&cmd);
free(url);
if (free_path) if (free_path)
free(path); free(path);
return conn; return conn;

View File

@ -2389,7 +2389,7 @@ int main(int argc, char **argv)
if (!remote_tail) if (!remote_tail)
remote_tail = &remote_refs; remote_tail = &remote_refs;
if (match_refs(local_refs, remote_refs, &remote_tail, if (match_refs(local_refs, remote_refs, &remote_tail,
nr_refspec, refspec, push_all)) nr_refspec, (const char **) refspec, push_all))
return -1; return -1;
if (!remote_refs) { if (!remote_refs) {
fprintf(stderr, "No refs in common and none specified; doing nothing.\n"); fprintf(stderr, "No refs in common and none specified; doing nothing.\n");

View File

@ -485,7 +485,7 @@ struct ref *alloc_ref(unsigned namelen)
return ret; return ret;
} }
static struct ref *copy_ref(struct ref *ref) static struct ref *copy_ref(const struct ref *ref)
{ {
struct ref *ret = xmalloc(sizeof(struct ref) + strlen(ref->name) + 1); struct ref *ret = xmalloc(sizeof(struct ref) + strlen(ref->name) + 1);
memcpy(ret, ref, sizeof(struct ref) + strlen(ref->name) + 1); memcpy(ret, ref, sizeof(struct ref) + strlen(ref->name) + 1);
@ -493,6 +493,18 @@ static struct ref *copy_ref(struct ref *ref)
return ret; return ret;
} }
struct ref *copy_ref_list(const struct ref *ref)
{
struct ref *ret = NULL;
struct ref **tail = &ret;
while (ref) {
*tail = copy_ref(ref);
ref = ref->next;
tail = &((*tail)->next);
}
return ret;
}
void free_refs(struct ref *ref) void free_refs(struct ref *ref)
{ {
struct ref *next; struct ref *next;
@ -710,7 +722,7 @@ static const struct refspec *check_pattern_match(const struct refspec *rs,
* without thinking. * without thinking.
*/ */
int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail, int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
int nr_refspec, char **refspec, int all) int nr_refspec, const char **refspec, int all)
{ {
struct refspec *rs = struct refspec *rs =
parse_ref_spec(nr_refspec, (const char **) refspec); parse_ref_spec(nr_refspec, (const char **) refspec);
@ -810,10 +822,10 @@ int branch_merge_matches(struct branch *branch,
return ref_matches_abbrev(branch->merge[i]->src, refname); return ref_matches_abbrev(branch->merge[i]->src, refname);
} }
static struct ref *get_expanded_map(struct ref *remote_refs, static struct ref *get_expanded_map(const struct ref *remote_refs,
const struct refspec *refspec) const struct refspec *refspec)
{ {
struct ref *ref; const struct ref *ref;
struct ref *ret = NULL; struct ref *ret = NULL;
struct ref **tail = &ret; struct ref **tail = &ret;
@ -824,7 +836,7 @@ static struct ref *get_expanded_map(struct ref *remote_refs,
if (strchr(ref->name, '^')) if (strchr(ref->name, '^'))
continue; /* a dereference item */ continue; /* a dereference item */
if (!prefixcmp(ref->name, refspec->src)) { if (!prefixcmp(ref->name, refspec->src)) {
char *match; const char *match;
struct ref *cpy = copy_ref(ref); struct ref *cpy = copy_ref(ref);
match = ref->name + remote_prefix_len; match = ref->name + remote_prefix_len;
@ -842,9 +854,9 @@ static struct ref *get_expanded_map(struct ref *remote_refs,
return ret; return ret;
} }
static struct ref *find_ref_by_name_abbrev(struct ref *refs, const char *name) static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name)
{ {
struct ref *ref; const struct ref *ref;
for (ref = refs; ref; ref = ref->next) { for (ref = refs; ref; ref = ref->next) {
if (ref_matches_abbrev(name, ref->name)) if (ref_matches_abbrev(name, ref->name))
return ref; return ref;
@ -852,9 +864,9 @@ static struct ref *find_ref_by_name_abbrev(struct ref *refs, const char *name)
return NULL; return NULL;
} }
struct ref *get_remote_ref(struct ref *remote_refs, const char *name) struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)
{ {
struct ref *ref = find_ref_by_name_abbrev(remote_refs, name); const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
if (!ref) if (!ref)
return NULL; return NULL;
@ -887,7 +899,7 @@ static struct ref *get_local_ref(const char *name)
return ret; return ret;
} }
int get_fetch_map(struct ref *remote_refs, int get_fetch_map(const struct ref *remote_refs,
const struct refspec *refspec, const struct refspec *refspec,
struct ref ***tail, struct ref ***tail,
int missing_ok) int missing_ok)

View File

@ -44,6 +44,10 @@ struct refspec {
struct ref *alloc_ref(unsigned namelen); struct ref *alloc_ref(unsigned namelen);
struct ref *copy_ref_list(const struct ref *ref);
int check_ref_type(const struct ref *ref, int flags);
/* /*
* Frees the entire list and peers of elements. * Frees the entire list and peers of elements.
*/ */
@ -57,7 +61,7 @@ void ref_remove_duplicates(struct ref *ref_map);
struct refspec *parse_ref_spec(int nr_refspec, const char **refspec); struct refspec *parse_ref_spec(int nr_refspec, const char **refspec);
int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail, int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
int nr_refspec, char **refspec, int all); int nr_refspec, const char **refspec, int all);
/* /*
* Given a list of the remote refs and the specification of things to * Given a list of the remote refs and the specification of things to
@ -71,10 +75,10 @@ int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
* missing_ok is usually false, but when we are adding branch.$name.merge * missing_ok is usually false, but when we are adding branch.$name.merge
* it is Ok if the branch is not at the remote anymore. * it is Ok if the branch is not at the remote anymore.
*/ */
int get_fetch_map(struct ref *remote_refs, const struct refspec *refspec, int get_fetch_map(const struct ref *remote_refs, const struct refspec *refspec,
struct ref ***tail, int missing_ok); struct ref ***tail, int missing_ok);
struct ref *get_remote_ref(struct ref *remote_refs, const char *name); struct ref *get_remote_ref(const struct ref *remote_refs, const char *name);
/* /*
* For the given remote, reads the refspec's src and sets the other fields. * For the given remote, reads the refspec's src and sets the other fields.

View File

@ -207,7 +207,7 @@ static void update_tracking_ref(struct remote *remote, struct ref *ref)
} }
} }
static int send_pack(int in, int out, struct remote *remote, int nr_refspec, char **refspec) static int send_pack(int in, int out, struct remote *remote, int nr_refspec, const char **refspec)
{ {
struct ref *ref; struct ref *ref;
int new_refs; int new_refs;
@ -357,7 +357,7 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, cha
return ret; return ret;
} }
static void verify_remote_names(int nr_heads, char **heads) static void verify_remote_names(int nr_heads, const char **heads)
{ {
int i; int i;
@ -382,7 +382,7 @@ int main(int argc, char **argv)
{ {
int i, nr_heads = 0; int i, nr_heads = 0;
char *dest = NULL; char *dest = NULL;
char **heads = NULL; const char **heads = NULL;
int fd[2], ret; int fd[2], ret;
struct child_process *conn; struct child_process *conn;
char *remote_name = NULL; char *remote_name = NULL;
@ -434,7 +434,7 @@ int main(int argc, char **argv)
dest = arg; dest = arg;
continue; continue;
} }
heads = argv; heads = (const char **) argv;
nr_heads = argc - i; nr_heads = argc - i;
break; break;
} }

View File

@ -141,7 +141,7 @@ static void insert_packed_refs(const char *packed_refs, struct ref **list)
} }
} }
static struct ref *get_refs_via_rsync(const struct transport *transport) static struct ref *get_refs_via_rsync(struct transport *transport)
{ {
struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT; struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT;
struct ref dummy, *tail = &dummy; struct ref dummy, *tail = &dummy;
@ -427,7 +427,7 @@ static int missing__target(int code, int result)
#define missing_target(a) missing__target((a)->http_code, (a)->curl_result) #define missing_target(a) missing__target((a)->http_code, (a)->curl_result)
static struct ref *get_refs_via_curl(const struct transport *transport) static struct ref *get_refs_via_curl(struct transport *transport)
{ {
struct buffer buffer; struct buffer buffer;
char *data, *start, *mid; char *data, *start, *mid;
@ -524,7 +524,7 @@ struct bundle_transport_data {
struct bundle_header header; struct bundle_header header;
}; };
static struct ref *get_refs_from_bundle(const struct transport *transport) static struct ref *get_refs_from_bundle(struct transport *transport)
{ {
struct bundle_transport_data *data = transport->data; struct bundle_transport_data *data = transport->data;
struct ref *result = NULL; struct ref *result = NULL;
@ -596,7 +596,7 @@ static int set_git_option(struct transport *connection,
return 1; return 1;
} }
static struct ref *get_refs_via_connect(const struct transport *transport) static struct ref *get_refs_via_connect(struct transport *transport)
{ {
struct git_transport_data *data = transport->data; struct git_transport_data *data = transport->data;
struct ref *refs; struct ref *refs;
@ -781,7 +781,7 @@ int transport_push(struct transport *transport,
return transport->push(transport, refspec_nr, refspec, flags); return transport->push(transport, refspec_nr, refspec, flags);
} }
struct ref *transport_get_remote_refs(struct transport *transport) const struct ref *transport_get_remote_refs(struct transport *transport)
{ {
if (!transport->remote_refs) if (!transport->remote_refs)
transport->remote_refs = transport->get_refs_list(transport); transport->remote_refs = transport->get_refs_list(transport);

View File

@ -8,7 +8,7 @@ struct transport {
struct remote *remote; struct remote *remote;
const char *url; const char *url;
void *data; void *data;
struct ref *remote_refs; const struct ref *remote_refs;
/** /**
* Returns 0 if successful, positive if the option is not * Returns 0 if successful, positive if the option is not
@ -18,7 +18,7 @@ struct transport {
int (*set_option)(struct transport *connection, const char *name, int (*set_option)(struct transport *connection, const char *name,
const char *value); const char *value);
struct ref *(*get_refs_list)(const struct transport *transport); struct ref *(*get_refs_list)(struct transport *transport);
int (*fetch)(struct transport *transport, int refs_nr, struct ref **refs); int (*fetch)(struct transport *transport, int refs_nr, struct ref **refs);
int (*push)(struct transport *connection, int refspec_nr, const char **refspec, int flags); int (*push)(struct transport *connection, int refspec_nr, const char **refspec, int flags);
@ -61,7 +61,7 @@ int transport_set_option(struct transport *transport, const char *name,
int transport_push(struct transport *connection, int transport_push(struct transport *connection,
int refspec_nr, const char **refspec, int flags); int refspec_nr, const char **refspec, int flags);
struct ref *transport_get_remote_refs(struct transport *transport); const struct ref *transport_get_remote_refs(struct transport *transport);
int transport_fetch_refs(struct transport *transport, struct ref *refs); int transport_fetch_refs(struct transport *transport, struct ref *refs);
void transport_unlock_pack(struct transport *transport); void transport_unlock_pack(struct transport *transport);