2 #include "parse-options.h"
5 #include "string-list.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 int show_all(void);
24 static inline int postfixcmp(const char *string
, const char *postfix
)
26 int len1
= strlen(string
), len2
= strlen(postfix
);
29 return strcmp(string
+ len1
- len2
, postfix
);
32 static int opt_parse_track(const struct option
*opt
, const char *arg
, int not)
34 struct string_list
*list
= opt
->value
;
36 string_list_clear(list
, 0);
38 string_list_append(arg
, list
);
42 static int fetch_remote(const char *name
)
44 const char *argv
[] = { "fetch", name
, NULL
};
45 printf("Updating %s\n", name
);
46 if (run_command_v_opt(argv
, RUN_GIT_CMD
))
47 return error("Could not fetch %s", name
);
51 static int add(int argc
, const char **argv
)
53 int fetch
= 0, mirror
= 0;
54 struct string_list track
= { NULL
, 0, 0 };
55 const char *master
= NULL
;
56 struct remote
*remote
;
57 struct strbuf buf
, buf2
;
58 const char *name
, *url
;
61 struct option options
[] = {
62 OPT_GROUP("add specific options"),
63 OPT_BOOLEAN('f', "fetch", &fetch
, "fetch the remote branches"),
64 OPT_CALLBACK('t', "track", &track
, "branch",
65 "branch(es) to track", opt_parse_track
),
66 OPT_STRING('m', "master", &master
, "branch", "master branch"),
67 OPT_BOOLEAN(0, "mirror", &mirror
, "no separate remotes"),
71 argc
= parse_options(argc
, argv
, options
, builtin_remote_usage
, 0);
74 usage_with_options(builtin_remote_usage
, options
);
79 remote
= remote_get(name
);
80 if (remote
&& (remote
->url_nr
> 1 || strcmp(name
, remote
->url
[0]) ||
81 remote
->fetch_refspec_nr
))
82 die("remote %s already exists.", name
);
85 strbuf_init(&buf2
, 0);
87 strbuf_addf(&buf2
, "refs/heads/test:refs/remotes/%s/test", name
);
88 if (!valid_fetch_refspec(buf2
.buf
))
89 die("'%s' is not a valid remote name", name
);
91 strbuf_addf(&buf
, "remote.%s.url", name
);
92 if (git_config_set(buf
.buf
, url
))
96 strbuf_addf(&buf
, "remote.%s.fetch", name
);
99 string_list_append("*", &track
);
100 for (i
= 0; i
< track
.nr
; i
++) {
101 struct string_list_item
*item
= track
.items
+ i
;
104 strbuf_addch(&buf2
, '+');
106 strbuf_addf(&buf2
, "refs/%s:refs/%s",
107 item
->string
, item
->string
);
109 strbuf_addf(&buf2
, "refs/heads/%s:refs/remotes/%s/%s",
110 item
->string
, name
, item
->string
);
111 if (git_config_set_multivar(buf
.buf
, buf2
.buf
, "^$", 0))
117 strbuf_addf(&buf
, "remote.%s.mirror", name
);
118 if (git_config_set(buf
.buf
, "true"))
122 if (fetch
&& fetch_remote(name
))
127 strbuf_addf(&buf
, "refs/remotes/%s/HEAD", name
);
130 strbuf_addf(&buf2
, "refs/remotes/%s/%s", name
, master
);
132 if (create_symref(buf
.buf
, buf2
.buf
, "remote add"))
133 return error("Could not setup master '%s'", master
);
136 strbuf_release(&buf
);
137 strbuf_release(&buf2
);
138 string_list_clear(&track
, 0);
145 struct string_list merge
;
148 static struct string_list branch_list
;
150 static const char *abbrev_ref(const char *name
, const char *prefix
)
152 const char *abbrev
= skip_prefix(name
, prefix
);
157 #define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
159 static int config_read_branches(const char *key
, const char *value
, void *cb
)
161 if (!prefixcmp(key
, "branch.")) {
163 struct string_list_item
*item
;
164 struct branch_info
*info
;
165 enum { REMOTE
, MERGE
} type
;
168 if (!postfixcmp(key
, ".remote")) {
169 name
= xstrndup(key
, strlen(key
) - 7);
171 } else if (!postfixcmp(key
, ".merge")) {
172 name
= xstrndup(key
, strlen(key
) - 6);
177 item
= string_list_insert(name
, &branch_list
);
180 item
->util
= xcalloc(sizeof(struct branch_info
), 1);
182 if (type
== REMOTE
) {
184 warning("more than one branch.%s", key
);
185 info
->remote
= xstrdup(value
);
187 char *space
= strchr(value
, ' ');
188 value
= abbrev_branch(value
);
191 merge
= xstrndup(value
, space
- value
);
192 string_list_append(merge
, &info
->merge
);
193 value
= abbrev_branch(space
+ 1);
194 space
= strchr(value
, ' ');
196 string_list_append(xstrdup(value
), &info
->merge
);
202 static void read_branches(void)
206 git_config(config_read_branches
, NULL
);
207 sort_string_list(&branch_list
);
211 struct remote
*remote
;
212 struct string_list
new, stale
, tracked
;
215 static int handle_one_branch(const char *refname
,
216 const unsigned char *sha1
, int flags
, void *cb_data
)
218 struct ref_states
*states
= cb_data
;
219 struct refspec refspec
;
221 memset(&refspec
, 0, sizeof(refspec
));
222 refspec
.dst
= (char *)refname
;
223 if (!remote_find_tracking(states
->remote
, &refspec
)) {
224 struct string_list_item
*item
;
225 const char *name
= abbrev_branch(refspec
.src
);
226 /* symbolic refs pointing nowhere were handled already */
227 if ((flags
& REF_ISSYMREF
) ||
228 unsorted_string_list_has_string(&states
->tracked
,
230 unsorted_string_list_has_string(&states
->new,
233 item
= string_list_append(name
, &states
->stale
);
234 item
->util
= xstrdup(refname
);
239 static int get_ref_states(const struct ref
*ref
, struct ref_states
*states
)
241 struct ref
*fetch_map
= NULL
, **tail
= &fetch_map
;
244 for (i
= 0; i
< states
->remote
->fetch_refspec_nr
; i
++)
245 if (get_fetch_map(ref
, states
->remote
->fetch
+ i
, &tail
, 1))
246 die("Could not get fetch map for refspec %s",
247 states
->remote
->fetch_refspec
[i
]);
249 states
->new.strdup_strings
= states
->tracked
.strdup_strings
= 1;
250 for (ref
= fetch_map
; ref
; ref
= ref
->next
) {
251 struct string_list
*target
= &states
->tracked
;
252 unsigned char sha1
[20];
255 if (!ref
->peer_ref
|| read_ref(ref
->peer_ref
->name
, sha1
))
256 target
= &states
->new;
258 target
= &states
->tracked
;
259 if (hashcmp(sha1
, ref
->new_sha1
))
262 string_list_append(abbrev_branch(ref
->name
), target
)->util
= util
;
264 free_refs(fetch_map
);
266 for_each_ref(handle_one_branch
, states
);
267 sort_string_list(&states
->stale
);
272 struct known_remote
{
273 struct known_remote
*next
;
274 struct remote
*remote
;
277 struct known_remotes
{
278 struct remote
*to_delete
;
279 struct known_remote
*list
;
282 static int add_known_remote(struct remote
*remote
, void *cb_data
)
284 struct known_remotes
*all
= cb_data
;
285 struct known_remote
*r
;
287 if (!strcmp(all
->to_delete
->name
, remote
->name
))
290 r
= xmalloc(sizeof(*r
));
297 struct branches_for_remote
{
298 struct remote
*remote
;
299 struct string_list
*branches
;
300 struct known_remotes
*keep
;
303 static int add_branch_for_removal(const char *refname
,
304 const unsigned char *sha1
, int flags
, void *cb_data
)
306 struct branches_for_remote
*branches
= cb_data
;
307 struct refspec refspec
;
308 struct string_list_item
*item
;
309 struct known_remote
*kr
;
311 memset(&refspec
, 0, sizeof(refspec
));
312 refspec
.dst
= (char *)refname
;
313 if (remote_find_tracking(branches
->remote
, &refspec
))
316 /* don't delete a branch if another remote also uses it */
317 for (kr
= branches
->keep
->list
; kr
; kr
= kr
->next
) {
318 memset(&refspec
, 0, sizeof(refspec
));
319 refspec
.dst
= (char *)refname
;
320 if (!remote_find_tracking(kr
->remote
, &refspec
))
324 /* make sure that symrefs are deleted */
325 if (flags
& REF_ISSYMREF
)
326 return unlink(git_path(refname
));
328 item
= string_list_append(refname
, branches
->branches
);
329 item
->util
= xmalloc(20);
330 hashcpy(item
->util
, sha1
);
335 static int remove_branches(struct string_list
*branches
)
338 for (i
= 0; i
< branches
->nr
; i
++) {
339 struct string_list_item
*item
= branches
->items
+ i
;
340 const char *refname
= item
->string
;
341 unsigned char *sha1
= item
->util
;
343 if (delete_ref(refname
, sha1
))
344 result
|= error("Could not remove branch %s", refname
);
349 static int rm(int argc
, const char **argv
)
351 struct option options
[] = {
354 struct remote
*remote
;
356 struct known_remotes known_remotes
= { NULL
, NULL
};
357 struct string_list branches
= { NULL
, 0, 0, 1 };
358 struct branches_for_remote cb_data
= { NULL
, &branches
, &known_remotes
};
362 usage_with_options(builtin_remote_usage
, options
);
364 remote
= remote_get(argv
[1]);
366 die("No such remote: %s", argv
[1]);
368 known_remotes
.to_delete
= remote
;
369 for_each_remote(add_known_remote
, &known_remotes
);
371 strbuf_init(&buf
, 0);
372 strbuf_addf(&buf
, "remote.%s", remote
->name
);
373 if (git_config_rename_section(buf
.buf
, NULL
) < 1)
374 return error("Could not remove config section '%s'", buf
.buf
);
377 for (i
= 0; i
< branch_list
.nr
; i
++) {
378 struct string_list_item
*item
= branch_list
.items
+ i
;
379 struct branch_info
*info
= item
->util
;
380 if (info
->remote
&& !strcmp(info
->remote
, remote
->name
)) {
381 const char *keys
[] = { "remote", "merge", NULL
}, **k
;
382 for (k
= keys
; *k
; k
++) {
384 strbuf_addf(&buf
, "branch.%s.%s",
386 if (git_config_set(buf
.buf
, NULL
)) {
387 strbuf_release(&buf
);
395 * We cannot just pass a function to for_each_ref() which deletes
396 * the branches one by one, since for_each_ref() relies on cached
397 * refs, which are invalidated when deleting a branch.
399 cb_data
.remote
= remote
;
400 i
= for_each_ref(add_branch_for_removal
, &cb_data
);
401 strbuf_release(&buf
);
404 i
= remove_branches(&branches
);
405 string_list_clear(&branches
, 1);
410 static void show_list(const char *title
, struct string_list
*list
,
411 const char *extra_arg
)
418 printf(title
, list
->nr
> 1 ? "es" : "", extra_arg
);
420 for (i
= 0; i
< list
->nr
; i
++)
421 printf("%s%s", i
? " " : "", list
->items
[i
].string
);
425 static int get_remote_ref_states(const char *name
,
426 struct ref_states
*states
,
429 struct transport
*transport
;
430 const struct ref
*ref
;
432 states
->remote
= remote_get(name
);
434 return error("No such remote: %s", name
);
439 transport
= transport_get(NULL
, states
->remote
->url_nr
> 0 ?
440 states
->remote
->url
[0] : NULL
);
441 ref
= transport_get_remote_refs(transport
);
442 transport_disconnect(transport
);
444 get_ref_states(ref
, states
);
450 static int append_ref_to_tracked_list(const char *refname
,
451 const unsigned char *sha1
, int flags
, void *cb_data
)
453 struct ref_states
*states
= cb_data
;
454 struct refspec refspec
;
456 memset(&refspec
, 0, sizeof(refspec
));
457 refspec
.dst
= (char *)refname
;
458 if (!remote_find_tracking(states
->remote
, &refspec
))
459 string_list_append(abbrev_branch(refspec
.src
), &states
->tracked
);
464 static int show(int argc
, const char **argv
)
466 int no_query
= 0, result
= 0;
467 struct option options
[] = {
468 OPT_GROUP("show specific options"),
469 OPT_BOOLEAN('n', NULL
, &no_query
, "do not query remotes"),
472 struct ref_states states
;
474 argc
= parse_options(argc
, argv
, options
, builtin_remote_usage
, 0);
479 memset(&states
, 0, sizeof(states
));
480 for (; argc
; argc
--, argv
++) {
483 get_remote_ref_states(*argv
, &states
, !no_query
);
485 printf("* remote %s\n URL: %s\n", *argv
,
486 states
.remote
->url_nr
> 0 ?
487 states
.remote
->url
[0] : "(no URL)");
489 for (i
= 0; i
< branch_list
.nr
; i
++) {
490 struct string_list_item
*branch
= branch_list
.items
+ i
;
491 struct branch_info
*info
= branch
->util
;
494 if (!info
->merge
.nr
|| strcmp(*argv
, info
->remote
))
496 printf(" Remote branch%s merged with 'git pull' "
497 "while on branch %s\n ",
498 info
->merge
.nr
> 1 ? "es" : "",
500 for (j
= 0; j
< info
->merge
.nr
; j
++)
501 printf(" %s", info
->merge
.items
[j
].string
);
506 show_list(" New remote branch%s (next fetch "
507 "will store in remotes/%s)",
508 &states
.new, states
.remote
->name
);
509 show_list(" Stale tracking branch%s (use 'git remote "
510 "prune')", &states
.stale
, "");
514 for_each_ref(append_ref_to_tracked_list
, &states
);
515 show_list(" Tracked remote branch%s", &states
.tracked
, "");
517 if (states
.remote
->push_refspec_nr
) {
518 printf(" Local branch%s pushed with 'git push'\n ",
519 states
.remote
->push_refspec_nr
> 1 ?
521 for (i
= 0; i
< states
.remote
->push_refspec_nr
; i
++) {
522 struct refspec
*spec
= states
.remote
->push
+ i
;
523 printf(" %s%s%s%s", spec
->force
? "+" : "",
524 abbrev_branch(spec
->src
),
525 spec
->dst
? ":" : "",
526 spec
->dst
? abbrev_branch(spec
->dst
) : "");
531 /* NEEDSWORK: free remote */
532 string_list_clear(&states
.new, 0);
533 string_list_clear(&states
.stale
, 0);
534 string_list_clear(&states
.tracked
, 0);
540 static int prune(int argc
, const char **argv
)
542 int dry_run
= 0, result
= 0;
543 struct option options
[] = {
544 OPT_GROUP("prune specific options"),
545 OPT__DRY_RUN(&dry_run
),
548 struct ref_states states
;
550 argc
= parse_options(argc
, argv
, options
, builtin_remote_usage
, 0);
553 usage_with_options(builtin_remote_usage
, options
);
555 memset(&states
, 0, sizeof(states
));
556 for (; argc
; argc
--, argv
++) {
559 get_remote_ref_states(*argv
, &states
, 1);
561 if (states
.stale
.nr
) {
562 printf("Pruning %s\n", *argv
);
564 states
.remote
->url_nr
565 ? states
.remote
->url
[0]
569 for (i
= 0; i
< states
.stale
.nr
; i
++) {
570 const char *refname
= states
.stale
.items
[i
].util
;
573 result
|= delete_ref(refname
, NULL
);
575 printf(" * [%s] %s\n", dry_run
? "would prune" : "pruned",
576 abbrev_ref(refname
, "refs/remotes/"));
579 /* NEEDSWORK: free remote */
580 string_list_clear(&states
.new, 0);
581 string_list_clear(&states
.stale
, 0);
582 string_list_clear(&states
.tracked
, 0);
588 static int get_one_remote_for_update(struct remote
*remote
, void *priv
)
590 struct string_list
*list
= priv
;
591 if (!remote
->skip_default_update
)
592 string_list_append(xstrdup(remote
->name
), list
);
596 struct remote_group
{
598 struct string_list
*list
;
601 static int get_remote_group(const char *key
, const char *value
, void *cb
)
603 if (!prefixcmp(key
, "remotes.") &&
604 !strcmp(key
+ 8, remote_group
.name
)) {
605 /* split list by white space */
606 int space
= strcspn(value
, " \t\n");
609 string_list_append(xstrndup(value
, space
),
611 value
+= space
+ (value
[space
] != '\0');
612 space
= strcspn(value
, " \t\n");
619 static int update(int argc
, const char **argv
)
622 struct string_list list
= { NULL
, 0, 0, 0 };
623 static const char *default_argv
[] = { NULL
, "default", NULL
};
630 remote_group
.list
= &list
;
631 for (i
= 1; i
< argc
; i
++) {
632 remote_group
.name
= argv
[i
];
633 result
= git_config(get_remote_group
, NULL
);
636 if (!result
&& !list
.nr
&& argc
== 2 && !strcmp(argv
[1], "default"))
637 result
= for_each_remote(get_one_remote_for_update
, &list
);
639 for (i
= 0; i
< list
.nr
; i
++)
640 result
|= fetch_remote(list
.items
[i
].string
);
642 /* all names were strdup()ed or strndup()ed */
643 list
.strdup_strings
= 1;
644 string_list_clear(&list
, 0);
649 static int get_one_entry(struct remote
*remote
, void *priv
)
651 struct string_list
*list
= priv
;
653 string_list_append(remote
->name
, list
)->util
= remote
->url_nr
?
654 (void *)remote
->url
[0] : NULL
;
655 if (remote
->url_nr
> 1)
656 warning("Remote %s has more than one URL", remote
->name
);
661 static int show_all(void)
663 struct string_list list
= { NULL
, 0, 0 };
664 int result
= for_each_remote(get_one_entry
, &list
);
669 sort_string_list(&list
);
670 for (i
= 0; i
< list
.nr
; i
++) {
671 struct string_list_item
*item
= list
.items
+ i
;
672 printf("%s%s%s\n", item
->string
,
674 verbose
&& item
->util
?
675 (const char *)item
->util
: "");
681 int cmd_remote(int argc
, const char **argv
, const char *prefix
)
683 struct option options
[] = {
684 OPT__VERBOSE(&verbose
),
689 argc
= parse_options(argc
, argv
, options
, builtin_remote_usage
,
690 PARSE_OPT_STOP_AT_NON_OPTION
);
694 else if (!strcmp(argv
[0], "add"))
695 result
= add(argc
, argv
);
696 else if (!strcmp(argv
[0], "rm"))
697 result
= rm(argc
, argv
);
698 else if (!strcmp(argv
[0], "show"))
699 result
= show(argc
, argv
);
700 else if (!strcmp(argv
[0], "prune"))
701 result
= prune(argc
, argv
);
702 else if (!strcmp(argv
[0], "update"))
703 result
= update(argc
, argv
);
705 error("Unknown subcommand: %s", argv
[0]);
706 usage_with_options(builtin_remote_usage
, options
);
709 return result
? 1 : 0;