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>
Daniel Barkalow committed
Oct 29, 2007 at 21:05 UTC
4577370e9bfeca8652880b99b8499f76d18865ba
9 files changed
+57
-33
builtin-fetch.c
+5
-5
@@ -29,7 +29,7 @@ static void unlock_pack_on_signal(int signo)
29
}
30
31
static void add_merge_config(struct ref **head,
32
- struct ref *remote_refs,
32
+ const struct ref *remote_refs,
33
struct branch *branch,
34
struct ref ***tail)
35
{
@@ -77,7 +77,7 @@ static struct ref *get_ref_map(struct transport *transport,
77
struct ref *ref_map = NULL;
78
struct ref **tail = &ref_map;
79
80
- struct ref *remote_refs = transport_get_remote_refs(transport);
80
+ const struct ref *remote_refs = transport_get_remote_refs(transport);
81
82
if (ref_count || tags) {
83
for (i = 0; i < ref_count; i++) {
@@ -345,12 +345,12 @@ static struct ref *find_non_local_tags(struct transport *transport,
345
struct path_list new_refs = { NULL, 0, 0, 1 };
346
char *ref_name;
347
int ref_name_len;
348
- unsigned char *ref_sha1;
349
- struct ref *tag_ref;
348
+ const unsigned char *ref_sha1;
349
+ const struct ref *tag_ref;
350
struct ref *rm = NULL;
351
struct ref *ref_map = NULL;
352
struct ref **tail = &ref_map;
353
- struct ref *ref;
353
+ const struct ref *ref;
354
355
for_each_ref(add_existing, &existing_refs);
356
for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
cache.h
+1
-1
@@ -503,7 +503,7 @@ struct ref {
503
#define REF_TAGS (1u << 2)
504
505
#define CONNECT_VERBOSE (1u << 0)
506
-extern struct child_process *git_connect(int fd[2], char *url, const char *prog, int flags);
506
+extern struct child_process *git_connect(int fd[2], const char *url, const char *prog, int flags);
507
extern int finish_connect(struct child_process *conn);
508
extern int path_match(const char *path, int nr, char **match);
509
extern int get_ack(int fd, unsigned char *result_sha1);
connect.c
+9
-1
@@ -36,6 +36,11 @@ static int check_ref(const char *name, int len, unsigned int flags)
36
return !(flags & ~REF_NORMAL);
37
}
38
39
+int check_ref_type(const struct ref *ref, int flags)
40
+{
41
+ return check_ref(ref->name, strlen(ref->name), flags);
42
+}
43
+
44
/*
45
* Read all the refs from the other end
46
*/
@@ -476,9 +481,10 @@ char *get_port(char *host)
481
*
482
* If it returns, the connect is successful; it just dies on errors.
483
*/
479
-struct child_process *git_connect(int fd[2], char *url,
484
+struct child_process *git_connect(int fd[2], const char *url_orig,
485
const char *prog, int flags)
486
{
487
+ char *url = xstrdup(url_orig);
488
char *host, *path = url;
489
char *end;
490
int c;
@@ -568,6 +574,7 @@ struct child_process *git_connect(int fd[2], char *url,
574
prog, path, 0,
575
target_host, 0);
576
free(target_host);
577
+ free(url);
578
if (free_path)
579
free(path);
580
return NULL;
@@ -619,6 +626,7 @@ struct child_process *git_connect(int fd[2], char *url,
626
fd[0] = conn->out; /* read from child's stdout */
627
fd[1] = conn->in; /* write to child's stdin */
628
strbuf_release(&cmd);
629
+ free(url);
630
if (free_path)
631
free(path);
632
return conn;
http-push.c
+1
-1
@@ -2389,7 +2389,7 @@ int main(int argc, char **argv)
2389
if (!remote_tail)
2390
remote_tail = &remote_refs;
2391
if (match_refs(local_refs, remote_refs, &remote_tail,
2392
- nr_refspec, refspec, push_all))
2392
+ nr_refspec, (const char **) refspec, push_all))
2393
return -1;
2394
if (!remote_refs) {
2395
fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
remote.c
+22
-10
@@ -485,7 +485,7 @@ struct ref *alloc_ref(unsigned namelen)
485
return ret;
486
}
487
488
-static struct ref *copy_ref(struct ref *ref)
488
+static struct ref *copy_ref(const struct ref *ref)
489
{
490
struct ref *ret = xmalloc(sizeof(struct ref) + strlen(ref->name) + 1);
491
memcpy(ret, ref, sizeof(struct ref) + strlen(ref->name) + 1);
@@ -493,6 +493,18 @@ static struct ref *copy_ref(struct ref *ref)
493
return ret;
494
}
495
496
+struct ref *copy_ref_list(const struct ref *ref)
497
+{
498
+ struct ref *ret = NULL;
499
+ struct ref **tail = &ret;
500
+ while (ref) {
501
+ *tail = copy_ref(ref);
502
+ ref = ref->next;
503
+ tail = &((*tail)->next);
504
+ }
505
+ return ret;
506
+}
507
+
508
void free_refs(struct ref *ref)
509
{
510
struct ref *next;
@@ -710,7 +722,7 @@ static const struct refspec *check_pattern_match(const struct refspec *rs,
722
* without thinking.
723
*/
724
int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
713
- int nr_refspec, char **refspec, int all)
725
+ int nr_refspec, const char **refspec, int all)
726
{
727
struct refspec *rs =
728
parse_ref_spec(nr_refspec, (const char **) refspec);
@@ -810,10 +822,10 @@ int branch_merge_matches(struct branch *branch,
822
return ref_matches_abbrev(branch->merge[i]->src, refname);
823
}
824
813
-static struct ref *get_expanded_map(struct ref *remote_refs,
825
+static struct ref *get_expanded_map(const struct ref *remote_refs,
826
const struct refspec *refspec)
827
{
816
- struct ref *ref;
828
+ const struct ref *ref;
829
struct ref *ret = NULL;
830
struct ref **tail = &ret;
831
@@ -824,7 +836,7 @@ static struct ref *get_expanded_map(struct ref *remote_refs,
836
if (strchr(ref->name, '^'))
837
continue; /* a dereference item */
838
if (!prefixcmp(ref->name, refspec->src)) {
827
- char *match;
839
+ const char *match;
840
struct ref *cpy = copy_ref(ref);
841
match = ref->name + remote_prefix_len;
842
@@ -842,9 +854,9 @@ static struct ref *get_expanded_map(struct ref *remote_refs,
854
return ret;
855
}
856
845
-static struct ref *find_ref_by_name_abbrev(struct ref *refs, const char *name)
857
+static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name)
858
{
847
- struct ref *ref;
859
+ const struct ref *ref;
860
for (ref = refs; ref; ref = ref->next) {
861
if (ref_matches_abbrev(name, ref->name))
862
return ref;
@@ -852,9 +864,9 @@ static struct ref *find_ref_by_name_abbrev(struct ref *refs, const char *name)
864
return NULL;
865
}
866
855
-struct ref *get_remote_ref(struct ref *remote_refs, const char *name)
867
+struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)
868
{
857
- struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
869
+ const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
870
871
if (!ref)
872
return NULL;
@@ -887,7 +899,7 @@ static struct ref *get_local_ref(const char *name)
899
return ret;
900
}
901
890
-int get_fetch_map(struct ref *remote_refs,
902
+int get_fetch_map(const struct ref *remote_refs,
903
const struct refspec *refspec,
904
struct ref ***tail,
905
int missing_ok)
remote.h
+7
-3
@@ -44,6 +44,10 @@ struct refspec {
44
45
struct ref *alloc_ref(unsigned namelen);
46
47
+struct ref *copy_ref_list(const struct ref *ref);
48
+
49
+int check_ref_type(const struct ref *ref, int flags);
50
+
51
/*
52
* Frees the entire list and peers of elements.
53
*/
@@ -57,7 +61,7 @@ void ref_remove_duplicates(struct ref *ref_map);
61
struct refspec *parse_ref_spec(int nr_refspec, const char **refspec);
62
63
int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
60
- int nr_refspec, char **refspec, int all);
64
+ int nr_refspec, const char **refspec, int all);
65
66
/*
67
* 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,
75
* missing_ok is usually false, but when we are adding branch.$name.merge
76
* it is Ok if the branch is not at the remote anymore.
77
*/
74
-int get_fetch_map(struct ref *remote_refs, const struct refspec *refspec,
78
+int get_fetch_map(const struct ref *remote_refs, const struct refspec *refspec,
79
struct ref ***tail, int missing_ok);
80
77
-struct ref *get_remote_ref(struct ref *remote_refs, const char *name);
81
+struct ref *get_remote_ref(const struct ref *remote_refs, const char *name);
82
83
/*
84
* For the given remote, reads the refspec's src and sets the other fields.
send-pack.c
+4
-4
@@ -207,7 +207,7 @@ static void update_tracking_ref(struct remote *remote, struct ref *ref)
207
}
208
}
209
210
-static int send_pack(int in, int out, struct remote *remote, int nr_refspec, char **refspec)
210
+static int send_pack(int in, int out, struct remote *remote, int nr_refspec, const char **refspec)
211
{
212
struct ref *ref;
213
int new_refs;
@@ -357,7 +357,7 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, cha
357
return ret;
358
}
359
360
-static void verify_remote_names(int nr_heads, char **heads)
360
+static void verify_remote_names(int nr_heads, const char **heads)
361
{
362
int i;
363
@@ -382,7 +382,7 @@ int main(int argc, char **argv)
382
{
383
int i, nr_heads = 0;
384
char *dest = NULL;
385
- char **heads = NULL;
385
+ const char **heads = NULL;
386
int fd[2], ret;
387
struct child_process *conn;
388
char *remote_name = NULL;
@@ -434,7 +434,7 @@ int main(int argc, char **argv)
434
dest = arg;
435
continue;
436
}
437
- heads = argv;
437
+ heads = (const char **) argv;
438
nr_heads = argc - i;
439
break;
440
}
transport.c
+5
-5
@@ -141,7 +141,7 @@ static void insert_packed_refs(const char *packed_refs, struct ref **list)
141
}
142
}
143
144
-static struct ref *get_refs_via_rsync(const struct transport *transport)
144
+static struct ref *get_refs_via_rsync(struct transport *transport)
145
{
146
struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT;
147
struct ref dummy, *tail = &dummy;
@@ -427,7 +427,7 @@ static int missing__target(int code, int result)
427
428
#define missing_target(a) missing__target((a)->http_code, (a)->curl_result)
429
430
-static struct ref *get_refs_via_curl(const struct transport *transport)
430
+static struct ref *get_refs_via_curl(struct transport *transport)
431
{
432
struct buffer buffer;
433
char *data, *start, *mid;
@@ -524,7 +524,7 @@ struct bundle_transport_data {
524
struct bundle_header header;
525
};
526
527
-static struct ref *get_refs_from_bundle(const struct transport *transport)
527
+static struct ref *get_refs_from_bundle(struct transport *transport)
528
{
529
struct bundle_transport_data *data = transport->data;
530
struct ref *result = NULL;
@@ -596,7 +596,7 @@ static int set_git_option(struct transport *connection,
596
return 1;
597
}
598
599
-static struct ref *get_refs_via_connect(const struct transport *transport)
599
+static struct ref *get_refs_via_connect(struct transport *transport)
600
{
601
struct git_transport_data *data = transport->data;
602
struct ref *refs;
@@ -781,7 +781,7 @@ int transport_push(struct transport *transport,
781
return transport->push(transport, refspec_nr, refspec, flags);
782
}
783
784
-struct ref *transport_get_remote_refs(struct transport *transport)
784
+const struct ref *transport_get_remote_refs(struct transport *transport)
785
{
786
if (!transport->remote_refs)
787
transport->remote_refs = transport->get_refs_list(transport);
transport.h
+3
-3
@@ -8,7 +8,7 @@ struct transport {
8
struct remote *remote;
9
const char *url;
10
void *data;
11
- struct ref *remote_refs;
11
+ const struct ref *remote_refs;
12
13
/**
14
* Returns 0 if successful, positive if the option is not
@@ -18,7 +18,7 @@ struct transport {
18
int (*set_option)(struct transport *connection, const char *name,
19
const char *value);
20
21
- struct ref *(*get_refs_list)(const struct transport *transport);
21
+ struct ref *(*get_refs_list)(struct transport *transport);
22
int (*fetch)(struct transport *transport, int refs_nr, struct ref **refs);
23
int (*push)(struct transport *connection, int refspec_nr, const char **refspec, int flags);
24
@@ -61,7 +61,7 @@ int transport_set_option(struct transport *transport, const char *name,
61
int transport_push(struct transport *connection,
62
int refspec_nr, const char **refspec, int flags);
63
64
-struct ref *transport_get_remote_refs(struct transport *transport);
64
+const struct ref *transport_get_remote_refs(struct transport *transport);
65
66
int transport_fetch_refs(struct transport *transport, struct ref *refs);
67
void transport_unlock_pack(struct transport *transport);