2 #include "parse-options.h"
5 #include "string-list.h"
7 #include "run-command.h"
10 static const char * const builtin_remote_usage
[] = {
11 "git remote [-v | --verbose]",
12 "git remote add [-t <branch>] [-m <master>] [-f] [--mirror] <name> <url>",
13 "git remote rename <old> <new>",
14 "git remote rm <name>",
15 "git remote set-head <name> (-a | -d | <branch>)",
16 "git remote [-v | --verbose] show [-n] <name>",
17 "git remote prune [-n | --dry-run] <name>",
18 "git remote [-v | --verbose] update [-p | --prune] [group | remote]",
19 "git remote set-url <name> <newurl> [<oldurl>]",
20 "git remote set-url --add <name> <newurl>",
21 "git remote set-url --delete <name> <url>",
25 static const char * const builtin_remote_add_usage
[] = {
26 "git remote add [<options>] <name> <url>",
30 static const char * const builtin_remote_rename_usage
[] = {
31 "git remote rename <old> <new>",
35 static const char * const builtin_remote_rm_usage
[] = {
36 "git remote rm <name>",
40 static const char * const builtin_remote_sethead_usage
[] = {
41 "git remote set-head <name> (-a | -d | <branch>])",
45 static const char * const builtin_remote_show_usage
[] = {
46 "git remote show [<options>] <name>",
50 static const char * const builtin_remote_prune_usage
[] = {
51 "git remote prune [<options>] <name>",
55 static const char * const builtin_remote_update_usage
[] = {
56 "git remote update [<options>] [<group> | <remote>]...",
60 static const char * const builtin_remote_seturl_usage
[] = {
61 "git remote set-url [--push] <name> <newurl> [<oldurl>]",
62 "git remote set-url --add <name> <newurl>",
63 "git remote set-url --delete <name> <url>",
67 #define GET_REF_STATES (1<<0)
68 #define GET_HEAD_NAMES (1<<1)
69 #define GET_PUSH_REF_STATES (1<<2)
73 static int show_all(void);
74 static int prune_remote(const char *remote
, int dry_run
);
76 static inline int postfixcmp(const char *string
, const char *postfix
)
78 int len1
= strlen(string
), len2
= strlen(postfix
);
81 return strcmp(string
+ len1
- len2
, postfix
);
84 static int opt_parse_track(const struct option
*opt
, const char *arg
, int not)
86 struct string_list
*list
= opt
->value
;
88 string_list_clear(list
, 0);
90 string_list_append(arg
, list
);
94 static int fetch_remote(const char *name
)
96 const char *argv
[] = { "fetch", name
, NULL
, NULL
};
101 printf("Updating %s\n", name
);
102 if (run_command_v_opt(argv
, RUN_GIT_CMD
))
103 return error("Could not fetch %s", name
);
107 static int add(int argc
, const char **argv
)
109 int fetch
= 0, mirror
= 0;
110 struct string_list track
= { NULL
, 0, 0 };
111 const char *master
= NULL
;
112 struct remote
*remote
;
113 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
;
114 const char *name
, *url
;
117 struct option options
[] = {
118 OPT_BOOLEAN('f', "fetch", &fetch
, "fetch the remote branches"),
119 OPT_CALLBACK('t', "track", &track
, "branch",
120 "branch(es) to track", opt_parse_track
),
121 OPT_STRING('m', "master", &master
, "branch", "master branch"),
122 OPT_BOOLEAN(0, "mirror", &mirror
, "no separate remotes"),
126 argc
= parse_options(argc
, argv
, NULL
, options
, builtin_remote_add_usage
,
130 usage_with_options(builtin_remote_add_usage
, options
);
135 remote
= remote_get(name
);
136 if (remote
&& (remote
->url_nr
> 1 || strcmp(name
, remote
->url
[0]) ||
137 remote
->fetch_refspec_nr
))
138 die("remote %s already exists.", name
);
140 strbuf_addf(&buf2
, "refs/heads/test:refs/remotes/%s/test", name
);
141 if (!valid_fetch_refspec(buf2
.buf
))
142 die("'%s' is not a valid remote name", name
);
144 strbuf_addf(&buf
, "remote.%s.url", name
);
145 if (git_config_set(buf
.buf
, url
))
149 strbuf_addf(&buf
, "remote.%s.fetch", name
);
152 string_list_append("*", &track
);
153 for (i
= 0; i
< track
.nr
; i
++) {
154 struct string_list_item
*item
= track
.items
+ i
;
157 strbuf_addch(&buf2
, '+');
159 strbuf_addf(&buf2
, "refs/%s:refs/%s",
160 item
->string
, item
->string
);
162 strbuf_addf(&buf2
, "refs/heads/%s:refs/remotes/%s/%s",
163 item
->string
, name
, item
->string
);
164 if (git_config_set_multivar(buf
.buf
, buf2
.buf
, "^$", 0))
170 strbuf_addf(&buf
, "remote.%s.mirror", name
);
171 if (git_config_set(buf
.buf
, "true"))
175 if (fetch
&& fetch_remote(name
))
180 strbuf_addf(&buf
, "refs/remotes/%s/HEAD", name
);
183 strbuf_addf(&buf2
, "refs/remotes/%s/%s", name
, master
);
185 if (create_symref(buf
.buf
, buf2
.buf
, "remote add"))
186 return error("Could not setup master '%s'", master
);
189 strbuf_release(&buf
);
190 strbuf_release(&buf2
);
191 string_list_clear(&track
, 0);
198 struct string_list merge
;
202 static struct string_list branch_list
;
204 static const char *abbrev_ref(const char *name
, const char *prefix
)
206 const char *abbrev
= skip_prefix(name
, prefix
);
211 #define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
213 static int config_read_branches(const char *key
, const char *value
, void *cb
)
215 if (!prefixcmp(key
, "branch.")) {
216 const char *orig_key
= key
;
218 struct string_list_item
*item
;
219 struct branch_info
*info
;
220 enum { REMOTE
, MERGE
, REBASE
} type
;
223 if (!postfixcmp(key
, ".remote")) {
224 name
= xstrndup(key
, strlen(key
) - 7);
226 } else if (!postfixcmp(key
, ".merge")) {
227 name
= xstrndup(key
, strlen(key
) - 6);
229 } else if (!postfixcmp(key
, ".rebase")) {
230 name
= xstrndup(key
, strlen(key
) - 7);
235 item
= string_list_insert(name
, &branch_list
);
238 item
->util
= xcalloc(sizeof(struct branch_info
), 1);
240 if (type
== REMOTE
) {
241 if (info
->remote_name
)
242 warning("more than one %s", orig_key
);
243 info
->remote_name
= xstrdup(value
);
244 } else if (type
== MERGE
) {
245 char *space
= strchr(value
, ' ');
246 value
= abbrev_branch(value
);
249 merge
= xstrndup(value
, space
- value
);
250 string_list_append(merge
, &info
->merge
);
251 value
= abbrev_branch(space
+ 1);
252 space
= strchr(value
, ' ');
254 string_list_append(xstrdup(value
), &info
->merge
);
256 info
->rebase
= git_config_bool(orig_key
, value
);
261 static void read_branches(void)
265 git_config(config_read_branches
, NULL
);
269 struct remote
*remote
;
270 struct string_list
new, stale
, tracked
, heads
, push
;
274 static int get_ref_states(const struct ref
*remote_refs
, struct ref_states
*states
)
276 struct ref
*fetch_map
= NULL
, **tail
= &fetch_map
;
277 struct ref
*ref
, *stale_refs
;
280 for (i
= 0; i
< states
->remote
->fetch_refspec_nr
; i
++)
281 if (get_fetch_map(remote_refs
, states
->remote
->fetch
+ i
, &tail
, 1))
282 die("Could not get fetch map for refspec %s",
283 states
->remote
->fetch_refspec
[i
]);
285 states
->new.strdup_strings
= 1;
286 states
->tracked
.strdup_strings
= 1;
287 states
->stale
.strdup_strings
= 1;
288 for (ref
= fetch_map
; ref
; ref
= ref
->next
) {
289 unsigned char sha1
[20];
290 if (!ref
->peer_ref
|| read_ref(ref
->peer_ref
->name
, sha1
))
291 string_list_append(abbrev_branch(ref
->name
), &states
->new);
293 string_list_append(abbrev_branch(ref
->name
), &states
->tracked
);
295 stale_refs
= get_stale_heads(states
->remote
, fetch_map
);
296 for (ref
= stale_refs
; ref
; ref
= ref
->next
) {
297 struct string_list_item
*item
=
298 string_list_append(abbrev_branch(ref
->name
), &states
->stale
);
299 item
->util
= xstrdup(ref
->name
);
301 free_refs(stale_refs
);
302 free_refs(fetch_map
);
304 sort_string_list(&states
->new);
305 sort_string_list(&states
->tracked
);
306 sort_string_list(&states
->stale
);
315 PUSH_STATUS_CREATE
= 0,
317 PUSH_STATUS_UPTODATE
,
318 PUSH_STATUS_FASTFORWARD
,
319 PUSH_STATUS_OUTOFDATE
,
320 PUSH_STATUS_NOTQUERIED
,
324 static int get_push_ref_states(const struct ref
*remote_refs
,
325 struct ref_states
*states
)
327 struct remote
*remote
= states
->remote
;
328 struct ref
*ref
, *local_refs
, *push_map
;
332 local_refs
= get_local_heads();
333 push_map
= copy_ref_list(remote_refs
);
335 match_refs(local_refs
, &push_map
, remote
->push_refspec_nr
,
336 remote
->push_refspec
, MATCH_REFS_NONE
);
338 states
->push
.strdup_strings
= 1;
339 for (ref
= push_map
; ref
; ref
= ref
->next
) {
340 struct string_list_item
*item
;
341 struct push_info
*info
;
345 hashcpy(ref
->new_sha1
, ref
->peer_ref
->new_sha1
);
347 item
= string_list_append(abbrev_branch(ref
->peer_ref
->name
),
349 item
->util
= xcalloc(sizeof(struct push_info
), 1);
351 info
->forced
= ref
->force
;
352 info
->dest
= xstrdup(abbrev_branch(ref
->name
));
354 if (is_null_sha1(ref
->new_sha1
)) {
355 info
->status
= PUSH_STATUS_DELETE
;
356 } else if (!hashcmp(ref
->old_sha1
, ref
->new_sha1
))
357 info
->status
= PUSH_STATUS_UPTODATE
;
358 else if (is_null_sha1(ref
->old_sha1
))
359 info
->status
= PUSH_STATUS_CREATE
;
360 else if (has_sha1_file(ref
->old_sha1
) &&
361 ref_newer(ref
->new_sha1
, ref
->old_sha1
))
362 info
->status
= PUSH_STATUS_FASTFORWARD
;
364 info
->status
= PUSH_STATUS_OUTOFDATE
;
366 free_refs(local_refs
);
371 static int get_push_ref_states_noquery(struct ref_states
*states
)
374 struct remote
*remote
= states
->remote
;
375 struct string_list_item
*item
;
376 struct push_info
*info
;
381 states
->push
.strdup_strings
= 1;
382 if (!remote
->push_refspec_nr
) {
383 item
= string_list_append("(matching)", &states
->push
);
384 info
= item
->util
= xcalloc(sizeof(struct push_info
), 1);
385 info
->status
= PUSH_STATUS_NOTQUERIED
;
386 info
->dest
= xstrdup(item
->string
);
388 for (i
= 0; i
< remote
->push_refspec_nr
; i
++) {
389 struct refspec
*spec
= remote
->push
+ i
;
391 item
= string_list_append("(matching)", &states
->push
);
392 else if (strlen(spec
->src
))
393 item
= string_list_append(spec
->src
, &states
->push
);
395 item
= string_list_append("(delete)", &states
->push
);
397 info
= item
->util
= xcalloc(sizeof(struct push_info
), 1);
398 info
->forced
= spec
->force
;
399 info
->status
= PUSH_STATUS_NOTQUERIED
;
400 info
->dest
= xstrdup(spec
->dst
? spec
->dst
: item
->string
);
405 static int get_head_names(const struct ref
*remote_refs
, struct ref_states
*states
)
407 struct ref
*ref
, *matches
;
408 struct ref
*fetch_map
= NULL
, **fetch_map_tail
= &fetch_map
;
409 struct refspec refspec
;
413 refspec
.src
= refspec
.dst
= "refs/heads/*";
414 states
->heads
.strdup_strings
= 1;
415 get_fetch_map(remote_refs
, &refspec
, &fetch_map_tail
, 0);
416 matches
= guess_remote_head(find_ref_by_name(remote_refs
, "HEAD"),
418 for (ref
= matches
; ref
; ref
= ref
->next
)
419 string_list_append(abbrev_branch(ref
->name
), &states
->heads
);
421 free_refs(fetch_map
);
427 struct known_remote
{
428 struct known_remote
*next
;
429 struct remote
*remote
;
432 struct known_remotes
{
433 struct remote
*to_delete
;
434 struct known_remote
*list
;
437 static int add_known_remote(struct remote
*remote
, void *cb_data
)
439 struct known_remotes
*all
= cb_data
;
440 struct known_remote
*r
;
442 if (!strcmp(all
->to_delete
->name
, remote
->name
))
445 r
= xmalloc(sizeof(*r
));
452 struct branches_for_remote
{
453 struct remote
*remote
;
454 struct string_list
*branches
, *skipped
;
455 struct known_remotes
*keep
;
458 static int add_branch_for_removal(const char *refname
,
459 const unsigned char *sha1
, int flags
, void *cb_data
)
461 struct branches_for_remote
*branches
= cb_data
;
462 struct refspec refspec
;
463 struct string_list_item
*item
;
464 struct known_remote
*kr
;
466 memset(&refspec
, 0, sizeof(refspec
));
467 refspec
.dst
= (char *)refname
;
468 if (remote_find_tracking(branches
->remote
, &refspec
))
471 /* don't delete a branch if another remote also uses it */
472 for (kr
= branches
->keep
->list
; kr
; kr
= kr
->next
) {
473 memset(&refspec
, 0, sizeof(refspec
));
474 refspec
.dst
= (char *)refname
;
475 if (!remote_find_tracking(kr
->remote
, &refspec
))
479 /* don't delete non-remote refs */
480 if (prefixcmp(refname
, "refs/remotes")) {
481 /* advise user how to delete local branches */
482 if (!prefixcmp(refname
, "refs/heads/"))
483 string_list_append(abbrev_branch(refname
),
485 /* silently skip over other non-remote refs */
489 /* make sure that symrefs are deleted */
490 if (flags
& REF_ISSYMREF
)
491 return unlink(git_path("%s", refname
));
493 item
= string_list_append(refname
, branches
->branches
);
494 item
->util
= xmalloc(20);
495 hashcpy(item
->util
, sha1
);
503 struct string_list
*remote_branches
;
506 static int read_remote_branches(const char *refname
,
507 const unsigned char *sha1
, int flags
, void *cb_data
)
509 struct rename_info
*rename
= cb_data
;
510 struct strbuf buf
= STRBUF_INIT
;
511 struct string_list_item
*item
;
513 unsigned char orig_sha1
[20];
516 strbuf_addf(&buf
, "refs/remotes/%s", rename
->old
);
517 if (!prefixcmp(refname
, buf
.buf
)) {
518 item
= string_list_append(xstrdup(refname
), rename
->remote_branches
);
519 symref
= resolve_ref(refname
, orig_sha1
, 1, &flag
);
520 if (flag
& REF_ISSYMREF
)
521 item
->util
= xstrdup(symref
);
529 static int migrate_file(struct remote
*remote
)
531 struct strbuf buf
= STRBUF_INIT
;
535 strbuf_addf(&buf
, "remote.%s.url", remote
->name
);
536 for (i
= 0; i
< remote
->url_nr
; i
++)
537 if (git_config_set_multivar(buf
.buf
, remote
->url
[i
], "^$", 0))
538 return error("Could not append '%s' to '%s'",
539 remote
->url
[i
], buf
.buf
);
541 strbuf_addf(&buf
, "remote.%s.push", remote
->name
);
542 for (i
= 0; i
< remote
->push_refspec_nr
; i
++)
543 if (git_config_set_multivar(buf
.buf
, remote
->push_refspec
[i
], "^$", 0))
544 return error("Could not append '%s' to '%s'",
545 remote
->push_refspec
[i
], buf
.buf
);
547 strbuf_addf(&buf
, "remote.%s.fetch", remote
->name
);
548 for (i
= 0; i
< remote
->fetch_refspec_nr
; i
++)
549 if (git_config_set_multivar(buf
.buf
, remote
->fetch_refspec
[i
], "^$", 0))
550 return error("Could not append '%s' to '%s'",
551 remote
->fetch_refspec
[i
], buf
.buf
);
552 if (remote
->origin
== REMOTE_REMOTES
)
553 path
= git_path("remotes/%s", remote
->name
);
554 else if (remote
->origin
== REMOTE_BRANCHES
)
555 path
= git_path("branches/%s", remote
->name
);
557 unlink_or_warn(path
);
561 static int mv(int argc
, const char **argv
)
563 struct option options
[] = {
566 struct remote
*oldremote
, *newremote
;
567 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
, buf3
= STRBUF_INIT
;
568 struct string_list remote_branches
= { NULL
, 0, 0, 0 };
569 struct rename_info rename
;
573 usage_with_options(builtin_remote_rename_usage
, options
);
575 rename
.old
= argv
[1];
576 rename
.new = argv
[2];
577 rename
.remote_branches
= &remote_branches
;
579 oldremote
= remote_get(rename
.old
);
581 die("No such remote: %s", rename
.old
);
583 if (!strcmp(rename
.old
, rename
.new) && oldremote
->origin
!= REMOTE_CONFIG
)
584 return migrate_file(oldremote
);
586 newremote
= remote_get(rename
.new);
587 if (newremote
&& (newremote
->url_nr
> 1 || newremote
->fetch_refspec_nr
))
588 die("remote %s already exists.", rename
.new);
590 strbuf_addf(&buf
, "refs/heads/test:refs/remotes/%s/test", rename
.new);
591 if (!valid_fetch_refspec(buf
.buf
))
592 die("'%s' is not a valid remote name", rename
.new);
595 strbuf_addf(&buf
, "remote.%s", rename
.old
);
596 strbuf_addf(&buf2
, "remote.%s", rename
.new);
597 if (git_config_rename_section(buf
.buf
, buf2
.buf
) < 1)
598 return error("Could not rename config section '%s' to '%s'",
602 strbuf_addf(&buf
, "remote.%s.fetch", rename
.new);
603 if (git_config_set_multivar(buf
.buf
, NULL
, NULL
, 1))
604 return error("Could not remove config section '%s'", buf
.buf
);
605 for (i
= 0; i
< oldremote
->fetch_refspec_nr
; i
++) {
609 strbuf_addstr(&buf2
, oldremote
->fetch_refspec
[i
]);
610 ptr
= strstr(buf2
.buf
, rename
.old
);
612 strbuf_splice(&buf2
, ptr
-buf2
.buf
, strlen(rename
.old
),
613 rename
.new, strlen(rename
.new));
614 if (git_config_set_multivar(buf
.buf
, buf2
.buf
, "^$", 0))
615 return error("Could not append '%s'", buf
.buf
);
619 for (i
= 0; i
< branch_list
.nr
; i
++) {
620 struct string_list_item
*item
= branch_list
.items
+ i
;
621 struct branch_info
*info
= item
->util
;
622 if (info
->remote_name
&& !strcmp(info
->remote_name
, rename
.old
)) {
624 strbuf_addf(&buf
, "branch.%s.remote", item
->string
);
625 if (git_config_set(buf
.buf
, rename
.new)) {
626 return error("Could not set '%s'", buf
.buf
);
632 * First remove symrefs, then rename the rest, finally create
635 for_each_ref(read_remote_branches
, &rename
);
636 for (i
= 0; i
< remote_branches
.nr
; i
++) {
637 struct string_list_item
*item
= remote_branches
.items
+ i
;
639 unsigned char sha1
[20];
641 resolve_ref(item
->string
, sha1
, 1, &flag
);
642 if (!(flag
& REF_ISSYMREF
))
644 if (delete_ref(item
->string
, NULL
, REF_NODEREF
))
645 die("deleting '%s' failed", item
->string
);
647 for (i
= 0; i
< remote_branches
.nr
; i
++) {
648 struct string_list_item
*item
= remote_branches
.items
+ i
;
653 strbuf_addstr(&buf
, item
->string
);
654 strbuf_splice(&buf
, strlen("refs/remotes/"), strlen(rename
.old
),
655 rename
.new, strlen(rename
.new));
657 strbuf_addf(&buf2
, "remote: renamed %s to %s",
658 item
->string
, buf
.buf
);
659 if (rename_ref(item
->string
, buf
.buf
, buf2
.buf
))
660 die("renaming '%s' failed", item
->string
);
662 for (i
= 0; i
< remote_branches
.nr
; i
++) {
663 struct string_list_item
*item
= remote_branches
.items
+ i
;
668 strbuf_addstr(&buf
, item
->string
);
669 strbuf_splice(&buf
, strlen("refs/remotes/"), strlen(rename
.old
),
670 rename
.new, strlen(rename
.new));
672 strbuf_addstr(&buf2
, item
->util
);
673 strbuf_splice(&buf2
, strlen("refs/remotes/"), strlen(rename
.old
),
674 rename
.new, strlen(rename
.new));
676 strbuf_addf(&buf3
, "remote: renamed %s to %s",
677 item
->string
, buf
.buf
);
678 if (create_symref(buf
.buf
, buf2
.buf
, buf3
.buf
))
679 die("creating '%s' failed", buf
.buf
);
684 static int remove_branches(struct string_list
*branches
)
687 for (i
= 0; i
< branches
->nr
; i
++) {
688 struct string_list_item
*item
= branches
->items
+ i
;
689 const char *refname
= item
->string
;
690 unsigned char *sha1
= item
->util
;
692 if (delete_ref(refname
, sha1
, 0))
693 result
|= error("Could not remove branch %s", refname
);
698 static int rm(int argc
, const char **argv
)
700 struct option options
[] = {
703 struct remote
*remote
;
704 struct strbuf buf
= STRBUF_INIT
;
705 struct known_remotes known_remotes
= { NULL
, NULL
};
706 struct string_list branches
= { NULL
, 0, 0, 1 };
707 struct string_list skipped
= { NULL
, 0, 0, 1 };
708 struct branches_for_remote cb_data
;
711 memset(&cb_data
, 0, sizeof(cb_data
));
712 cb_data
.branches
= &branches
;
713 cb_data
.skipped
= &skipped
;
714 cb_data
.keep
= &known_remotes
;
717 usage_with_options(builtin_remote_rm_usage
, options
);
719 remote
= remote_get(argv
[1]);
721 die("No such remote: %s", argv
[1]);
723 known_remotes
.to_delete
= remote
;
724 for_each_remote(add_known_remote
, &known_remotes
);
726 strbuf_addf(&buf
, "remote.%s", remote
->name
);
727 if (git_config_rename_section(buf
.buf
, NULL
) < 1)
728 return error("Could not remove config section '%s'", buf
.buf
);
731 for (i
= 0; i
< branch_list
.nr
; i
++) {
732 struct string_list_item
*item
= branch_list
.items
+ i
;
733 struct branch_info
*info
= item
->util
;
734 if (info
->remote_name
&& !strcmp(info
->remote_name
, remote
->name
)) {
735 const char *keys
[] = { "remote", "merge", NULL
}, **k
;
736 for (k
= keys
; *k
; k
++) {
738 strbuf_addf(&buf
, "branch.%s.%s",
740 if (git_config_set(buf
.buf
, NULL
)) {
741 strbuf_release(&buf
);
749 * We cannot just pass a function to for_each_ref() which deletes
750 * the branches one by one, since for_each_ref() relies on cached
751 * refs, which are invalidated when deleting a branch.
753 cb_data
.remote
= remote
;
754 result
= for_each_ref(add_branch_for_removal
, &cb_data
);
755 strbuf_release(&buf
);
758 result
= remove_branches(&branches
);
759 string_list_clear(&branches
, 1);
762 fprintf(stderr
, skipped
.nr
== 1 ?
763 "Note: A non-remote branch was not removed; "
764 "to delete it, use:\n" :
765 "Note: Non-remote branches were not removed; "
766 "to delete them, use:\n");
767 for (i
= 0; i
< skipped
.nr
; i
++)
768 fprintf(stderr
, " git branch -d %s\n",
769 skipped
.items
[i
].string
);
771 string_list_clear(&skipped
, 0);
776 static void clear_push_info(void *util
, const char *string
)
778 struct push_info
*info
= util
;
783 static void free_remote_ref_states(struct ref_states
*states
)
785 string_list_clear(&states
->new, 0);
786 string_list_clear(&states
->stale
, 1);
787 string_list_clear(&states
->tracked
, 0);
788 string_list_clear(&states
->heads
, 0);
789 string_list_clear_func(&states
->push
, clear_push_info
);
792 static int append_ref_to_tracked_list(const char *refname
,
793 const unsigned char *sha1
, int flags
, void *cb_data
)
795 struct ref_states
*states
= cb_data
;
796 struct refspec refspec
;
798 if (flags
& REF_ISSYMREF
)
801 memset(&refspec
, 0, sizeof(refspec
));
802 refspec
.dst
= (char *)refname
;
803 if (!remote_find_tracking(states
->remote
, &refspec
))
804 string_list_append(abbrev_branch(refspec
.src
), &states
->tracked
);
809 static int get_remote_ref_states(const char *name
,
810 struct ref_states
*states
,
813 struct transport
*transport
;
814 const struct ref
*remote_refs
;
816 states
->remote
= remote_get(name
);
818 return error("No such remote: %s", name
);
823 transport
= transport_get(states
->remote
, states
->remote
->url_nr
> 0 ?
824 states
->remote
->url
[0] : NULL
);
825 remote_refs
= transport_get_remote_refs(transport
);
826 transport_disconnect(transport
);
829 if (query
& GET_REF_STATES
)
830 get_ref_states(remote_refs
, states
);
831 if (query
& GET_HEAD_NAMES
)
832 get_head_names(remote_refs
, states
);
833 if (query
& GET_PUSH_REF_STATES
)
834 get_push_ref_states(remote_refs
, states
);
836 for_each_ref(append_ref_to_tracked_list
, states
);
837 sort_string_list(&states
->tracked
);
838 get_push_ref_states_noquery(states
);
845 struct string_list
*list
;
846 struct ref_states
*states
;
851 static int add_remote_to_show_info(struct string_list_item
*item
, void *cb_data
)
853 struct show_info
*info
= cb_data
;
854 int n
= strlen(item
->string
);
857 string_list_insert(item
->string
, info
->list
);
861 static int show_remote_info_item(struct string_list_item
*item
, void *cb_data
)
863 struct show_info
*info
= cb_data
;
864 struct ref_states
*states
= info
->states
;
865 const char *name
= item
->string
;
867 if (states
->queried
) {
868 const char *fmt
= "%s";
869 const char *arg
= "";
870 if (string_list_has_string(&states
->new, name
)) {
871 fmt
= " new (next fetch will store in remotes/%s)";
872 arg
= states
->remote
->name
;
873 } else if (string_list_has_string(&states
->tracked
, name
))
875 else if (string_list_has_string(&states
->stale
, name
))
876 arg
= " stale (use 'git remote prune' to remove)";
879 printf(" %-*s", info
->width
, name
);
883 printf(" %s\n", name
);
888 static int add_local_to_show_info(struct string_list_item
*branch_item
, void *cb_data
)
890 struct show_info
*show_info
= cb_data
;
891 struct ref_states
*states
= show_info
->states
;
892 struct branch_info
*branch_info
= branch_item
->util
;
893 struct string_list_item
*item
;
896 if (!branch_info
->merge
.nr
|| !branch_info
->remote_name
||
897 strcmp(states
->remote
->name
, branch_info
->remote_name
))
899 if ((n
= strlen(branch_item
->string
)) > show_info
->width
)
900 show_info
->width
= n
;
901 if (branch_info
->rebase
)
902 show_info
->any_rebase
= 1;
904 item
= string_list_insert(branch_item
->string
, show_info
->list
);
905 item
->util
= branch_info
;
910 static int show_local_info_item(struct string_list_item
*item
, void *cb_data
)
912 struct show_info
*show_info
= cb_data
;
913 struct branch_info
*branch_info
= item
->util
;
914 struct string_list
*merge
= &branch_info
->merge
;
918 if (branch_info
->rebase
&& branch_info
->merge
.nr
> 1) {
919 error("invalid branch.%s.merge; cannot rebase onto > 1 branch",
924 printf(" %-*s ", show_info
->width
, item
->string
);
925 if (branch_info
->rebase
) {
926 printf("rebases onto remote %s\n", merge
->items
[0].string
);
928 } else if (show_info
->any_rebase
) {
929 printf(" merges with remote %s\n", merge
->items
[0].string
);
930 also
= " and with remote";
932 printf("merges with remote %s\n", merge
->items
[0].string
);
933 also
= " and with remote";
935 for (i
= 1; i
< merge
->nr
; i
++)
936 printf(" %-*s %s %s\n", show_info
->width
, "", also
,
937 merge
->items
[i
].string
);
942 static int add_push_to_show_info(struct string_list_item
*push_item
, void *cb_data
)
944 struct show_info
*show_info
= cb_data
;
945 struct push_info
*push_info
= push_item
->util
;
946 struct string_list_item
*item
;
948 if ((n
= strlen(push_item
->string
)) > show_info
->width
)
949 show_info
->width
= n
;
950 if ((n
= strlen(push_info
->dest
)) > show_info
->width2
)
951 show_info
->width2
= n
;
952 item
= string_list_append(push_item
->string
, show_info
->list
);
953 item
->util
= push_item
->util
;
958 * Sorting comparison for a string list that has push_info
959 * structs in its util field
961 static int cmp_string_with_push(const void *va
, const void *vb
)
963 const struct string_list_item
*a
= va
;
964 const struct string_list_item
*b
= vb
;
965 const struct push_info
*a_push
= a
->util
;
966 const struct push_info
*b_push
= b
->util
;
967 int cmp
= strcmp(a
->string
, b
->string
);
968 return cmp
? cmp
: strcmp(a_push
->dest
, b_push
->dest
);
971 static int show_push_info_item(struct string_list_item
*item
, void *cb_data
)
973 struct show_info
*show_info
= cb_data
;
974 struct push_info
*push_info
= item
->util
;
975 char *src
= item
->string
, *status
= NULL
;
977 switch (push_info
->status
) {
978 case PUSH_STATUS_CREATE
:
981 case PUSH_STATUS_DELETE
:
985 case PUSH_STATUS_UPTODATE
:
986 status
= "up to date";
988 case PUSH_STATUS_FASTFORWARD
:
989 status
= "fast-forwardable";
991 case PUSH_STATUS_OUTOFDATE
:
992 status
= "local out of date";
994 case PUSH_STATUS_NOTQUERIED
:
998 printf(" %-*s %s to %-*s (%s)\n", show_info
->width
, src
,
999 push_info
->forced
? "forces" : "pushes",
1000 show_info
->width2
, push_info
->dest
, status
);
1002 printf(" %-*s %s to %s\n", show_info
->width
, src
,
1003 push_info
->forced
? "forces" : "pushes",
1008 static int show(int argc
, const char **argv
)
1010 int no_query
= 0, result
= 0, query_flag
= 0;
1011 struct option options
[] = {
1012 OPT_BOOLEAN('n', NULL
, &no_query
, "do not query remotes"),
1015 struct ref_states states
;
1016 struct string_list info_list
= { NULL
, 0, 0, 0 };
1017 struct show_info info
;
1019 argc
= parse_options(argc
, argv
, NULL
, options
, builtin_remote_show_usage
,
1026 query_flag
= (GET_REF_STATES
| GET_HEAD_NAMES
| GET_PUSH_REF_STATES
);
1028 memset(&states
, 0, sizeof(states
));
1029 memset(&info
, 0, sizeof(info
));
1030 info
.states
= &states
;
1031 info
.list
= &info_list
;
1032 for (; argc
; argc
--, argv
++) {
1037 get_remote_ref_states(*argv
, &states
, query_flag
);
1039 printf("* remote %s\n", *argv
);
1040 printf(" Fetch URL: %s\n", states
.remote
->url_nr
> 0 ?
1041 states
.remote
->url
[0] : "(no URL)");
1042 if (states
.remote
->pushurl_nr
) {
1043 url
= states
.remote
->pushurl
;
1044 url_nr
= states
.remote
->pushurl_nr
;
1046 url
= states
.remote
->url
;
1047 url_nr
= states
.remote
->url_nr
;
1049 for (i
=0; i
< url_nr
; i
++)
1050 printf(" Push URL: %s\n", url
[i
]);
1052 printf(" Push URL: %s\n", "(no URL)");
1054 printf(" HEAD branch: (not queried)\n");
1055 else if (!states
.heads
.nr
)
1056 printf(" HEAD branch: (unknown)\n");
1057 else if (states
.heads
.nr
== 1)
1058 printf(" HEAD branch: %s\n", states
.heads
.items
[0].string
);
1060 printf(" HEAD branch (remote HEAD is ambiguous,"
1061 " may be one of the following):\n");
1062 for (i
= 0; i
< states
.heads
.nr
; i
++)
1063 printf(" %s\n", states
.heads
.items
[i
].string
);
1066 /* remote branch info */
1068 for_each_string_list(add_remote_to_show_info
, &states
.new, &info
);
1069 for_each_string_list(add_remote_to_show_info
, &states
.tracked
, &info
);
1070 for_each_string_list(add_remote_to_show_info
, &states
.stale
, &info
);
1072 printf(" Remote branch%s:%s\n",
1073 info
.list
->nr
> 1 ? "es" : "",
1074 no_query
? " (status not queried)" : "");
1075 for_each_string_list(show_remote_info_item
, info
.list
, &info
);
1076 string_list_clear(info
.list
, 0);
1080 info
.any_rebase
= 0;
1081 for_each_string_list(add_local_to_show_info
, &branch_list
, &info
);
1083 printf(" Local branch%s configured for 'git pull':\n",
1084 info
.list
->nr
> 1 ? "es" : "");
1085 for_each_string_list(show_local_info_item
, info
.list
, &info
);
1086 string_list_clear(info
.list
, 0);
1089 if (states
.remote
->mirror
)
1090 printf(" Local refs will be mirrored by 'git push'\n");
1092 info
.width
= info
.width2
= 0;
1093 for_each_string_list(add_push_to_show_info
, &states
.push
, &info
);
1094 qsort(info
.list
->items
, info
.list
->nr
,
1095 sizeof(*info
.list
->items
), cmp_string_with_push
);
1097 printf(" Local ref%s configured for 'git push'%s:\n",
1098 info
.list
->nr
> 1 ? "s" : "",
1099 no_query
? " (status not queried)" : "");
1100 for_each_string_list(show_push_info_item
, info
.list
, &info
);
1101 string_list_clear(info
.list
, 0);
1103 free_remote_ref_states(&states
);
1109 static int set_head(int argc
, const char **argv
)
1111 int i
, opt_a
= 0, opt_d
= 0, result
= 0;
1112 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
;
1113 char *head_name
= NULL
;
1115 struct option options
[] = {
1116 OPT_BOOLEAN('a', "auto", &opt_a
,
1117 "set refs/remotes/<name>/HEAD according to remote"),
1118 OPT_BOOLEAN('d', "delete", &opt_d
,
1119 "delete refs/remotes/<name>/HEAD"),
1122 argc
= parse_options(argc
, argv
, NULL
, options
, builtin_remote_sethead_usage
,
1125 strbuf_addf(&buf
, "refs/remotes/%s/HEAD", argv
[0]);
1127 if (!opt_a
&& !opt_d
&& argc
== 2) {
1128 head_name
= xstrdup(argv
[1]);
1129 } else if (opt_a
&& !opt_d
&& argc
== 1) {
1130 struct ref_states states
;
1131 memset(&states
, 0, sizeof(states
));
1132 get_remote_ref_states(argv
[0], &states
, GET_HEAD_NAMES
);
1133 if (!states
.heads
.nr
)
1134 result
|= error("Cannot determine remote HEAD");
1135 else if (states
.heads
.nr
> 1) {
1136 result
|= error("Multiple remote HEAD branches. "
1137 "Please choose one explicitly with:");
1138 for (i
= 0; i
< states
.heads
.nr
; i
++)
1139 fprintf(stderr
, " git remote set-head %s %s\n",
1140 argv
[0], states
.heads
.items
[i
].string
);
1142 head_name
= xstrdup(states
.heads
.items
[0].string
);
1143 free_remote_ref_states(&states
);
1144 } else if (opt_d
&& !opt_a
&& argc
== 1) {
1145 if (delete_ref(buf
.buf
, NULL
, REF_NODEREF
))
1146 result
|= error("Could not delete %s", buf
.buf
);
1148 usage_with_options(builtin_remote_sethead_usage
, options
);
1151 unsigned char sha1
[20];
1152 strbuf_addf(&buf2
, "refs/remotes/%s/%s", argv
[0], head_name
);
1153 /* make sure it's valid */
1154 if (!resolve_ref(buf2
.buf
, sha1
, 1, NULL
))
1155 result
|= error("Not a valid ref: %s", buf2
.buf
);
1156 else if (create_symref(buf
.buf
, buf2
.buf
, "remote set-head"))
1157 result
|= error("Could not setup %s", buf
.buf
);
1159 printf("%s/HEAD set to %s\n", argv
[0], head_name
);
1163 strbuf_release(&buf
);
1164 strbuf_release(&buf2
);
1168 static int prune(int argc
, const char **argv
)
1170 int dry_run
= 0, result
= 0;
1171 struct option options
[] = {
1172 OPT__DRY_RUN(&dry_run
),
1176 argc
= parse_options(argc
, argv
, NULL
, options
, builtin_remote_prune_usage
,
1180 usage_with_options(builtin_remote_prune_usage
, options
);
1182 for (; argc
; argc
--, argv
++)
1183 result
|= prune_remote(*argv
, dry_run
);
1188 static int prune_remote(const char *remote
, int dry_run
)
1191 struct ref_states states
;
1192 const char *dangling_msg
= dry_run
1193 ? " %s will become dangling!\n"
1194 : " %s has become dangling!\n";
1196 memset(&states
, 0, sizeof(states
));
1197 get_remote_ref_states(remote
, &states
, GET_REF_STATES
);
1199 if (states
.stale
.nr
) {
1200 printf("Pruning %s\n", remote
);
1202 states
.remote
->url_nr
1203 ? states
.remote
->url
[0]
1207 for (i
= 0; i
< states
.stale
.nr
; i
++) {
1208 const char *refname
= states
.stale
.items
[i
].util
;
1211 result
|= delete_ref(refname
, NULL
, 0);
1213 printf(" * [%s] %s\n", dry_run
? "would prune" : "pruned",
1214 abbrev_ref(refname
, "refs/remotes/"));
1215 warn_dangling_symref(stdout
, dangling_msg
, refname
);
1218 free_remote_ref_states(&states
);
1222 static int get_remote_default(const char *key
, const char *value
, void *priv
)
1224 if (strcmp(key
, "remotes.default") == 0) {
1231 static int update(int argc
, const char **argv
)
1234 struct option options
[] = {
1235 OPT_BOOLEAN('p', "prune", &prune
,
1236 "prune remotes after fetching"),
1239 const char **fetch_argv
;
1241 int default_defined
= 0;
1243 fetch_argv
= xmalloc(sizeof(char *) * (argc
+5));
1245 argc
= parse_options(argc
, argv
, NULL
, options
, builtin_remote_update_usage
,
1246 PARSE_OPT_KEEP_ARGV0
);
1248 fetch_argv
[fetch_argc
++] = "fetch";
1251 fetch_argv
[fetch_argc
++] = "--prune";
1253 fetch_argv
[fetch_argc
++] = "-v";
1254 fetch_argv
[fetch_argc
++] = "--multiple";
1256 fetch_argv
[fetch_argc
++] = "default";
1257 for (i
= 1; i
< argc
; i
++)
1258 fetch_argv
[fetch_argc
++] = argv
[i
];
1260 if (strcmp(fetch_argv
[fetch_argc
-1], "default") == 0) {
1261 git_config(get_remote_default
, &default_defined
);
1262 if (!default_defined
)
1263 fetch_argv
[fetch_argc
-1] = "--all";
1266 fetch_argv
[fetch_argc
] = NULL
;
1268 return run_command_v_opt(fetch_argv
, RUN_GIT_CMD
);
1271 static int set_url(int argc
, const char **argv
)
1273 int i
, push_mode
= 0, add_mode
= 0, delete_mode
= 0;
1274 int matches
= 0, negative_matches
= 0;
1275 const char *remotename
= NULL
;
1276 const char *newurl
= NULL
;
1277 const char *oldurl
= NULL
;
1278 struct remote
*remote
;
1280 const char **urlset
;
1282 struct strbuf name_buf
= STRBUF_INIT
;
1283 struct option options
[] = {
1284 OPT_BOOLEAN('\0', "push", &push_mode
,
1285 "manipulate push URLs"),
1286 OPT_BOOLEAN('\0', "add", &add_mode
,
1288 OPT_BOOLEAN('\0', "delete", &delete_mode
,
1292 argc
= parse_options(argc
, argv
, NULL
, options
, builtin_remote_update_usage
,
1293 PARSE_OPT_KEEP_ARGV0
);
1295 if (add_mode
&& delete_mode
)
1296 die("--add --delete doesn't make sense");
1298 if (argc
< 3 || argc
> 4 || ((add_mode
|| delete_mode
) && argc
!= 3))
1299 usage_with_options(builtin_remote_seturl_usage
, options
);
1301 remotename
= argv
[1];
1309 if (!remote_is_configured(remotename
))
1310 die("No such remote '%s'", remotename
);
1311 remote
= remote_get(remotename
);
1314 strbuf_addf(&name_buf
, "remote.%s.pushurl", remotename
);
1315 urlset
= remote
->pushurl
;
1316 urlset_nr
= remote
->pushurl_nr
;
1318 strbuf_addf(&name_buf
, "remote.%s.url", remotename
);
1319 urlset
= remote
->url
;
1320 urlset_nr
= remote
->url_nr
;
1323 /* Special cases that add new entry. */
1324 if ((!oldurl
&& !delete_mode
) || add_mode
) {
1326 git_config_set_multivar(name_buf
.buf
, newurl
,
1329 git_config_set(name_buf
.buf
, newurl
);
1330 strbuf_release(&name_buf
);
1334 /* Old URL specified. Demand that one matches. */
1335 if (regcomp(&old_regex
, oldurl
, REG_EXTENDED
))
1336 die("Invalid old URL pattern: %s", oldurl
);
1338 for (i
= 0; i
< urlset_nr
; i
++)
1339 if (!regexec(&old_regex
, urlset
[i
], 0, NULL
, 0))
1343 if (!delete_mode
&& !matches
)
1344 die("No such URL found: %s", oldurl
);
1345 if (delete_mode
&& !negative_matches
&& !push_mode
)
1346 die("Will not delete all non-push URLs");
1348 regfree(&old_regex
);
1351 git_config_set_multivar(name_buf
.buf
, newurl
, oldurl
, 0);
1353 git_config_set_multivar(name_buf
.buf
, NULL
, oldurl
, 1);
1357 static int get_one_entry(struct remote
*remote
, void *priv
)
1359 struct string_list
*list
= priv
;
1360 struct strbuf url_buf
= STRBUF_INIT
;
1364 if (remote
->url_nr
> 0) {
1365 strbuf_addf(&url_buf
, "%s (fetch)", remote
->url
[0]);
1366 string_list_append(remote
->name
, list
)->util
=
1367 strbuf_detach(&url_buf
, NULL
);
1369 string_list_append(remote
->name
, list
)->util
= NULL
;
1370 if (remote
->pushurl_nr
) {
1371 url
= remote
->pushurl
;
1372 url_nr
= remote
->pushurl_nr
;
1375 url_nr
= remote
->url_nr
;
1377 for (i
= 0; i
< url_nr
; i
++)
1379 strbuf_addf(&url_buf
, "%s (push)", url
[i
]);
1380 string_list_append(remote
->name
, list
)->util
=
1381 strbuf_detach(&url_buf
, NULL
);
1387 static int show_all(void)
1389 struct string_list list
= { NULL
, 0, 0 };
1392 list
.strdup_strings
= 1;
1393 result
= for_each_remote(get_one_entry
, &list
);
1398 sort_string_list(&list
);
1399 for (i
= 0; i
< list
.nr
; i
++) {
1400 struct string_list_item
*item
= list
.items
+ i
;
1402 printf("%s\t%s\n", item
->string
,
1403 item
->util
? (const char *)item
->util
: "");
1405 if (i
&& !strcmp((item
- 1)->string
, item
->string
))
1407 printf("%s\n", item
->string
);
1411 string_list_clear(&list
, 1);
1415 int cmd_remote(int argc
, const char **argv
, const char *prefix
)
1417 struct option options
[] = {
1418 OPT_BOOLEAN('v', "verbose", &verbose
, "be verbose; must be placed before a subcommand"),
1423 argc
= parse_options(argc
, argv
, prefix
, options
, builtin_remote_usage
,
1424 PARSE_OPT_STOP_AT_NON_OPTION
);
1427 result
= show_all();
1428 else if (!strcmp(argv
[0], "add"))
1429 result
= add(argc
, argv
);
1430 else if (!strcmp(argv
[0], "rename"))
1431 result
= mv(argc
, argv
);
1432 else if (!strcmp(argv
[0], "rm"))
1433 result
= rm(argc
, argv
);
1434 else if (!strcmp(argv
[0], "set-head"))
1435 result
= set_head(argc
, argv
);
1436 else if (!strcmp(argv
[0], "set-url"))
1437 result
= set_url(argc
, argv
);
1438 else if (!strcmp(argv
[0], "show"))
1439 result
= show(argc
, argv
);
1440 else if (!strcmp(argv
[0], "prune"))
1441 result
= prune(argc
, argv
);
1442 else if (!strcmp(argv
[0], "update"))
1443 result
= update(argc
, argv
);
1445 error("Unknown subcommand: %s", argv
[0]);
1446 usage_with_options(builtin_remote_usage
, options
);
1449 return result
? 1 : 0;