ls-remote.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include "builtin.h"
  2. #include "cache.h"
  3. #include "transport.h"
  4. #include "ref-filter.h"
  5. #include "remote.h"
  6. #include "refs.h"
  7. static const char * const ls_remote_usage[] = {
  8. N_("git ls-remote [--heads] [--tags] [--refs] [--upload-pack=<exec>]\n"
  9. " [-q | --quiet] [--exit-code] [--get-url]\n"
  10. " [--symref] [<repository> [<refs>...]]"),
  11. NULL
  12. };
  13. /*
  14. * Is there one among the list of patterns that match the tail part
  15. * of the path?
  16. */
  17. static int tail_match(const char **pattern, const char *path)
  18. {
  19. const char *p;
  20. char *pathbuf;
  21. if (!pattern)
  22. return 1; /* no restriction */
  23. pathbuf = xstrfmt("/%s", path);
  24. while ((p = *(pattern++)) != NULL) {
  25. if (!wildmatch(p, pathbuf, 0)) {
  26. free(pathbuf);
  27. return 1;
  28. }
  29. }
  30. free(pathbuf);
  31. return 0;
  32. }
  33. int cmd_ls_remote(int argc, const char **argv, const char *prefix)
  34. {
  35. const char *dest = NULL;
  36. unsigned flags = 0;
  37. int get_url = 0;
  38. int quiet = 0;
  39. int status = 0;
  40. int show_symref_target = 0;
  41. const char *uploadpack = NULL;
  42. const char **pattern = NULL;
  43. struct strvec ref_prefixes = STRVEC_INIT;
  44. int i;
  45. struct string_list server_options = STRING_LIST_INIT_DUP;
  46. struct remote *remote;
  47. struct transport *transport;
  48. const struct ref *ref;
  49. struct ref_array ref_array;
  50. static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
  51. struct option options[] = {
  52. OPT__QUIET(&quiet, N_("do not print remote URL")),
  53. OPT_STRING(0, "upload-pack", &uploadpack, N_("exec"),
  54. N_("path of git-upload-pack on the remote host")),
  55. { OPTION_STRING, 0, "exec", &uploadpack, N_("exec"),
  56. N_("path of git-upload-pack on the remote host"),
  57. PARSE_OPT_HIDDEN },
  58. OPT_BIT('t', "tags", &flags, N_("limit to tags"), REF_TAGS),
  59. OPT_BIT('h', "heads", &flags, N_("limit to heads"), REF_HEADS),
  60. OPT_BIT(0, "refs", &flags, N_("do not show peeled tags"), REF_NORMAL),
  61. OPT_BOOL(0, "get-url", &get_url,
  62. N_("take url.<base>.insteadOf into account")),
  63. OPT_REF_SORT(sorting_tail),
  64. OPT_SET_INT_F(0, "exit-code", &status,
  65. N_("exit with exit code 2 if no matching refs are found"),
  66. 2, PARSE_OPT_NOCOMPLETE),
  67. OPT_BOOL(0, "symref", &show_symref_target,
  68. N_("show underlying ref in addition to the object pointed by it")),
  69. OPT_STRING_LIST('o', "server-option", &server_options, N_("server-specific"), N_("option to transmit")),
  70. OPT_END()
  71. };
  72. memset(&ref_array, 0, sizeof(ref_array));
  73. argc = parse_options(argc, argv, prefix, options, ls_remote_usage,
  74. PARSE_OPT_STOP_AT_NON_OPTION);
  75. dest = argv[0];
  76. UNLEAK(sorting);
  77. if (argc > 1) {
  78. int i;
  79. pattern = xcalloc(argc, sizeof(const char *));
  80. for (i = 1; i < argc; i++) {
  81. pattern[i - 1] = xstrfmt("*/%s", argv[i]);
  82. }
  83. }
  84. if (flags & REF_TAGS)
  85. strvec_push(&ref_prefixes, "refs/tags/");
  86. if (flags & REF_HEADS)
  87. strvec_push(&ref_prefixes, "refs/heads/");
  88. remote = remote_get(dest);
  89. if (!remote) {
  90. if (dest)
  91. die("bad repository '%s'", dest);
  92. die("No remote configured to list refs from.");
  93. }
  94. if (!remote->url_nr)
  95. die("remote %s has no configured URL", dest);
  96. if (get_url) {
  97. printf("%s\n", *remote->url);
  98. return 0;
  99. }
  100. transport = transport_get(remote, NULL);
  101. if (uploadpack != NULL)
  102. transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack);
  103. if (server_options.nr)
  104. transport->server_options = &server_options;
  105. ref = transport_get_remote_refs(transport, &ref_prefixes);
  106. if (ref) {
  107. int hash_algo = hash_algo_by_ptr(transport_get_hash_algo(transport));
  108. repo_set_hash_algo(the_repository, hash_algo);
  109. }
  110. if (transport_disconnect(transport))
  111. return 1;
  112. if (!dest && !quiet)
  113. fprintf(stderr, "From %s\n", *remote->url);
  114. for ( ; ref; ref = ref->next) {
  115. struct ref_array_item *item;
  116. if (!check_ref_type(ref, flags))
  117. continue;
  118. if (!tail_match(pattern, ref->name))
  119. continue;
  120. item = ref_array_push(&ref_array, ref->name, &ref->old_oid);
  121. item->symref = xstrdup_or_null(ref->symref);
  122. }
  123. if (sorting)
  124. ref_array_sort(sorting, &ref_array);
  125. for (i = 0; i < ref_array.nr; i++) {
  126. const struct ref_array_item *ref = ref_array.items[i];
  127. if (show_symref_target && ref->symref)
  128. printf("ref: %s\t%s\n", ref->symref, ref->refname);
  129. printf("%s\t%s\n", oid_to_hex(&ref->objectname), ref->refname);
  130. status = 0; /* we found something */
  131. }
  132. ref_array_clear(&ref_array);
  133. return status;
  134. }