THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

2320 lines
58KB

  1. #include "cache.h"
  2. #include "config.h"
  3. #include "remote.h"
  4. #include "refs.h"
  5. #include "refspec.h"
  6. #include "object-store.h"
  7. #include "commit.h"
  8. #include "diff.h"
  9. #include "revision.h"
  10. #include "dir.h"
  11. #include "tag.h"
  12. #include "string-list.h"
  13. #include "mergesort.h"
  14. #include "argv-array.h"
  15. #include "commit-reach.h"
  16. #include "advice.h"
  17. enum map_direction { FROM_SRC, FROM_DST };
  18. struct counted_string {
  19. size_t len;
  20. const char *s;
  21. };
  22. struct rewrite {
  23. const char *base;
  24. size_t baselen;
  25. struct counted_string *instead_of;
  26. int instead_of_nr;
  27. int instead_of_alloc;
  28. };
  29. struct rewrites {
  30. struct rewrite **rewrite;
  31. int rewrite_alloc;
  32. int rewrite_nr;
  33. };
  34. static struct remote **remotes;
  35. static int remotes_alloc;
  36. static int remotes_nr;
  37. static struct hashmap remotes_hash;
  38. static struct branch **branches;
  39. static int branches_alloc;
  40. static int branches_nr;
  41. static struct branch *current_branch;
  42. static const char *pushremote_name;
  43. static struct rewrites rewrites;
  44. static struct rewrites rewrites_push;
  45. static int valid_remote(const struct remote *remote)
  46. {
  47. return (!!remote->url) || (!!remote->foreign_vcs);
  48. }
  49. static const char *alias_url(const char *url, struct rewrites *r)
  50. {
  51. int i, j;
  52. struct counted_string *longest;
  53. int longest_i;
  54. longest = NULL;
  55. longest_i = -1;
  56. for (i = 0; i < r->rewrite_nr; i++) {
  57. if (!r->rewrite[i])
  58. continue;
  59. for (j = 0; j < r->rewrite[i]->instead_of_nr; j++) {
  60. if (starts_with(url, r->rewrite[i]->instead_of[j].s) &&
  61. (!longest ||
  62. longest->len < r->rewrite[i]->instead_of[j].len)) {
  63. longest = &(r->rewrite[i]->instead_of[j]);
  64. longest_i = i;
  65. }
  66. }
  67. }
  68. if (!longest)
  69. return url;
  70. return xstrfmt("%s%s", r->rewrite[longest_i]->base, url + longest->len);
  71. }
  72. static void add_url(struct remote *remote, const char *url)
  73. {
  74. ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);
  75. remote->url[remote->url_nr++] = url;
  76. }
  77. static void add_pushurl(struct remote *remote, const char *pushurl)
  78. {
  79. ALLOC_GROW(remote->pushurl, remote->pushurl_nr + 1, remote->pushurl_alloc);
  80. remote->pushurl[remote->pushurl_nr++] = pushurl;
  81. }
  82. static void add_pushurl_alias(struct remote *remote, const char *url)
  83. {
  84. const char *pushurl = alias_url(url, &rewrites_push);
  85. if (pushurl != url)
  86. add_pushurl(remote, pushurl);
  87. }
  88. static void add_url_alias(struct remote *remote, const char *url)
  89. {
  90. add_url(remote, alias_url(url, &rewrites));
  91. add_pushurl_alias(remote, url);
  92. }
  93. struct remotes_hash_key {
  94. const char *str;
  95. int len;
  96. };
  97. static int remotes_hash_cmp(const void *unused_cmp_data,
  98. const struct hashmap_entry *eptr,
  99. const struct hashmap_entry *entry_or_key,
  100. const void *keydata)
  101. {
  102. const struct remote *a, *b;
  103. const struct remotes_hash_key *key = keydata;
  104. a = container_of(eptr, const struct remote, ent);
  105. b = container_of(entry_or_key, const struct remote, ent);
  106. if (key)
  107. return strncmp(a->name, key->str, key->len) || a->name[key->len];
  108. else
  109. return strcmp(a->name, b->name);
  110. }
  111. static inline void init_remotes_hash(void)
  112. {
  113. if (!remotes_hash.cmpfn)
  114. hashmap_init(&remotes_hash, remotes_hash_cmp, NULL, 0);
  115. }
  116. static struct remote *make_remote(const char *name, int len)
  117. {
  118. struct remote *ret, *replaced;
  119. struct remotes_hash_key lookup;
  120. struct hashmap_entry lookup_entry, *e;
  121. if (!len)
  122. len = strlen(name);
  123. init_remotes_hash();
  124. lookup.str = name;
  125. lookup.len = len;
  126. hashmap_entry_init(&lookup_entry, memhash(name, len));
  127. e = hashmap_get(&remotes_hash, &lookup_entry, &lookup);
  128. if (e)
  129. return container_of(e, struct remote, ent);
  130. ret = xcalloc(1, sizeof(struct remote));
  131. ret->prune = -1; /* unspecified */
  132. ret->prune_tags = -1; /* unspecified */
  133. ret->name = xstrndup(name, len);
  134. refspec_init(&ret->push, REFSPEC_PUSH);
  135. refspec_init(&ret->fetch, REFSPEC_FETCH);
  136. ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc);
  137. remotes[remotes_nr++] = ret;
  138. hashmap_entry_init(&ret->ent, lookup_entry.hash);
  139. replaced = hashmap_put_entry(&remotes_hash, ret, ent);
  140. assert(replaced == NULL); /* no previous entry overwritten */
  141. return ret;
  142. }
  143. static void add_merge(struct branch *branch, const char *name)
  144. {
  145. ALLOC_GROW(branch->merge_name, branch->merge_nr + 1,
  146. branch->merge_alloc);
  147. branch->merge_name[branch->merge_nr++] = name;
  148. }
  149. static struct branch *make_branch(const char *name, int len)
  150. {
  151. struct branch *ret;
  152. int i;
  153. for (i = 0; i < branches_nr; i++) {
  154. if (len ? (!strncmp(name, branches[i]->name, len) &&
  155. !branches[i]->name[len]) :
  156. !strcmp(name, branches[i]->name))
  157. return branches[i];
  158. }
  159. ALLOC_GROW(branches, branches_nr + 1, branches_alloc);
  160. ret = xcalloc(1, sizeof(struct branch));
  161. branches[branches_nr++] = ret;
  162. if (len)
  163. ret->name = xstrndup(name, len);
  164. else
  165. ret->name = xstrdup(name);
  166. ret->refname = xstrfmt("refs/heads/%s", ret->name);
  167. return ret;
  168. }
  169. static struct rewrite *make_rewrite(struct rewrites *r, const char *base, int len)
  170. {
  171. struct rewrite *ret;
  172. int i;
  173. for (i = 0; i < r->rewrite_nr; i++) {
  174. if (len
  175. ? (len == r->rewrite[i]->baselen &&
  176. !strncmp(base, r->rewrite[i]->base, len))
  177. : !strcmp(base, r->rewrite[i]->base))
  178. return r->rewrite[i];
  179. }
  180. ALLOC_GROW(r->rewrite, r->rewrite_nr + 1, r->rewrite_alloc);
  181. ret = xcalloc(1, sizeof(struct rewrite));
  182. r->rewrite[r->rewrite_nr++] = ret;
  183. if (len) {
  184. ret->base = xstrndup(base, len);
  185. ret->baselen = len;
  186. }
  187. else {
  188. ret->base = xstrdup(base);
  189. ret->baselen = strlen(base);
  190. }
  191. return ret;
  192. }
  193. static void add_instead_of(struct rewrite *rewrite, const char *instead_of)
  194. {
  195. ALLOC_GROW(rewrite->instead_of, rewrite->instead_of_nr + 1, rewrite->instead_of_alloc);
  196. rewrite->instead_of[rewrite->instead_of_nr].s = instead_of;
  197. rewrite->instead_of[rewrite->instead_of_nr].len = strlen(instead_of);
  198. rewrite->instead_of_nr++;
  199. }
  200. static const char *skip_spaces(const char *s)
  201. {
  202. while (isspace(*s))
  203. s++;
  204. return s;
  205. }
  206. static void read_remotes_file(struct remote *remote)
  207. {
  208. struct strbuf buf = STRBUF_INIT;
  209. FILE *f = fopen_or_warn(git_path("remotes/%s", remote->name), "r");
  210. if (!f)
  211. return;
  212. remote->configured_in_repo = 1;
  213. remote->origin = REMOTE_REMOTES;
  214. while (strbuf_getline(&buf, f) != EOF) {
  215. const char *v;
  216. strbuf_rtrim(&buf);
  217. if (skip_prefix(buf.buf, "URL:", &v))
  218. add_url_alias(remote, xstrdup(skip_spaces(v)));
  219. else if (skip_prefix(buf.buf, "Push:", &v))
  220. refspec_append(&remote->push, skip_spaces(v));
  221. else if (skip_prefix(buf.buf, "Pull:", &v))
  222. refspec_append(&remote->fetch, skip_spaces(v));
  223. }
  224. strbuf_release(&buf);
  225. fclose(f);
  226. }
  227. static void read_branches_file(struct remote *remote)
  228. {
  229. char *frag;
  230. struct strbuf buf = STRBUF_INIT;
  231. FILE *f = fopen_or_warn(git_path("branches/%s", remote->name), "r");
  232. if (!f)
  233. return;
  234. strbuf_getline_lf(&buf, f);
  235. fclose(f);
  236. strbuf_trim(&buf);
  237. if (!buf.len) {
  238. strbuf_release(&buf);
  239. return;
  240. }
  241. remote->configured_in_repo = 1;
  242. remote->origin = REMOTE_BRANCHES;
  243. /*
  244. * The branches file would have URL and optionally
  245. * #branch specified. The "master" (or specified) branch is
  246. * fetched and stored in the local branch matching the
  247. * remote name.
  248. */
  249. frag = strchr(buf.buf, '#');
  250. if (frag)
  251. *(frag++) = '\0';
  252. else
  253. frag = "master";
  254. add_url_alias(remote, strbuf_detach(&buf, NULL));
  255. strbuf_addf(&buf, "refs/heads/%s:refs/heads/%s",
  256. frag, remote->name);
  257. refspec_append(&remote->fetch, buf.buf);
  258. /*
  259. * Cogito compatible push: push current HEAD to remote #branch
  260. * (master if missing)
  261. */
  262. strbuf_reset(&buf);
  263. strbuf_addf(&buf, "HEAD:refs/heads/%s", frag);
  264. refspec_append(&remote->push, buf.buf);
  265. remote->fetch_tags = 1; /* always auto-follow */
  266. strbuf_release(&buf);
  267. }
  268. static int handle_config(const char *key, const char *value, void *cb)
  269. {
  270. const char *name;
  271. int namelen;
  272. const char *subkey;
  273. struct remote *remote;
  274. struct branch *branch;
  275. if (parse_config_key(key, "branch", &name, &namelen, &subkey) >= 0) {
  276. if (!name)
  277. return 0;
  278. branch = make_branch(name, namelen);
  279. if (!strcmp(subkey, "remote")) {
  280. return git_config_string(&branch->remote_name, key, value);
  281. } else if (!strcmp(subkey, "pushremote")) {
  282. return git_config_string(&branch->pushremote_name, key, value);
  283. } else if (!strcmp(subkey, "merge")) {
  284. if (!value)
  285. return config_error_nonbool(key);
  286. add_merge(branch, xstrdup(value));
  287. }
  288. return 0;
  289. }
  290. if (parse_config_key(key, "url", &name, &namelen, &subkey) >= 0) {
  291. struct rewrite *rewrite;
  292. if (!name)
  293. return 0;
  294. if (!strcmp(subkey, "insteadof")) {
  295. if (!value)
  296. return config_error_nonbool(key);
  297. rewrite = make_rewrite(&rewrites, name, namelen);
  298. add_instead_of(rewrite, xstrdup(value));
  299. } else if (!strcmp(subkey, "pushinsteadof")) {
  300. if (!value)
  301. return config_error_nonbool(key);
  302. rewrite = make_rewrite(&rewrites_push, name, namelen);
  303. add_instead_of(rewrite, xstrdup(value));
  304. }
  305. }
  306. if (parse_config_key(key, "remote", &name, &namelen, &subkey) < 0)
  307. return 0;
  308. /* Handle remote.* variables */
  309. if (!name && !strcmp(subkey, "pushdefault"))
  310. return git_config_string(&pushremote_name, key, value);
  311. if (!name)
  312. return 0;
  313. /* Handle remote.<name>.* variables */
  314. if (*name == '/') {
  315. warning(_("config remote shorthand cannot begin with '/': %s"),
  316. name);
  317. return 0;
  318. }
  319. remote = make_remote(name, namelen);
  320. remote->origin = REMOTE_CONFIG;
  321. if (current_config_scope() == CONFIG_SCOPE_REPO)
  322. remote->configured_in_repo = 1;
  323. if (!strcmp(subkey, "mirror"))
  324. remote->mirror = git_config_bool(key, value);
  325. else if (!strcmp(subkey, "skipdefaultupdate"))
  326. remote->skip_default_update = git_config_bool(key, value);
  327. else if (!strcmp(subkey, "skipfetchall"))
  328. remote->skip_default_update = git_config_bool(key, value);
  329. else if (!strcmp(subkey, "prune"))
  330. remote->prune = git_config_bool(key, value);
  331. else if (!strcmp(subkey, "prunetags"))
  332. remote->prune_tags = git_config_bool(key, value);
  333. else if (!strcmp(subkey, "url")) {
  334. const char *v;
  335. if (git_config_string(&v, key, value))
  336. return -1;
  337. add_url(remote, v);
  338. } else if (!strcmp(subkey, "pushurl")) {
  339. const char *v;
  340. if (git_config_string(&v, key, value))
  341. return -1;
  342. add_pushurl(remote, v);
  343. } else if (!strcmp(subkey, "push")) {
  344. const char *v;
  345. if (git_config_string(&v, key, value))
  346. return -1;
  347. refspec_append(&remote->push, v);
  348. free((char *)v);
  349. } else if (!strcmp(subkey, "fetch")) {
  350. const char *v;
  351. if (git_config_string(&v, key, value))
  352. return -1;
  353. refspec_append(&remote->fetch, v);
  354. free((char *)v);
  355. } else if (!strcmp(subkey, "receivepack")) {
  356. const char *v;
  357. if (git_config_string(&v, key, value))
  358. return -1;
  359. if (!remote->receivepack)
  360. remote->receivepack = v;
  361. else
  362. error(_("more than one receivepack given, using the first"));
  363. } else if (!strcmp(subkey, "uploadpack")) {
  364. const char *v;
  365. if (git_config_string(&v, key, value))
  366. return -1;
  367. if (!remote->uploadpack)
  368. remote->uploadpack = v;
  369. else
  370. error(_("more than one uploadpack given, using the first"));
  371. } else if (!strcmp(subkey, "tagopt")) {
  372. if (!strcmp(value, "--no-tags"))
  373. remote->fetch_tags = -1;
  374. else if (!strcmp(value, "--tags"))
  375. remote->fetch_tags = 2;
  376. } else if (!strcmp(subkey, "proxy")) {
  377. return git_config_string((const char **)&remote->http_proxy,
  378. key, value);
  379. } else if (!strcmp(subkey, "proxyauthmethod")) {
  380. return git_config_string((const char **)&remote->http_proxy_authmethod,
  381. key, value);
  382. } else if (!strcmp(subkey, "vcs")) {
  383. return git_config_string(&remote->foreign_vcs, key, value);
  384. }
  385. return 0;
  386. }
  387. static void alias_all_urls(void)
  388. {
  389. int i, j;
  390. for (i = 0; i < remotes_nr; i++) {
  391. int add_pushurl_aliases;
  392. if (!remotes[i])
  393. continue;
  394. for (j = 0; j < remotes[i]->pushurl_nr; j++) {
  395. remotes[i]->pushurl[j] = alias_url(remotes[i]->pushurl[j], &rewrites);
  396. }
  397. add_pushurl_aliases = remotes[i]->pushurl_nr == 0;
  398. for (j = 0; j < remotes[i]->url_nr; j++) {
  399. if (add_pushurl_aliases)
  400. add_pushurl_alias(remotes[i], remotes[i]->url[j]);
  401. remotes[i]->url[j] = alias_url(remotes[i]->url[j], &rewrites);
  402. }
  403. }
  404. }
  405. static void read_config(void)
  406. {
  407. static int loaded;
  408. int flag;
  409. if (loaded)
  410. return;
  411. loaded = 1;
  412. current_branch = NULL;
  413. if (startup_info->have_repository) {
  414. const char *head_ref = resolve_ref_unsafe("HEAD", 0, NULL, &flag);
  415. if (head_ref && (flag & REF_ISSYMREF) &&
  416. skip_prefix(head_ref, "refs/heads/", &head_ref)) {
  417. current_branch = make_branch(head_ref, 0);
  418. }
  419. }
  420. git_config(handle_config, NULL);
  421. alias_all_urls();
  422. }
  423. static int valid_remote_nick(const char *name)
  424. {
  425. if (!name[0] || is_dot_or_dotdot(name))
  426. return 0;
  427. /* remote nicknames cannot contain slashes */
  428. while (*name)
  429. if (is_dir_sep(*name++))
  430. return 0;
  431. return 1;
  432. }
  433. const char *remote_for_branch(struct branch *branch, int *explicit)
  434. {
  435. if (branch && branch->remote_name) {
  436. if (explicit)
  437. *explicit = 1;
  438. return branch->remote_name;
  439. }
  440. if (explicit)
  441. *explicit = 0;
  442. return "origin";
  443. }
  444. const char *pushremote_for_branch(struct branch *branch, int *explicit)
  445. {
  446. if (branch && branch->pushremote_name) {
  447. if (explicit)
  448. *explicit = 1;
  449. return branch->pushremote_name;
  450. }
  451. if (pushremote_name) {
  452. if (explicit)
  453. *explicit = 1;
  454. return pushremote_name;
  455. }
  456. return remote_for_branch(branch, explicit);
  457. }
  458. const char *remote_ref_for_branch(struct branch *branch, int for_push,
  459. int *explicit)
  460. {
  461. if (branch) {
  462. if (!for_push) {
  463. if (branch->merge_nr) {
  464. if (explicit)
  465. *explicit = 1;
  466. return branch->merge_name[0];
  467. }
  468. } else {
  469. const char *dst, *remote_name =
  470. pushremote_for_branch(branch, NULL);
  471. struct remote *remote = remote_get(remote_name);
  472. if (remote && remote->push.nr &&
  473. (dst = apply_refspecs(&remote->push,
  474. branch->refname))) {
  475. if (explicit)
  476. *explicit = 1;
  477. return dst;
  478. }
  479. }
  480. }
  481. if (explicit)
  482. *explicit = 0;
  483. return "";
  484. }
  485. static struct remote *remote_get_1(const char *name,
  486. const char *(*get_default)(struct branch *, int *))
  487. {
  488. struct remote *ret;
  489. int name_given = 0;
  490. read_config();
  491. if (name)
  492. name_given = 1;
  493. else
  494. name = get_default(current_branch, &name_given);
  495. ret = make_remote(name, 0);
  496. if (valid_remote_nick(name) && have_git_dir()) {
  497. if (!valid_remote(ret))
  498. read_remotes_file(ret);
  499. if (!valid_remote(ret))
  500. read_branches_file(ret);
  501. }
  502. if (name_given && !valid_remote(ret))
  503. add_url_alias(ret, name);
  504. if (!valid_remote(ret))
  505. return NULL;
  506. return ret;
  507. }
  508. struct remote *remote_get(const char *name)
  509. {
  510. return remote_get_1(name, remote_for_branch);
  511. }
  512. struct remote *pushremote_get(const char *name)
  513. {
  514. return remote_get_1(name, pushremote_for_branch);
  515. }
  516. int remote_is_configured(struct remote *remote, int in_repo)
  517. {
  518. if (!remote)
  519. return 0;
  520. if (in_repo)
  521. return remote->configured_in_repo;
  522. return !!remote->origin;
  523. }
  524. int for_each_remote(each_remote_fn fn, void *priv)
  525. {
  526. int i, result = 0;
  527. read_config();
  528. for (i = 0; i < remotes_nr && !result; i++) {
  529. struct remote *r = remotes[i];
  530. if (!r)
  531. continue;
  532. result = fn(r, priv);
  533. }
  534. return result;
  535. }
  536. static void handle_duplicate(struct ref *ref1, struct ref *ref2)
  537. {
  538. if (strcmp(ref1->name, ref2->name)) {
  539. if (ref1->fetch_head_status != FETCH_HEAD_IGNORE &&
  540. ref2->fetch_head_status != FETCH_HEAD_IGNORE) {
  541. die(_("Cannot fetch both %s and %s to %s"),
  542. ref1->name, ref2->name, ref2->peer_ref->name);
  543. } else if (ref1->fetch_head_status != FETCH_HEAD_IGNORE &&
  544. ref2->fetch_head_status == FETCH_HEAD_IGNORE) {
  545. warning(_("%s usually tracks %s, not %s"),
  546. ref2->peer_ref->name, ref2->name, ref1->name);
  547. } else if (ref1->fetch_head_status == FETCH_HEAD_IGNORE &&
  548. ref2->fetch_head_status == FETCH_HEAD_IGNORE) {
  549. die(_("%s tracks both %s and %s"),
  550. ref2->peer_ref->name, ref1->name, ref2->name);
  551. } else {
  552. /*
  553. * This last possibility doesn't occur because
  554. * FETCH_HEAD_IGNORE entries always appear at
  555. * the end of the list.
  556. */
  557. BUG("Internal error");
  558. }
  559. }
  560. free(ref2->peer_ref);
  561. free(ref2);
  562. }
  563. struct ref *ref_remove_duplicates(struct ref *ref_map)
  564. {
  565. struct string_list refs = STRING_LIST_INIT_NODUP;
  566. struct ref *retval = NULL;
  567. struct ref **p = &retval;
  568. while (ref_map) {
  569. struct ref *ref = ref_map;
  570. ref_map = ref_map->next;
  571. ref->next = NULL;
  572. if (!ref->peer_ref) {
  573. *p = ref;
  574. p = &ref->next;
  575. } else {
  576. struct string_list_item *item =
  577. string_list_insert(&refs, ref->peer_ref->name);
  578. if (item->util) {
  579. /* Entry already existed */
  580. handle_duplicate((struct ref *)item->util, ref);
  581. } else {
  582. *p = ref;
  583. p = &ref->next;
  584. item->util = ref;
  585. }
  586. }
  587. }
  588. string_list_clear(&refs, 0);
  589. return retval;
  590. }
  591. int remote_has_url(struct remote *remote, const char *url)
  592. {
  593. int i;
  594. for (i = 0; i < remote->url_nr; i++) {
  595. if (!strcmp(remote->url[i], url))
  596. return 1;
  597. }
  598. return 0;
  599. }
  600. static int match_name_with_pattern(const char *key, const char *name,
  601. const char *value, char **result)
  602. {
  603. const char *kstar = strchr(key, '*');
  604. size_t klen;
  605. size_t ksuffixlen;
  606. size_t namelen;
  607. int ret;
  608. if (!kstar)
  609. die(_("key '%s' of pattern had no '*'"), key);
  610. klen = kstar - key;
  611. ksuffixlen = strlen(kstar + 1);
  612. namelen = strlen(name);
  613. ret = !strncmp(name, key, klen) && namelen >= klen + ksuffixlen &&
  614. !memcmp(name + namelen - ksuffixlen, kstar + 1, ksuffixlen);
  615. if (ret && value) {
  616. struct strbuf sb = STRBUF_INIT;
  617. const char *vstar = strchr(value, '*');
  618. if (!vstar)
  619. die(_("value '%s' of pattern has no '*'"), value);
  620. strbuf_add(&sb, value, vstar - value);
  621. strbuf_add(&sb, name + klen, namelen - klen - ksuffixlen);
  622. strbuf_addstr(&sb, vstar + 1);
  623. *result = strbuf_detach(&sb, NULL);
  624. }
  625. return ret;
  626. }
  627. static void query_refspecs_multiple(struct refspec *rs,
  628. struct refspec_item *query,
  629. struct string_list *results)
  630. {
  631. int i;
  632. int find_src = !query->src;
  633. if (find_src && !query->dst)
  634. BUG("query_refspecs_multiple: need either src or dst");
  635. for (i = 0; i < rs->nr; i++) {
  636. struct refspec_item *refspec = &rs->items[i];
  637. const char *key = find_src ? refspec->dst : refspec->src;
  638. const char *value = find_src ? refspec->src : refspec->dst;
  639. const char *needle = find_src ? query->dst : query->src;
  640. char **result = find_src ? &query->src : &query->dst;
  641. if (!refspec->dst)
  642. continue;
  643. if (refspec->pattern) {
  644. if (match_name_with_pattern(key, needle, value, result))
  645. string_list_append_nodup(results, *result);
  646. } else if (!strcmp(needle, key)) {
  647. string_list_append(results, value);
  648. }
  649. }
  650. }
  651. int query_refspecs(struct refspec *rs, struct refspec_item *query)
  652. {
  653. int i;
  654. int find_src = !query->src;
  655. const char *needle = find_src ? query->dst : query->src;
  656. char **result = find_src ? &query->src : &query->dst;
  657. if (find_src && !query->dst)
  658. BUG("query_refspecs: need either src or dst");
  659. for (i = 0; i < rs->nr; i++) {
  660. struct refspec_item *refspec = &rs->items[i];
  661. const char *key = find_src ? refspec->dst : refspec->src;
  662. const char *value = find_src ? refspec->src : refspec->dst;
  663. if (!refspec->dst)
  664. continue;
  665. if (refspec->pattern) {
  666. if (match_name_with_pattern(key, needle, value, result)) {
  667. query->force = refspec->force;
  668. return 0;
  669. }
  670. } else if (!strcmp(needle, key)) {
  671. *result = xstrdup(value);
  672. query->force = refspec->force;
  673. return 0;
  674. }
  675. }
  676. return -1;
  677. }
  678. char *apply_refspecs(struct refspec *rs, const char *name)
  679. {
  680. struct refspec_item query;
  681. memset(&query, 0, sizeof(struct refspec_item));
  682. query.src = (char *)name;
  683. if (query_refspecs(rs, &query))
  684. return NULL;
  685. return query.dst;
  686. }
  687. int remote_find_tracking(struct remote *remote, struct refspec_item *refspec)
  688. {
  689. return query_refspecs(&remote->fetch, refspec);
  690. }
  691. static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,
  692. const char *name)
  693. {
  694. size_t len = strlen(name);
  695. struct ref *ref = xcalloc(1, st_add4(sizeof(*ref), prefixlen, len, 1));
  696. memcpy(ref->name, prefix, prefixlen);
  697. memcpy(ref->name + prefixlen, name, len);
  698. return ref;
  699. }
  700. struct ref *alloc_ref(const char *name)
  701. {
  702. return alloc_ref_with_prefix("", 0, name);
  703. }
  704. struct ref *copy_ref(const struct ref *ref)
  705. {
  706. struct ref *cpy;
  707. size_t len;
  708. if (!ref)
  709. return NULL;
  710. len = st_add3(sizeof(struct ref), strlen(ref->name), 1);
  711. cpy = xmalloc(len);
  712. memcpy(cpy, ref, len);
  713. cpy->next = NULL;
  714. cpy->symref = xstrdup_or_null(ref->symref);
  715. cpy->remote_status = xstrdup_or_null(ref->remote_status);
  716. cpy->peer_ref = copy_ref(ref->peer_ref);
  717. return cpy;
  718. }
  719. struct ref *copy_ref_list(const struct ref *ref)
  720. {
  721. struct ref *ret = NULL;
  722. struct ref **tail = &ret;
  723. while (ref) {
  724. *tail = copy_ref(ref);
  725. ref = ref->next;
  726. tail = &((*tail)->next);
  727. }
  728. return ret;
  729. }
  730. void free_one_ref(struct ref *ref)
  731. {
  732. if (!ref)
  733. return;
  734. free_one_ref(ref->peer_ref);
  735. free(ref->remote_status);
  736. free(ref->symref);
  737. free(ref);
  738. }
  739. void free_refs(struct ref *ref)
  740. {
  741. struct ref *next;
  742. while (ref) {
  743. next = ref->next;
  744. free_one_ref(ref);
  745. ref = next;
  746. }
  747. }
  748. int ref_compare_name(const void *va, const void *vb)
  749. {
  750. const struct ref *a = va, *b = vb;
  751. return strcmp(a->name, b->name);
  752. }
  753. static void *ref_list_get_next(const void *a)
  754. {
  755. return ((const struct ref *)a)->next;
  756. }
  757. static void ref_list_set_next(void *a, void *next)
  758. {
  759. ((struct ref *)a)->next = next;
  760. }
  761. void sort_ref_list(struct ref **l, int (*cmp)(const void *, const void *))
  762. {
  763. *l = llist_mergesort(*l, ref_list_get_next, ref_list_set_next, cmp);
  764. }
  765. int count_refspec_match(const char *pattern,
  766. struct ref *refs,
  767. struct ref **matched_ref)
  768. {
  769. int patlen = strlen(pattern);
  770. struct ref *matched_weak = NULL;
  771. struct ref *matched = NULL;
  772. int weak_match = 0;
  773. int match = 0;
  774. for (weak_match = match = 0; refs; refs = refs->next) {
  775. char *name = refs->name;
  776. int namelen = strlen(name);
  777. if (!refname_match(pattern, name))
  778. continue;
  779. /* A match is "weak" if it is with refs outside
  780. * heads or tags, and did not specify the pattern
  781. * in full (e.g. "refs/remotes/origin/master") or at
  782. * least from the toplevel (e.g. "remotes/origin/master");
  783. * otherwise "git push $URL master" would result in
  784. * ambiguity between remotes/origin/master and heads/master
  785. * at the remote site.
  786. */
  787. if (namelen != patlen &&
  788. patlen != namelen - 5 &&
  789. !starts_with(name, "refs/heads/") &&
  790. !starts_with(name, "refs/tags/")) {
  791. /* We want to catch the case where only weak
  792. * matches are found and there are multiple
  793. * matches, and where more than one strong
  794. * matches are found, as ambiguous. One
  795. * strong match with zero or more weak matches
  796. * are acceptable as a unique match.
  797. */
  798. matched_weak = refs;
  799. weak_match++;
  800. }
  801. else {
  802. matched = refs;
  803. match++;
  804. }
  805. }
  806. if (!matched) {
  807. if (matched_ref)
  808. *matched_ref = matched_weak;
  809. return weak_match;
  810. }
  811. else {
  812. if (matched_ref)
  813. *matched_ref = matched;
  814. return match;
  815. }
  816. }
  817. static void tail_link_ref(struct ref *ref, struct ref ***tail)
  818. {
  819. **tail = ref;
  820. while (ref->next)
  821. ref = ref->next;
  822. *tail = &ref->next;
  823. }
  824. static struct ref *alloc_delete_ref(void)
  825. {
  826. struct ref *ref = alloc_ref("(delete)");
  827. oidclr(&ref->new_oid);
  828. return ref;
  829. }
  830. static int try_explicit_object_name(const char *name,
  831. struct ref **match)
  832. {
  833. struct object_id oid;
  834. if (!*name) {
  835. if (match)
  836. *match = alloc_delete_ref();
  837. return 0;
  838. }
  839. if (get_oid(name, &oid))
  840. return -1;
  841. if (match) {
  842. *match = alloc_ref(name);
  843. oidcpy(&(*match)->new_oid, &oid);
  844. }
  845. return 0;
  846. }
  847. static struct ref *make_linked_ref(const char *name, struct ref ***tail)
  848. {
  849. struct ref *ret = alloc_ref(name);
  850. tail_link_ref(ret, tail);
  851. return ret;
  852. }
  853. static char *guess_ref(const char *name, struct ref *peer)
  854. {
  855. struct strbuf buf = STRBUF_INIT;
  856. const char *r = resolve_ref_unsafe(peer->name, RESOLVE_REF_READING,
  857. NULL, NULL);
  858. if (!r)
  859. return NULL;
  860. if (starts_with(r, "refs/heads/")) {
  861. strbuf_addstr(&buf, "refs/heads/");
  862. } else if (starts_with(r, "refs/tags/")) {
  863. strbuf_addstr(&buf, "refs/tags/");
  864. } else {
  865. return NULL;
  866. }
  867. strbuf_addstr(&buf, name);
  868. return strbuf_detach(&buf, NULL);
  869. }
  870. static int match_explicit_lhs(struct ref *src,
  871. struct refspec_item *rs,
  872. struct ref **match,
  873. int *allocated_match)
  874. {
  875. switch (count_refspec_match(rs->src, src, match)) {
  876. case 1:
  877. if (allocated_match)
  878. *allocated_match = 0;
  879. return 0;
  880. case 0:
  881. /* The source could be in the get_sha1() format
  882. * not a reference name. :refs/other is a
  883. * way to delete 'other' ref at the remote end.
  884. */
  885. if (try_explicit_object_name(rs->src, match) < 0)
  886. return error(_("src refspec %s does not match any"), rs->src);
  887. if (allocated_match)
  888. *allocated_match = 1;
  889. return 0;
  890. default:
  891. return error(_("src refspec %s matches more than one"), rs->src);
  892. }
  893. }
  894. static void show_push_unqualified_ref_name_error(const char *dst_value,
  895. const char *matched_src_name)
  896. {
  897. struct object_id oid;
  898. enum object_type type;
  899. /*
  900. * TRANSLATORS: "matches '%s'%" is the <dst> part of "git push
  901. * <remote> <src>:<dst>" push, and "being pushed ('%s')" is
  902. * the <src>.
  903. */
  904. error(_("The destination you provided is not a full refname (i.e.,\n"
  905. "starting with \"refs/\"). We tried to guess what you meant by:\n"
  906. "\n"
  907. "- Looking for a ref that matches '%s' on the remote side.\n"
  908. "- Checking if the <src> being pushed ('%s')\n"
  909. " is a ref in \"refs/{heads,tags}/\". If so we add a corresponding\n"
  910. " refs/{heads,tags}/ prefix on the remote side.\n"
  911. "\n"
  912. "Neither worked, so we gave up. You must fully qualify the ref."),
  913. dst_value, matched_src_name);
  914. if (!advice_push_unqualified_ref_name)
  915. return;
  916. if (get_oid(matched_src_name, &oid))
  917. BUG("'%s' is not a valid object, "
  918. "match_explicit_lhs() should catch this!",
  919. matched_src_name);
  920. type = oid_object_info(the_repository, &oid, NULL);
  921. if (type == OBJ_COMMIT) {
  922. advise(_("The <src> part of the refspec is a commit object.\n"
  923. "Did you mean to create a new branch by pushing to\n"
  924. "'%s:refs/heads/%s'?"),
  925. matched_src_name, dst_value);
  926. } else if (type == OBJ_TAG) {
  927. advise(_("The <src> part of the refspec is a tag object.\n"
  928. "Did you mean to create a new tag by pushing to\n"
  929. "'%s:refs/tags/%s'?"),
  930. matched_src_name, dst_value);
  931. } else if (type == OBJ_TREE) {
  932. advise(_("The <src> part of the refspec is a tree object.\n"
  933. "Did you mean to tag a new tree by pushing to\n"
  934. "'%s:refs/tags/%s'?"),
  935. matched_src_name, dst_value);
  936. } else if (type == OBJ_BLOB) {
  937. advise(_("The <src> part of the refspec is a blob object.\n"
  938. "Did you mean to tag a new blob by pushing to\n"
  939. "'%s:refs/tags/%s'?"),
  940. matched_src_name, dst_value);
  941. } else {
  942. BUG("'%s' should be commit/tag/tree/blob, is '%d'",
  943. matched_src_name, type);
  944. }
  945. }
  946. static int match_explicit(struct ref *src, struct ref *dst,
  947. struct ref ***dst_tail,
  948. struct refspec_item *rs)
  949. {
  950. struct ref *matched_src, *matched_dst;
  951. int allocated_src;
  952. const char *dst_value = rs->dst;
  953. char *dst_guess;
  954. if (rs->pattern || rs->matching)
  955. return 0;
  956. matched_src = matched_dst = NULL;
  957. if (match_explicit_lhs(src, rs, &matched_src, &allocated_src) < 0)
  958. return -1;
  959. if (!dst_value) {
  960. int flag;
  961. dst_value = resolve_ref_unsafe(matched_src->name,
  962. RESOLVE_REF_READING,
  963. NULL, &flag);
  964. if (!dst_value ||
  965. ((flag & REF_ISSYMREF) &&
  966. !starts_with(dst_value, "refs/heads/")))
  967. die(_("%s cannot be resolved to branch"),
  968. matched_src->name);
  969. }
  970. switch (count_refspec_match(dst_value, dst, &matched_dst)) {
  971. case 1:
  972. break;
  973. case 0:
  974. if (starts_with(dst_value, "refs/")) {
  975. matched_dst = make_linked_ref(dst_value, dst_tail);
  976. } else if (is_null_oid(&matched_src->new_oid)) {
  977. error(_("unable to delete '%s': remote ref does not exist"),
  978. dst_value);
  979. } else if ((dst_guess = guess_ref(dst_value, matched_src))) {
  980. matched_dst = make_linked_ref(dst_guess, dst_tail);
  981. free(dst_guess);
  982. } else {
  983. show_push_unqualified_ref_name_error(dst_value,
  984. matched_src->name);
  985. }
  986. break;
  987. default:
  988. matched_dst = NULL;
  989. error(_("dst refspec %s matches more than one"),
  990. dst_value);
  991. break;
  992. }
  993. if (!matched_dst)
  994. return -1;
  995. if (matched_dst->peer_ref)
  996. return error(_("dst ref %s receives from more than one src"),
  997. matched_dst->name);
  998. else {
  999. matched_dst->peer_ref = allocated_src ?
  1000. matched_src :
  1001. copy_ref(matched_src);
  1002. matched_dst->force = rs->force;
  1003. }
  1004. return 0;
  1005. }
  1006. static int match_explicit_refs(struct ref *src, struct ref *dst,
  1007. struct ref ***dst_tail, struct refspec *rs)
  1008. {
  1009. int i, errs;
  1010. for (i = errs = 0; i < rs->nr; i++)
  1011. errs += match_explicit(src, dst, dst_tail, &rs->items[i]);
  1012. return errs;
  1013. }
  1014. static char *get_ref_match(const struct refspec *rs, const struct ref *ref,
  1015. int send_mirror, int direction,
  1016. const struct refspec_item **ret_pat)
  1017. {
  1018. const struct refspec_item *pat;
  1019. char *name;
  1020. int i;
  1021. int matching_refs = -1;
  1022. for (i = 0; i < rs->nr; i++) {
  1023. const struct refspec_item *item = &rs->items[i];
  1024. if (item->matching &&
  1025. (matching_refs == -1 || item->force)) {
  1026. matching_refs = i;
  1027. continue;
  1028. }
  1029. if (item->pattern) {
  1030. const char *dst_side = item->dst ? item->dst : item->src;
  1031. int match;
  1032. if (direction == FROM_SRC)
  1033. match = match_name_with_pattern(item->src, ref->name, dst_side, &name);
  1034. else
  1035. match = match_name_with_pattern(dst_side, ref->name, item->src, &name);
  1036. if (match) {
  1037. matching_refs = i;
  1038. break;
  1039. }
  1040. }
  1041. }
  1042. if (matching_refs == -1)
  1043. return NULL;
  1044. pat = &rs->items[matching_refs];
  1045. if (pat->matching) {
  1046. /*
  1047. * "matching refs"; traditionally we pushed everything
  1048. * including refs outside refs/heads/ hierarchy, but
  1049. * that does not make much sense these days.
  1050. */
  1051. if (!send_mirror && !starts_with(ref->name, "refs/heads/"))
  1052. return NULL;
  1053. name = xstrdup(ref->name);
  1054. }
  1055. if (ret_pat)
  1056. *ret_pat = pat;
  1057. return name;
  1058. }
  1059. static struct ref **tail_ref(struct ref **head)
  1060. {
  1061. struct ref **tail = head;
  1062. while (*tail)
  1063. tail = &((*tail)->next);
  1064. return tail;
  1065. }
  1066. struct tips {
  1067. struct commit **tip;
  1068. int nr, alloc;
  1069. };
  1070. static void add_to_tips(struct tips *tips, const struct object_id *oid)
  1071. {
  1072. struct commit *commit;
  1073. if (is_null_oid(oid))
  1074. return;
  1075. commit = lookup_commit_reference_gently(the_repository, oid, 1);
  1076. if (!commit || (commit->object.flags & TMP_MARK))
  1077. return;
  1078. commit->object.flags |= TMP_MARK;
  1079. ALLOC_GROW(tips->tip, tips->nr + 1, tips->alloc);
  1080. tips->tip[tips->nr++] = commit;
  1081. }
  1082. static void add_missing_tags(struct ref *src, struct ref **dst, struct ref ***dst_tail)
  1083. {
  1084. struct string_list dst_tag = STRING_LIST_INIT_NODUP;
  1085. struct string_list src_tag = STRING_LIST_INIT_NODUP;
  1086. struct string_list_item *item;
  1087. struct ref *ref;
  1088. struct tips sent_tips;
  1089. /*
  1090. * Collect everything we know they would have at the end of
  1091. * this push, and collect all tags they have.
  1092. */
  1093. memset(&sent_tips, 0, sizeof(sent_tips));
  1094. for (ref = *dst; ref; ref = ref->next) {
  1095. if (ref->peer_ref &&
  1096. !is_null_oid(&ref->peer_ref->new_oid))
  1097. add_to_tips(&sent_tips, &ref->peer_ref->new_oid);
  1098. else
  1099. add_to_tips(&sent_tips, &ref->old_oid);
  1100. if (starts_with(ref->name, "refs/tags/"))
  1101. string_list_append(&dst_tag, ref->name);
  1102. }
  1103. clear_commit_marks_many(sent_tips.nr, sent_tips.tip, TMP_MARK);
  1104. string_list_sort(&dst_tag);
  1105. /* Collect tags they do not have. */
  1106. for (ref = src; ref; ref = ref->next) {
  1107. if (!starts_with(ref->name, "refs/tags/"))
  1108. continue; /* not a tag */
  1109. if (string_list_has_string(&dst_tag, ref->name))
  1110. continue; /* they already have it */
  1111. if (oid_object_info(the_repository, &ref->new_oid, NULL) != OBJ_TAG)
  1112. continue; /* be conservative */
  1113. item = string_list_append(&src_tag, ref->name);
  1114. item->util = ref;
  1115. }
  1116. string_list_clear(&dst_tag, 0);
  1117. /*
  1118. * At this point, src_tag lists tags that are missing from
  1119. * dst, and sent_tips lists the tips we are pushing or those
  1120. * that we know they already have. An element in the src_tag
  1121. * that is an ancestor of any of the sent_tips needs to be
  1122. * sent to the other side.
  1123. */
  1124. if (sent_tips.nr) {
  1125. const int reachable_flag = 1;
  1126. struct commit_list *found_commits;
  1127. struct commit **src_commits;
  1128. int nr_src_commits = 0, alloc_src_commits = 16;
  1129. ALLOC_ARRAY(src_commits, alloc_src_commits);
  1130. for_each_string_list_item(item, &src_tag) {
  1131. struct ref *ref = item->util;
  1132. struct commit *commit;
  1133. if (is_null_oid(&ref->new_oid))
  1134. continue;
  1135. commit = lookup_commit_reference_gently(the_repository,
  1136. &ref->new_oid,
  1137. 1);
  1138. if (!commit)
  1139. /* not pushing a commit, which is not an error */
  1140. continue;
  1141. ALLOC_GROW(src_commits, nr_src_commits + 1, alloc_src_commits);
  1142. src_commits[nr_src_commits++] = commit;
  1143. }
  1144. found_commits = get_reachable_subset(sent_tips.tip, sent_tips.nr,
  1145. src_commits, nr_src_commits,
  1146. reachable_flag);
  1147. for_each_string_list_item(item, &src_tag) {
  1148. struct ref *dst_ref;
  1149. struct ref *ref = item->util;
  1150. struct commit *commit;
  1151. if (is_null_oid(&ref->new_oid))
  1152. continue;
  1153. commit = lookup_commit_reference_gently(the_repository,
  1154. &ref->new_oid,
  1155. 1);
  1156. if (!commit)
  1157. /* not pushing a commit, which is not an error */
  1158. continue;
  1159. /*
  1160. * Is this tag, which they do not have, reachable from
  1161. * any of the commits we are sending?
  1162. */
  1163. if (!(commit->object.flags & reachable_flag))
  1164. continue;
  1165. /* Add it in */
  1166. dst_ref = make_linked_ref(ref->name, dst_tail);
  1167. oidcpy(&dst_ref->new_oid, &ref->new_oid);
  1168. dst_ref->peer_ref = copy_ref(ref);
  1169. }
  1170. clear_commit_marks_many(nr_src_commits, src_commits, reachable_flag);
  1171. free(src_commits);
  1172. free_commit_list(found_commits);
  1173. }
  1174. string_list_clear(&src_tag, 0);
  1175. free(sent_tips.tip);
  1176. }
  1177. struct ref *find_ref_by_name(const struct ref *list, const char *name)
  1178. {
  1179. for ( ; list; list = list->next)
  1180. if (!strcmp(list->name, name))
  1181. return (struct ref *)list;
  1182. return NULL;
  1183. }
  1184. static void prepare_ref_index(struct string_list *ref_index, struct ref *ref)
  1185. {
  1186. for ( ; ref; ref = ref->next)
  1187. string_list_append_nodup(ref_index, ref->name)->util = ref;
  1188. string_list_sort(ref_index);
  1189. }
  1190. /*
  1191. * Given only the set of local refs, sanity-check the set of push
  1192. * refspecs. We can't catch all errors that match_push_refs would,
  1193. * but we can catch some errors early before even talking to the
  1194. * remote side.
  1195. */
  1196. int check_push_refs(struct ref *src, struct refspec *rs)
  1197. {
  1198. int ret = 0;
  1199. int i;
  1200. for (i = 0; i < rs->nr; i++) {
  1201. struct refspec_item *item = &rs->items[i];
  1202. if (item->pattern || item->matching)
  1203. continue;
  1204. ret |= match_explicit_lhs(src, item, NULL, NULL);
  1205. }
  1206. return ret;
  1207. }
  1208. /*
  1209. * Given the set of refs the local repository has, the set of refs the
  1210. * remote repository has, and the refspec used for push, determine
  1211. * what remote refs we will update and with what value by setting
  1212. * peer_ref (which object is being pushed) and force (if the push is
  1213. * forced) in elements of "dst". The function may add new elements to
  1214. * dst (e.g. pushing to a new branch, done in match_explicit_refs).
  1215. */
  1216. int match_push_refs(struct ref *src, struct ref **dst,
  1217. struct refspec *rs, int flags)
  1218. {
  1219. int send_all = flags & MATCH_REFS_ALL;
  1220. int send_mirror = flags & MATCH_REFS_MIRROR;
  1221. int send_prune = flags & MATCH_REFS_PRUNE;
  1222. int errs;
  1223. struct ref *ref, **dst_tail = tail_ref(dst);
  1224. struct string_list dst_ref_index = STRING_LIST_INIT_NODUP;
  1225. /* If no refspec is provided, use the default ":" */
  1226. if (!rs->nr)
  1227. refspec_append(rs, ":");
  1228. errs = match_explicit_refs(src, *dst, &dst_tail, rs);
  1229. /* pick the remainder */
  1230. for (ref = src; ref; ref = ref->next) {
  1231. struct string_list_item *dst_item;
  1232. struct ref *dst_peer;
  1233. const struct refspec_item *pat = NULL;
  1234. char *dst_name;
  1235. dst_name = get_ref_match(rs, ref, send_mirror, FROM_SRC, &pat);
  1236. if (!dst_name)
  1237. continue;
  1238. if (!dst_ref_index.nr)
  1239. prepare_ref_index(&dst_ref_index, *dst);
  1240. dst_item = string_list_lookup(&dst_ref_index, dst_name);
  1241. dst_peer = dst_item ? dst_item->util : NULL;
  1242. if (dst_peer) {
  1243. if (dst_peer->peer_ref)
  1244. /* We're already sending something to this ref. */
  1245. goto free_name;
  1246. } else {
  1247. if (pat->matching && !(send_all || send_mirror))
  1248. /*
  1249. * Remote doesn't have it, and we have no
  1250. * explicit pattern, and we don't have
  1251. * --all or --mirror.
  1252. */
  1253. goto free_name;
  1254. /* Create a new one and link it */
  1255. dst_peer = make_linked_ref(dst_name, &dst_tail);
  1256. oidcpy(&dst_peer->new_oid, &ref->new_oid);
  1257. string_list_insert(&dst_ref_index,
  1258. dst_peer->name)->util = dst_peer;
  1259. }
  1260. dst_peer->peer_ref = copy_ref(ref);
  1261. dst_peer->force = pat->force;
  1262. free_name:
  1263. free(dst_name);
  1264. }
  1265. string_list_clear(&dst_ref_index, 0);
  1266. if (flags & MATCH_REFS_FOLLOW_TAGS)
  1267. add_missing_tags(src, dst, &dst_tail);
  1268. if (send_prune) {
  1269. struct string_list src_ref_index = STRING_LIST_INIT_NODUP;
  1270. /* check for missing refs on the remote */
  1271. for (ref = *dst; ref; ref = ref->next) {
  1272. char *src_name;
  1273. if (ref->peer_ref)
  1274. /* We're already sending something to this ref. */
  1275. continue;
  1276. src_name = get_ref_match(rs, ref, send_mirror, FROM_DST, NULL);
  1277. if (src_name) {
  1278. if (!src_ref_index.nr)
  1279. prepare_ref_index(&src_ref_index, src);
  1280. if (!string_list_has_string(&src_ref_index,
  1281. src_name))
  1282. ref->peer_ref = alloc_delete_ref();
  1283. free(src_name);
  1284. }
  1285. }
  1286. string_list_clear(&src_ref_index, 0);
  1287. }
  1288. if (errs)
  1289. return -1;
  1290. return 0;
  1291. }
  1292. void set_ref_status_for_push(struct ref *remote_refs, int send_mirror,
  1293. int force_update)
  1294. {
  1295. struct ref *ref;
  1296. for (ref = remote_refs; ref; ref = ref->next) {
  1297. int force_ref_update = ref->force || force_update;
  1298. int reject_reason = 0;
  1299. if (ref->peer_ref)
  1300. oidcpy(&ref->new_oid, &ref->peer_ref->new_oid);
  1301. else if (!send_mirror)
  1302. continue;
  1303. ref->deletion = is_null_oid(&ref->new_oid);
  1304. if (!ref->deletion &&
  1305. oideq(&ref->old_oid, &ref->new_oid)) {
  1306. ref->status = REF_STATUS_UPTODATE;
  1307. continue;
  1308. }
  1309. /*
  1310. * If the remote ref has moved and is now different
  1311. * from what we expect, reject any push.
  1312. *
  1313. * It also is an error if the user told us to check
  1314. * with the remote-tracking branch to find the value
  1315. * to expect, but we did not have such a tracking
  1316. * branch.
  1317. */
  1318. if (ref->expect_old_sha1) {
  1319. if (!oideq(&ref->old_oid, &ref->old_oid_expect))
  1320. reject_reason = REF_STATUS_REJECT_STALE;
  1321. else
  1322. /* If the ref isn't stale then force the update. */
  1323. force_ref_update = 1;
  1324. }
  1325. /*
  1326. * If the update isn't already rejected then check
  1327. * the usual "must fast-forward" rules.
  1328. *
  1329. * Decide whether an individual refspec A:B can be
  1330. * pushed. The push will succeed if any of the
  1331. * following are true:
  1332. *
  1333. * (1) the remote reference B does not exist
  1334. *
  1335. * (2) the remote reference B is being removed (i.e.,
  1336. * pushing :B where no source is specified)
  1337. *
  1338. * (3) the destination is not under refs/tags/, and
  1339. * if the old and new value is a commit, the new
  1340. * is a descendant of the old.
  1341. *
  1342. * (4) it is forced using the +A:B notation, or by
  1343. * passing the --force argument
  1344. */
  1345. if (!reject_reason && !ref->deletion && !is_null_oid(&ref->old_oid)) {
  1346. if (starts_with(ref->name, "refs/tags/"))
  1347. reject_reason = REF_STATUS_REJECT_ALREADY_EXISTS;
  1348. else if (!has_object_file(&ref->old_oid))
  1349. reject_reason = REF_STATUS_REJECT_FETCH_FIRST;
  1350. else if (!lookup_commit_reference_gently(the_repository, &ref->old_oid, 1) ||
  1351. !lookup_commit_reference_gently(the_repository, &ref->new_oid, 1))
  1352. reject_reason = REF_STATUS_REJECT_NEEDS_FORCE;
  1353. else if (!ref_newer(&ref->new_oid, &ref->old_oid))
  1354. reject_reason = REF_STATUS_REJECT_NONFASTFORWARD;
  1355. }
  1356. /*
  1357. * "--force" will defeat any rejection implemented
  1358. * by the rules above.
  1359. */
  1360. if (!force_ref_update)
  1361. ref->status = reject_reason;
  1362. else if (reject_reason)
  1363. ref->forced_update = 1;
  1364. }
  1365. }
  1366. static void set_merge(struct branch *ret)
  1367. {
  1368. struct remote *remote;
  1369. char *ref;
  1370. struct object_id oid;
  1371. int i;
  1372. if (!ret)
  1373. return; /* no branch */
  1374. if (ret->merge)
  1375. return; /* already run */
  1376. if (!ret->remote_name || !ret->merge_nr) {
  1377. /*
  1378. * no merge config; let's make sure we don't confuse callers
  1379. * with a non-zero merge_nr but a NULL merge
  1380. */
  1381. ret->merge_nr = 0;
  1382. return;
  1383. }
  1384. remote = remote_get(ret->remote_name);
  1385. ret->merge = xcalloc(ret->merge_nr, sizeof(*ret->merge));
  1386. for (i = 0; i < ret->merge_nr; i++) {
  1387. ret->merge[i] = xcalloc(1, sizeof(**ret->merge));
  1388. ret->merge[i]->src = xstrdup(ret->merge_name[i]);
  1389. if (!remote_find_tracking(remote, ret->merge[i]) ||
  1390. strcmp(ret->remote_name, "."))
  1391. continue;
  1392. if (dwim_ref(ret->merge_name[i], strlen(ret->merge_name[i]),
  1393. &oid, &ref) == 1)
  1394. ret->merge[i]->dst = ref;
  1395. else
  1396. ret->merge[i]->dst = xstrdup(ret->merge_name[i]);
  1397. }
  1398. }
  1399. struct branch *branch_get(const char *name)
  1400. {
  1401. struct branch *ret;
  1402. read_config();
  1403. if (!name || !*name || !strcmp(name, "HEAD"))
  1404. ret = current_branch;
  1405. else
  1406. ret = make_branch(name, 0);
  1407. set_merge(ret);
  1408. return ret;
  1409. }
  1410. int branch_has_merge_config(struct branch *branch)
  1411. {
  1412. return branch && !!branch->merge;
  1413. }
  1414. int branch_merge_matches(struct branch *branch,
  1415. int i,
  1416. const char *refname)
  1417. {
  1418. if (!branch || i < 0 || i >= branch->merge_nr)
  1419. return 0;
  1420. return refname_match(branch->merge[i]->src, refname);
  1421. }
  1422. __attribute__((format (printf,2,3)))
  1423. static const char *error_buf(struct strbuf *err, const char *fmt, ...)
  1424. {
  1425. if (err) {
  1426. va_list ap;
  1427. va_start(ap, fmt);
  1428. strbuf_vaddf(err, fmt, ap);
  1429. va_end(ap);
  1430. }
  1431. return NULL;
  1432. }
  1433. const char *branch_get_upstream(struct branch *branch, struct strbuf *err)
  1434. {
  1435. if (!branch)
  1436. return error_buf(err, _("HEAD does not point to a branch"));
  1437. if (!branch->merge || !branch->merge[0]) {
  1438. /*
  1439. * no merge config; is it because the user didn't define any,
  1440. * or because it is not a real branch, and get_branch
  1441. * auto-vivified it?
  1442. */
  1443. if (!ref_exists(branch->refname))
  1444. return error_buf(err, _("no such branch: '%s'"),
  1445. branch->name);
  1446. return error_buf(err,
  1447. _("no upstream configured for branch '%s'"),
  1448. branch->name);
  1449. }
  1450. if (!branch->merge[0]->dst)
  1451. return error_buf(err,
  1452. _("upstream branch '%s' not stored as a remote-tracking branch"),
  1453. branch->merge[0]->src);
  1454. return branch->merge[0]->dst;
  1455. }
  1456. static const char *tracking_for_push_dest(struct remote *remote,
  1457. const char *refname,
  1458. struct strbuf *err)
  1459. {
  1460. char *ret;
  1461. ret = apply_refspecs(&remote->fetch, refname);
  1462. if (!ret)
  1463. return error_buf(err,
  1464. _("push destination '%s' on remote '%s' has no local tracking branch"),
  1465. refname, remote->name);
  1466. return ret;
  1467. }
  1468. static const char *branch_get_push_1(struct branch *branch, struct strbuf *err)
  1469. {
  1470. struct remote *remote;
  1471. remote = remote_get(pushremote_for_branch(branch, NULL));
  1472. if (!remote)
  1473. return error_buf(err,
  1474. _("branch '%s' has no remote for pushing"),
  1475. branch->name);
  1476. if (remote->push.nr) {
  1477. char *dst;
  1478. const char *ret;
  1479. dst = apply_refspecs(&remote->push, branch->refname);
  1480. if (!dst)
  1481. return error_buf(err,
  1482. _("push refspecs for '%s' do not include '%s'"),
  1483. remote->name, branch->name);
  1484. ret = tracking_for_push_dest(remote, dst, err);
  1485. free(dst);
  1486. return ret;
  1487. }
  1488. if (remote->mirror)
  1489. return tracking_for_push_dest(remote, branch->refname, err);
  1490. switch (push_default) {
  1491. case PUSH_DEFAULT_NOTHING:
  1492. return error_buf(err, _("push has no destination (push.default is 'nothing')"));
  1493. case PUSH_DEFAULT_MATCHING:
  1494. case PUSH_DEFAULT_CURRENT:
  1495. return tracking_for_push_dest(remote, branch->refname, err);
  1496. case PUSH_DEFAULT_UPSTREAM:
  1497. return branch_get_upstream(branch, err);
  1498. case PUSH_DEFAULT_UNSPECIFIED:
  1499. case PUSH_DEFAULT_SIMPLE:
  1500. {
  1501. const char *up, *cur;
  1502. up = branch_get_upstream(branch, err);
  1503. if (!up)
  1504. return NULL;
  1505. cur = tracking_for_push_dest(remote, branch->refname, err);
  1506. if (!cur)
  1507. return NULL;
  1508. if (strcmp(cur, up))
  1509. return error_buf(err,
  1510. _("cannot resolve 'simple' push to a single destination"));
  1511. return cur;
  1512. }
  1513. }
  1514. BUG("unhandled push situation");
  1515. }
  1516. const char *branch_get_push(struct branch *branch, struct strbuf *err)
  1517. {
  1518. if (!branch)
  1519. return error_buf(err, _("HEAD does not point to a branch"));
  1520. if (!branch->push_tracking_ref)
  1521. branch->push_tracking_ref = branch_get_push_1(branch, err);
  1522. return branch->push_tracking_ref;
  1523. }
  1524. static int ignore_symref_update(const char *refname)
  1525. {
  1526. int flag;
  1527. if (!resolve_ref_unsafe(refname, 0, NULL, &flag))
  1528. return 0; /* non-existing refs are OK */
  1529. return (flag & REF_ISSYMREF);
  1530. }
  1531. /*
  1532. * Create and return a list of (struct ref) consisting of copies of
  1533. * each remote_ref that matches refspec. refspec must be a pattern.
  1534. * Fill in the copies' peer_ref to describe the local tracking refs to
  1535. * which they map. Omit any references that would map to an existing
  1536. * local symbolic ref.
  1537. */
  1538. static struct ref *get_expanded_map(const struct ref *remote_refs,
  1539. const struct refspec_item *refspec)
  1540. {
  1541. const struct ref *ref;
  1542. struct ref *ret = NULL;
  1543. struct ref **tail = &ret;
  1544. for (ref = remote_refs; ref; ref = ref->next) {
  1545. char *expn_name = NULL;
  1546. if (strchr(ref->name, '^'))
  1547. continue; /* a dereference item */
  1548. if (match_name_with_pattern(refspec->src, ref->name,
  1549. refspec->dst, &expn_name) &&
  1550. !ignore_symref_update(expn_name)) {
  1551. struct ref *cpy = copy_ref(ref);
  1552. cpy->peer_ref = alloc_ref(expn_name);
  1553. if (refspec->force)
  1554. cpy->peer_ref->force = 1;
  1555. *tail = cpy;
  1556. tail = &cpy->next;
  1557. }
  1558. free(expn_name);
  1559. }
  1560. return ret;
  1561. }
  1562. static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name)
  1563. {
  1564. const struct ref *ref;
  1565. const struct ref *best_match = NULL;
  1566. int best_score = 0;
  1567. for (ref = refs; ref; ref = ref->next) {
  1568. int score = refname_match(name, ref->name);
  1569. if (best_score < score) {
  1570. best_match = ref;
  1571. best_score = score;
  1572. }
  1573. }
  1574. return best_match;
  1575. }
  1576. struct ref *get_remote_ref(const struct ref *remote_refs, const char *name)
  1577. {
  1578. const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name);
  1579. if (!ref)
  1580. return NULL;
  1581. return copy_ref(ref);
  1582. }
  1583. static struct ref *get_local_ref(const char *name)
  1584. {
  1585. if (!name || name[0] == '\0')
  1586. return NULL;
  1587. if (starts_with(name, "refs/"))
  1588. return alloc_ref(name);
  1589. if (starts_with(name, "heads/") ||
  1590. starts_with(name, "tags/") ||
  1591. starts_with(name, "remotes/"))
  1592. return alloc_ref_with_prefix("refs/", 5, name);
  1593. return alloc_ref_with_prefix("refs/heads/", 11, name);
  1594. }
  1595. int get_fetch_map(const struct ref *remote_refs,
  1596. const struct refspec_item *refspec,
  1597. struct ref ***tail,
  1598. int missing_ok)
  1599. {
  1600. struct ref *ref_map, **rmp;
  1601. if (refspec->pattern) {
  1602. ref_map = get_expanded_map(remote_refs, refspec);
  1603. } else {
  1604. const char *name = refspec->src[0] ? refspec->src : "HEAD";
  1605. if (refspec->exact_sha1) {
  1606. ref_map = alloc_ref(name);
  1607. get_oid_hex(name, &ref_map->old_oid);
  1608. ref_map->exact_oid = 1;
  1609. } else {
  1610. ref_map = get_remote_ref(remote_refs, name);
  1611. }
  1612. if (!missing_ok && !ref_map)
  1613. die(_("couldn't find remote ref %s"), name);
  1614. if (ref_map) {
  1615. ref_map->peer_ref = get_local_ref(refspec->dst);
  1616. if (ref_map->peer_ref && refspec->force)
  1617. ref_map->peer_ref->force = 1;
  1618. }
  1619. }
  1620. for (rmp = &ref_map; *rmp; ) {
  1621. if ((*rmp)->peer_ref) {
  1622. if (!starts_with((*rmp)->peer_ref->name, "refs/") ||
  1623. check_refname_format((*rmp)->peer_ref->name, 0)) {
  1624. struct ref *ignore = *rmp;
  1625. error(_("* Ignoring funny ref '%s' locally"),
  1626. (*rmp)->peer_ref->name);
  1627. *rmp = (*rmp)->next;
  1628. free(ignore->peer_ref);
  1629. free(ignore);
  1630. continue;
  1631. }
  1632. }
  1633. rmp = &((*rmp)->next);
  1634. }
  1635. if (ref_map)
  1636. tail_link_ref(ref_map, tail);
  1637. return 0;
  1638. }
  1639. int resolve_remote_symref(struct ref *ref, struct ref *list)
  1640. {
  1641. if (!ref->symref)
  1642. return 0;
  1643. for (; list; list = list->next)
  1644. if (!strcmp(ref->symref, list->name)) {
  1645. oidcpy(&ref->old_oid, &list->old_oid);
  1646. return 0;
  1647. }
  1648. return 1;
  1649. }
  1650. /*
  1651. * Compute the commit ahead/behind values for the pair branch_name, base.
  1652. *
  1653. * If abf is AHEAD_BEHIND_FULL, compute the full ahead/behind and return the
  1654. * counts in *num_ours and *num_theirs. If abf is AHEAD_BEHIND_QUICK, skip
  1655. * the (potentially expensive) a/b computation (*num_ours and *num_theirs are
  1656. * set to zero).
  1657. *
  1658. * Returns -1 if num_ours and num_theirs could not be filled in (e.g., ref
  1659. * does not exist). Returns 0 if the commits are identical. Returns 1 if
  1660. * commits are different.
  1661. */
  1662. static int stat_branch_pair(const char *branch_name, const char *base,
  1663. int *num_ours, int *num_theirs,
  1664. enum ahead_behind_flags abf)
  1665. {
  1666. struct object_id oid;
  1667. struct commit *ours, *theirs;
  1668. struct rev_info revs;
  1669. struct argv_array argv = ARGV_ARRAY_INIT;
  1670. /* Cannot stat if what we used to build on no longer exists */
  1671. if (read_ref(base, &oid))
  1672. return -1;
  1673. theirs = lookup_commit_reference(the_repository, &oid);
  1674. if (!theirs)
  1675. return -1;
  1676. if (read_ref(branch_name, &oid))
  1677. return -1;
  1678. ours = lookup_commit_reference(the_repository, &oid);
  1679. if (!ours)
  1680. return -1;
  1681. *num_theirs = *num_ours = 0;
  1682. /* are we the same? */
  1683. if (theirs == ours)
  1684. return 0;
  1685. if (abf == AHEAD_BEHIND_QUICK)
  1686. return 1;
  1687. if (abf != AHEAD_BEHIND_FULL)
  1688. BUG("stat_branch_pair: invalid abf '%d'", abf);
  1689. /* Run "rev-list --left-right ours...theirs" internally... */
  1690. argv_array_push(&argv, ""); /* ignored */
  1691. argv_array_push(&argv, "--left-right");
  1692. argv_array_pushf(&argv, "%s...%s",
  1693. oid_to_hex(&ours->object.oid),
  1694. oid_to_hex(&theirs->object.oid));
  1695. argv_array_push(&argv, "--");
  1696. repo_init_revisions(the_repository, &revs, NULL);
  1697. setup_revisions(argv.argc, argv.argv, &revs, NULL);
  1698. if (prepare_revision_walk(&revs))
  1699. die(_("revision walk setup failed"));
  1700. /* ... and count the commits on each side. */
  1701. while (1) {
  1702. struct commit *c = get_revision(&revs);
  1703. if (!c)
  1704. break;
  1705. if (c->object.flags & SYMMETRIC_LEFT)
  1706. (*num_ours)++;
  1707. else
  1708. (*num_theirs)++;
  1709. }
  1710. /* clear object flags smudged by the above traversal */
  1711. clear_commit_marks(ours, ALL_REV_FLAGS);
  1712. clear_commit_marks(theirs, ALL_REV_FLAGS);
  1713. argv_array_clear(&argv);
  1714. return 1;
  1715. }
  1716. /*
  1717. * Lookup the tracking branch for the given branch and if present, optionally
  1718. * compute the commit ahead/behind values for the pair.
  1719. *
  1720. * If for_push is true, the tracking branch refers to the push branch,
  1721. * otherwise it refers to the upstream branch.
  1722. *
  1723. * The name of the tracking branch (or NULL if it is not defined) is
  1724. * returned via *tracking_name, if it is not itself NULL.
  1725. *
  1726. * If abf is AHEAD_BEHIND_FULL, compute the full ahead/behind and return the
  1727. * counts in *num_ours and *num_theirs. If abf is AHEAD_BEHIND_QUICK, skip
  1728. * the (potentially expensive) a/b computation (*num_ours and *num_theirs are
  1729. * set to zero).
  1730. *
  1731. * Returns -1 if num_ours and num_theirs could not be filled in (e.g., no
  1732. * upstream defined, or ref does not exist). Returns 0 if the commits are
  1733. * identical. Returns 1 if commits are different.
  1734. */
  1735. int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
  1736. const char **tracking_name, int for_push,
  1737. enum ahead_behind_flags abf)
  1738. {
  1739. const char *base;
  1740. /* Cannot stat unless we are marked to build on top of somebody else. */
  1741. base = for_push ? branch_get_push(branch, NULL) :
  1742. branch_get_upstream(branch, NULL);
  1743. if (tracking_name)
  1744. *tracking_name = base;
  1745. if (!base)
  1746. return -1;
  1747. return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
  1748. }
  1749. /*
  1750. * Return true when there is anything to report, otherwise false.
  1751. */
  1752. int format_tracking_info(struct branch *branch, struct strbuf *sb,
  1753. enum ahead_behind_flags abf)
  1754. {
  1755. int ours, theirs, sti;
  1756. const char *full_base;
  1757. char *base;
  1758. int upstream_is_gone = 0;
  1759. sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
  1760. if (sti < 0) {
  1761. if (!full_base)
  1762. return 0;
  1763. upstream_is_gone = 1;
  1764. }
  1765. base = shorten_unambiguous_ref(full_base, 0);
  1766. if (upstream_is_gone) {
  1767. strbuf_addf(sb,
  1768. _("Your branch is based on '%s', but the upstream is gone.\n"),
  1769. base);
  1770. if (advice_status_hints)
  1771. strbuf_addstr(sb,
  1772. _(" (use \"git branch --unset-upstream\" to fixup)\n"));
  1773. } else if (!sti) {
  1774. strbuf_addf(sb,
  1775. _("Your branch is up to date with '%s'.\n"),
  1776. base);
  1777. } else if (abf == AHEAD_BEHIND_QUICK) {
  1778. strbuf_addf(sb,
  1779. _("Your branch and '%s' refer to different commits.\n"),
  1780. base);
  1781. if (advice_status_hints)
  1782. strbuf_addf(sb, _(" (use \"%s\" for details)\n"),
  1783. "git status --ahead-behind");
  1784. } else if (!theirs) {
  1785. strbuf_addf(sb,
  1786. Q_("Your branch is ahead of '%s' by %d commit.\n",
  1787. "Your branch is ahead of '%s' by %d commits.\n",
  1788. ours),
  1789. base, ours);
  1790. if (advice_status_hints)
  1791. strbuf_addstr(sb,
  1792. _(" (use \"git push\" to publish your local commits)\n"));
  1793. } else if (!ours) {
  1794. strbuf_addf(sb,
  1795. Q_("Your branch is behind '%s' by %d commit, "
  1796. "and can be fast-forwarded.\n",
  1797. "Your branch is behind '%s' by %d commits, "
  1798. "and can be fast-forwarded.\n",
  1799. theirs),
  1800. base, theirs);
  1801. if (advice_status_hints)
  1802. strbuf_addstr(sb,
  1803. _(" (use \"git pull\" to update your local branch)\n"));
  1804. } else {
  1805. strbuf_addf(sb,
  1806. Q_("Your branch and '%s' have diverged,\n"
  1807. "and have %d and %d different commit each, "
  1808. "respectively.\n",
  1809. "Your branch and '%s' have diverged,\n"
  1810. "and have %d and %d different commits each, "
  1811. "respectively.\n",
  1812. ours + theirs),
  1813. base, ours, theirs);
  1814. if (advice_status_hints)
  1815. strbuf_addstr(sb,
  1816. _(" (use \"git pull\" to merge the remote branch into yours)\n"));
  1817. }
  1818. free(base);
  1819. return 1;
  1820. }
  1821. static int one_local_ref(const char *refname, const struct object_id *oid,
  1822. int flag, void *cb_data)
  1823. {
  1824. struct ref ***local_tail = cb_data;
  1825. struct ref *ref;
  1826. /* we already know it starts with refs/ to get here */
  1827. if (check_refname_format(refname + 5, 0))
  1828. return 0;
  1829. ref = alloc_ref(refname);
  1830. oidcpy(&ref->new_oid, oid);
  1831. **local_tail = ref;
  1832. *local_tail = &ref->next;
  1833. return 0;
  1834. }
  1835. struct ref *get_local_heads(void)
  1836. {
  1837. struct ref *local_refs = NULL, **local_tail = &local_refs;
  1838. for_each_ref(one_local_ref, &local_tail);
  1839. return local_refs;
  1840. }
  1841. struct ref *guess_remote_head(const struct ref *head,
  1842. const struct ref *refs,
  1843. int all)
  1844. {
  1845. const struct ref *r;
  1846. struct ref *list = NULL;
  1847. struct ref **tail = &list;
  1848. if (!head)
  1849. return NULL;
  1850. /*
  1851. * Some transports support directly peeking at
  1852. * where HEAD points; if that is the case, then
  1853. * we don't have to guess.
  1854. */
  1855. if (head->symref)
  1856. return copy_ref(find_ref_by_name(refs, head->symref));
  1857. /* If refs/heads/master could be right, it is. */
  1858. if (!all) {
  1859. r = find_ref_by_name(refs, "refs/heads/master");
  1860. if (r && oideq(&r->old_oid, &head->old_oid))
  1861. return copy_ref(r);
  1862. }
  1863. /* Look for another ref that points there */
  1864. for (r = refs; r; r = r->next) {
  1865. if (r != head &&
  1866. starts_with(r->name, "refs/heads/") &&
  1867. oideq(&r->old_oid, &head->old_oid)) {
  1868. *tail = copy_ref(r);
  1869. tail = &((*tail)->next);
  1870. if (!all)
  1871. break;
  1872. }
  1873. }
  1874. return list;
  1875. }
  1876. struct stale_heads_info {
  1877. struct string_list *ref_names;
  1878. struct ref **stale_refs_tail;
  1879. struct refspec *rs;
  1880. };
  1881. static int get_stale_heads_cb(const char *refname, const struct object_id *oid,
  1882. int flags, void *cb_data)
  1883. {
  1884. struct stale_heads_info *info = cb_data;
  1885. struct string_list matches = STRING_LIST_INIT_DUP;
  1886. struct refspec_item query;
  1887. int i, stale = 1;
  1888. memset(&query, 0, sizeof(struct refspec_item));
  1889. query.dst = (char *)refname;
  1890. query_refspecs_multiple(info->rs, &query, &matches);
  1891. if (matches.nr == 0)
  1892. goto clean_exit; /* No matches */
  1893. /*
  1894. * If we did find a suitable refspec and it's not a symref and
  1895. * it's not in the list of refs that currently exist in that
  1896. * remote, we consider it to be stale. In order to deal with
  1897. * overlapping refspecs, we need to go over all of the
  1898. * matching refs.
  1899. */
  1900. if (flags & REF_ISSYMREF)
  1901. goto clean_exit;
  1902. for (i = 0; stale && i < matches.nr; i++)
  1903. if (string_list_has_string(info->ref_names, matches.items[i].string))
  1904. stale = 0;
  1905. if (stale) {
  1906. struct ref *ref = make_linked_ref(refname, &info->stale_refs_tail);
  1907. oidcpy(&ref->new_oid, oid);
  1908. }
  1909. clean_exit:
  1910. string_list_clear(&matches, 0);
  1911. return 0;
  1912. }
  1913. struct ref *get_stale_heads(struct refspec *rs, struct ref *fetch_map)
  1914. {
  1915. struct ref *ref, *stale_refs = NULL;
  1916. struct string_list ref_names = STRING_LIST_INIT_NODUP;
  1917. struct stale_heads_info info;
  1918. info.ref_names = &ref_names;
  1919. info.stale_refs_tail = &stale_refs;
  1920. info.rs = rs;
  1921. for (ref = fetch_map; ref; ref = ref->next)
  1922. string_list_append(&ref_names, ref->name);
  1923. string_list_sort(&ref_names);
  1924. for_each_ref(get_stale_heads_cb, &info);
  1925. string_list_clear(&ref_names, 0);
  1926. return stale_refs;
  1927. }
  1928. /*
  1929. * Compare-and-swap
  1930. */
  1931. static void clear_cas_option(struct push_cas_option *cas)
  1932. {
  1933. int i;
  1934. for (i = 0; i < cas->nr; i++)
  1935. free(cas->entry[i].refname);
  1936. free(cas->entry);
  1937. memset(cas, 0, sizeof(*cas));
  1938. }
  1939. static struct push_cas *add_cas_entry(struct push_cas_option *cas,
  1940. const char *refname,
  1941. size_t refnamelen)
  1942. {
  1943. struct push_cas *entry;
  1944. ALLOC_GROW(cas->entry, cas->nr + 1, cas->alloc);
  1945. entry = &cas->entry[cas->nr++];
  1946. memset(entry, 0, sizeof(*entry));
  1947. entry->refname = xmemdupz(refname, refnamelen);
  1948. return entry;
  1949. }
  1950. static int parse_push_cas_option(struct push_cas_option *cas, const char *arg, int unset)
  1951. {
  1952. const char *colon;
  1953. struct push_cas *entry;
  1954. if (unset) {
  1955. /* "--no-<option>" */
  1956. clear_cas_option(cas);
  1957. return 0;
  1958. }
  1959. if (!arg) {
  1960. /* just "--<option>" */
  1961. cas->use_tracking_for_rest = 1;
  1962. return 0;
  1963. }
  1964. /* "--<option>=refname" or "--<option>=refname:value" */
  1965. colon = strchrnul(arg, ':');
  1966. entry = add_cas_entry(cas, arg, colon - arg);
  1967. if (!*colon)
  1968. entry->use_tracking = 1;
  1969. else if (!colon[1])
  1970. oidclr(&entry->expect);
  1971. else if (get_oid(colon + 1, &entry->expect))
  1972. return error(_("cannot parse expected object name '%s'"),
  1973. colon + 1);
  1974. return 0;
  1975. }
  1976. int parseopt_push_cas_option(const struct option *opt, const char *arg, int unset)
  1977. {
  1978. return parse_push_cas_option(opt->value, arg, unset);
  1979. }
  1980. int is_empty_cas(const struct push_cas_option *cas)
  1981. {
  1982. return !cas->use_tracking_for_rest && !cas->nr;
  1983. }
  1984. /*
  1985. * Look at remote.fetch refspec and see if we have a remote
  1986. * tracking branch for the refname there. Fill its current
  1987. * value in sha1[].
  1988. * If we cannot do so, return negative to signal an error.
  1989. */
  1990. static int remote_tracking(struct remote *remote, const char *refname,
  1991. struct object_id *oid)
  1992. {
  1993. char *dst;
  1994. dst = apply_refspecs(&remote->fetch, refname);
  1995. if (!dst)
  1996. return -1; /* no tracking ref for refname at remote */
  1997. if (read_ref(dst, oid))
  1998. return -1; /* we know what the tracking ref is but we cannot read it */
  1999. return 0;
  2000. }
  2001. static void apply_cas(struct push_cas_option *cas,
  2002. struct remote *remote,
  2003. struct ref *ref)
  2004. {
  2005. int i;
  2006. /* Find an explicit --<option>=<name>[:<value>] entry */
  2007. for (i = 0; i < cas->nr; i++) {
  2008. struct push_cas *entry = &cas->entry[i];
  2009. if (!refname_match(entry->refname, ref->name))
  2010. continue;
  2011. ref->expect_old_sha1 = 1;
  2012. if (!entry->use_tracking)
  2013. oidcpy(&ref->old_oid_expect, &entry->expect);
  2014. else if (remote_tracking(remote, ref->name, &ref->old_oid_expect))
  2015. oidclr(&ref->old_oid_expect);
  2016. return;
  2017. }
  2018. /* Are we using "--<option>" to cover all? */
  2019. if (!cas->use_tracking_for_rest)
  2020. return;
  2021. ref->expect_old_sha1 = 1;
  2022. if (remote_tracking(remote, ref->name, &ref->old_oid_expect))
  2023. oidclr(&ref->old_oid_expect);
  2024. }
  2025. void apply_push_cas(struct push_cas_option *cas,
  2026. struct remote *remote,
  2027. struct ref *remote_refs)
  2028. {
  2029. struct ref *ref;
  2030. for (ref = remote_refs; ref; ref = ref->next)
  2031. apply_cas(cas, remote, ref);
  2032. }