remote.c 61 KB

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