5 static struct remote
**remotes
;
6 static int allocated_remotes
;
8 #define BUF_SIZE (2048)
9 static char buffer
[BUF_SIZE
];
11 static void add_push_refspec(struct remote
*remote
, const char *ref
)
13 int nr
= remote
->push_refspec_nr
+ 1;
14 remote
->push_refspec
=
15 xrealloc(remote
->push_refspec
, nr
* sizeof(char *));
16 remote
->push_refspec
[nr
-1] = ref
;
17 remote
->push_refspec_nr
= nr
;
20 static void add_uri(struct remote
*remote
, const char *uri
)
22 int nr
= remote
->uri_nr
+ 1;
24 xrealloc(remote
->uri
, nr
* sizeof(char *));
25 remote
->uri
[nr
-1] = uri
;
29 static struct remote
*make_remote(const char *name
, int len
)
33 for (i
= 0; i
< allocated_remotes
; i
++) {
38 if (len
? (!strncmp(name
, remotes
[i
]->name
, len
) &&
39 !remotes
[i
]->name
[len
]) :
40 !strcmp(name
, remotes
[i
]->name
))
46 empty
= allocated_remotes
;
47 allocated_remotes
+= allocated_remotes
? allocated_remotes
: 1;
48 remotes
= xrealloc(remotes
,
49 sizeof(*remotes
) * allocated_remotes
);
50 memset(remotes
+ empty
, 0,
51 (allocated_remotes
- empty
) * sizeof(*remotes
));
53 remotes
[empty
] = xcalloc(1, sizeof(struct remote
));
55 remotes
[empty
]->name
= xstrndup(name
, len
);
57 remotes
[empty
]->name
= xstrdup(name
);
58 return remotes
[empty
];
61 static void read_remotes_file(struct remote
*remote
)
63 FILE *f
= fopen(git_path("remotes/%s", remote
->name
), "r");
67 while (fgets(buffer
, BUF_SIZE
, f
)) {
71 if (!prefixcmp(buffer
, "URL:")) {
74 } else if (!prefixcmp(buffer
, "Push:")) {
86 while (isspace(p
[-1]))
91 add_uri(remote
, xstrdup(s
));
94 add_push_refspec(remote
, xstrdup(s
));
101 static void read_branches_file(struct remote
*remote
)
103 const char *slash
= strchr(remote
->name
, '/');
104 int n
= slash
? slash
- remote
->name
: 1000;
105 FILE *f
= fopen(git_path("branches/%.*s", n
, remote
->name
), "r");
111 s
= fgets(buffer
, BUF_SIZE
, f
);
120 while (isspace(p
[-1]))
124 len
+= strlen(slash
);
125 p
= xmalloc(len
+ 1);
132 static char *default_remote_name
= NULL
;
133 static const char *current_branch
= NULL
;
134 static int current_branch_len
= 0;
136 static int handle_config(const char *key
, const char *value
)
140 struct remote
*remote
;
141 if (!prefixcmp(key
, "branch.") && current_branch
&&
142 !strncmp(key
+ 7, current_branch
, current_branch_len
) &&
143 !strcmp(key
+ 7 + current_branch_len
, ".remote")) {
144 free(default_remote_name
);
145 default_remote_name
= xstrdup(value
);
147 if (prefixcmp(key
, "remote."))
150 subkey
= strrchr(name
, '.');
152 return error("Config with no key for remote %s", name
);
153 if (*subkey
== '/') {
154 warning("Config remote shorthand cannot begin with '/': %s", name
);
157 remote
= make_remote(name
, subkey
- name
);
159 /* if we ever have a boolean variable, e.g. "remote.*.disabled"
162 * is a valid way to set it to true; we get NULL in value so
163 * we need to handle it here.
165 * if (!strcmp(subkey, ".disabled")) {
166 * val = git_config_bool(key, value);
171 return 0; /* ignore unknown booleans */
173 if (!strcmp(subkey
, ".url")) {
174 add_uri(remote
, xstrdup(value
));
175 } else if (!strcmp(subkey
, ".push")) {
176 add_push_refspec(remote
, xstrdup(value
));
177 } else if (!strcmp(subkey
, ".receivepack")) {
178 if (!remote
->receivepack
)
179 remote
->receivepack
= xstrdup(value
);
181 error("more than one receivepack given, using the first");
186 static void read_config(void)
188 unsigned char sha1
[20];
189 const char *head_ref
;
191 if (default_remote_name
) // did this already
193 default_remote_name
= xstrdup("origin");
194 current_branch
= NULL
;
195 head_ref
= resolve_ref("HEAD", sha1
, 0, &flag
);
196 if (head_ref
&& (flag
& REF_ISSYMREF
) &&
197 !prefixcmp(head_ref
, "refs/heads/")) {
198 current_branch
= head_ref
+ strlen("refs/heads/");
199 current_branch_len
= strlen(current_branch
);
201 git_config(handle_config
);
204 struct remote
*remote_get(const char *name
)
210 name
= default_remote_name
;
211 ret
= make_remote(name
, 0);
212 if (name
[0] != '/') {
214 read_remotes_file(ret
);
216 read_branches_file(ret
);