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
= {
709 NULL
, &branches
, &skipped
, &known_remotes
714 usage_with_options(builtin_remote_rm_usage
, options
);
716 remote
= remote_get(argv
[1]);
718 die("No such remote: %s", argv
[1]);
720 known_remotes
.to_delete
= remote
;
721 for_each_remote(add_known_remote
, &known_remotes
);
723 strbuf_addf(&buf
, "remote.%s", remote
->name
);
724 if (git_config_rename_section(buf
.buf
, NULL
) < 1)
725 return error("Could not remove config section '%s'", buf
.buf
);
728 for (i
= 0; i
< branch_list
.nr
; i
++) {
729 struct string_list_item
*item
= branch_list
.items
+ i
;
730 struct branch_info
*info
= item
->util
;
731 if (info
->remote_name
&& !strcmp(info
->remote_name
, remote
->name
)) {
732 const char *keys
[] = { "remote", "merge", NULL
}, **k
;
733 for (k
= keys
; *k
; k
++) {
735 strbuf_addf(&buf
, "branch.%s.%s",
737 if (git_config_set(buf
.buf
, NULL
)) {
738 strbuf_release(&buf
);
746 * We cannot just pass a function to for_each_ref() which deletes
747 * the branches one by one, since for_each_ref() relies on cached
748 * refs, which are invalidated when deleting a branch.
750 cb_data
.remote
= remote
;
751 result
= for_each_ref(add_branch_for_removal
, &cb_data
);
752 strbuf_release(&buf
);
755 result
= remove_branches(&branches
);
756 string_list_clear(&branches
, 1);
759 fprintf(stderr
, skipped
.nr
== 1 ?
760 "Note: A non-remote branch was not removed; "
761 "to delete it, use:\n" :
762 "Note: Non-remote branches were not removed; "
763 "to delete them, use:\n");
764 for (i
= 0; i
< skipped
.nr
; i
++)
765 fprintf(stderr
, " git branch -d %s\n",
766 skipped
.items
[i
].string
);
768 string_list_clear(&skipped
, 0);
773 static void clear_push_info(void *util
, const char *string
)
775 struct push_info
*info
= util
;
780 static void free_remote_ref_states(struct ref_states
*states
)
782 string_list_clear(&states
->new, 0);
783 string_list_clear(&states
->stale
, 1);
784 string_list_clear(&states
->tracked
, 0);
785 string_list_clear(&states
->heads
, 0);
786 string_list_clear_func(&states
->push
, clear_push_info
);
789 static int append_ref_to_tracked_list(const char *refname
,
790 const unsigned char *sha1
, int flags
, void *cb_data
)
792 struct ref_states
*states
= cb_data
;
793 struct refspec refspec
;
795 if (flags
& REF_ISSYMREF
)
798 memset(&refspec
, 0, sizeof(refspec
));
799 refspec
.dst
= (char *)refname
;
800 if (!remote_find_tracking(states
->remote
, &refspec
))
801 string_list_append(abbrev_branch(refspec
.src
), &states
->tracked
);
806 static int get_remote_ref_states(const char *name
,
807 struct ref_states
*states
,
810 struct transport
*transport
;
811 const struct ref
*remote_refs
;
813 states
->remote
= remote_get(name
);
815 return error("No such remote: %s", name
);
820 transport
= transport_get(states
->remote
, states
->remote
->url_nr
> 0 ?
821 states
->remote
->url
[0] : NULL
);
822 remote_refs
= transport_get_remote_refs(transport
);
823 transport_disconnect(transport
);
826 if (query
& GET_REF_STATES
)
827 get_ref_states(remote_refs
, states
);
828 if (query
& GET_HEAD_NAMES
)
829 get_head_names(remote_refs
, states
);
830 if (query
& GET_PUSH_REF_STATES
)
831 get_push_ref_states(remote_refs
, states
);
833 for_each_ref(append_ref_to_tracked_list
, states
);
834 sort_string_list(&states
->tracked
);
835 get_push_ref_states_noquery(states
);
842 struct string_list
*list
;
843 struct ref_states
*states
;
848 static int add_remote_to_show_info(struct string_list_item
*item
, void *cb_data
)
850 struct show_info
*info
= cb_data
;
851 int n
= strlen(item
->string
);
854 string_list_insert(item
->string
, info
->list
);
858 static int show_remote_info_item(struct string_list_item
*item
, void *cb_data
)
860 struct show_info
*info
= cb_data
;
861 struct ref_states
*states
= info
->states
;
862 const char *name
= item
->string
;
864 if (states
->queried
) {
865 const char *fmt
= "%s";
866 const char *arg
= "";
867 if (string_list_has_string(&states
->new, name
)) {
868 fmt
= " new (next fetch will store in remotes/%s)";
869 arg
= states
->remote
->name
;
870 } else if (string_list_has_string(&states
->tracked
, name
))
872 else if (string_list_has_string(&states
->stale
, name
))
873 arg
= " stale (use 'git remote prune' to remove)";
876 printf(" %-*s", info
->width
, name
);
880 printf(" %s\n", name
);
885 static int add_local_to_show_info(struct string_list_item
*branch_item
, void *cb_data
)
887 struct show_info
*show_info
= cb_data
;
888 struct ref_states
*states
= show_info
->states
;
889 struct branch_info
*branch_info
= branch_item
->util
;
890 struct string_list_item
*item
;
893 if (!branch_info
->merge
.nr
|| !branch_info
->remote_name
||
894 strcmp(states
->remote
->name
, branch_info
->remote_name
))
896 if ((n
= strlen(branch_item
->string
)) > show_info
->width
)
897 show_info
->width
= n
;
898 if (branch_info
->rebase
)
899 show_info
->any_rebase
= 1;
901 item
= string_list_insert(branch_item
->string
, show_info
->list
);
902 item
->util
= branch_info
;
907 static int show_local_info_item(struct string_list_item
*item
, void *cb_data
)
909 struct show_info
*show_info
= cb_data
;
910 struct branch_info
*branch_info
= item
->util
;
911 struct string_list
*merge
= &branch_info
->merge
;
915 if (branch_info
->rebase
&& branch_info
->merge
.nr
> 1) {
916 error("invalid branch.%s.merge; cannot rebase onto > 1 branch",
921 printf(" %-*s ", show_info
->width
, item
->string
);
922 if (branch_info
->rebase
) {
923 printf("rebases onto remote %s\n", merge
->items
[0].string
);
925 } else if (show_info
->any_rebase
) {
926 printf(" merges with remote %s\n", merge
->items
[0].string
);
927 also
= " and with remote";
929 printf("merges with remote %s\n", merge
->items
[0].string
);
930 also
= " and with remote";
932 for (i
= 1; i
< merge
->nr
; i
++)
933 printf(" %-*s %s %s\n", show_info
->width
, "", also
,
934 merge
->items
[i
].string
);
939 static int add_push_to_show_info(struct string_list_item
*push_item
, void *cb_data
)
941 struct show_info
*show_info
= cb_data
;
942 struct push_info
*push_info
= push_item
->util
;
943 struct string_list_item
*item
;
945 if ((n
= strlen(push_item
->string
)) > show_info
->width
)
946 show_info
->width
= n
;
947 if ((n
= strlen(push_info
->dest
)) > show_info
->width2
)
948 show_info
->width2
= n
;
949 item
= string_list_append(push_item
->string
, show_info
->list
);
950 item
->util
= push_item
->util
;
955 * Sorting comparison for a string list that has push_info
956 * structs in its util field
958 static int cmp_string_with_push(const void *va
, const void *vb
)
960 const struct string_list_item
*a
= va
;
961 const struct string_list_item
*b
= vb
;
962 const struct push_info
*a_push
= a
->util
;
963 const struct push_info
*b_push
= b
->util
;
964 int cmp
= strcmp(a
->string
, b
->string
);
965 return cmp
? cmp
: strcmp(a_push
->dest
, b_push
->dest
);
968 static int show_push_info_item(struct string_list_item
*item
, void *cb_data
)
970 struct show_info
*show_info
= cb_data
;
971 struct push_info
*push_info
= item
->util
;
972 char *src
= item
->string
, *status
= NULL
;
974 switch (push_info
->status
) {
975 case PUSH_STATUS_CREATE
:
978 case PUSH_STATUS_DELETE
:
982 case PUSH_STATUS_UPTODATE
:
983 status
= "up to date";
985 case PUSH_STATUS_FASTFORWARD
:
986 status
= "fast-forwardable";
988 case PUSH_STATUS_OUTOFDATE
:
989 status
= "local out of date";
991 case PUSH_STATUS_NOTQUERIED
:
995 printf(" %-*s %s to %-*s (%s)\n", show_info
->width
, src
,
996 push_info
->forced
? "forces" : "pushes",
997 show_info
->width2
, push_info
->dest
, status
);
999 printf(" %-*s %s to %s\n", show_info
->width
, src
,
1000 push_info
->forced
? "forces" : "pushes",
1005 static int show(int argc
, const char **argv
)
1007 int no_query
= 0, result
= 0, query_flag
= 0;
1008 struct option options
[] = {
1009 OPT_BOOLEAN('n', NULL
, &no_query
, "do not query remotes"),
1012 struct ref_states states
;
1013 struct string_list info_list
= { NULL
, 0, 0, 0 };
1014 struct show_info info
;
1016 argc
= parse_options(argc
, argv
, NULL
, options
, builtin_remote_show_usage
,
1023 query_flag
= (GET_REF_STATES
| GET_HEAD_NAMES
| GET_PUSH_REF_STATES
);
1025 memset(&states
, 0, sizeof(states
));
1026 memset(&info
, 0, sizeof(info
));
1027 info
.states
= &states
;
1028 info
.list
= &info_list
;
1029 for (; argc
; argc
--, argv
++) {
1034 get_remote_ref_states(*argv
, &states
, query_flag
);
1036 printf("* remote %s\n", *argv
);
1037 printf(" Fetch URL: %s\n", states
.remote
->url_nr
> 0 ?
1038 states
.remote
->url
[0] : "(no URL)");
1039 if (states
.remote
->pushurl_nr
) {
1040 url
= states
.remote
->pushurl
;
1041 url_nr
= states
.remote
->pushurl_nr
;
1043 url
= states
.remote
->url
;
1044 url_nr
= states
.remote
->url_nr
;
1046 for (i
=0; i
< url_nr
; i
++)
1047 printf(" Push URL: %s\n", url
[i
]);
1049 printf(" Push URL: %s\n", "(no URL)");
1051 printf(" HEAD branch: (not queried)\n");
1052 else if (!states
.heads
.nr
)
1053 printf(" HEAD branch: (unknown)\n");
1054 else if (states
.heads
.nr
== 1)
1055 printf(" HEAD branch: %s\n", states
.heads
.items
[0].string
);
1057 printf(" HEAD branch (remote HEAD is ambiguous,"
1058 " may be one of the following):\n");
1059 for (i
= 0; i
< states
.heads
.nr
; i
++)
1060 printf(" %s\n", states
.heads
.items
[i
].string
);
1063 /* remote branch info */
1065 for_each_string_list(add_remote_to_show_info
, &states
.new, &info
);
1066 for_each_string_list(add_remote_to_show_info
, &states
.tracked
, &info
);
1067 for_each_string_list(add_remote_to_show_info
, &states
.stale
, &info
);
1069 printf(" Remote branch%s:%s\n",
1070 info
.list
->nr
> 1 ? "es" : "",
1071 no_query
? " (status not queried)" : "");
1072 for_each_string_list(show_remote_info_item
, info
.list
, &info
);
1073 string_list_clear(info
.list
, 0);
1077 info
.any_rebase
= 0;
1078 for_each_string_list(add_local_to_show_info
, &branch_list
, &info
);
1080 printf(" Local branch%s configured for 'git pull':\n",
1081 info
.list
->nr
> 1 ? "es" : "");
1082 for_each_string_list(show_local_info_item
, info
.list
, &info
);
1083 string_list_clear(info
.list
, 0);
1086 if (states
.remote
->mirror
)
1087 printf(" Local refs will be mirrored by 'git push'\n");
1089 info
.width
= info
.width2
= 0;
1090 for_each_string_list(add_push_to_show_info
, &states
.push
, &info
);
1091 qsort(info
.list
->items
, info
.list
->nr
,
1092 sizeof(*info
.list
->items
), cmp_string_with_push
);
1094 printf(" Local ref%s configured for 'git push'%s:\n",
1095 info
.list
->nr
> 1 ? "s" : "",
1096 no_query
? " (status not queried)" : "");
1097 for_each_string_list(show_push_info_item
, info
.list
, &info
);
1098 string_list_clear(info
.list
, 0);
1100 free_remote_ref_states(&states
);
1106 static int set_head(int argc
, const char **argv
)
1108 int i
, opt_a
= 0, opt_d
= 0, result
= 0;
1109 struct strbuf buf
= STRBUF_INIT
, buf2
= STRBUF_INIT
;
1110 char *head_name
= NULL
;
1112 struct option options
[] = {
1113 OPT_BOOLEAN('a', "auto", &opt_a
,
1114 "set refs/remotes/<name>/HEAD according to remote"),
1115 OPT_BOOLEAN('d', "delete", &opt_d
,
1116 "delete refs/remotes/<name>/HEAD"),
1119 argc
= parse_options(argc
, argv
, NULL
, options
, builtin_remote_sethead_usage
,
1122 strbuf_addf(&buf
, "refs/remotes/%s/HEAD", argv
[0]);
1124 if (!opt_a
&& !opt_d
&& argc
== 2) {
1125 head_name
= xstrdup(argv
[1]);
1126 } else if (opt_a
&& !opt_d
&& argc
== 1) {
1127 struct ref_states states
;
1128 memset(&states
, 0, sizeof(states
));
1129 get_remote_ref_states(argv
[0], &states
, GET_HEAD_NAMES
);
1130 if (!states
.heads
.nr
)
1131 result
|= error("Cannot determine remote HEAD");
1132 else if (states
.heads
.nr
> 1) {
1133 result
|= error("Multiple remote HEAD branches. "
1134 "Please choose one explicitly with:");
1135 for (i
= 0; i
< states
.heads
.nr
; i
++)
1136 fprintf(stderr
, " git remote set-head %s %s\n",
1137 argv
[0], states
.heads
.items
[i
].string
);
1139 head_name
= xstrdup(states
.heads
.items
[0].string
);
1140 free_remote_ref_states(&states
);
1141 } else if (opt_d
&& !opt_a
&& argc
== 1) {
1142 if (delete_ref(buf
.buf
, NULL
, REF_NODEREF
))
1143 result
|= error("Could not delete %s", buf
.buf
);
1145 usage_with_options(builtin_remote_sethead_usage
, options
);
1148 unsigned char sha1
[20];
1149 strbuf_addf(&buf2
, "refs/remotes/%s/%s", argv
[0], head_name
);
1150 /* make sure it's valid */
1151 if (!resolve_ref(buf2
.buf
, sha1
, 1, NULL
))
1152 result
|= error("Not a valid ref: %s", buf2
.buf
);
1153 else if (create_symref(buf
.buf
, buf2
.buf
, "remote set-head"))
1154 result
|= error("Could not setup %s", buf
.buf
);
1156 printf("%s/HEAD set to %s\n", argv
[0], head_name
);
1160 strbuf_release(&buf
);
1161 strbuf_release(&buf2
);
1165 static int prune(int argc
, const char **argv
)
1167 int dry_run
= 0, result
= 0;
1168 struct option options
[] = {
1169 OPT__DRY_RUN(&dry_run
),
1173 argc
= parse_options(argc
, argv
, NULL
, options
, builtin_remote_prune_usage
,
1177 usage_with_options(builtin_remote_prune_usage
, options
);
1179 for (; argc
; argc
--, argv
++)
1180 result
|= prune_remote(*argv
, dry_run
);
1185 static int prune_remote(const char *remote
, int dry_run
)
1188 struct ref_states states
;
1189 const char *dangling_msg
= dry_run
1190 ? " %s will become dangling!\n"
1191 : " %s has become dangling!\n";
1193 memset(&states
, 0, sizeof(states
));
1194 get_remote_ref_states(remote
, &states
, GET_REF_STATES
);
1196 if (states
.stale
.nr
) {
1197 printf("Pruning %s\n", remote
);
1199 states
.remote
->url_nr
1200 ? states
.remote
->url
[0]
1204 for (i
= 0; i
< states
.stale
.nr
; i
++) {
1205 const char *refname
= states
.stale
.items
[i
].util
;
1208 result
|= delete_ref(refname
, NULL
, 0);
1210 printf(" * [%s] %s\n", dry_run
? "would prune" : "pruned",
1211 abbrev_ref(refname
, "refs/remotes/"));
1212 warn_dangling_symref(stdout
, dangling_msg
, refname
);
1215 free_remote_ref_states(&states
);
1219 static int get_remote_default(const char *key
, const char *value
, void *priv
)
1221 if (strcmp(key
, "remotes.default") == 0) {
1228 static int update(int argc
, const char **argv
)
1231 struct option options
[] = {
1232 OPT_BOOLEAN('p', "prune", &prune
,
1233 "prune remotes after fetching"),
1236 const char **fetch_argv
;
1238 int default_defined
= 0;
1240 fetch_argv
= xmalloc(sizeof(char *) * (argc
+5));
1242 argc
= parse_options(argc
, argv
, NULL
, options
, builtin_remote_update_usage
,
1243 PARSE_OPT_KEEP_ARGV0
);
1245 fetch_argv
[fetch_argc
++] = "fetch";
1248 fetch_argv
[fetch_argc
++] = "--prune";
1250 fetch_argv
[fetch_argc
++] = "-v";
1251 fetch_argv
[fetch_argc
++] = "--multiple";
1253 fetch_argv
[fetch_argc
++] = "default";
1254 for (i
= 1; i
< argc
; i
++)
1255 fetch_argv
[fetch_argc
++] = argv
[i
];
1257 if (strcmp(fetch_argv
[fetch_argc
-1], "default") == 0) {
1258 git_config(get_remote_default
, &default_defined
);
1259 if (!default_defined
)
1260 fetch_argv
[fetch_argc
-1] = "--all";
1263 fetch_argv
[fetch_argc
] = NULL
;
1265 return run_command_v_opt(fetch_argv
, RUN_GIT_CMD
);
1268 static int set_url(int argc
, const char **argv
)
1270 int i
, push_mode
= 0, add_mode
= 0, delete_mode
= 0;
1271 int matches
= 0, negative_matches
= 0;
1272 const char *remotename
= NULL
;
1273 const char *newurl
= NULL
;
1274 const char *oldurl
= NULL
;
1275 struct remote
*remote
;
1277 const char **urlset
;
1279 struct strbuf name_buf
= STRBUF_INIT
;
1280 struct option options
[] = {
1281 OPT_BOOLEAN('\0', "push", &push_mode
,
1282 "manipulate push URLs"),
1283 OPT_BOOLEAN('\0', "add", &add_mode
,
1285 OPT_BOOLEAN('\0', "delete", &delete_mode
,
1289 argc
= parse_options(argc
, argv
, NULL
, options
, builtin_remote_update_usage
,
1290 PARSE_OPT_KEEP_ARGV0
);
1292 if (add_mode
&& delete_mode
)
1293 die("--add --delete doesn't make sense");
1295 if (argc
< 3 || argc
> 4 || ((add_mode
|| delete_mode
) && argc
!= 3))
1296 usage_with_options(builtin_remote_seturl_usage
, options
);
1298 remotename
= argv
[1];
1306 if (!remote_is_configured(remotename
))
1307 die("No such remote '%s'", remotename
);
1308 remote
= remote_get(remotename
);
1311 strbuf_addf(&name_buf
, "remote.%s.pushurl", remotename
);
1312 urlset
= remote
->pushurl
;
1313 urlset_nr
= remote
->pushurl_nr
;
1315 strbuf_addf(&name_buf
, "remote.%s.url", remotename
);
1316 urlset
= remote
->url
;
1317 urlset_nr
= remote
->url_nr
;
1320 /* Special cases that add new entry. */
1321 if ((!oldurl
&& !delete_mode
) || add_mode
) {
1323 git_config_set_multivar(name_buf
.buf
, newurl
,
1326 git_config_set(name_buf
.buf
, newurl
);
1327 strbuf_release(&name_buf
);
1331 /* Old URL specified. Demand that one matches. */
1332 if (regcomp(&old_regex
, oldurl
, REG_EXTENDED
))
1333 die("Invalid old URL pattern: %s", oldurl
);
1335 for (i
= 0; i
< urlset_nr
; i
++)
1336 if (!regexec(&old_regex
, urlset
[i
], 0, NULL
, 0))
1340 if (!delete_mode
&& !matches
)
1341 die("No such URL found: %s", oldurl
);
1342 if (delete_mode
&& !negative_matches
&& !push_mode
)
1343 die("Will not delete all non-push URLs");
1345 regfree(&old_regex
);
1348 git_config_set_multivar(name_buf
.buf
, newurl
, oldurl
, 0);
1350 git_config_set_multivar(name_buf
.buf
, NULL
, oldurl
, 1);
1354 static int get_one_entry(struct remote
*remote
, void *priv
)
1356 struct string_list
*list
= priv
;
1357 struct strbuf url_buf
= STRBUF_INIT
;
1361 if (remote
->url_nr
> 0) {
1362 strbuf_addf(&url_buf
, "%s (fetch)", remote
->url
[0]);
1363 string_list_append(remote
->name
, list
)->util
=
1364 strbuf_detach(&url_buf
, NULL
);
1366 string_list_append(remote
->name
, list
)->util
= NULL
;
1367 if (remote
->pushurl_nr
) {
1368 url
= remote
->pushurl
;
1369 url_nr
= remote
->pushurl_nr
;
1372 url_nr
= remote
->url_nr
;
1374 for (i
= 0; i
< url_nr
; i
++)
1376 strbuf_addf(&url_buf
, "%s (push)", url
[i
]);
1377 string_list_append(remote
->name
, list
)->util
=
1378 strbuf_detach(&url_buf
, NULL
);
1384 static int show_all(void)
1386 struct string_list list
= { NULL
, 0, 0 };
1389 list
.strdup_strings
= 1;
1390 result
= for_each_remote(get_one_entry
, &list
);
1395 sort_string_list(&list
);
1396 for (i
= 0; i
< list
.nr
; i
++) {
1397 struct string_list_item
*item
= list
.items
+ i
;
1399 printf("%s\t%s\n", item
->string
,
1400 item
->util
? (const char *)item
->util
: "");
1402 if (i
&& !strcmp((item
- 1)->string
, item
->string
))
1404 printf("%s\n", item
->string
);
1408 string_list_clear(&list
, 1);
1412 int cmd_remote(int argc
, const char **argv
, const char *prefix
)
1414 struct option options
[] = {
1415 OPT_BOOLEAN('v', "verbose", &verbose
, "be verbose; must be placed before a subcommand"),
1420 argc
= parse_options(argc
, argv
, prefix
, options
, builtin_remote_usage
,
1421 PARSE_OPT_STOP_AT_NON_OPTION
);
1424 result
= show_all();
1425 else if (!strcmp(argv
[0], "add"))
1426 result
= add(argc
, argv
);
1427 else if (!strcmp(argv
[0], "rename"))
1428 result
= mv(argc
, argv
);
1429 else if (!strcmp(argv
[0], "rm"))
1430 result
= rm(argc
, argv
);
1431 else if (!strcmp(argv
[0], "set-head"))
1432 result
= set_head(argc
, argv
);
1433 else if (!strcmp(argv
[0], "set-url"))
1434 result
= set_url(argc
, argv
);
1435 else if (!strcmp(argv
[0], "show"))
1436 result
= show(argc
, argv
);
1437 else if (!strcmp(argv
[0], "prune"))
1438 result
= prune(argc
, argv
);
1439 else if (!strcmp(argv
[0], "update"))
1440 result
= update(argc
, argv
);
1442 error("Unknown subcommand: %s", argv
[0]);
1443 usage_with_options(builtin_remote_usage
, options
);
1446 return result
? 1 : 0;