2 #include "parse-options.h"
7 #include "run-command.h"
10 static const char * const builtin_remote_usage
[] = {
12 "git remote add <name> <url>",
13 "git remote rm <name>",
14 "git remote show <name>",
15 "git remote prune <name>",
16 "git remote update [group]",
22 static inline int postfixcmp(const char *string
, const char *postfix
)
24 int len1
= strlen(string
), len2
= strlen(postfix
);
27 return strcmp(string
+ len1
- len2
, postfix
);
30 static inline const char *skip_prefix(const char *name
, const char *prefix
)
33 prefixcmp(name
, prefix
) ? name
: name
+ strlen(prefix
);
36 static int opt_parse_track(const struct option
*opt
, const char *arg
, int not)
38 struct path_list
*list
= opt
->value
;
40 path_list_clear(list
, 0);
42 path_list_append(arg
, list
);
46 static int fetch_remote(const char *name
)
48 const char *argv
[] = { "fetch", name
, NULL
};
49 if (run_command_v_opt(argv
, RUN_GIT_CMD
))
50 return error("Could not fetch %s", name
);
54 static int add(int argc
, const char **argv
)
56 int fetch
= 0, mirror
= 0;
57 struct path_list track
= { NULL
, 0, 0 };
58 const char *master
= NULL
;
59 struct remote
*remote
;
60 struct strbuf buf
, buf2
;
61 const char *name
, *url
;
64 struct option options
[] = {
65 OPT_GROUP("add specific options"),
66 OPT_BOOLEAN('f', "fetch", &fetch
, "fetch the remote branches"),
67 OPT_CALLBACK('t', "track", &track
, "branch",
68 "branch(es) to track", opt_parse_track
),
69 OPT_STRING('m', "master", &master
, "branch", "master branch"),
70 OPT_BOOLEAN(0, "mirror", &mirror
, "no separate remotes"),
74 argc
= parse_options(argc
, argv
, options
, builtin_remote_usage
, 0);
77 usage_with_options(builtin_remote_usage
, options
);
82 remote
= remote_get(name
);
83 if (remote
&& (remote
->url_nr
> 1 || strcmp(name
, remote
->url
[0]) ||
84 remote
->fetch_refspec_nr
))
85 die("remote %s already exists.", name
);
88 strbuf_init(&buf2
, 0);
90 strbuf_addf(&buf
, "remote.%s.url", name
);
91 if (git_config_set(buf
.buf
, url
))
95 path_list_append("*", &track
);
96 for (i
= 0; i
< track
.nr
; i
++) {
97 struct path_list_item
*item
= track
.items
+ i
;
100 strbuf_addf(&buf
, "remote.%s.fetch", name
);
104 strbuf_addf(&buf2
, "refs/%s:refs/%s",
105 item
->path
, item
->path
);
107 strbuf_addf(&buf2
, "refs/heads/%s:refs/remotes/%s/%s",
108 item
->path
, name
, item
->path
);
109 if (git_config_set_multivar(buf
.buf
, buf2
.buf
, "^$", 0))
113 if (fetch
&& fetch_remote(name
))
118 strbuf_addf(&buf
, "refs/remotes/%s/HEAD", name
);
121 strbuf_addf(&buf2
, "refs/remotes/%s/%s", name
, master
);
123 if (create_symref(buf
.buf
, buf2
.buf
, "remote add"))
124 return error("Could not setup master '%s'", master
);
127 strbuf_release(&buf
);
128 strbuf_release(&buf2
);
129 path_list_clear(&track
, 0);
136 struct path_list merge
;
139 static struct path_list branch_list
;
141 static int config_read_branches(const char *key
, const char *value
)
143 if (!prefixcmp(key
, "branch.")) {
145 struct path_list_item
*item
;
146 struct branch_info
*info
;
147 enum { REMOTE
, MERGE
} type
;
150 if (!postfixcmp(key
, ".remote")) {
151 name
= xstrndup(key
, strlen(key
) - 7);
153 } else if (!postfixcmp(key
, ".merge")) {
154 name
= xstrndup(key
, strlen(key
) - 6);
159 item
= path_list_insert(name
, &branch_list
);
162 item
->util
= xcalloc(sizeof(struct branch_info
), 1);
164 if (type
== REMOTE
) {
166 warning("more than one branch.%s", key
);
167 info
->remote
= xstrdup(value
);
169 char *space
= strchr(value
, ' ');
170 value
= skip_prefix(value
, "refs/heads/");
173 merge
= xstrndup(value
, space
- value
);
174 path_list_append(merge
, &info
->merge
);
175 value
= skip_prefix(space
+ 1, "refs/heads/");
176 space
= strchr(value
, ' ');
178 path_list_append(xstrdup(value
), &info
->merge
);
184 static void read_branches(void)
188 git_config(config_read_branches
);
189 sort_path_list(&branch_list
);
193 struct remote
*remote
;
194 struct strbuf remote_prefix
;
195 struct path_list
new, stale
, tracked
;
198 static int handle_one_branch(const char *refname
,
199 const unsigned char *sha1
, int flags
, void *cb_data
)
201 struct ref_states
*states
= cb_data
;
202 struct refspec refspec
;
204 memset(&refspec
, 0, sizeof(refspec
));
205 refspec
.dst
= (char *)refname
;
206 if (!remote_find_tracking(states
->remote
, &refspec
)) {
207 struct path_list_item
*item
;
208 const char *name
= skip_prefix(refspec
.src
, "refs/heads/");
209 if (unsorted_path_list_has_path(&states
->tracked
, name
) ||
210 unsorted_path_list_has_path(&states
->new,
213 item
= path_list_append(name
, &states
->stale
);
214 item
->util
= xstrdup(refname
);
219 static int get_ref_states(const struct ref
*ref
, struct ref_states
*states
)
221 struct ref
*fetch_map
= NULL
, **tail
= &fetch_map
;
224 for (i
= 0; i
< states
->remote
->fetch_refspec_nr
; i
++)
225 if (get_fetch_map(ref
, states
->remote
->fetch
+ i
, &tail
, 1))
226 die("Could not get fetch map for refspec %s",
227 states
->remote
->fetch_refspec
[i
]);
229 states
->new.strdup_paths
= states
->tracked
.strdup_paths
= 1;
230 for (ref
= fetch_map
; ref
; ref
= ref
->next
) {
231 struct path_list
*target
= &states
->tracked
;
232 unsigned char sha1
[20];
235 if (!ref
->peer_ref
|| read_ref(ref
->peer_ref
->name
, sha1
))
236 target
= &states
->new;
238 target
= &states
->tracked
;
239 if (hashcmp(sha1
, ref
->new_sha1
))
242 path_list_append(skip_prefix(ref
->name
, "refs/heads/"),
243 target
)->util
= util
;
245 free_refs(fetch_map
);
247 strbuf_addf(&states
->remote_prefix
,
248 "refs/remotes/%s/", states
->remote
->name
);
249 for_each_ref(handle_one_branch
, states
);
250 sort_path_list(&states
->stale
);
255 struct branches_for_remote
{
257 struct path_list
*branches
;
260 static int add_branch_for_removal(const char *refname
,
261 const unsigned char *sha1
, int flags
, void *cb_data
)
263 struct branches_for_remote
*branches
= cb_data
;
265 if (!prefixcmp(refname
, branches
->prefix
)) {
266 struct path_list_item
*item
;
267 item
= path_list_append(refname
, branches
->branches
);
268 item
->util
= xmalloc(20);
269 hashcpy(item
->util
, sha1
);
275 static int remove_branches(struct path_list
*branches
)
278 for (i
= 0; i
< branches
->nr
; i
++) {
279 struct path_list_item
*item
= branches
->items
+ i
;
280 const char *refname
= item
->path
;
281 unsigned char *sha1
= item
->util
;
283 if (delete_ref(refname
, sha1
))
284 result
|= error("Could not remove branch %s", refname
);
289 static int rm(int argc
, const char **argv
)
291 struct option options
[] = {
294 struct remote
*remote
;
296 struct path_list branches
= { NULL
, 0, 0, 1 };
297 struct branches_for_remote cb_data
= { NULL
, &branches
};
301 usage_with_options(builtin_remote_usage
, options
);
303 remote
= remote_get(argv
[1]);
305 die("No such remote: %s", argv
[1]);
307 strbuf_init(&buf
, 0);
308 strbuf_addf(&buf
, "remote.%s", remote
->name
);
309 if (git_config_rename_section(buf
.buf
, NULL
) < 1)
310 return error("Could not remove config section '%s'", buf
.buf
);
313 for (i
= 0; i
< branch_list
.nr
; i
++) {
314 struct path_list_item
*item
= branch_list
.items
+ i
;
315 struct branch_info
*info
= item
->util
;
316 if (info
->remote
&& !strcmp(info
->remote
, remote
->name
)) {
317 const char *keys
[] = { "remote", "merge", NULL
}, **k
;
318 for (k
= keys
; *k
; k
++) {
320 strbuf_addf(&buf
, "branch.%s.%s",
322 if (git_config_set(buf
.buf
, NULL
)) {
323 strbuf_release(&buf
);
331 * We cannot just pass a function to for_each_ref() which deletes
332 * the branches one by one, since for_each_ref() relies on cached
333 * refs, which are invalidated when deleting a branch.
336 strbuf_addf(&buf
, "refs/remotes/%s/", remote
->name
);
337 cb_data
.prefix
= buf
.buf
;
338 i
= for_each_ref(add_branch_for_removal
, &cb_data
);
339 strbuf_release(&buf
);
342 i
= remove_branches(&branches
);
343 path_list_clear(&branches
, 1);
348 static void show_list(const char *title
, struct path_list
*list
)
355 printf(title
, list
->nr
> 1 ? "es" : "");
357 for (i
= 0; i
< list
->nr
; i
++)
358 printf("%s%s", i
? " " : "", list
->items
[i
].path
);
362 static int show_or_prune(int argc
, const char **argv
, int prune
)
364 int dry_run
= 0, result
= 0;
365 struct option options
[] = {
366 OPT_GROUP("show specific options"),
367 OPT__DRY_RUN(&dry_run
),
370 struct ref_states states
;
372 argc
= parse_options(argc
, argv
, options
, builtin_remote_usage
, 0);
375 usage_with_options(builtin_remote_usage
, options
);
377 memset(&states
, 0, sizeof(states
));
378 for (; argc
; argc
--, argv
++) {
379 struct transport
*transport
;
380 const struct ref
*ref
;
384 states
.remote
= remote_get(*argv
);
386 return error("No such remote: %s", *argv
);
387 transport
= transport_get(NULL
, states
.remote
->url_nr
> 0 ?
388 states
.remote
->url
[0] : NULL
);
389 ref
= transport_get_remote_refs(transport
);
392 got_states
= get_ref_states(ref
, &states
);
394 result
= error("Error getting local info for '%s'",
395 states
.remote
->name
);
400 strbuf_init(&buf
, 0);
401 for (i
= 0; i
< states
.stale
.nr
; i
++) {
403 strbuf_addf(&buf
, "refs/remotes/%s/%s", *argv
,
404 states
.stale
.items
[i
].path
);
405 result
|= delete_ref(buf
.buf
, NULL
);
408 strbuf_release(&buf
);
412 printf("* remote %s\n URL: %s\n", *argv
,
413 states
.remote
->url_nr
> 0 ?
414 states
.remote
->url
[0] : "(no URL)");
416 for (i
= 0; i
< branch_list
.nr
; i
++) {
417 struct path_list_item
*branch
= branch_list
.items
+ i
;
418 struct branch_info
*info
= branch
->util
;
421 if (!info
->merge
.nr
|| strcmp(*argv
, info
->remote
))
423 printf(" Remote branch%s merged with 'git pull' "
424 "while on branch %s\n ",
425 info
->merge
.nr
> 1 ? "es" : "",
427 for (j
= 0; j
< info
->merge
.nr
; j
++)
428 printf(" %s", info
->merge
.items
[j
].path
);
434 strbuf_init(&buf
, 0);
435 strbuf_addf(&buf
, " New remote branch%%s (next fetch will "
436 "store in remotes/%s)", states
.remote
->name
);
437 show_list(buf
.buf
, &states
.new);
438 strbuf_release(&buf
);
439 show_list(" Stale tracking branch%s (use 'git remote prune')",
441 show_list(" Tracked remote branch%s",
444 if (states
.remote
->push_refspec_nr
) {
445 printf(" Local branch%s pushed with 'git push'\n ",
446 states
.remote
->push_refspec_nr
> 1 ?
448 for (i
= 0; i
< states
.remote
->push_refspec_nr
; i
++) {
449 struct refspec
*spec
= states
.remote
->push
+ i
;
450 printf(" %s%s%s%s", spec
->force
? "+" : "",
451 skip_prefix(spec
->src
, "refs/heads/"),
452 spec
->dst
? ":" : "",
453 skip_prefix(spec
->dst
, "refs/heads/"));
457 /* NEEDSWORK: free remote */
458 path_list_clear(&states
.new, 0);
459 path_list_clear(&states
.stale
, 0);
460 path_list_clear(&states
.tracked
, 0);
466 static int update_one(struct remote
*remote
, void *priv
)
468 if (!remote
->skip_default_update
)
469 return fetch_remote(remote
->name
);
473 static int update(int argc
, const char **argv
)
478 return for_each_remote(update_one
, NULL
);
480 for (i
= 1; i
< argc
; i
++)
481 if (fetch_remote(argv
[i
]))
486 static int get_one_entry(struct remote
*remote
, void *priv
)
488 struct path_list
*list
= priv
;
490 path_list_append(remote
->name
, list
)->util
= remote
->url_nr
?
491 (void *)remote
->url
[0] : NULL
;
492 if (remote
->url_nr
> 1)
493 warning("Remote %s has more than one URL", remote
->name
);
498 static int show_all(void)
500 struct path_list list
= { NULL
, 0, 0 };
501 int result
= for_each_remote(get_one_entry
, &list
);
506 sort_path_list(&list
);
507 for (i
= 0; i
< list
.nr
; i
++) {
508 struct path_list_item
*item
= list
.items
+ i
;
509 printf("%s%s%s\n", item
->path
,
511 verbose
&& item
->util
?
512 (const char *)item
->util
: "");
518 int cmd_remote(int argc
, const char **argv
, const char *prefix
)
520 struct option options
[] = {
521 OPT__VERBOSE(&verbose
),
526 argc
= parse_options(argc
, argv
, options
, builtin_remote_usage
,
527 PARSE_OPT_STOP_AT_NON_OPTION
);
531 else if (!strcmp(argv
[0], "add"))
532 result
= add(argc
, argv
);
533 else if (!strcmp(argv
[0], "rm"))
534 result
= rm(argc
, argv
);
535 else if (!strcmp(argv
[0], "show"))
536 result
= show_or_prune(argc
, argv
, 0);
537 else if (!strcmp(argv
[0], "prune"))
538 result
= show_or_prune(argc
, argv
, 1);
539 else if (!strcmp(argv
[0], "update"))
540 result
= update(argc
, argv
);
542 error("Unknown subcommand: %s", argv
[0]);
543 usage_with_options(builtin_remote_usage
, options
);
546 return result
? 1 : 0;