sha1-name.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955
  1. #include "cache.h"
  2. #include "config.h"
  3. #include "tag.h"
  4. #include "commit.h"
  5. #include "tree.h"
  6. #include "blob.h"
  7. #include "tree-walk.h"
  8. #include "refs.h"
  9. #include "remote.h"
  10. #include "dir.h"
  11. #include "oid-array.h"
  12. #include "packfile.h"
  13. #include "object-store.h"
  14. #include "repository.h"
  15. #include "submodule.h"
  16. #include "midx.h"
  17. #include "commit-reach.h"
  18. static int get_oid_oneline(struct repository *r, const char *, struct object_id *, struct commit_list *);
  19. typedef int (*disambiguate_hint_fn)(struct repository *, const struct object_id *, void *);
  20. struct disambiguate_state {
  21. int len; /* length of prefix in hex chars */
  22. char hex_pfx[GIT_MAX_HEXSZ + 1];
  23. struct object_id bin_pfx;
  24. struct repository *repo;
  25. disambiguate_hint_fn fn;
  26. void *cb_data;
  27. struct object_id candidate;
  28. unsigned candidate_exists:1;
  29. unsigned candidate_checked:1;
  30. unsigned candidate_ok:1;
  31. unsigned disambiguate_fn_used:1;
  32. unsigned ambiguous:1;
  33. unsigned always_call_fn:1;
  34. };
  35. static void update_candidates(struct disambiguate_state *ds, const struct object_id *current)
  36. {
  37. if (ds->always_call_fn) {
  38. ds->ambiguous = ds->fn(ds->repo, current, ds->cb_data) ? 1 : 0;
  39. return;
  40. }
  41. if (!ds->candidate_exists) {
  42. /* this is the first candidate */
  43. oidcpy(&ds->candidate, current);
  44. ds->candidate_exists = 1;
  45. return;
  46. } else if (oideq(&ds->candidate, current)) {
  47. /* the same as what we already have seen */
  48. return;
  49. }
  50. if (!ds->fn) {
  51. /* cannot disambiguate between ds->candidate and current */
  52. ds->ambiguous = 1;
  53. return;
  54. }
  55. if (!ds->candidate_checked) {
  56. ds->candidate_ok = ds->fn(ds->repo, &ds->candidate, ds->cb_data);
  57. ds->disambiguate_fn_used = 1;
  58. ds->candidate_checked = 1;
  59. }
  60. if (!ds->candidate_ok) {
  61. /* discard the candidate; we know it does not satisfy fn */
  62. oidcpy(&ds->candidate, current);
  63. ds->candidate_checked = 0;
  64. return;
  65. }
  66. /* if we reach this point, we know ds->candidate satisfies fn */
  67. if (ds->fn(ds->repo, current, ds->cb_data)) {
  68. /*
  69. * if both current and candidate satisfy fn, we cannot
  70. * disambiguate.
  71. */
  72. ds->candidate_ok = 0;
  73. ds->ambiguous = 1;
  74. }
  75. /* otherwise, current can be discarded and candidate is still good */
  76. }
  77. static int match_sha(unsigned, const unsigned char *, const unsigned char *);
  78. static void find_short_object_filename(struct disambiguate_state *ds)
  79. {
  80. struct object_directory *odb;
  81. for (odb = ds->repo->objects->odb; odb && !ds->ambiguous; odb = odb->next) {
  82. int pos;
  83. struct oid_array *loose_objects;
  84. loose_objects = odb_loose_cache(odb, &ds->bin_pfx);
  85. pos = oid_array_lookup(loose_objects, &ds->bin_pfx);
  86. if (pos < 0)
  87. pos = -1 - pos;
  88. while (!ds->ambiguous && pos < loose_objects->nr) {
  89. const struct object_id *oid;
  90. oid = loose_objects->oid + pos;
  91. if (!match_sha(ds->len, ds->bin_pfx.hash, oid->hash))
  92. break;
  93. update_candidates(ds, oid);
  94. pos++;
  95. }
  96. }
  97. }
  98. static int match_sha(unsigned len, const unsigned char *a, const unsigned char *b)
  99. {
  100. do {
  101. if (*a != *b)
  102. return 0;
  103. a++;
  104. b++;
  105. len -= 2;
  106. } while (len > 1);
  107. if (len)
  108. if ((*a ^ *b) & 0xf0)
  109. return 0;
  110. return 1;
  111. }
  112. static void unique_in_midx(struct multi_pack_index *m,
  113. struct disambiguate_state *ds)
  114. {
  115. uint32_t num, i, first = 0;
  116. const struct object_id *current = NULL;
  117. num = m->num_objects;
  118. if (!num)
  119. return;
  120. bsearch_midx(&ds->bin_pfx, m, &first);
  121. /*
  122. * At this point, "first" is the location of the lowest object
  123. * with an object name that could match "bin_pfx". See if we have
  124. * 0, 1 or more objects that actually match(es).
  125. */
  126. for (i = first; i < num && !ds->ambiguous; i++) {
  127. struct object_id oid;
  128. current = nth_midxed_object_oid(&oid, m, i);
  129. if (!match_sha(ds->len, ds->bin_pfx.hash, current->hash))
  130. break;
  131. update_candidates(ds, current);
  132. }
  133. }
  134. static void unique_in_pack(struct packed_git *p,
  135. struct disambiguate_state *ds)
  136. {
  137. uint32_t num, i, first = 0;
  138. if (p->multi_pack_index)
  139. return;
  140. if (open_pack_index(p) || !p->num_objects)
  141. return;
  142. num = p->num_objects;
  143. bsearch_pack(&ds->bin_pfx, p, &first);
  144. /*
  145. * At this point, "first" is the location of the lowest object
  146. * with an object name that could match "bin_pfx". See if we have
  147. * 0, 1 or more objects that actually match(es).
  148. */
  149. for (i = first; i < num && !ds->ambiguous; i++) {
  150. struct object_id oid;
  151. nth_packed_object_id(&oid, p, i);
  152. if (!match_sha(ds->len, ds->bin_pfx.hash, oid.hash))
  153. break;
  154. update_candidates(ds, &oid);
  155. }
  156. }
  157. static void find_short_packed_object(struct disambiguate_state *ds)
  158. {
  159. struct multi_pack_index *m;
  160. struct packed_git *p;
  161. for (m = get_multi_pack_index(ds->repo); m && !ds->ambiguous;
  162. m = m->next)
  163. unique_in_midx(m, ds);
  164. for (p = get_packed_git(ds->repo); p && !ds->ambiguous;
  165. p = p->next)
  166. unique_in_pack(p, ds);
  167. }
  168. static int finish_object_disambiguation(struct disambiguate_state *ds,
  169. struct object_id *oid)
  170. {
  171. if (ds->ambiguous)
  172. return SHORT_NAME_AMBIGUOUS;
  173. if (!ds->candidate_exists)
  174. return MISSING_OBJECT;
  175. if (!ds->candidate_checked)
  176. /*
  177. * If this is the only candidate, there is no point
  178. * calling the disambiguation hint callback.
  179. *
  180. * On the other hand, if the current candidate
  181. * replaced an earlier candidate that did _not_ pass
  182. * the disambiguation hint callback, then we do have
  183. * more than one objects that match the short name
  184. * given, so we should make sure this one matches;
  185. * otherwise, if we discovered this one and the one
  186. * that we previously discarded in the reverse order,
  187. * we would end up showing different results in the
  188. * same repository!
  189. */
  190. ds->candidate_ok = (!ds->disambiguate_fn_used ||
  191. ds->fn(ds->repo, &ds->candidate, ds->cb_data));
  192. if (!ds->candidate_ok)
  193. return SHORT_NAME_AMBIGUOUS;
  194. oidcpy(oid, &ds->candidate);
  195. return 0;
  196. }
  197. static int disambiguate_commit_only(struct repository *r,
  198. const struct object_id *oid,
  199. void *cb_data_unused)
  200. {
  201. int kind = oid_object_info(r, oid, NULL);
  202. return kind == OBJ_COMMIT;
  203. }
  204. static int disambiguate_committish_only(struct repository *r,
  205. const struct object_id *oid,
  206. void *cb_data_unused)
  207. {
  208. struct object *obj;
  209. int kind;
  210. kind = oid_object_info(r, oid, NULL);
  211. if (kind == OBJ_COMMIT)
  212. return 1;
  213. if (kind != OBJ_TAG)
  214. return 0;
  215. /* We need to do this the hard way... */
  216. obj = deref_tag(r, parse_object(r, oid), NULL, 0);
  217. if (obj && obj->type == OBJ_COMMIT)
  218. return 1;
  219. return 0;
  220. }
  221. static int disambiguate_tree_only(struct repository *r,
  222. const struct object_id *oid,
  223. void *cb_data_unused)
  224. {
  225. int kind = oid_object_info(r, oid, NULL);
  226. return kind == OBJ_TREE;
  227. }
  228. static int disambiguate_treeish_only(struct repository *r,
  229. const struct object_id *oid,
  230. void *cb_data_unused)
  231. {
  232. struct object *obj;
  233. int kind;
  234. kind = oid_object_info(r, oid, NULL);
  235. if (kind == OBJ_TREE || kind == OBJ_COMMIT)
  236. return 1;
  237. if (kind != OBJ_TAG)
  238. return 0;
  239. /* We need to do this the hard way... */
  240. obj = deref_tag(r, parse_object(r, oid), NULL, 0);
  241. if (obj && (obj->type == OBJ_TREE || obj->type == OBJ_COMMIT))
  242. return 1;
  243. return 0;
  244. }
  245. static int disambiguate_blob_only(struct repository *r,
  246. const struct object_id *oid,
  247. void *cb_data_unused)
  248. {
  249. int kind = oid_object_info(r, oid, NULL);
  250. return kind == OBJ_BLOB;
  251. }
  252. static disambiguate_hint_fn default_disambiguate_hint;
  253. int set_disambiguate_hint_config(const char *var, const char *value)
  254. {
  255. static const struct {
  256. const char *name;
  257. disambiguate_hint_fn fn;
  258. } hints[] = {
  259. { "none", NULL },
  260. { "commit", disambiguate_commit_only },
  261. { "committish", disambiguate_committish_only },
  262. { "tree", disambiguate_tree_only },
  263. { "treeish", disambiguate_treeish_only },
  264. { "blob", disambiguate_blob_only }
  265. };
  266. int i;
  267. if (!value)
  268. return config_error_nonbool(var);
  269. for (i = 0; i < ARRAY_SIZE(hints); i++) {
  270. if (!strcasecmp(value, hints[i].name)) {
  271. default_disambiguate_hint = hints[i].fn;
  272. return 0;
  273. }
  274. }
  275. return error("unknown hint type for '%s': %s", var, value);
  276. }
  277. static int init_object_disambiguation(struct repository *r,
  278. const char *name, int len,
  279. struct disambiguate_state *ds)
  280. {
  281. int i;
  282. if (len < MINIMUM_ABBREV || len > the_hash_algo->hexsz)
  283. return -1;
  284. memset(ds, 0, sizeof(*ds));
  285. for (i = 0; i < len ;i++) {
  286. unsigned char c = name[i];
  287. unsigned char val;
  288. if (c >= '0' && c <= '9')
  289. val = c - '0';
  290. else if (c >= 'a' && c <= 'f')
  291. val = c - 'a' + 10;
  292. else if (c >= 'A' && c <='F') {
  293. val = c - 'A' + 10;
  294. c -= 'A' - 'a';
  295. }
  296. else
  297. return -1;
  298. ds->hex_pfx[i] = c;
  299. if (!(i & 1))
  300. val <<= 4;
  301. ds->bin_pfx.hash[i >> 1] |= val;
  302. }
  303. ds->len = len;
  304. ds->hex_pfx[len] = '\0';
  305. ds->repo = r;
  306. prepare_alt_odb(r);
  307. return 0;
  308. }
  309. static int show_ambiguous_object(const struct object_id *oid, void *data)
  310. {
  311. const struct disambiguate_state *ds = data;
  312. struct strbuf desc = STRBUF_INIT;
  313. int type;
  314. if (ds->fn && !ds->fn(ds->repo, oid, ds->cb_data))
  315. return 0;
  316. type = oid_object_info(ds->repo, oid, NULL);
  317. if (type == OBJ_COMMIT) {
  318. struct commit *commit = lookup_commit(ds->repo, oid);
  319. if (commit) {
  320. struct pretty_print_context pp = {0};
  321. pp.date_mode.type = DATE_SHORT;
  322. format_commit_message(commit, " %ad - %s", &desc, &pp);
  323. }
  324. } else if (type == OBJ_TAG) {
  325. struct tag *tag = lookup_tag(ds->repo, oid);
  326. if (!parse_tag(tag) && tag->tag)
  327. strbuf_addf(&desc, " %s", tag->tag);
  328. }
  329. advise(" %s %s%s",
  330. repo_find_unique_abbrev(ds->repo, oid, DEFAULT_ABBREV),
  331. type_name(type) ? type_name(type) : "unknown type",
  332. desc.buf);
  333. strbuf_release(&desc);
  334. return 0;
  335. }
  336. static int collect_ambiguous(const struct object_id *oid, void *data)
  337. {
  338. oid_array_append(data, oid);
  339. return 0;
  340. }
  341. static int repo_collect_ambiguous(struct repository *r,
  342. const struct object_id *oid,
  343. void *data)
  344. {
  345. return collect_ambiguous(oid, data);
  346. }
  347. static int sort_ambiguous(const void *a, const void *b, void *ctx)
  348. {
  349. struct repository *sort_ambiguous_repo = ctx;
  350. int a_type = oid_object_info(sort_ambiguous_repo, a, NULL);
  351. int b_type = oid_object_info(sort_ambiguous_repo, b, NULL);
  352. int a_type_sort;
  353. int b_type_sort;
  354. /*
  355. * Sorts by hash within the same object type, just as
  356. * oid_array_for_each_unique() would do.
  357. */
  358. if (a_type == b_type)
  359. return oidcmp(a, b);
  360. /*
  361. * Between object types show tags, then commits, and finally
  362. * trees and blobs.
  363. *
  364. * The object_type enum is commit, tree, blob, tag, but we
  365. * want tag, commit, tree blob. Cleverly (perhaps too
  366. * cleverly) do that with modulus, since the enum assigns 1 to
  367. * commit, so tag becomes 0.
  368. */
  369. a_type_sort = a_type % 4;
  370. b_type_sort = b_type % 4;
  371. return a_type_sort > b_type_sort ? 1 : -1;
  372. }
  373. static void sort_ambiguous_oid_array(struct repository *r, struct oid_array *a)
  374. {
  375. QSORT_S(a->oid, a->nr, sort_ambiguous, r);
  376. }
  377. static enum get_oid_result get_short_oid(struct repository *r,
  378. const char *name, int len,
  379. struct object_id *oid,
  380. unsigned flags)
  381. {
  382. int status;
  383. struct disambiguate_state ds;
  384. int quietly = !!(flags & GET_OID_QUIETLY);
  385. if (init_object_disambiguation(r, name, len, &ds) < 0)
  386. return -1;
  387. if (HAS_MULTI_BITS(flags & GET_OID_DISAMBIGUATORS))
  388. BUG("multiple get_short_oid disambiguator flags");
  389. if (flags & GET_OID_COMMIT)
  390. ds.fn = disambiguate_commit_only;
  391. else if (flags & GET_OID_COMMITTISH)
  392. ds.fn = disambiguate_committish_only;
  393. else if (flags & GET_OID_TREE)
  394. ds.fn = disambiguate_tree_only;
  395. else if (flags & GET_OID_TREEISH)
  396. ds.fn = disambiguate_treeish_only;
  397. else if (flags & GET_OID_BLOB)
  398. ds.fn = disambiguate_blob_only;
  399. else
  400. ds.fn = default_disambiguate_hint;
  401. find_short_object_filename(&ds);
  402. find_short_packed_object(&ds);
  403. status = finish_object_disambiguation(&ds, oid);
  404. /*
  405. * If we didn't find it, do the usual reprepare() slow-path,
  406. * since the object may have recently been added to the repository
  407. * or migrated from loose to packed.
  408. */
  409. if (status == MISSING_OBJECT) {
  410. reprepare_packed_git(r);
  411. find_short_object_filename(&ds);
  412. find_short_packed_object(&ds);
  413. status = finish_object_disambiguation(&ds, oid);
  414. }
  415. if (!quietly && (status == SHORT_NAME_AMBIGUOUS)) {
  416. struct oid_array collect = OID_ARRAY_INIT;
  417. error(_("short SHA1 %s is ambiguous"), ds.hex_pfx);
  418. /*
  419. * We may still have ambiguity if we simply saw a series of
  420. * candidates that did not satisfy our hint function. In
  421. * that case, we still want to show them, so disable the hint
  422. * function entirely.
  423. */
  424. if (!ds.ambiguous)
  425. ds.fn = NULL;
  426. advise(_("The candidates are:"));
  427. repo_for_each_abbrev(r, ds.hex_pfx, collect_ambiguous, &collect);
  428. sort_ambiguous_oid_array(r, &collect);
  429. if (oid_array_for_each(&collect, show_ambiguous_object, &ds))
  430. BUG("show_ambiguous_object shouldn't return non-zero");
  431. oid_array_clear(&collect);
  432. }
  433. return status;
  434. }
  435. int repo_for_each_abbrev(struct repository *r, const char *prefix,
  436. each_abbrev_fn fn, void *cb_data)
  437. {
  438. struct oid_array collect = OID_ARRAY_INIT;
  439. struct disambiguate_state ds;
  440. int ret;
  441. if (init_object_disambiguation(r, prefix, strlen(prefix), &ds) < 0)
  442. return -1;
  443. ds.always_call_fn = 1;
  444. ds.fn = repo_collect_ambiguous;
  445. ds.cb_data = &collect;
  446. find_short_object_filename(&ds);
  447. find_short_packed_object(&ds);
  448. ret = oid_array_for_each_unique(&collect, fn, cb_data);
  449. oid_array_clear(&collect);
  450. return ret;
  451. }
  452. /*
  453. * Return the slot of the most-significant bit set in "val". There are various
  454. * ways to do this quickly with fls() or __builtin_clzl(), but speed is
  455. * probably not a big deal here.
  456. */
  457. static unsigned msb(unsigned long val)
  458. {
  459. unsigned r = 0;
  460. while (val >>= 1)
  461. r++;
  462. return r;
  463. }
  464. struct min_abbrev_data {
  465. unsigned int init_len;
  466. unsigned int cur_len;
  467. char *hex;
  468. struct repository *repo;
  469. const struct object_id *oid;
  470. };
  471. static inline char get_hex_char_from_oid(const struct object_id *oid,
  472. unsigned int pos)
  473. {
  474. static const char hex[] = "0123456789abcdef";
  475. if ((pos & 1) == 0)
  476. return hex[oid->hash[pos >> 1] >> 4];
  477. else
  478. return hex[oid->hash[pos >> 1] & 0xf];
  479. }
  480. static int extend_abbrev_len(const struct object_id *oid, void *cb_data)
  481. {
  482. struct min_abbrev_data *mad = cb_data;
  483. unsigned int i = mad->init_len;
  484. while (mad->hex[i] && mad->hex[i] == get_hex_char_from_oid(oid, i))
  485. i++;
  486. if (i < GIT_MAX_RAWSZ && i >= mad->cur_len)
  487. mad->cur_len = i + 1;
  488. return 0;
  489. }
  490. static int repo_extend_abbrev_len(struct repository *r,
  491. const struct object_id *oid,
  492. void *cb_data)
  493. {
  494. return extend_abbrev_len(oid, cb_data);
  495. }
  496. static void find_abbrev_len_for_midx(struct multi_pack_index *m,
  497. struct min_abbrev_data *mad)
  498. {
  499. int match = 0;
  500. uint32_t num, first = 0;
  501. struct object_id oid;
  502. const struct object_id *mad_oid;
  503. if (!m->num_objects)
  504. return;
  505. num = m->num_objects;
  506. mad_oid = mad->oid;
  507. match = bsearch_midx(mad_oid, m, &first);
  508. /*
  509. * first is now the position in the packfile where we would insert
  510. * mad->hash if it does not exist (or the position of mad->hash if
  511. * it does exist). Hence, we consider a maximum of two objects
  512. * nearby for the abbreviation length.
  513. */
  514. mad->init_len = 0;
  515. if (!match) {
  516. if (nth_midxed_object_oid(&oid, m, first))
  517. extend_abbrev_len(&oid, mad);
  518. } else if (first < num - 1) {
  519. if (nth_midxed_object_oid(&oid, m, first + 1))
  520. extend_abbrev_len(&oid, mad);
  521. }
  522. if (first > 0) {
  523. if (nth_midxed_object_oid(&oid, m, first - 1))
  524. extend_abbrev_len(&oid, mad);
  525. }
  526. mad->init_len = mad->cur_len;
  527. }
  528. static void find_abbrev_len_for_pack(struct packed_git *p,
  529. struct min_abbrev_data *mad)
  530. {
  531. int match = 0;
  532. uint32_t num, first = 0;
  533. struct object_id oid;
  534. const struct object_id *mad_oid;
  535. if (p->multi_pack_index)
  536. return;
  537. if (open_pack_index(p) || !p->num_objects)
  538. return;
  539. num = p->num_objects;
  540. mad_oid = mad->oid;
  541. match = bsearch_pack(mad_oid, p, &first);
  542. /*
  543. * first is now the position in the packfile where we would insert
  544. * mad->hash if it does not exist (or the position of mad->hash if
  545. * it does exist). Hence, we consider a maximum of two objects
  546. * nearby for the abbreviation length.
  547. */
  548. mad->init_len = 0;
  549. if (!match) {
  550. if (!nth_packed_object_id(&oid, p, first))
  551. extend_abbrev_len(&oid, mad);
  552. } else if (first < num - 1) {
  553. if (!nth_packed_object_id(&oid, p, first + 1))
  554. extend_abbrev_len(&oid, mad);
  555. }
  556. if (first > 0) {
  557. if (!nth_packed_object_id(&oid, p, first - 1))
  558. extend_abbrev_len(&oid, mad);
  559. }
  560. mad->init_len = mad->cur_len;
  561. }
  562. static void find_abbrev_len_packed(struct min_abbrev_data *mad)
  563. {
  564. struct multi_pack_index *m;
  565. struct packed_git *p;
  566. for (m = get_multi_pack_index(mad->repo); m; m = m->next)
  567. find_abbrev_len_for_midx(m, mad);
  568. for (p = get_packed_git(mad->repo); p; p = p->next)
  569. find_abbrev_len_for_pack(p, mad);
  570. }
  571. int repo_find_unique_abbrev_r(struct repository *r, char *hex,
  572. const struct object_id *oid, int len)
  573. {
  574. struct disambiguate_state ds;
  575. struct min_abbrev_data mad;
  576. struct object_id oid_ret;
  577. const unsigned hexsz = r->hash_algo->hexsz;
  578. if (len < 0) {
  579. unsigned long count = repo_approximate_object_count(r);
  580. /*
  581. * Add one because the MSB only tells us the highest bit set,
  582. * not including the value of all the _other_ bits (so "15"
  583. * is only one off of 2^4, but the MSB is the 3rd bit.
  584. */
  585. len = msb(count) + 1;
  586. /*
  587. * We now know we have on the order of 2^len objects, which
  588. * expects a collision at 2^(len/2). But we also care about hex
  589. * chars, not bits, and there are 4 bits per hex. So all
  590. * together we need to divide by 2 and round up.
  591. */
  592. len = DIV_ROUND_UP(len, 2);
  593. /*
  594. * For very small repos, we stick with our regular fallback.
  595. */
  596. if (len < FALLBACK_DEFAULT_ABBREV)
  597. len = FALLBACK_DEFAULT_ABBREV;
  598. }
  599. oid_to_hex_r(hex, oid);
  600. if (len == hexsz || !len)
  601. return hexsz;
  602. mad.repo = r;
  603. mad.init_len = len;
  604. mad.cur_len = len;
  605. mad.hex = hex;
  606. mad.oid = oid;
  607. find_abbrev_len_packed(&mad);
  608. if (init_object_disambiguation(r, hex, mad.cur_len, &ds) < 0)
  609. return -1;
  610. ds.fn = repo_extend_abbrev_len;
  611. ds.always_call_fn = 1;
  612. ds.cb_data = (void *)&mad;
  613. find_short_object_filename(&ds);
  614. (void)finish_object_disambiguation(&ds, &oid_ret);
  615. hex[mad.cur_len] = 0;
  616. return mad.cur_len;
  617. }
  618. const char *repo_find_unique_abbrev(struct repository *r,
  619. const struct object_id *oid,
  620. int len)
  621. {
  622. static int bufno;
  623. static char hexbuffer[4][GIT_MAX_HEXSZ + 1];
  624. char *hex = hexbuffer[bufno];
  625. bufno = (bufno + 1) % ARRAY_SIZE(hexbuffer);
  626. repo_find_unique_abbrev_r(r, hex, oid, len);
  627. return hex;
  628. }
  629. static int ambiguous_path(const char *path, int len)
  630. {
  631. int slash = 1;
  632. int cnt;
  633. for (cnt = 0; cnt < len; cnt++) {
  634. switch (*path++) {
  635. case '\0':
  636. break;
  637. case '/':
  638. if (slash)
  639. break;
  640. slash = 1;
  641. continue;
  642. case '.':
  643. continue;
  644. default:
  645. slash = 0;
  646. continue;
  647. }
  648. break;
  649. }
  650. return slash;
  651. }
  652. static inline int at_mark(const char *string, int len,
  653. const char **suffix, int nr)
  654. {
  655. int i;
  656. for (i = 0; i < nr; i++) {
  657. int suffix_len = strlen(suffix[i]);
  658. if (suffix_len <= len
  659. && !strncasecmp(string, suffix[i], suffix_len))
  660. return suffix_len;
  661. }
  662. return 0;
  663. }
  664. static inline int upstream_mark(const char *string, int len)
  665. {
  666. const char *suffix[] = { "@{upstream}", "@{u}" };
  667. return at_mark(string, len, suffix, ARRAY_SIZE(suffix));
  668. }
  669. static inline int push_mark(const char *string, int len)
  670. {
  671. const char *suffix[] = { "@{push}" };
  672. return at_mark(string, len, suffix, ARRAY_SIZE(suffix));
  673. }
  674. static enum get_oid_result get_oid_1(struct repository *r, const char *name, int len, struct object_id *oid, unsigned lookup_flags);
  675. static int interpret_nth_prior_checkout(struct repository *r, const char *name, int namelen, struct strbuf *buf);
  676. static int get_oid_basic(struct repository *r, const char *str, int len,
  677. struct object_id *oid, unsigned int flags)
  678. {
  679. static const char *warn_msg = "refname '%.*s' is ambiguous.";
  680. static const char *object_name_msg = N_(
  681. "Git normally never creates a ref that ends with 40 hex characters\n"
  682. "because it will be ignored when you just specify 40-hex. These refs\n"
  683. "may be created by mistake. For example,\n"
  684. "\n"
  685. " git switch -c $br $(git rev-parse ...)\n"
  686. "\n"
  687. "where \"$br\" is somehow empty and a 40-hex ref is created. Please\n"
  688. "examine these refs and maybe delete them. Turn this message off by\n"
  689. "running \"git config advice.objectNameWarning false\"");
  690. struct object_id tmp_oid;
  691. char *real_ref = NULL;
  692. int refs_found = 0;
  693. int at, reflog_len, nth_prior = 0;
  694. if (len == r->hash_algo->hexsz && !get_oid_hex(str, oid)) {
  695. if (warn_ambiguous_refs && warn_on_object_refname_ambiguity) {
  696. refs_found = repo_dwim_ref(r, str, len, &tmp_oid, &real_ref, 0);
  697. if (refs_found > 0) {
  698. warning(warn_msg, len, str);
  699. if (advice_object_name_warning)
  700. fprintf(stderr, "%s\n", _(object_name_msg));
  701. }
  702. free(real_ref);
  703. }
  704. return 0;
  705. }
  706. /* basic@{time or number or -number} format to query ref-log */
  707. reflog_len = at = 0;
  708. if (len && str[len-1] == '}') {
  709. for (at = len-4; at >= 0; at--) {
  710. if (str[at] == '@' && str[at+1] == '{') {
  711. if (str[at+2] == '-') {
  712. if (at != 0)
  713. /* @{-N} not at start */
  714. return -1;
  715. nth_prior = 1;
  716. continue;
  717. }
  718. if (!upstream_mark(str + at, len - at) &&
  719. !push_mark(str + at, len - at)) {
  720. reflog_len = (len-1) - (at+2);
  721. len = at;
  722. }
  723. break;
  724. }
  725. }
  726. }
  727. /* Accept only unambiguous ref paths. */
  728. if (len && ambiguous_path(str, len))
  729. return -1;
  730. if (nth_prior) {
  731. struct strbuf buf = STRBUF_INIT;
  732. int detached;
  733. if (interpret_nth_prior_checkout(r, str, len, &buf) > 0) {
  734. detached = (buf.len == r->hash_algo->hexsz && !get_oid_hex(buf.buf, oid));
  735. strbuf_release(&buf);
  736. if (detached)
  737. return 0;
  738. }
  739. }
  740. if (!len && reflog_len)
  741. /* allow "@{...}" to mean the current branch reflog */
  742. refs_found = repo_dwim_ref(r, "HEAD", 4, oid, &real_ref, 0);
  743. else if (reflog_len)
  744. refs_found = repo_dwim_log(r, str, len, oid, &real_ref);
  745. else
  746. refs_found = repo_dwim_ref(r, str, len, oid, &real_ref, 0);
  747. if (!refs_found)
  748. return -1;
  749. if (warn_ambiguous_refs && !(flags & GET_OID_QUIETLY) &&
  750. (refs_found > 1 ||
  751. !get_short_oid(r, str, len, &tmp_oid, GET_OID_QUIETLY)))
  752. warning(warn_msg, len, str);
  753. if (reflog_len) {
  754. int nth, i;
  755. timestamp_t at_time;
  756. timestamp_t co_time;
  757. int co_tz, co_cnt;
  758. /* Is it asking for N-th entry, or approxidate? */
  759. for (i = nth = 0; 0 <= nth && i < reflog_len; i++) {
  760. char ch = str[at+2+i];
  761. if ('0' <= ch && ch <= '9')
  762. nth = nth * 10 + ch - '0';
  763. else
  764. nth = -1;
  765. }
  766. if (100000000 <= nth) {
  767. at_time = nth;
  768. nth = -1;
  769. } else if (0 <= nth)
  770. at_time = 0;
  771. else {
  772. int errors = 0;
  773. char *tmp = xstrndup(str + at + 2, reflog_len);
  774. at_time = approxidate_careful(tmp, &errors);
  775. free(tmp);
  776. if (errors) {
  777. free(real_ref);
  778. return -1;
  779. }
  780. }
  781. if (read_ref_at(get_main_ref_store(r),
  782. real_ref, flags, at_time, nth, oid, NULL,
  783. &co_time, &co_tz, &co_cnt)) {
  784. if (!len) {
  785. if (!skip_prefix(real_ref, "refs/heads/", &str))
  786. str = "HEAD";
  787. len = strlen(str);
  788. }
  789. if (at_time) {
  790. if (!(flags & GET_OID_QUIETLY)) {
  791. warning(_("log for '%.*s' only goes back to %s"),
  792. len, str,
  793. show_date(co_time, co_tz, DATE_MODE(RFC2822)));
  794. }
  795. } else {
  796. if (flags & GET_OID_QUIETLY) {
  797. exit(128);
  798. }
  799. die(_("log for '%.*s' only has %d entries"),
  800. len, str, co_cnt);
  801. }
  802. }
  803. }
  804. free(real_ref);
  805. return 0;
  806. }
  807. static enum get_oid_result get_parent(struct repository *r,
  808. const char *name, int len,
  809. struct object_id *result, int idx)
  810. {
  811. struct object_id oid;
  812. enum get_oid_result ret = get_oid_1(r, name, len, &oid,
  813. GET_OID_COMMITTISH);
  814. struct commit *commit;
  815. struct commit_list *p;
  816. if (ret)
  817. return ret;
  818. commit = lookup_commit_reference(r, &oid);
  819. if (parse_commit(commit))
  820. return MISSING_OBJECT;
  821. if (!idx) {
  822. oidcpy(result, &commit->object.oid);
  823. return FOUND;
  824. }
  825. p = commit->parents;
  826. while (p) {
  827. if (!--idx) {
  828. oidcpy(result, &p->item->object.oid);
  829. return FOUND;
  830. }
  831. p = p->next;
  832. }
  833. return MISSING_OBJECT;
  834. }
  835. static enum get_oid_result get_nth_ancestor(struct repository *r,
  836. const char *name, int len,
  837. struct object_id *result,
  838. int generation)
  839. {
  840. struct object_id oid;
  841. struct commit *commit;
  842. int ret;
  843. ret = get_oid_1(r, name, len, &oid, GET_OID_COMMITTISH);
  844. if (ret)
  845. return ret;
  846. commit = lookup_commit_reference(r, &oid);
  847. if (!commit)
  848. return MISSING_OBJECT;
  849. while (generation--) {
  850. if (parse_commit(commit) || !commit->parents)
  851. return MISSING_OBJECT;
  852. commit = commit->parents->item;
  853. }
  854. oidcpy(result, &commit->object.oid);
  855. return FOUND;
  856. }
  857. struct object *repo_peel_to_type(struct repository *r, const char *name, int namelen,
  858. struct object *o, enum object_type expected_type)
  859. {
  860. if (name && !namelen)
  861. namelen = strlen(name);
  862. while (1) {
  863. if (!o || (!o->parsed && !parse_object(r, &o->oid)))
  864. return NULL;
  865. if (expected_type == OBJ_ANY || o->type == expected_type)
  866. return o;
  867. if (o->type == OBJ_TAG)
  868. o = ((struct tag*) o)->tagged;
  869. else if (o->type == OBJ_COMMIT)
  870. o = &(repo_get_commit_tree(r, ((struct commit *)o))->object);
  871. else {
  872. if (name)
  873. error("%.*s: expected %s type, but the object "
  874. "dereferences to %s type",
  875. namelen, name, type_name(expected_type),
  876. type_name(o->type));
  877. return NULL;
  878. }
  879. }
  880. }
  881. static int peel_onion(struct repository *r, const char *name, int len,
  882. struct object_id *oid, unsigned lookup_flags)
  883. {
  884. struct object_id outer;
  885. const char *sp;
  886. unsigned int expected_type = 0;
  887. struct object *o;
  888. /*
  889. * "ref^{type}" dereferences ref repeatedly until you cannot
  890. * dereference anymore, or you get an object of given type,
  891. * whichever comes first. "ref^{}" means just dereference
  892. * tags until you get a non-tag. "ref^0" is a shorthand for
  893. * "ref^{commit}". "commit^{tree}" could be used to find the
  894. * top-level tree of the given commit.
  895. */
  896. if (len < 4 || name[len-1] != '}')
  897. return -1;
  898. for (sp = name + len - 1; name <= sp; sp--) {
  899. int ch = *sp;
  900. if (ch == '{' && name < sp && sp[-1] == '^')
  901. break;
  902. }
  903. if (sp <= name)
  904. return -1;
  905. sp++; /* beginning of type name, or closing brace for empty */
  906. if (starts_with(sp, "commit}"))
  907. expected_type = OBJ_COMMIT;
  908. else if (starts_with(sp, "tag}"))
  909. expected_type = OBJ_TAG;
  910. else if (starts_with(sp, "tree}"))
  911. expected_type = OBJ_TREE;
  912. else if (starts_with(sp, "blob}"))
  913. expected_type = OBJ_BLOB;
  914. else if (starts_with(sp, "object}"))
  915. expected_type = OBJ_ANY;
  916. else if (sp[0] == '}')
  917. expected_type = OBJ_NONE;
  918. else if (sp[0] == '/')
  919. expected_type = OBJ_COMMIT;
  920. else
  921. return -1;
  922. lookup_flags &= ~GET_OID_DISAMBIGUATORS;
  923. if (expected_type == OBJ_COMMIT)
  924. lookup_flags |= GET_OID_COMMITTISH;
  925. else if (expected_type == OBJ_TREE)
  926. lookup_flags |= GET_OID_TREEISH;
  927. if (get_oid_1(r, name, sp - name - 2, &outer, lookup_flags))
  928. return -1;
  929. o = parse_object(r, &outer);
  930. if (!o)
  931. return -1;
  932. if (!expected_type) {
  933. o = deref_tag(r, o, name, sp - name - 2);
  934. if (!o || (!o->parsed && !parse_object(r, &o->oid)))
  935. return -1;
  936. oidcpy(oid, &o->oid);
  937. return 0;
  938. }
  939. /*
  940. * At this point, the syntax look correct, so
  941. * if we do not get the needed object, we should
  942. * barf.
  943. */
  944. o = repo_peel_to_type(r, name, len, o, expected_type);
  945. if (!o)
  946. return -1;
  947. oidcpy(oid, &o->oid);
  948. if (sp[0] == '/') {
  949. /* "$commit^{/foo}" */
  950. char *prefix;
  951. int ret;
  952. struct commit_list *list = NULL;
  953. /*
  954. * $commit^{/}. Some regex implementation may reject.
  955. * We don't need regex anyway. '' pattern always matches.
  956. */
  957. if (sp[1] == '}')
  958. return 0;
  959. prefix = xstrndup(sp + 1, name + len - 1 - (sp + 1));
  960. commit_list_insert((struct commit *)o, &list);
  961. ret = get_oid_oneline(r, prefix, oid, list);
  962. free(prefix);
  963. return ret;
  964. }
  965. return 0;
  966. }
  967. static int get_describe_name(struct repository *r,
  968. const char *name, int len,
  969. struct object_id *oid)
  970. {
  971. const char *cp;
  972. unsigned flags = GET_OID_QUIETLY | GET_OID_COMMIT;
  973. for (cp = name + len - 1; name + 2 <= cp; cp--) {
  974. char ch = *cp;
  975. if (!isxdigit(ch)) {
  976. /* We must be looking at g in "SOMETHING-g"
  977. * for it to be describe output.
  978. */
  979. if (ch == 'g' && cp[-1] == '-') {
  980. cp++;
  981. len -= cp - name;
  982. return get_short_oid(r,
  983. cp, len, oid, flags);
  984. }
  985. }
  986. }
  987. return -1;
  988. }
  989. static enum get_oid_result get_oid_1(struct repository *r,
  990. const char *name, int len,
  991. struct object_id *oid,
  992. unsigned lookup_flags)
  993. {
  994. int ret, has_suffix;
  995. const char *cp;
  996. /*
  997. * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1".
  998. */
  999. has_suffix = 0;
  1000. for (cp = name + len - 1; name <= cp; cp--) {
  1001. int ch = *cp;
  1002. if ('0' <= ch && ch <= '9')
  1003. continue;
  1004. if (ch == '~' || ch == '^')
  1005. has_suffix = ch;
  1006. break;
  1007. }
  1008. if (has_suffix) {
  1009. unsigned int num = 0;
  1010. int len1 = cp - name;
  1011. cp++;
  1012. while (cp < name + len) {
  1013. unsigned int digit = *cp++ - '0';
  1014. if (unsigned_mult_overflows(num, 10))
  1015. return MISSING_OBJECT;
  1016. num *= 10;
  1017. if (unsigned_add_overflows(num, digit))
  1018. return MISSING_OBJECT;
  1019. num += digit;
  1020. }
  1021. if (!num && len1 == len - 1)
  1022. num = 1;
  1023. else if (num > INT_MAX)
  1024. return MISSING_OBJECT;
  1025. if (has_suffix == '^')
  1026. return get_parent(r, name, len1, oid, num);
  1027. /* else if (has_suffix == '~') -- goes without saying */
  1028. return get_nth_ancestor(r, name, len1, oid, num);
  1029. }
  1030. ret = peel_onion(r, name, len, oid, lookup_flags);
  1031. if (!ret)
  1032. return FOUND;
  1033. ret = get_oid_basic(r, name, len, oid, lookup_flags);
  1034. if (!ret)
  1035. return FOUND;
  1036. /* It could be describe output that is "SOMETHING-gXXXX" */
  1037. ret = get_describe_name(r, name, len, oid);
  1038. if (!ret)
  1039. return FOUND;
  1040. return get_short_oid(r, name, len, oid, lookup_flags);
  1041. }
  1042. /*
  1043. * This interprets names like ':/Initial revision of "git"' by searching
  1044. * through history and returning the first commit whose message starts
  1045. * the given regular expression.
  1046. *
  1047. * For negative-matching, prefix the pattern-part with '!-', like: ':/!-WIP'.
  1048. *
  1049. * For a literal '!' character at the beginning of a pattern, you have to repeat
  1050. * that, like: ':/!!foo'
  1051. *
  1052. * For future extension, all other sequences beginning with ':/!' are reserved.
  1053. */
  1054. /* Remember to update object flag allocation in object.h */
  1055. #define ONELINE_SEEN (1u<<20)
  1056. struct handle_one_ref_cb {
  1057. struct repository *repo;
  1058. struct commit_list **list;
  1059. };
  1060. static int handle_one_ref(const char *path, const struct object_id *oid,
  1061. int flag, void *cb_data)
  1062. {
  1063. struct handle_one_ref_cb *cb = cb_data;
  1064. struct commit_list **list = cb->list;
  1065. struct object *object = parse_object(cb->repo, oid);
  1066. if (!object)
  1067. return 0;
  1068. if (object->type == OBJ_TAG) {
  1069. object = deref_tag(cb->repo, object, path,
  1070. strlen(path));
  1071. if (!object)
  1072. return 0;
  1073. }
  1074. if (object->type != OBJ_COMMIT)
  1075. return 0;
  1076. commit_list_insert((struct commit *)object, list);
  1077. return 0;
  1078. }
  1079. static int get_oid_oneline(struct repository *r,
  1080. const char *prefix, struct object_id *oid,
  1081. struct commit_list *list)
  1082. {
  1083. struct commit_list *backup = NULL, *l;
  1084. int found = 0;
  1085. int negative = 0;
  1086. regex_t regex;
  1087. if (prefix[0] == '!') {
  1088. prefix++;
  1089. if (prefix[0] == '-') {
  1090. prefix++;
  1091. negative = 1;
  1092. } else if (prefix[0] != '!') {
  1093. return -1;
  1094. }
  1095. }
  1096. if (regcomp(&regex, prefix, REG_EXTENDED))
  1097. return -1;
  1098. for (l = list; l; l = l->next) {
  1099. l->item->object.flags |= ONELINE_SEEN;
  1100. commit_list_insert(l->item, &backup);
  1101. }
  1102. while (list) {
  1103. const char *p, *buf;
  1104. struct commit *commit;
  1105. int matches;
  1106. commit = pop_most_recent_commit(&list, ONELINE_SEEN);
  1107. if (!parse_object(r, &commit->object.oid))
  1108. continue;
  1109. buf = get_commit_buffer(commit, NULL);
  1110. p = strstr(buf, "\n\n");
  1111. matches = negative ^ (p && !regexec(&regex, p + 2, 0, NULL, 0));
  1112. unuse_commit_buffer(commit, buf);
  1113. if (matches) {
  1114. oidcpy(oid, &commit->object.oid);
  1115. found = 1;
  1116. break;
  1117. }
  1118. }
  1119. regfree(&regex);
  1120. free_commit_list(list);
  1121. for (l = backup; l; l = l->next)
  1122. clear_commit_marks(l->item, ONELINE_SEEN);
  1123. free_commit_list(backup);
  1124. return found ? 0 : -1;
  1125. }
  1126. struct grab_nth_branch_switch_cbdata {
  1127. int remaining;
  1128. struct strbuf *sb;
  1129. };
  1130. static int grab_nth_branch_switch(struct object_id *ooid, struct object_id *noid,
  1131. const char *email, timestamp_t timestamp, int tz,
  1132. const char *message, void *cb_data)
  1133. {
  1134. struct grab_nth_branch_switch_cbdata *cb = cb_data;
  1135. const char *match = NULL, *target = NULL;
  1136. size_t len;
  1137. if (skip_prefix(message, "checkout: moving from ", &match))
  1138. target = strstr(match, " to ");
  1139. if (!match || !target)
  1140. return 0;
  1141. if (--(cb->remaining) == 0) {
  1142. len = target - match;
  1143. strbuf_reset(cb->sb);
  1144. strbuf_add(cb->sb, match, len);
  1145. return 1; /* we are done */
  1146. }
  1147. return 0;
  1148. }
  1149. /*
  1150. * Parse @{-N} syntax, return the number of characters parsed
  1151. * if successful; otherwise signal an error with negative value.
  1152. */
  1153. static int interpret_nth_prior_checkout(struct repository *r,
  1154. const char *name, int namelen,
  1155. struct strbuf *buf)
  1156. {
  1157. long nth;
  1158. int retval;
  1159. struct grab_nth_branch_switch_cbdata cb;
  1160. const char *brace;
  1161. char *num_end;
  1162. if (namelen < 4)
  1163. return -1;
  1164. if (name[0] != '@' || name[1] != '{' || name[2] != '-')
  1165. return -1;
  1166. brace = memchr(name, '}', namelen);
  1167. if (!brace)
  1168. return -1;
  1169. nth = strtol(name + 3, &num_end, 10);
  1170. if (num_end != brace)
  1171. return -1;
  1172. if (nth <= 0)
  1173. return -1;
  1174. cb.remaining = nth;
  1175. cb.sb = buf;
  1176. retval = refs_for_each_reflog_ent_reverse(get_main_ref_store(r),
  1177. "HEAD", grab_nth_branch_switch, &cb);
  1178. if (0 < retval) {
  1179. retval = brace - name + 1;
  1180. } else
  1181. retval = 0;
  1182. return retval;
  1183. }
  1184. int repo_get_oid_mb(struct repository *r,
  1185. const char *name,
  1186. struct object_id *oid)
  1187. {
  1188. struct commit *one, *two;
  1189. struct commit_list *mbs;
  1190. struct object_id oid_tmp;
  1191. const char *dots;
  1192. int st;
  1193. dots = strstr(name, "...");
  1194. if (!dots)
  1195. return repo_get_oid(r, name, oid);
  1196. if (dots == name)
  1197. st = repo_get_oid(r, "HEAD", &oid_tmp);
  1198. else {
  1199. struct strbuf sb;
  1200. strbuf_init(&sb, dots - name);
  1201. strbuf_add(&sb, name, dots - name);
  1202. st = repo_get_oid_committish(r, sb.buf, &oid_tmp);
  1203. strbuf_release(&sb);
  1204. }
  1205. if (st)
  1206. return st;
  1207. one = lookup_commit_reference_gently(r, &oid_tmp, 0);
  1208. if (!one)
  1209. return -1;
  1210. if (repo_get_oid_committish(r, dots[3] ? (dots + 3) : "HEAD", &oid_tmp))
  1211. return -1;
  1212. two = lookup_commit_reference_gently(r, &oid_tmp, 0);
  1213. if (!two)
  1214. return -1;
  1215. mbs = repo_get_merge_bases(r, one, two);
  1216. if (!mbs || mbs->next)
  1217. st = -1;
  1218. else {
  1219. st = 0;
  1220. oidcpy(oid, &mbs->item->object.oid);
  1221. }
  1222. free_commit_list(mbs);
  1223. return st;
  1224. }
  1225. /* parse @something syntax, when 'something' is not {.*} */
  1226. static int interpret_empty_at(const char *name, int namelen, int len, struct strbuf *buf)
  1227. {
  1228. const char *next;
  1229. if (len || name[1] == '{')
  1230. return -1;
  1231. /* make sure it's a single @, or @@{.*}, not @foo */
  1232. next = memchr(name + len + 1, '@', namelen - len - 1);
  1233. if (next && next[1] != '{')
  1234. return -1;
  1235. if (!next)
  1236. next = name + namelen;
  1237. if (next != name + 1)
  1238. return -1;
  1239. strbuf_reset(buf);
  1240. strbuf_add(buf, "HEAD", 4);
  1241. return 1;
  1242. }
  1243. static int reinterpret(struct repository *r,
  1244. const char *name, int namelen, int len,
  1245. struct strbuf *buf, unsigned allowed)
  1246. {
  1247. /* we have extra data, which might need further processing */
  1248. struct strbuf tmp = STRBUF_INIT;
  1249. int used = buf->len;
  1250. int ret;
  1251. struct interpret_branch_name_options options = {
  1252. .allowed = allowed
  1253. };
  1254. strbuf_add(buf, name + len, namelen - len);
  1255. ret = repo_interpret_branch_name(r, buf->buf, buf->len, &tmp, &options);
  1256. /* that data was not interpreted, remove our cruft */
  1257. if (ret < 0) {
  1258. strbuf_setlen(buf, used);
  1259. return len;
  1260. }
  1261. strbuf_reset(buf);
  1262. strbuf_addbuf(buf, &tmp);
  1263. strbuf_release(&tmp);
  1264. /* tweak for size of {-N} versus expanded ref name */
  1265. return ret - used + len;
  1266. }
  1267. static void set_shortened_ref(struct repository *r, struct strbuf *buf, const char *ref)
  1268. {
  1269. char *s = refs_shorten_unambiguous_ref(get_main_ref_store(r), ref, 0);
  1270. strbuf_reset(buf);
  1271. strbuf_addstr(buf, s);
  1272. free(s);
  1273. }
  1274. static int branch_interpret_allowed(const char *refname, unsigned allowed)
  1275. {
  1276. if (!allowed)
  1277. return 1;
  1278. if ((allowed & INTERPRET_BRANCH_LOCAL) &&
  1279. starts_with(refname, "refs/heads/"))
  1280. return 1;
  1281. if ((allowed & INTERPRET_BRANCH_REMOTE) &&
  1282. starts_with(refname, "refs/remotes/"))
  1283. return 1;
  1284. return 0;
  1285. }
  1286. static int interpret_branch_mark(struct repository *r,
  1287. const char *name, int namelen,
  1288. int at, struct strbuf *buf,
  1289. int (*get_mark)(const char *, int),
  1290. const char *(*get_data)(struct branch *,
  1291. struct strbuf *),
  1292. const struct interpret_branch_name_options *options)
  1293. {
  1294. int len;
  1295. struct branch *branch;
  1296. struct strbuf err = STRBUF_INIT;
  1297. const char *value;
  1298. len = get_mark(name + at, namelen - at);
  1299. if (!len)
  1300. return -1;
  1301. if (memchr(name, ':', at))
  1302. return -1;
  1303. if (at) {
  1304. char *name_str = xmemdupz(name, at);
  1305. branch = branch_get(name_str);
  1306. free(name_str);
  1307. } else
  1308. branch = branch_get(NULL);
  1309. value = get_data(branch, &err);
  1310. if (!value) {
  1311. if (options->nonfatal_dangling_mark) {
  1312. strbuf_release(&err);
  1313. return -1;
  1314. } else {
  1315. die("%s", err.buf);
  1316. }
  1317. }
  1318. if (!branch_interpret_allowed(value, options->allowed))
  1319. return -1;
  1320. set_shortened_ref(r, buf, value);
  1321. return len + at;
  1322. }
  1323. int repo_interpret_branch_name(struct repository *r,
  1324. const char *name, int namelen,
  1325. struct strbuf *buf,
  1326. const struct interpret_branch_name_options *options)
  1327. {
  1328. char *at;
  1329. const char *start;
  1330. int len;
  1331. if (!namelen)
  1332. namelen = strlen(name);
  1333. if (!options->allowed || (options->allowed & INTERPRET_BRANCH_LOCAL)) {
  1334. len = interpret_nth_prior_checkout(r, name, namelen, buf);
  1335. if (!len) {
  1336. return len; /* syntax Ok, not enough switches */
  1337. } else if (len > 0) {
  1338. if (len == namelen)
  1339. return len; /* consumed all */
  1340. else
  1341. return reinterpret(r, name, namelen, len, buf,
  1342. options->allowed);
  1343. }
  1344. }
  1345. for (start = name;
  1346. (at = memchr(start, '@', namelen - (start - name)));
  1347. start = at + 1) {
  1348. if (!options->allowed || (options->allowed & INTERPRET_BRANCH_HEAD)) {
  1349. len = interpret_empty_at(name, namelen, at - name, buf);
  1350. if (len > 0)
  1351. return reinterpret(r, name, namelen, len, buf,
  1352. options->allowed);
  1353. }
  1354. len = interpret_branch_mark(r, name, namelen, at - name, buf,
  1355. upstream_mark, branch_get_upstream,
  1356. options);
  1357. if (len > 0)
  1358. return len;
  1359. len = interpret_branch_mark(r, name, namelen, at - name, buf,
  1360. push_mark, branch_get_push,
  1361. options);
  1362. if (len > 0)
  1363. return len;
  1364. }
  1365. return -1;
  1366. }
  1367. void strbuf_branchname(struct strbuf *sb, const char *name, unsigned allowed)
  1368. {
  1369. int len = strlen(name);
  1370. struct interpret_branch_name_options options = {
  1371. .allowed = allowed
  1372. };
  1373. int used = interpret_branch_name(name, len, sb, &options);
  1374. if (used < 0)
  1375. used = 0;
  1376. strbuf_add(sb, name + used, len - used);
  1377. }
  1378. int strbuf_check_branch_ref(struct strbuf *sb, const char *name)
  1379. {
  1380. if (startup_info->have_repository)
  1381. strbuf_branchname(sb, name, INTERPRET_BRANCH_LOCAL);
  1382. else
  1383. strbuf_addstr(sb, name);
  1384. /*
  1385. * This splice must be done even if we end up rejecting the
  1386. * name; builtin/branch.c::copy_or_rename_branch() still wants
  1387. * to see what the name expanded to so that "branch -m" can be
  1388. * used as a tool to correct earlier mistakes.
  1389. */
  1390. strbuf_splice(sb, 0, 0, "refs/heads/", 11);
  1391. if (*name == '-' ||
  1392. !strcmp(sb->buf, "refs/heads/HEAD"))
  1393. return -1;
  1394. return check_refname_format(sb->buf, 0);
  1395. }
  1396. /*
  1397. * This is like "get_oid_basic()", except it allows "object ID expressions",
  1398. * notably "xyz^" for "parent of xyz"
  1399. */
  1400. int repo_get_oid(struct repository *r, const char *name, struct object_id *oid)
  1401. {
  1402. struct object_context unused;
  1403. return get_oid_with_context(r, name, 0, oid, &unused);
  1404. }
  1405. /*
  1406. * This returns a non-zero value if the string (built using printf
  1407. * format and the given arguments) is not a valid object.
  1408. */
  1409. int get_oidf(struct object_id *oid, const char *fmt, ...)
  1410. {
  1411. va_list ap;
  1412. int ret;
  1413. struct strbuf sb = STRBUF_INIT;
  1414. va_start(ap, fmt);
  1415. strbuf_vaddf(&sb, fmt, ap);
  1416. va_end(ap);
  1417. ret = get_oid(sb.buf, oid);
  1418. strbuf_release(&sb);
  1419. return ret;
  1420. }
  1421. /*
  1422. * Many callers know that the user meant to name a commit-ish by
  1423. * syntactical positions where the object name appears. Calling this
  1424. * function allows the machinery to disambiguate shorter-than-unique
  1425. * abbreviated object names between commit-ish and others.
  1426. *
  1427. * Note that this does NOT error out when the named object is not a
  1428. * commit-ish. It is merely to give a hint to the disambiguation
  1429. * machinery.
  1430. */
  1431. int repo_get_oid_committish(struct repository *r,
  1432. const char *name,
  1433. struct object_id *oid)
  1434. {
  1435. struct object_context unused;
  1436. return get_oid_with_context(r, name, GET_OID_COMMITTISH,
  1437. oid, &unused);
  1438. }
  1439. int repo_get_oid_treeish(struct repository *r,
  1440. const char *name,
  1441. struct object_id *oid)
  1442. {
  1443. struct object_context unused;
  1444. return get_oid_with_context(r, name, GET_OID_TREEISH,
  1445. oid, &unused);
  1446. }
  1447. int repo_get_oid_commit(struct repository *r,
  1448. const char *name,
  1449. struct object_id *oid)
  1450. {
  1451. struct object_context unused;
  1452. return get_oid_with_context(r, name, GET_OID_COMMIT,
  1453. oid, &unused);
  1454. }
  1455. int repo_get_oid_tree(struct repository *r,
  1456. const char *name,
  1457. struct object_id *oid)
  1458. {
  1459. struct object_context unused;
  1460. return get_oid_with_context(r, name, GET_OID_TREE,
  1461. oid, &unused);
  1462. }
  1463. int repo_get_oid_blob(struct repository *r,
  1464. const char *name,
  1465. struct object_id *oid)
  1466. {
  1467. struct object_context unused;
  1468. return get_oid_with_context(r, name, GET_OID_BLOB,
  1469. oid, &unused);
  1470. }
  1471. /* Must be called only when object_name:filename doesn't exist. */
  1472. static void diagnose_invalid_oid_path(struct repository *r,
  1473. const char *prefix,
  1474. const char *filename,
  1475. const struct object_id *tree_oid,
  1476. const char *object_name,
  1477. int object_name_len)
  1478. {
  1479. struct object_id oid;
  1480. unsigned short mode;
  1481. if (!prefix)
  1482. prefix = "";
  1483. if (file_exists(filename))
  1484. die(_("path '%s' exists on disk, but not in '%.*s'"),
  1485. filename, object_name_len, object_name);
  1486. if (is_missing_file_error(errno)) {
  1487. char *fullname = xstrfmt("%s%s", prefix, filename);
  1488. if (!get_tree_entry(r, tree_oid, fullname, &oid, &mode)) {
  1489. die(_("path '%s' exists, but not '%s'\n"
  1490. "hint: Did you mean '%.*s:%s' aka '%.*s:./%s'?"),
  1491. fullname,
  1492. filename,
  1493. object_name_len, object_name,
  1494. fullname,
  1495. object_name_len, object_name,
  1496. filename);
  1497. }
  1498. die(_("path '%s' does not exist in '%.*s'"),
  1499. filename, object_name_len, object_name);
  1500. }
  1501. }
  1502. /* Must be called only when :stage:filename doesn't exist. */
  1503. static void diagnose_invalid_index_path(struct repository *r,
  1504. int stage,
  1505. const char *prefix,
  1506. const char *filename)
  1507. {
  1508. struct index_state *istate = r->index;
  1509. const struct cache_entry *ce;
  1510. int pos;
  1511. unsigned namelen = strlen(filename);
  1512. struct strbuf fullname = STRBUF_INIT;
  1513. if (!prefix)
  1514. prefix = "";
  1515. /* Wrong stage number? */
  1516. pos = index_name_pos(istate, filename, namelen);
  1517. if (pos < 0)
  1518. pos = -pos - 1;
  1519. if (pos < istate->cache_nr) {
  1520. ce = istate->cache[pos];
  1521. if (ce_namelen(ce) == namelen &&
  1522. !memcmp(ce->name, filename, namelen))
  1523. die(_("path '%s' is in the index, but not at stage %d\n"
  1524. "hint: Did you mean ':%d:%s'?"),
  1525. filename, stage,
  1526. ce_stage(ce), filename);
  1527. }
  1528. /* Confusion between relative and absolute filenames? */
  1529. strbuf_addstr(&fullname, prefix);
  1530. strbuf_addstr(&fullname, filename);
  1531. pos = index_name_pos(istate, fullname.buf, fullname.len);
  1532. if (pos < 0)
  1533. pos = -pos - 1;
  1534. if (pos < istate->cache_nr) {
  1535. ce = istate->cache[pos];
  1536. if (ce_namelen(ce) == fullname.len &&
  1537. !memcmp(ce->name, fullname.buf, fullname.len))
  1538. die(_("path '%s' is in the index, but not '%s'\n"
  1539. "hint: Did you mean ':%d:%s' aka ':%d:./%s'?"),
  1540. fullname.buf, filename,
  1541. ce_stage(ce), fullname.buf,
  1542. ce_stage(ce), filename);
  1543. }
  1544. if (repo_file_exists(r, filename))
  1545. die(_("path '%s' exists on disk, but not in the index"), filename);
  1546. if (is_missing_file_error(errno))
  1547. die(_("path '%s' does not exist (neither on disk nor in the index)"),
  1548. filename);
  1549. strbuf_release(&fullname);
  1550. }
  1551. static char *resolve_relative_path(struct repository *r, const char *rel)
  1552. {
  1553. if (!starts_with(rel, "./") && !starts_with(rel, "../"))
  1554. return NULL;
  1555. if (r != the_repository || !is_inside_work_tree())
  1556. die(_("relative path syntax can't be used outside working tree"));
  1557. /* die() inside prefix_path() if resolved path is outside worktree */
  1558. return prefix_path(startup_info->prefix,
  1559. startup_info->prefix ? strlen(startup_info->prefix) : 0,
  1560. rel);
  1561. }
  1562. static enum get_oid_result get_oid_with_context_1(struct repository *repo,
  1563. const char *name,
  1564. unsigned flags,
  1565. const char *prefix,
  1566. struct object_id *oid,
  1567. struct object_context *oc)
  1568. {
  1569. int ret, bracket_depth;
  1570. int namelen = strlen(name);
  1571. const char *cp;
  1572. int only_to_die = flags & GET_OID_ONLY_TO_DIE;
  1573. if (only_to_die)
  1574. flags |= GET_OID_QUIETLY;
  1575. memset(oc, 0, sizeof(*oc));
  1576. oc->mode = S_IFINVALID;
  1577. strbuf_init(&oc->symlink_path, 0);
  1578. ret = get_oid_1(repo, name, namelen, oid, flags);
  1579. if (!ret)
  1580. return ret;
  1581. /*
  1582. * sha1:path --> object name of path in ent sha1
  1583. * :path -> object name of absolute path in index
  1584. * :./path -> object name of path relative to cwd in index
  1585. * :[0-3]:path -> object name of path in index at stage
  1586. * :/foo -> recent commit matching foo
  1587. */
  1588. if (name[0] == ':') {
  1589. int stage = 0;
  1590. const struct cache_entry *ce;
  1591. char *new_path = NULL;
  1592. int pos;
  1593. if (!only_to_die && namelen > 2 && name[1] == '/') {
  1594. struct handle_one_ref_cb cb;
  1595. struct commit_list *list = NULL;
  1596. cb.repo = repo;
  1597. cb.list = &list;
  1598. refs_for_each_ref(get_main_ref_store(repo), handle_one_ref, &cb);
  1599. refs_head_ref(get_main_ref_store(repo), handle_one_ref, &cb);
  1600. commit_list_sort_by_date(&list);
  1601. return get_oid_oneline(repo, name + 2, oid, list);
  1602. }
  1603. if (namelen < 3 ||
  1604. name[2] != ':' ||
  1605. name[1] < '0' || '3' < name[1])
  1606. cp = name + 1;
  1607. else {
  1608. stage = name[1] - '0';
  1609. cp = name + 3;
  1610. }
  1611. new_path = resolve_relative_path(repo, cp);
  1612. if (!new_path) {
  1613. namelen = namelen - (cp - name);
  1614. } else {
  1615. cp = new_path;
  1616. namelen = strlen(cp);
  1617. }
  1618. if (flags & GET_OID_RECORD_PATH)
  1619. oc->path = xstrdup(cp);
  1620. if (!repo->index || !repo->index->cache)
  1621. repo_read_index(repo);
  1622. pos = index_name_pos(repo->index, cp, namelen);
  1623. if (pos < 0)
  1624. pos = -pos - 1;
  1625. while (pos < repo->index->cache_nr) {
  1626. ce = repo->index->cache[pos];
  1627. if (ce_namelen(ce) != namelen ||
  1628. memcmp(ce->name, cp, namelen))
  1629. break;
  1630. if (ce_stage(ce) == stage) {
  1631. oidcpy(oid, &ce->oid);
  1632. oc->mode = ce->ce_mode;
  1633. free(new_path);
  1634. return 0;
  1635. }
  1636. pos++;
  1637. }
  1638. if (only_to_die && name[1] && name[1] != '/')
  1639. diagnose_invalid_index_path(repo, stage, prefix, cp);
  1640. free(new_path);
  1641. return -1;
  1642. }
  1643. for (cp = name, bracket_depth = 0; *cp; cp++) {
  1644. if (*cp == '{')
  1645. bracket_depth++;
  1646. else if (bracket_depth && *cp == '}')
  1647. bracket_depth--;
  1648. else if (!bracket_depth && *cp == ':')
  1649. break;
  1650. }
  1651. if (*cp == ':') {
  1652. struct object_id tree_oid;
  1653. int len = cp - name;
  1654. unsigned sub_flags = flags;
  1655. sub_flags &= ~GET_OID_DISAMBIGUATORS;
  1656. sub_flags |= GET_OID_TREEISH;
  1657. if (!get_oid_1(repo, name, len, &tree_oid, sub_flags)) {
  1658. const char *filename = cp+1;
  1659. char *new_filename = NULL;
  1660. new_filename = resolve_relative_path(repo, filename);
  1661. if (new_filename)
  1662. filename = new_filename;
  1663. if (flags & GET_OID_FOLLOW_SYMLINKS) {
  1664. ret = get_tree_entry_follow_symlinks(repo, &tree_oid,
  1665. filename, oid, &oc->symlink_path,
  1666. &oc->mode);
  1667. } else {
  1668. ret = get_tree_entry(repo, &tree_oid, filename, oid,
  1669. &oc->mode);
  1670. if (ret && only_to_die) {
  1671. diagnose_invalid_oid_path(repo, prefix,
  1672. filename,
  1673. &tree_oid,
  1674. name, len);
  1675. }
  1676. }
  1677. if (flags & GET_OID_RECORD_PATH)
  1678. oc->path = xstrdup(filename);
  1679. free(new_filename);
  1680. return ret;
  1681. } else {
  1682. if (only_to_die)
  1683. die(_("invalid object name '%.*s'."), len, name);
  1684. }
  1685. }
  1686. return ret;
  1687. }
  1688. /*
  1689. * Call this function when you know "name" given by the end user must
  1690. * name an object but it doesn't; the function _may_ die with a better
  1691. * diagnostic message than "no such object 'name'", e.g. "Path 'doc' does not
  1692. * exist in 'HEAD'" when given "HEAD:doc", or it may return in which case
  1693. * you have a chance to diagnose the error further.
  1694. */
  1695. void maybe_die_on_misspelt_object_name(struct repository *r,
  1696. const char *name,
  1697. const char *prefix)
  1698. {
  1699. struct object_context oc;
  1700. struct object_id oid;
  1701. get_oid_with_context_1(r, name, GET_OID_ONLY_TO_DIE,
  1702. prefix, &oid, &oc);
  1703. }
  1704. enum get_oid_result get_oid_with_context(struct repository *repo,
  1705. const char *str,
  1706. unsigned flags,
  1707. struct object_id *oid,
  1708. struct object_context *oc)
  1709. {
  1710. if (flags & GET_OID_FOLLOW_SYMLINKS && flags & GET_OID_ONLY_TO_DIE)
  1711. BUG("incompatible flags for get_sha1_with_context");
  1712. return get_oid_with_context_1(repo, str, flags, NULL, oid, oc);
  1713. }