@@ -21,33 +21,6 @@ struct counted_string {
21
21
size_t len ;
22
22
const char * s ;
23
23
};
24
- struct rewrite {
25
- const char * base ;
26
- size_t baselen ;
27
- struct counted_string * instead_of ;
28
- int instead_of_nr ;
29
- int instead_of_alloc ;
30
- };
31
- struct rewrites {
32
- struct rewrite * * rewrite ;
33
- int rewrite_alloc ;
34
- int rewrite_nr ;
35
- };
36
-
37
- static struct remote * * remotes ;
38
- static int remotes_alloc ;
39
- static int remotes_nr ;
40
- static struct hashmap remotes_hash ;
41
-
42
- static struct branch * * branches ;
43
- static int branches_alloc ;
44
- static int branches_nr ;
45
-
46
- static struct branch * current_branch ;
47
- static const char * pushremote_name ;
48
-
49
- static struct rewrites rewrites ;
50
- static struct rewrites rewrites_push ;
51
24
52
25
static int valid_remote (const struct remote * remote )
53
26
{
@@ -94,14 +67,16 @@ static void add_pushurl(struct remote *remote, const char *pushurl)
94
67
95
68
static void add_pushurl_alias (struct remote * remote , const char * url )
96
69
{
97
- const char * pushurl = alias_url (url , & rewrites_push );
70
+ const char * pushurl =
71
+ alias_url (url , & the_repository -> remote_state -> rewrites_push );
98
72
if (pushurl != url )
99
73
add_pushurl (remote , pushurl );
100
74
}
101
75
102
76
static void add_url_alias (struct remote * remote , const char * url )
103
77
{
104
- add_url (remote , alias_url (url , & rewrites ));
78
+ add_url (remote ,
79
+ alias_url (url , & the_repository -> remote_state -> rewrites ));
105
80
add_pushurl_alias (remote , url );
106
81
}
107
82
@@ -127,12 +102,6 @@ static int remotes_hash_cmp(const void *unused_cmp_data,
127
102
return strcmp (a -> name , b -> name );
128
103
}
129
104
130
- static inline void init_remotes_hash (void )
131
- {
132
- if (!remotes_hash .cmpfn )
133
- hashmap_init (& remotes_hash , remotes_hash_cmp , NULL , 0 );
134
- }
135
-
136
105
static struct remote * make_remote (const char * name , int len )
137
106
{
138
107
struct remote * ret ;
@@ -142,12 +111,12 @@ static struct remote *make_remote(const char *name, int len)
142
111
if (!len )
143
112
len = strlen (name );
144
113
145
- init_remotes_hash ();
146
114
lookup .str = name ;
147
115
lookup .len = len ;
148
116
hashmap_entry_init (& lookup_entry , memhash (name , len ));
149
117
150
- e = hashmap_get (& remotes_hash , & lookup_entry , & lookup );
118
+ e = hashmap_get (& the_repository -> remote_state -> remotes_hash ,
119
+ & lookup_entry , & lookup );
151
120
if (e )
152
121
return container_of (e , struct remote , ent );
153
122
@@ -158,15 +127,41 @@ static struct remote *make_remote(const char *name, int len)
158
127
refspec_init (& ret -> push , REFSPEC_PUSH );
159
128
refspec_init (& ret -> fetch , REFSPEC_FETCH );
160
129
161
- ALLOC_GROW (remotes , remotes_nr + 1 , remotes_alloc );
162
- remotes [remotes_nr ++ ] = ret ;
130
+ ALLOC_GROW (the_repository -> remote_state -> remotes ,
131
+ the_repository -> remote_state -> remotes_nr + 1 ,
132
+ the_repository -> remote_state -> remotes_alloc );
133
+ the_repository -> remote_state
134
+ -> remotes [the_repository -> remote_state -> remotes_nr ++ ] = ret ;
163
135
164
136
hashmap_entry_init (& ret -> ent , lookup_entry .hash );
165
- if (hashmap_put_entry (& remotes_hash , ret , ent ))
137
+ if (hashmap_put_entry (& the_repository -> remote_state -> remotes_hash , ret ,
138
+ ent ))
166
139
BUG ("hashmap_put overwrote entry after hashmap_get returned NULL" );
167
140
return ret ;
168
141
}
169
142
143
+ static void remote_clear (struct remote * remote )
144
+ {
145
+ int i ;
146
+
147
+ free ((char * )remote -> name );
148
+ free ((char * )remote -> foreign_vcs );
149
+
150
+ for (i = 0 ; i < remote -> url_nr ; i ++ ) {
151
+ free ((char * )remote -> url [i ]);
152
+ }
153
+ FREE_AND_NULL (remote -> pushurl );
154
+
155
+ for (i = 0 ; i < remote -> pushurl_nr ; i ++ ) {
156
+ free ((char * )remote -> pushurl [i ]);
157
+ }
158
+ FREE_AND_NULL (remote -> pushurl );
159
+ free ((char * )remote -> receivepack );
160
+ free ((char * )remote -> uploadpack );
161
+ FREE_AND_NULL (remote -> http_proxy );
162
+ FREE_AND_NULL (remote -> http_proxy_authmethod );
163
+ }
164
+
170
165
static void add_merge (struct branch * branch , const char * name )
171
166
{
172
167
ALLOC_GROW (branch -> merge_name , branch -> merge_nr + 1 ,
@@ -179,15 +174,20 @@ static struct branch *make_branch(const char *name, size_t len)
179
174
struct branch * ret ;
180
175
int i ;
181
176
182
- for (i = 0 ; i < branches_nr ; i ++ ) {
183
- if (!strncmp (name , branches [i ]-> name , len ) &&
184
- !branches [i ]-> name [len ])
185
- return branches [i ];
177
+ for (i = 0 ; i < the_repository -> remote_state -> branches_nr ; i ++ ) {
178
+ if (!strncmp (name ,
179
+ the_repository -> remote_state -> branches [i ]-> name ,
180
+ len ) &&
181
+ !the_repository -> remote_state -> branches [i ]-> name [len ])
182
+ return the_repository -> remote_state -> branches [i ];
186
183
}
187
184
188
- ALLOC_GROW (branches , branches_nr + 1 , branches_alloc );
185
+ ALLOC_GROW (the_repository -> remote_state -> branches ,
186
+ the_repository -> remote_state -> branches_nr + 1 ,
187
+ the_repository -> remote_state -> branches_alloc );
189
188
CALLOC_ARRAY (ret , 1 );
190
- branches [branches_nr ++ ] = ret ;
189
+ the_repository -> remote_state
190
+ -> branches [the_repository -> remote_state -> branches_nr ++ ] = ret ;
191
191
ret -> name = xstrndup (name , len );
192
192
ret -> refname = xstrfmt ("refs/heads/%s" , ret -> name );
193
193
@@ -327,12 +327,16 @@ static int handle_config(const char *key, const char *value, void *cb)
327
327
if (!strcmp (subkey , "insteadof" )) {
328
328
if (!value )
329
329
return config_error_nonbool (key );
330
- rewrite = make_rewrite (& rewrites , name , namelen );
330
+ rewrite = make_rewrite (
331
+ & the_repository -> remote_state -> rewrites , name ,
332
+ namelen );
331
333
add_instead_of (rewrite , xstrdup (value ));
332
334
} else if (!strcmp (subkey , "pushinsteadof" )) {
333
335
if (!value )
334
336
return config_error_nonbool (key );
335
- rewrite = make_rewrite (& rewrites_push , name , namelen );
337
+ rewrite = make_rewrite (
338
+ & the_repository -> remote_state -> rewrites_push ,
339
+ name , namelen );
336
340
add_instead_of (rewrite , xstrdup (value ));
337
341
}
338
342
}
@@ -342,7 +346,9 @@ static int handle_config(const char *key, const char *value, void *cb)
342
346
343
347
/* Handle remote.* variables */
344
348
if (!name && !strcmp (subkey , "pushdefault" ))
345
- return git_config_string (& pushremote_name , key , value );
349
+ return git_config_string (
350
+ & the_repository -> remote_state -> pushremote_name , key ,
351
+ value );
346
352
347
353
if (!name )
348
354
return 0 ;
@@ -425,18 +431,34 @@ static int handle_config(const char *key, const char *value, void *cb)
425
431
static void alias_all_urls (void )
426
432
{
427
433
int i , j ;
428
- for (i = 0 ; i < remotes_nr ; i ++ ) {
434
+ for (i = 0 ; i < the_repository -> remote_state -> remotes_nr ; i ++ ) {
429
435
int add_pushurl_aliases ;
430
- if (!remotes [i ])
436
+ if (!the_repository -> remote_state -> remotes [i ])
431
437
continue ;
432
- for (j = 0 ; j < remotes [i ]-> pushurl_nr ; j ++ ) {
433
- remotes [i ]-> pushurl [j ] = alias_url (remotes [i ]-> pushurl [j ], & rewrites );
438
+ for (j = 0 ;
439
+ j < the_repository -> remote_state -> remotes [i ]-> pushurl_nr ;
440
+ j ++ ) {
441
+ the_repository -> remote_state -> remotes [i ]-> pushurl [j ] =
442
+ alias_url (
443
+ the_repository -> remote_state -> remotes [i ]
444
+ -> pushurl [j ],
445
+ & the_repository -> remote_state -> rewrites );
434
446
}
435
- add_pushurl_aliases = remotes [i ]-> pushurl_nr == 0 ;
436
- for (j = 0 ; j < remotes [i ]-> url_nr ; j ++ ) {
447
+ add_pushurl_aliases =
448
+ the_repository -> remote_state -> remotes [i ]-> pushurl_nr ==
449
+ 0 ;
450
+ for (j = 0 ;
451
+ j < the_repository -> remote_state -> remotes [i ]-> url_nr ;
452
+ j ++ ) {
437
453
if (add_pushurl_aliases )
438
- add_pushurl_alias (remotes [i ], remotes [i ]-> url [j ]);
439
- remotes [i ]-> url [j ] = alias_url (remotes [i ]-> url [j ], & rewrites );
454
+ add_pushurl_alias (
455
+ the_repository -> remote_state -> remotes [i ],
456
+ the_repository -> remote_state -> remotes [i ]
457
+ -> url [j ]);
458
+ the_repository -> remote_state -> remotes [i ]
459
+ -> url [j ] = alias_url (
460
+ the_repository -> remote_state -> remotes [i ]-> url [j ],
461
+ & the_repository -> remote_state -> rewrites );
440
462
}
441
463
}
442
464
}
@@ -450,12 +472,13 @@ static void read_config(void)
450
472
return ;
451
473
loaded = 1 ;
452
474
453
- current_branch = NULL ;
475
+ the_repository -> remote_state -> current_branch = NULL ;
454
476
if (startup_info -> have_repository ) {
455
477
const char * head_ref = resolve_ref_unsafe ("HEAD" , 0 , NULL , & flag );
456
478
if (head_ref && (flag & REF_ISSYMREF ) &&
457
479
skip_prefix (head_ref , "refs/heads/" , & head_ref )) {
458
- current_branch = make_branch (head_ref , strlen (head_ref ));
480
+ the_repository -> remote_state -> current_branch =
481
+ make_branch (head_ref , strlen (head_ref ));
459
482
}
460
483
}
461
484
git_config (handle_config , NULL );
@@ -493,10 +516,10 @@ const char *pushremote_for_branch(struct branch *branch, int *explicit)
493
516
* explicit = 1 ;
494
517
return branch -> pushremote_name ;
495
518
}
496
- if (pushremote_name ) {
519
+ if (the_repository -> remote_state -> pushremote_name ) {
497
520
if (explicit )
498
521
* explicit = 1 ;
499
- return pushremote_name ;
522
+ return the_repository -> remote_state -> pushremote_name ;
500
523
}
501
524
return remote_for_branch (branch , explicit );
502
525
}
@@ -534,7 +557,8 @@ static struct remote *remote_get_1(const char *name,
534
557
if (name )
535
558
name_given = 1 ;
536
559
else
537
- name = get_default (current_branch , & name_given );
560
+ name = get_default (the_repository -> remote_state -> current_branch ,
561
+ & name_given );
538
562
539
563
ret = make_remote (name , 0 );
540
564
if (valid_remote_nick (name ) && have_git_dir ()) {
@@ -573,11 +597,13 @@ int for_each_remote(each_remote_fn fn, void *priv)
573
597
{
574
598
int i , result = 0 ;
575
599
read_config ();
576
- for (i = 0 ; i < remotes_nr && !result ; i ++ ) {
577
- struct remote * r = remotes [i ];
578
- if (!r )
600
+ for (i = 0 ; i < the_repository -> remote_state -> remotes_nr && !result ;
601
+ i ++ ) {
602
+ struct remote * remote =
603
+ the_repository -> remote_state -> remotes [i ];
604
+ if (!remote )
579
605
continue ;
580
- result = fn (r , priv );
606
+ result = fn (remote , priv );
581
607
}
582
608
return result ;
583
609
}
@@ -1685,7 +1711,7 @@ struct branch *branch_get(const char *name)
1685
1711
1686
1712
read_config ();
1687
1713
if (!name || !* name || !strcmp (name , "HEAD" ))
1688
- ret = current_branch ;
1714
+ ret = the_repository -> remote_state -> current_branch ;
1689
1715
else
1690
1716
ret = make_branch (name , strlen (name ));
1691
1717
set_merge (ret );
@@ -2585,3 +2611,34 @@ void apply_push_cas(struct push_cas_option *cas,
2585
2611
check_if_includes_upstream (ref );
2586
2612
}
2587
2613
}
2614
+
2615
+ struct remote_state * remote_state_new (void )
2616
+ {
2617
+ struct remote_state * r = xmalloc (sizeof (* r ));
2618
+
2619
+ memset (r , 0 , sizeof (* r ));
2620
+
2621
+ hashmap_init (& r -> remotes_hash , remotes_hash_cmp , NULL , 0 );
2622
+ return r ;
2623
+ }
2624
+
2625
+ void remote_state_clear (struct remote_state * remote_state )
2626
+ {
2627
+ int i ;
2628
+
2629
+ for (i = 0 ; i < remote_state -> remotes_nr ; i ++ ) {
2630
+ remote_clear (remote_state -> remotes [i ]);
2631
+ }
2632
+ FREE_AND_NULL (remote_state -> remotes );
2633
+ remote_state -> remotes_alloc = 0 ;
2634
+ remote_state -> remotes_nr = 0 ;
2635
+
2636
+ hashmap_clear_and_free (& remote_state -> remotes_hash , struct remote , ent );
2637
+
2638
+ for (i = 0 ; i < remote_state -> branches_nr ; i ++ ) {
2639
+ FREE_AND_NULL (remote_state -> branches [i ]);
2640
+ }
2641
+ FREE_AND_NULL (remote_state -> branches );
2642
+ remote_state -> branches_alloc = 0 ;
2643
+ remote_state -> branches_nr = 0 ;
2644
+ }
0 commit comments