{"thread":{"id":"10517","subject":"[PATCH 1/4] Miscellaneous const changes and utilities","startedAt":"2007-10-30T01:05:40Z","lastAt":"2007-10-30T01:05:40Z","messageCount":1,"participants":["Daniel Barkalow"],"isPatch":true,"patchVersion":1,"patchTotal":4},"messages":[{"id":"57506","messageId":"Pine.LNX.4.64.0710292049140.7357@iabervon.org","threadId":"10517","inReplyTo":null,"subject":"[PATCH 1/4] Miscellaneous const changes and utilities","fromName":"Daniel Barkalow","fromEmail":"barkalow@iabervon.org","sentAt":"2007-10-30T01:05:40Z","receivedAt":"2007-10-30T01:05:40Z","isPatch":true,"sender":{"key":"barkalow@iabervon.org","avatar":"https://avatars.githubusercontent.com/u/55364219?v=4"},"body":"The list of remote refs in struct transport should be const, because\nbuiltin-fetch will get confused if it changes.\n\nThe url in git_connect should be const (and work on a copy) instead of\nrequiring the caller to copy it.\n\nmatch_refs doesn't modify the refspecs it gets.\n\nget_fetch_map and get_remote_ref don't change the list they get.\n\nAllow transport get_refs_list methods to modify the struct transport.\n\nAdd a function to copy a list of refs, when a function needs a mutable\ncopy of a const list.\n\nAdd a function to check the type of a ref, as per the code in connect.c\n\nSigned-off-by: Daniel Barkalow <barkalow@iabervon.org>\n---\n builtin-fetch.c |   10 +++++-----\n cache.h         |    2 +-\n connect.c       |   10 +++++++++-\n http-push.c     |    2 +-\n remote.c        |   32 ++++++++++++++++++++++----------\n remote.h        |   10 +++++++---\n send-pack.c     |    8 ++++----\n transport.c     |   10 +++++-----\n transport.h     |    6 +++---\n 9 files changed, 57 insertions(+), 33 deletions(-)\n\ndiff --git a/builtin-fetch.c b/builtin-fetch.c\nindex b9d2b0c..fb64353 100644\n--- a/builtin-fetch.c\n+++ b/builtin-fetch.c\n@@ -29,7 +29,7 @@ static void unlock_pack_on_signal(int signo)\n }\n \n static void add_merge_config(struct ref **head,\n-\t\t           struct ref *remote_refs,\n+\t\t\t   const struct ref *remote_refs,\n \t\t           struct branch *branch,\n \t\t           struct ref ***tail)\n {\n@@ -71,7 +71,7 @@ static struct ref *get_ref_map(struct transport *transport,\n \tstruct ref *ref_map = NULL;\n \tstruct ref **tail = &ref_map;\n \n-\tstruct ref *remote_refs = transport_get_remote_refs(transport);\n+\tconst struct ref *remote_refs = transport_get_remote_refs(transport);\n \n \tif (ref_count || tags) {\n \t\tfor (i = 0; i < ref_count; i++) {\n@@ -337,12 +337,12 @@ static struct ref *find_non_local_tags(struct transport *transport,\n \tstruct path_list new_refs = { NULL, 0, 0, 1 };\n \tchar *ref_name;\n \tint ref_name_len;\n-\tunsigned char *ref_sha1;\n-\tstruct ref *tag_ref;\n+\tconst unsigned char *ref_sha1;\n+\tconst struct ref *tag_ref;\n \tstruct ref *rm = NULL;\n \tstruct ref *ref_map = NULL;\n \tstruct ref **tail = &ref_map;\n-\tstruct ref *ref;\n+\tconst struct ref *ref;\n \n \tfor_each_ref(add_existing, &existing_refs);\n \tfor (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {\ndiff --git a/cache.h b/cache.h\nindex 27485d3..0e3848d 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -503,7 +503,7 @@ struct ref {\n #define REF_TAGS\t(1u << 2)\n \n #define CONNECT_VERBOSE       (1u << 0)\n-extern pid_t git_connect(int fd[2], char *url, const char *prog, int flags);\n+extern pid_t git_connect(int fd[2], const char *url, const char *prog, int flags);\n extern int finish_connect(pid_t pid);\n extern int path_match(const char *path, int nr, char **match);\n extern int get_ack(int fd, unsigned char *result_sha1);\ndiff --git a/connect.c b/connect.c\nindex 3d5c4ab..2d62ea9 100644\n--- a/connect.c\n+++ b/connect.c\n@@ -36,6 +36,11 @@ static int check_ref(const char *name, int len, unsigned int flags)\n \treturn !(flags & ~REF_NORMAL);\n }\n \n+int check_ref_type(const struct ref *ref, int flags)\n+{\n+\treturn check_ref(ref->name, strlen(ref->name), flags);\n+}\n+\n /*\n  * Read all the refs from the other end\n  */\n@@ -476,8 +481,9 @@ char *get_port(char *host)\n  *\n  * Does not return a negative value on error; it just dies.\n  */\n-pid_t git_connect(int fd[2], char *url, const char *prog, int flags)\n+pid_t git_connect(int fd[2], const char *url_orig, const char *prog, int flags)\n {\n+\tchar *url = xstrdup(url_orig);\n \tchar *host, *path = url;\n \tchar *end;\n \tint c;\n@@ -566,6 +572,7 @@ pid_t git_connect(int fd[2], char *url, const char *prog, int flags)\n \t\t\t     prog, path, 0,\n \t\t\t     target_host, 0);\n \t\tfree(target_host);\n+\t\tfree(url);\n \t\tif (free_path)\n \t\t\tfree(path);\n \t\treturn 0;\n@@ -623,6 +630,7 @@ pid_t git_connect(int fd[2], char *url, const char *prog, int flags)\n \tfd[1] = pipefd[1][1];\n \tclose(pipefd[0][1]);\n \tclose(pipefd[1][0]);\n+\tfree(url);\n \tif (free_path)\n \t\tfree(path);\n \treturn pid;\ndiff --git a/http-push.c b/http-push.c\nindex c02a3af..f461bb3 100644\n--- a/http-push.c\n+++ b/http-push.c\n@@ -2389,7 +2389,7 @@ int main(int argc, char **argv)\n \tif (!remote_tail)\n \t\tremote_tail = &remote_refs;\n \tif (match_refs(local_refs, remote_refs, &remote_tail,\n-\t\t       nr_refspec, refspec, push_all))\n+\t\t       nr_refspec, (const char **) refspec, push_all))\n \t\treturn -1;\n \tif (!remote_refs) {\n \t\tfprintf(stderr, \"No refs in common and none specified; doing nothing.\\n\");\ndiff --git a/remote.c b/remote.c\nindex 170015a..ec89c97 100644\n--- a/remote.c\n+++ b/remote.c\n@@ -485,7 +485,7 @@ struct ref *alloc_ref(unsigned namelen)\n \treturn ret;\n }\n \n-static struct ref *copy_ref(struct ref *ref)\n+static struct ref *copy_ref(const struct ref *ref)\n {\n \tstruct ref *ret = xmalloc(sizeof(struct ref) + strlen(ref->name) + 1);\n \tmemcpy(ret, ref, sizeof(struct ref) + strlen(ref->name) + 1);\n@@ -493,6 +493,18 @@ static struct ref *copy_ref(struct ref *ref)\n \treturn ret;\n }\n \n+struct ref *copy_ref_list(const struct ref *ref)\n+{\n+\tstruct ref *ret = NULL;\n+\tstruct ref **tail = &ret;\n+\twhile (ref) {\n+\t\t*tail = copy_ref(ref);\n+\t\tref = ref->next;\n+\t\ttail = &((*tail)->next);\n+\t}\n+\treturn ret;\n+}\n+\n void free_refs(struct ref *ref)\n {\n \tstruct ref *next;\n@@ -710,7 +722,7 @@ static const struct refspec *check_pattern_match(const struct refspec *rs,\n  * without thinking.\n  */\n int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,\n-\t       int nr_refspec, char **refspec, int all)\n+\t       int nr_refspec, const char **refspec, int all)\n {\n \tstruct refspec *rs =\n \t\tparse_ref_spec(nr_refspec, (const char **) refspec);\n@@ -810,10 +822,10 @@ int branch_merge_matches(struct branch *branch,\n \treturn ref_matches_abbrev(branch->merge[i]->src, refname);\n }\n \n-static struct ref *get_expanded_map(struct ref *remote_refs,\n+static struct ref *get_expanded_map(const struct ref *remote_refs,\n \t\t\t\t    const struct refspec *refspec)\n {\n-\tstruct ref *ref;\n+\tconst struct ref *ref;\n \tstruct ref *ret = NULL;\n \tstruct ref **tail = &ret;\n \n@@ -824,7 +836,7 @@ static struct ref *get_expanded_map(struct ref *remote_refs,\n \t\tif (strchr(ref->name, '^'))\n \t\t\tcontinue; /* a dereference item */\n \t\tif (!prefixcmp(ref->name, refspec->src)) {\n-\t\t\tchar *match;\n+\t\t\tconst char *match;\n \t\t\tstruct ref *cpy = copy_ref(ref);\n \t\t\tmatch = ref->name + remote_prefix_len;\n \n@@ -842,9 +854,9 @@ static struct ref *get_expanded_map(struct ref *remote_refs,\n \treturn ret;\n }\n \n-static struct ref *find_ref_by_name_abbrev(struct ref *refs, const char *name)\n+static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name)\n {\n-\tstruct ref *ref;\n+\tconst struct ref *ref;\n \tfor (ref = refs; ref; ref = ref->next) {\n \t\tif (ref_matches_abbrev(name, ref->name))\n \t\t\treturn ref;\n@@ -852,9 +864,9 @@ static struct ref *find_ref_by_name_abbrev(struct ref *refs, const char *name)\n \treturn NULL;\n }\n \n-struct ref *get_remote_ref(struct ref *remote_refs, const char *name)\n+struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)\n {\n-\tstruct ref *ref = find_ref_by_name_abbrev(remote_refs, name);\n+\tconst struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);\n \n \tif (!ref)\n \t\tdie(\"Couldn't find remote ref %s\\n\", name);\n@@ -887,7 +899,7 @@ static struct ref *get_local_ref(const char *name)\n \treturn ret;\n }\n \n-int get_fetch_map(struct ref *remote_refs,\n+int get_fetch_map(const struct ref *remote_refs,\n \t\t  const struct refspec *refspec,\n \t\t  struct ref ***tail)\n {\ndiff --git a/remote.h b/remote.h\nindex c62636d..723c21a 100644\n--- a/remote.h\n+++ b/remote.h\n@@ -44,6 +44,10 @@ struct refspec {\n \n struct ref *alloc_ref(unsigned namelen);\n \n+struct ref *copy_ref_list(const struct ref *ref);\n+\n+int check_ref_type(const struct ref *ref, int flags);\n+\n /*\n  * Frees the entire list and peers of elements.\n  */\n@@ -57,7 +61,7 @@ void ref_remove_duplicates(struct ref *ref_map);\n struct refspec *parse_ref_spec(int nr_refspec, const char **refspec);\n \n int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,\n-\t       int nr_refspec, char **refspec, int all);\n+\t       int nr_refspec, const char **refspec, int all);\n \n /*\n  * Given a list of the remote refs and the specification of things to\n@@ -68,10 +72,10 @@ int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,\n  * beforehand, and will be set to the tail pointer of the list of\n  * results afterward.\n  */\n-int get_fetch_map(struct ref *remote_refs, const struct refspec *refspec,\n+int get_fetch_map(const struct ref *remote_refs, const struct refspec *refspec,\n \t\t  struct ref ***tail);\n \n-struct ref *get_remote_ref(struct ref *remote_refs, const char *name);\n+struct ref *get_remote_ref(const struct ref *remote_refs, const char *name);\n \n /*\n  * For the given remote, reads the refspec's src and sets the other fields.\ndiff --git a/send-pack.c b/send-pack.c\nindex e9b9a39..fe56617 100644\n--- a/send-pack.c\n+++ b/send-pack.c\n@@ -178,7 +178,7 @@ static int receive_status(int in)\n \treturn ret;\n }\n \n-static int send_pack(int in, int out, struct remote *remote, int nr_refspec, char **refspec)\n+static int send_pack(int in, int out, struct remote *remote, int nr_refspec, const char **refspec)\n {\n \tstruct ref *ref;\n \tint new_refs;\n@@ -339,7 +339,7 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, cha\n \treturn ret;\n }\n \n-static void verify_remote_names(int nr_heads, char **heads)\n+static void verify_remote_names(int nr_heads, const char **heads)\n {\n \tint i;\n \n@@ -364,7 +364,7 @@ int main(int argc, char **argv)\n {\n \tint i, nr_heads = 0;\n \tchar *dest = NULL;\n-\tchar **heads = NULL;\n+\tconst char **heads = NULL;\n \tint fd[2], ret;\n \tpid_t pid;\n \tchar *remote_name = NULL;\n@@ -416,7 +416,7 @@ int main(int argc, char **argv)\n \t\t\tdest = arg;\n \t\t\tcontinue;\n \t\t}\n-\t\theads = argv;\n+\t\theads = (const char **) argv;\n \t\tnr_heads = argc - i;\n \t\tbreak;\n \t}\ndiff --git a/transport.c b/transport.c\nindex 400af71..89b73dc 100644\n--- a/transport.c\n+++ b/transport.c\n@@ -141,7 +141,7 @@ static void insert_packed_refs(const char *packed_refs, struct ref **list)\n \t}\n }\n \n-static struct ref *get_refs_via_rsync(const struct transport *transport)\n+static struct ref *get_refs_via_rsync(struct transport *transport)\n {\n \tstruct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT;\n \tstruct ref dummy, *tail = &dummy;\n@@ -427,7 +427,7 @@ static int missing__target(int code, int result)\n \n #define missing_target(a) missing__target((a)->http_code, (a)->curl_result)\n \n-static struct ref *get_refs_via_curl(const struct transport *transport)\n+static struct ref *get_refs_via_curl(struct transport *transport)\n {\n \tstruct buffer buffer;\n \tchar *data, *start, *mid;\n@@ -524,7 +524,7 @@ struct bundle_transport_data {\n \tstruct bundle_header header;\n };\n \n-static struct ref *get_refs_from_bundle(const struct transport *transport)\n+static struct ref *get_refs_from_bundle(struct transport *transport)\n {\n \tstruct bundle_transport_data *data = transport->data;\n \tstruct ref *result = NULL;\n@@ -596,7 +596,7 @@ static int set_git_option(struct transport *connection,\n \treturn 1;\n }\n \n-static struct ref *get_refs_via_connect(const struct transport *transport)\n+static struct ref *get_refs_via_connect(struct transport *transport)\n {\n \tstruct git_transport_data *data = transport->data;\n \tstruct ref *refs;\n@@ -786,7 +786,7 @@ int transport_push(struct transport *transport,\n \treturn transport->push(transport, refspec_nr, refspec, flags);\n }\n \n-struct ref *transport_get_remote_refs(struct transport *transport)\n+const struct ref *transport_get_remote_refs(struct transport *transport)\n {\n \tif (!transport->remote_refs)\n \t\ttransport->remote_refs = transport->get_refs_list(transport);\ndiff --git a/transport.h b/transport.h\nindex df12ea7..d27f562 100644\n--- a/transport.h\n+++ b/transport.h\n@@ -8,7 +8,7 @@ struct transport {\n \tstruct remote *remote;\n \tconst char *url;\n \tvoid *data;\n-\tstruct ref *remote_refs;\n+\tconst struct ref *remote_refs;\n \n \t/**\n \t * Returns 0 if successful, positive if the option is not\n@@ -18,7 +18,7 @@ struct transport {\n \tint (*set_option)(struct transport *connection, const char *name,\n \t\t\t  const char *value);\n \n-\tstruct ref *(*get_refs_list)(const struct transport *transport);\n+\tstruct ref *(*get_refs_list)(struct transport *transport);\n \tint (*fetch)(struct transport *transport, int refs_nr, struct ref **refs);\n \tint (*push)(struct transport *connection, int refspec_nr, const char **refspec, int flags);\n \n@@ -61,7 +61,7 @@ int transport_set_option(struct transport *transport, const char *name,\n int transport_push(struct transport *connection,\n \t\t   int refspec_nr, const char **refspec, int flags);\n \n-struct ref *transport_get_remote_refs(struct transport *transport);\n+const struct ref *transport_get_remote_refs(struct transport *transport);\n \n int transport_fetch_refs(struct transport *transport, struct ref *refs);\n void transport_unlock_pack(struct transport *transport);\n-- \n1.5.3.4.1206.g5f96\n"}]}