ref-filter.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577
  1. #include "builtin.h"
  2. #include "cache.h"
  3. #include "parse-options.h"
  4. #include "refs.h"
  5. #include "wildmatch.h"
  6. #include "object-store.h"
  7. #include "repository.h"
  8. #include "commit.h"
  9. #include "remote.h"
  10. #include "color.h"
  11. #include "tag.h"
  12. #include "quote.h"
  13. #include "ref-filter.h"
  14. #include "revision.h"
  15. #include "utf8.h"
  16. #include "git-compat-util.h"
  17. #include "version.h"
  18. #include "trailer.h"
  19. #include "wt-status.h"
  20. #include "commit-slab.h"
  21. #include "commit-graph.h"
  22. #include "commit-reach.h"
  23. #include "worktree.h"
  24. #include "hashmap.h"
  25. #include "strvec.h"
  26. static struct ref_msg {
  27. const char *gone;
  28. const char *ahead;
  29. const char *behind;
  30. const char *ahead_behind;
  31. } msgs = {
  32. /* Untranslated plumbing messages: */
  33. "gone",
  34. "ahead %d",
  35. "behind %d",
  36. "ahead %d, behind %d"
  37. };
  38. void setup_ref_filter_porcelain_msg(void)
  39. {
  40. msgs.gone = _("gone");
  41. msgs.ahead = _("ahead %d");
  42. msgs.behind = _("behind %d");
  43. msgs.ahead_behind = _("ahead %d, behind %d");
  44. }
  45. typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;
  46. typedef enum { COMPARE_EQUAL, COMPARE_UNEQUAL, COMPARE_NONE } cmp_status;
  47. typedef enum { SOURCE_NONE = 0, SOURCE_OBJ, SOURCE_OTHER } info_source;
  48. struct align {
  49. align_type position;
  50. unsigned int width;
  51. };
  52. struct if_then_else {
  53. cmp_status cmp_status;
  54. const char *str;
  55. unsigned int then_atom_seen : 1,
  56. else_atom_seen : 1,
  57. condition_satisfied : 1;
  58. };
  59. struct refname_atom {
  60. enum { R_NORMAL, R_SHORT, R_LSTRIP, R_RSTRIP } option;
  61. int lstrip, rstrip;
  62. };
  63. static struct expand_data {
  64. struct object_id oid;
  65. enum object_type type;
  66. unsigned long size;
  67. off_t disk_size;
  68. struct object_id delta_base_oid;
  69. void *content;
  70. struct object_info info;
  71. } oi, oi_deref;
  72. struct ref_to_worktree_entry {
  73. struct hashmap_entry ent;
  74. struct worktree *wt; /* key is wt->head_ref */
  75. };
  76. static int ref_to_worktree_map_cmpfnc(const void *unused_lookupdata,
  77. const struct hashmap_entry *eptr,
  78. const struct hashmap_entry *kptr,
  79. const void *keydata_aka_refname)
  80. {
  81. const struct ref_to_worktree_entry *e, *k;
  82. e = container_of(eptr, const struct ref_to_worktree_entry, ent);
  83. k = container_of(kptr, const struct ref_to_worktree_entry, ent);
  84. return strcmp(e->wt->head_ref,
  85. keydata_aka_refname ? keydata_aka_refname : k->wt->head_ref);
  86. }
  87. static struct ref_to_worktree_map {
  88. struct hashmap map;
  89. struct worktree **worktrees;
  90. } ref_to_worktree_map;
  91. /*
  92. * An atom is a valid field atom listed below, possibly prefixed with
  93. * a "*" to denote deref_tag().
  94. *
  95. * We parse given format string and sort specifiers, and make a list
  96. * of properties that we need to extract out of objects. ref_array_item
  97. * structure will hold an array of values extracted that can be
  98. * indexed with the "atom number", which is an index into this
  99. * array.
  100. */
  101. static struct used_atom {
  102. const char *name;
  103. cmp_type type;
  104. info_source source;
  105. union {
  106. char color[COLOR_MAXLEN];
  107. struct align align;
  108. struct {
  109. enum {
  110. RR_REF, RR_TRACK, RR_TRACKSHORT, RR_REMOTE_NAME, RR_REMOTE_REF
  111. } option;
  112. struct refname_atom refname;
  113. unsigned int nobracket : 1, push : 1, push_remote : 1;
  114. } remote_ref;
  115. struct {
  116. enum { C_BARE, C_BODY, C_BODY_DEP, C_LENGTH, C_LINES,
  117. C_SIG, C_SUB, C_SUB_SANITIZE, C_TRAILERS } option;
  118. struct process_trailer_options trailer_opts;
  119. unsigned int nlines;
  120. } contents;
  121. struct {
  122. cmp_status cmp_status;
  123. const char *str;
  124. } if_then_else;
  125. struct {
  126. enum { O_FULL, O_LENGTH, O_SHORT } option;
  127. unsigned int length;
  128. } oid;
  129. struct email_option {
  130. enum { EO_RAW, EO_TRIM, EO_LOCALPART } option;
  131. } email_option;
  132. struct refname_atom refname;
  133. char *head;
  134. } u;
  135. } *used_atom;
  136. static int used_atom_cnt, need_tagged, need_symref;
  137. /*
  138. * Expand string, append it to strbuf *sb, then return error code ret.
  139. * Allow to save few lines of code.
  140. */
  141. static int strbuf_addf_ret(struct strbuf *sb, int ret, const char *fmt, ...)
  142. {
  143. va_list ap;
  144. va_start(ap, fmt);
  145. strbuf_vaddf(sb, fmt, ap);
  146. va_end(ap);
  147. return ret;
  148. }
  149. static int color_atom_parser(const struct ref_format *format, struct used_atom *atom,
  150. const char *color_value, struct strbuf *err)
  151. {
  152. if (!color_value)
  153. return strbuf_addf_ret(err, -1, _("expected format: %%(color:<color>)"));
  154. if (color_parse(color_value, atom->u.color) < 0)
  155. return strbuf_addf_ret(err, -1, _("unrecognized color: %%(color:%s)"),
  156. color_value);
  157. /*
  158. * We check this after we've parsed the color, which lets us complain
  159. * about syntactically bogus color names even if they won't be used.
  160. */
  161. if (!want_color(format->use_color))
  162. color_parse("", atom->u.color);
  163. return 0;
  164. }
  165. static int refname_atom_parser_internal(struct refname_atom *atom, const char *arg,
  166. const char *name, struct strbuf *err)
  167. {
  168. if (!arg)
  169. atom->option = R_NORMAL;
  170. else if (!strcmp(arg, "short"))
  171. atom->option = R_SHORT;
  172. else if (skip_prefix(arg, "lstrip=", &arg) ||
  173. skip_prefix(arg, "strip=", &arg)) {
  174. atom->option = R_LSTRIP;
  175. if (strtol_i(arg, 10, &atom->lstrip))
  176. return strbuf_addf_ret(err, -1, _("Integer value expected refname:lstrip=%s"), arg);
  177. } else if (skip_prefix(arg, "rstrip=", &arg)) {
  178. atom->option = R_RSTRIP;
  179. if (strtol_i(arg, 10, &atom->rstrip))
  180. return strbuf_addf_ret(err, -1, _("Integer value expected refname:rstrip=%s"), arg);
  181. } else
  182. return strbuf_addf_ret(err, -1, _("unrecognized %%(%s) argument: %s"), name, arg);
  183. return 0;
  184. }
  185. static int remote_ref_atom_parser(const struct ref_format *format, struct used_atom *atom,
  186. const char *arg, struct strbuf *err)
  187. {
  188. struct string_list params = STRING_LIST_INIT_DUP;
  189. int i;
  190. if (!strcmp(atom->name, "push") || starts_with(atom->name, "push:"))
  191. atom->u.remote_ref.push = 1;
  192. if (!arg) {
  193. atom->u.remote_ref.option = RR_REF;
  194. return refname_atom_parser_internal(&atom->u.remote_ref.refname,
  195. arg, atom->name, err);
  196. }
  197. atom->u.remote_ref.nobracket = 0;
  198. string_list_split(&params, arg, ',', -1);
  199. for (i = 0; i < params.nr; i++) {
  200. const char *s = params.items[i].string;
  201. if (!strcmp(s, "track"))
  202. atom->u.remote_ref.option = RR_TRACK;
  203. else if (!strcmp(s, "trackshort"))
  204. atom->u.remote_ref.option = RR_TRACKSHORT;
  205. else if (!strcmp(s, "nobracket"))
  206. atom->u.remote_ref.nobracket = 1;
  207. else if (!strcmp(s, "remotename")) {
  208. atom->u.remote_ref.option = RR_REMOTE_NAME;
  209. atom->u.remote_ref.push_remote = 1;
  210. } else if (!strcmp(s, "remoteref")) {
  211. atom->u.remote_ref.option = RR_REMOTE_REF;
  212. atom->u.remote_ref.push_remote = 1;
  213. } else {
  214. atom->u.remote_ref.option = RR_REF;
  215. if (refname_atom_parser_internal(&atom->u.remote_ref.refname,
  216. arg, atom->name, err)) {
  217. string_list_clear(&params, 0);
  218. return -1;
  219. }
  220. }
  221. }
  222. string_list_clear(&params, 0);
  223. return 0;
  224. }
  225. static int objecttype_atom_parser(const struct ref_format *format, struct used_atom *atom,
  226. const char *arg, struct strbuf *err)
  227. {
  228. if (arg)
  229. return strbuf_addf_ret(err, -1, _("%%(objecttype) does not take arguments"));
  230. if (*atom->name == '*')
  231. oi_deref.info.typep = &oi_deref.type;
  232. else
  233. oi.info.typep = &oi.type;
  234. return 0;
  235. }
  236. static int objectsize_atom_parser(const struct ref_format *format, struct used_atom *atom,
  237. const char *arg, struct strbuf *err)
  238. {
  239. if (!arg) {
  240. if (*atom->name == '*')
  241. oi_deref.info.sizep = &oi_deref.size;
  242. else
  243. oi.info.sizep = &oi.size;
  244. } else if (!strcmp(arg, "disk")) {
  245. if (*atom->name == '*')
  246. oi_deref.info.disk_sizep = &oi_deref.disk_size;
  247. else
  248. oi.info.disk_sizep = &oi.disk_size;
  249. } else
  250. return strbuf_addf_ret(err, -1, _("unrecognized %%(objectsize) argument: %s"), arg);
  251. return 0;
  252. }
  253. static int deltabase_atom_parser(const struct ref_format *format, struct used_atom *atom,
  254. const char *arg, struct strbuf *err)
  255. {
  256. if (arg)
  257. return strbuf_addf_ret(err, -1, _("%%(deltabase) does not take arguments"));
  258. if (*atom->name == '*')
  259. oi_deref.info.delta_base_oid = &oi_deref.delta_base_oid;
  260. else
  261. oi.info.delta_base_oid = &oi.delta_base_oid;
  262. return 0;
  263. }
  264. static int body_atom_parser(const struct ref_format *format, struct used_atom *atom,
  265. const char *arg, struct strbuf *err)
  266. {
  267. if (arg)
  268. return strbuf_addf_ret(err, -1, _("%%(body) does not take arguments"));
  269. atom->u.contents.option = C_BODY_DEP;
  270. return 0;
  271. }
  272. static int subject_atom_parser(const struct ref_format *format, struct used_atom *atom,
  273. const char *arg, struct strbuf *err)
  274. {
  275. if (!arg)
  276. atom->u.contents.option = C_SUB;
  277. else if (!strcmp(arg, "sanitize"))
  278. atom->u.contents.option = C_SUB_SANITIZE;
  279. else
  280. return strbuf_addf_ret(err, -1, _("unrecognized %%(subject) argument: %s"), arg);
  281. return 0;
  282. }
  283. static int trailers_atom_parser(const struct ref_format *format, struct used_atom *atom,
  284. const char *arg, struct strbuf *err)
  285. {
  286. struct string_list params = STRING_LIST_INIT_DUP;
  287. int i;
  288. atom->u.contents.trailer_opts.no_divider = 1;
  289. if (arg) {
  290. string_list_split(&params, arg, ',', -1);
  291. for (i = 0; i < params.nr; i++) {
  292. const char *s = params.items[i].string;
  293. if (!strcmp(s, "unfold"))
  294. atom->u.contents.trailer_opts.unfold = 1;
  295. else if (!strcmp(s, "only"))
  296. atom->u.contents.trailer_opts.only_trailers = 1;
  297. else {
  298. strbuf_addf(err, _("unknown %%(trailers) argument: %s"), s);
  299. string_list_clear(&params, 0);
  300. return -1;
  301. }
  302. }
  303. }
  304. atom->u.contents.option = C_TRAILERS;
  305. string_list_clear(&params, 0);
  306. return 0;
  307. }
  308. static int contents_atom_parser(const struct ref_format *format, struct used_atom *atom,
  309. const char *arg, struct strbuf *err)
  310. {
  311. if (!arg)
  312. atom->u.contents.option = C_BARE;
  313. else if (!strcmp(arg, "body"))
  314. atom->u.contents.option = C_BODY;
  315. else if (!strcmp(arg, "size"))
  316. atom->u.contents.option = C_LENGTH;
  317. else if (!strcmp(arg, "signature"))
  318. atom->u.contents.option = C_SIG;
  319. else if (!strcmp(arg, "subject"))
  320. atom->u.contents.option = C_SUB;
  321. else if (!strcmp(arg, "trailers")) {
  322. if (trailers_atom_parser(format, atom, NULL, err))
  323. return -1;
  324. } else if (skip_prefix(arg, "trailers:", &arg)) {
  325. if (trailers_atom_parser(format, atom, arg, err))
  326. return -1;
  327. } else if (skip_prefix(arg, "lines=", &arg)) {
  328. atom->u.contents.option = C_LINES;
  329. if (strtoul_ui(arg, 10, &atom->u.contents.nlines))
  330. return strbuf_addf_ret(err, -1, _("positive value expected contents:lines=%s"), arg);
  331. } else
  332. return strbuf_addf_ret(err, -1, _("unrecognized %%(contents) argument: %s"), arg);
  333. return 0;
  334. }
  335. static int oid_atom_parser(const struct ref_format *format, struct used_atom *atom,
  336. const char *arg, struct strbuf *err)
  337. {
  338. if (!arg)
  339. atom->u.oid.option = O_FULL;
  340. else if (!strcmp(arg, "short"))
  341. atom->u.oid.option = O_SHORT;
  342. else if (skip_prefix(arg, "short=", &arg)) {
  343. atom->u.oid.option = O_LENGTH;
  344. if (strtoul_ui(arg, 10, &atom->u.oid.length) ||
  345. atom->u.oid.length == 0)
  346. return strbuf_addf_ret(err, -1, _("positive value expected '%s' in %%(%s)"), arg, atom->name);
  347. if (atom->u.oid.length < MINIMUM_ABBREV)
  348. atom->u.oid.length = MINIMUM_ABBREV;
  349. } else
  350. return strbuf_addf_ret(err, -1, _("unrecognized argument '%s' in %%(%s)"), arg, atom->name);
  351. return 0;
  352. }
  353. static int person_email_atom_parser(const struct ref_format *format, struct used_atom *atom,
  354. const char *arg, struct strbuf *err)
  355. {
  356. if (!arg)
  357. atom->u.email_option.option = EO_RAW;
  358. else if (!strcmp(arg, "trim"))
  359. atom->u.email_option.option = EO_TRIM;
  360. else if (!strcmp(arg, "localpart"))
  361. atom->u.email_option.option = EO_LOCALPART;
  362. else
  363. return strbuf_addf_ret(err, -1, _("unrecognized email option: %s"), arg);
  364. return 0;
  365. }
  366. static int refname_atom_parser(const struct ref_format *format, struct used_atom *atom,
  367. const char *arg, struct strbuf *err)
  368. {
  369. return refname_atom_parser_internal(&atom->u.refname, arg, atom->name, err);
  370. }
  371. static align_type parse_align_position(const char *s)
  372. {
  373. if (!strcmp(s, "right"))
  374. return ALIGN_RIGHT;
  375. else if (!strcmp(s, "middle"))
  376. return ALIGN_MIDDLE;
  377. else if (!strcmp(s, "left"))
  378. return ALIGN_LEFT;
  379. return -1;
  380. }
  381. static int align_atom_parser(const struct ref_format *format, struct used_atom *atom,
  382. const char *arg, struct strbuf *err)
  383. {
  384. struct align *align = &atom->u.align;
  385. struct string_list params = STRING_LIST_INIT_DUP;
  386. int i;
  387. unsigned int width = ~0U;
  388. if (!arg)
  389. return strbuf_addf_ret(err, -1, _("expected format: %%(align:<width>,<position>)"));
  390. align->position = ALIGN_LEFT;
  391. string_list_split(&params, arg, ',', -1);
  392. for (i = 0; i < params.nr; i++) {
  393. const char *s = params.items[i].string;
  394. int position;
  395. if (skip_prefix(s, "position=", &s)) {
  396. position = parse_align_position(s);
  397. if (position < 0) {
  398. strbuf_addf(err, _("unrecognized position:%s"), s);
  399. string_list_clear(&params, 0);
  400. return -1;
  401. }
  402. align->position = position;
  403. } else if (skip_prefix(s, "width=", &s)) {
  404. if (strtoul_ui(s, 10, &width)) {
  405. strbuf_addf(err, _("unrecognized width:%s"), s);
  406. string_list_clear(&params, 0);
  407. return -1;
  408. }
  409. } else if (!strtoul_ui(s, 10, &width))
  410. ;
  411. else if ((position = parse_align_position(s)) >= 0)
  412. align->position = position;
  413. else {
  414. strbuf_addf(err, _("unrecognized %%(align) argument: %s"), s);
  415. string_list_clear(&params, 0);
  416. return -1;
  417. }
  418. }
  419. if (width == ~0U) {
  420. string_list_clear(&params, 0);
  421. return strbuf_addf_ret(err, -1, _("positive width expected with the %%(align) atom"));
  422. }
  423. align->width = width;
  424. string_list_clear(&params, 0);
  425. return 0;
  426. }
  427. static int if_atom_parser(const struct ref_format *format, struct used_atom *atom,
  428. const char *arg, struct strbuf *err)
  429. {
  430. if (!arg) {
  431. atom->u.if_then_else.cmp_status = COMPARE_NONE;
  432. return 0;
  433. } else if (skip_prefix(arg, "equals=", &atom->u.if_then_else.str)) {
  434. atom->u.if_then_else.cmp_status = COMPARE_EQUAL;
  435. } else if (skip_prefix(arg, "notequals=", &atom->u.if_then_else.str)) {
  436. atom->u.if_then_else.cmp_status = COMPARE_UNEQUAL;
  437. } else
  438. return strbuf_addf_ret(err, -1, _("unrecognized %%(if) argument: %s"), arg);
  439. return 0;
  440. }
  441. static int head_atom_parser(const struct ref_format *format, struct used_atom *atom,
  442. const char *arg, struct strbuf *unused_err)
  443. {
  444. atom->u.head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
  445. return 0;
  446. }
  447. static struct {
  448. const char *name;
  449. info_source source;
  450. cmp_type cmp_type;
  451. int (*parser)(const struct ref_format *format, struct used_atom *atom,
  452. const char *arg, struct strbuf *err);
  453. } valid_atom[] = {
  454. { "refname", SOURCE_NONE, FIELD_STR, refname_atom_parser },
  455. { "objecttype", SOURCE_OTHER, FIELD_STR, objecttype_atom_parser },
  456. { "objectsize", SOURCE_OTHER, FIELD_ULONG, objectsize_atom_parser },
  457. { "objectname", SOURCE_OTHER, FIELD_STR, oid_atom_parser },
  458. { "deltabase", SOURCE_OTHER, FIELD_STR, deltabase_atom_parser },
  459. { "tree", SOURCE_OBJ, FIELD_STR, oid_atom_parser },
  460. { "parent", SOURCE_OBJ, FIELD_STR, oid_atom_parser },
  461. { "numparent", SOURCE_OBJ, FIELD_ULONG },
  462. { "object", SOURCE_OBJ },
  463. { "type", SOURCE_OBJ },
  464. { "tag", SOURCE_OBJ },
  465. { "author", SOURCE_OBJ },
  466. { "authorname", SOURCE_OBJ },
  467. { "authoremail", SOURCE_OBJ, FIELD_STR, person_email_atom_parser },
  468. { "authordate", SOURCE_OBJ, FIELD_TIME },
  469. { "committer", SOURCE_OBJ },
  470. { "committername", SOURCE_OBJ },
  471. { "committeremail", SOURCE_OBJ, FIELD_STR, person_email_atom_parser },
  472. { "committerdate", SOURCE_OBJ, FIELD_TIME },
  473. { "tagger", SOURCE_OBJ },
  474. { "taggername", SOURCE_OBJ },
  475. { "taggeremail", SOURCE_OBJ, FIELD_STR, person_email_atom_parser },
  476. { "taggerdate", SOURCE_OBJ, FIELD_TIME },
  477. { "creator", SOURCE_OBJ },
  478. { "creatordate", SOURCE_OBJ, FIELD_TIME },
  479. { "subject", SOURCE_OBJ, FIELD_STR, subject_atom_parser },
  480. { "body", SOURCE_OBJ, FIELD_STR, body_atom_parser },
  481. { "trailers", SOURCE_OBJ, FIELD_STR, trailers_atom_parser },
  482. { "contents", SOURCE_OBJ, FIELD_STR, contents_atom_parser },
  483. { "upstream", SOURCE_NONE, FIELD_STR, remote_ref_atom_parser },
  484. { "push", SOURCE_NONE, FIELD_STR, remote_ref_atom_parser },
  485. { "symref", SOURCE_NONE, FIELD_STR, refname_atom_parser },
  486. { "flag", SOURCE_NONE },
  487. { "HEAD", SOURCE_NONE, FIELD_STR, head_atom_parser },
  488. { "color", SOURCE_NONE, FIELD_STR, color_atom_parser },
  489. { "worktreepath", SOURCE_NONE },
  490. { "align", SOURCE_NONE, FIELD_STR, align_atom_parser },
  491. { "end", SOURCE_NONE },
  492. { "if", SOURCE_NONE, FIELD_STR, if_atom_parser },
  493. { "then", SOURCE_NONE },
  494. { "else", SOURCE_NONE },
  495. /*
  496. * Please update $__git_ref_fieldlist in git-completion.bash
  497. * when you add new atoms
  498. */
  499. };
  500. #define REF_FORMATTING_STATE_INIT { 0, NULL }
  501. struct ref_formatting_stack {
  502. struct ref_formatting_stack *prev;
  503. struct strbuf output;
  504. void (*at_end)(struct ref_formatting_stack **stack);
  505. void *at_end_data;
  506. };
  507. struct ref_formatting_state {
  508. int quote_style;
  509. struct ref_formatting_stack *stack;
  510. };
  511. struct atom_value {
  512. const char *s;
  513. int (*handler)(struct atom_value *atomv, struct ref_formatting_state *state,
  514. struct strbuf *err);
  515. uintmax_t value; /* used for sorting when not FIELD_STR */
  516. struct used_atom *atom;
  517. };
  518. /*
  519. * Used to parse format string and sort specifiers
  520. */
  521. static int parse_ref_filter_atom(const struct ref_format *format,
  522. const char *atom, const char *ep,
  523. struct strbuf *err)
  524. {
  525. const char *sp;
  526. const char *arg;
  527. int i, at, atom_len;
  528. sp = atom;
  529. if (*sp == '*' && sp < ep)
  530. sp++; /* deref */
  531. if (ep <= sp)
  532. return strbuf_addf_ret(err, -1, _("malformed field name: %.*s"),
  533. (int)(ep-atom), atom);
  534. /* Do we have the atom already used elsewhere? */
  535. for (i = 0; i < used_atom_cnt; i++) {
  536. int len = strlen(used_atom[i].name);
  537. if (len == ep - atom && !memcmp(used_atom[i].name, atom, len))
  538. return i;
  539. }
  540. /*
  541. * If the atom name has a colon, strip it and everything after
  542. * it off - it specifies the format for this entry, and
  543. * shouldn't be used for checking against the valid_atom
  544. * table.
  545. */
  546. arg = memchr(sp, ':', ep - sp);
  547. atom_len = (arg ? arg : ep) - sp;
  548. /* Is the atom a valid one? */
  549. for (i = 0; i < ARRAY_SIZE(valid_atom); i++) {
  550. int len = strlen(valid_atom[i].name);
  551. if (len == atom_len && !memcmp(valid_atom[i].name, sp, len))
  552. break;
  553. }
  554. if (ARRAY_SIZE(valid_atom) <= i)
  555. return strbuf_addf_ret(err, -1, _("unknown field name: %.*s"),
  556. (int)(ep-atom), atom);
  557. if (valid_atom[i].source != SOURCE_NONE && !have_git_dir())
  558. return strbuf_addf_ret(err, -1,
  559. _("not a git repository, but the field '%.*s' requires access to object data"),
  560. (int)(ep-atom), atom);
  561. /* Add it in, including the deref prefix */
  562. at = used_atom_cnt;
  563. used_atom_cnt++;
  564. REALLOC_ARRAY(used_atom, used_atom_cnt);
  565. used_atom[at].name = xmemdupz(atom, ep - atom);
  566. used_atom[at].type = valid_atom[i].cmp_type;
  567. used_atom[at].source = valid_atom[i].source;
  568. if (used_atom[at].source == SOURCE_OBJ) {
  569. if (*atom == '*')
  570. oi_deref.info.contentp = &oi_deref.content;
  571. else
  572. oi.info.contentp = &oi.content;
  573. }
  574. if (arg) {
  575. arg = used_atom[at].name + (arg - atom) + 1;
  576. if (!*arg) {
  577. /*
  578. * Treat empty sub-arguments list as NULL (i.e.,
  579. * "%(atom:)" is equivalent to "%(atom)").
  580. */
  581. arg = NULL;
  582. }
  583. }
  584. memset(&used_atom[at].u, 0, sizeof(used_atom[at].u));
  585. if (valid_atom[i].parser && valid_atom[i].parser(format, &used_atom[at], arg, err))
  586. return -1;
  587. if (*atom == '*')
  588. need_tagged = 1;
  589. if (!strcmp(valid_atom[i].name, "symref"))
  590. need_symref = 1;
  591. return at;
  592. }
  593. static void quote_formatting(struct strbuf *s, const char *str, int quote_style)
  594. {
  595. switch (quote_style) {
  596. case QUOTE_NONE:
  597. strbuf_addstr(s, str);
  598. break;
  599. case QUOTE_SHELL:
  600. sq_quote_buf(s, str);
  601. break;
  602. case QUOTE_PERL:
  603. perl_quote_buf(s, str);
  604. break;
  605. case QUOTE_PYTHON:
  606. python_quote_buf(s, str);
  607. break;
  608. case QUOTE_TCL:
  609. tcl_quote_buf(s, str);
  610. break;
  611. }
  612. }
  613. static int append_atom(struct atom_value *v, struct ref_formatting_state *state,
  614. struct strbuf *unused_err)
  615. {
  616. /*
  617. * Quote formatting is only done when the stack has a single
  618. * element. Otherwise quote formatting is done on the
  619. * element's entire output strbuf when the %(end) atom is
  620. * encountered.
  621. */
  622. if (!state->stack->prev)
  623. quote_formatting(&state->stack->output, v->s, state->quote_style);
  624. else
  625. strbuf_addstr(&state->stack->output, v->s);
  626. return 0;
  627. }
  628. static void push_stack_element(struct ref_formatting_stack **stack)
  629. {
  630. struct ref_formatting_stack *s = xcalloc(1, sizeof(struct ref_formatting_stack));
  631. strbuf_init(&s->output, 0);
  632. s->prev = *stack;
  633. *stack = s;
  634. }
  635. static void pop_stack_element(struct ref_formatting_stack **stack)
  636. {
  637. struct ref_formatting_stack *current = *stack;
  638. struct ref_formatting_stack *prev = current->prev;
  639. if (prev)
  640. strbuf_addbuf(&prev->output, &current->output);
  641. strbuf_release(&current->output);
  642. free(current);
  643. *stack = prev;
  644. }
  645. static void end_align_handler(struct ref_formatting_stack **stack)
  646. {
  647. struct ref_formatting_stack *cur = *stack;
  648. struct align *align = (struct align *)cur->at_end_data;
  649. struct strbuf s = STRBUF_INIT;
  650. strbuf_utf8_align(&s, align->position, align->width, cur->output.buf);
  651. strbuf_swap(&cur->output, &s);
  652. strbuf_release(&s);
  653. }
  654. static int align_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state,
  655. struct strbuf *unused_err)
  656. {
  657. struct ref_formatting_stack *new_stack;
  658. push_stack_element(&state->stack);
  659. new_stack = state->stack;
  660. new_stack->at_end = end_align_handler;
  661. new_stack->at_end_data = &atomv->atom->u.align;
  662. return 0;
  663. }
  664. static void if_then_else_handler(struct ref_formatting_stack **stack)
  665. {
  666. struct ref_formatting_stack *cur = *stack;
  667. struct ref_formatting_stack *prev = cur->prev;
  668. struct if_then_else *if_then_else = (struct if_then_else *)cur->at_end_data;
  669. if (!if_then_else->then_atom_seen)
  670. die(_("format: %%(if) atom used without a %%(then) atom"));
  671. if (if_then_else->else_atom_seen) {
  672. /*
  673. * There is an %(else) atom: we need to drop one state from the
  674. * stack, either the %(else) branch if the condition is satisfied, or
  675. * the %(then) branch if it isn't.
  676. */
  677. if (if_then_else->condition_satisfied) {
  678. strbuf_reset(&cur->output);
  679. pop_stack_element(&cur);
  680. } else {
  681. strbuf_swap(&cur->output, &prev->output);
  682. strbuf_reset(&cur->output);
  683. pop_stack_element(&cur);
  684. }
  685. } else if (!if_then_else->condition_satisfied) {
  686. /*
  687. * No %(else) atom: just drop the %(then) branch if the
  688. * condition is not satisfied.
  689. */
  690. strbuf_reset(&cur->output);
  691. }
  692. *stack = cur;
  693. free(if_then_else);
  694. }
  695. static int if_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state,
  696. struct strbuf *unused_err)
  697. {
  698. struct ref_formatting_stack *new_stack;
  699. struct if_then_else *if_then_else = xcalloc(sizeof(struct if_then_else), 1);
  700. if_then_else->str = atomv->atom->u.if_then_else.str;
  701. if_then_else->cmp_status = atomv->atom->u.if_then_else.cmp_status;
  702. push_stack_element(&state->stack);
  703. new_stack = state->stack;
  704. new_stack->at_end = if_then_else_handler;
  705. new_stack->at_end_data = if_then_else;
  706. return 0;
  707. }
  708. static int is_empty(const char *s)
  709. {
  710. while (*s != '\0') {
  711. if (!isspace(*s))
  712. return 0;
  713. s++;
  714. }
  715. return 1;
  716. }
  717. static int then_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state,
  718. struct strbuf *err)
  719. {
  720. struct ref_formatting_stack *cur = state->stack;
  721. struct if_then_else *if_then_else = NULL;
  722. if (cur->at_end == if_then_else_handler)
  723. if_then_else = (struct if_then_else *)cur->at_end_data;
  724. if (!if_then_else)
  725. return strbuf_addf_ret(err, -1, _("format: %%(then) atom used without an %%(if) atom"));
  726. if (if_then_else->then_atom_seen)
  727. return strbuf_addf_ret(err, -1, _("format: %%(then) atom used more than once"));
  728. if (if_then_else->else_atom_seen)
  729. return strbuf_addf_ret(err, -1, _("format: %%(then) atom used after %%(else)"));
  730. if_then_else->then_atom_seen = 1;
  731. /*
  732. * If the 'equals' or 'notequals' attribute is used then
  733. * perform the required comparison. If not, only non-empty
  734. * strings satisfy the 'if' condition.
  735. */
  736. if (if_then_else->cmp_status == COMPARE_EQUAL) {
  737. if (!strcmp(if_then_else->str, cur->output.buf))
  738. if_then_else->condition_satisfied = 1;
  739. } else if (if_then_else->cmp_status == COMPARE_UNEQUAL) {
  740. if (strcmp(if_then_else->str, cur->output.buf))
  741. if_then_else->condition_satisfied = 1;
  742. } else if (cur->output.len && !is_empty(cur->output.buf))
  743. if_then_else->condition_satisfied = 1;
  744. strbuf_reset(&cur->output);
  745. return 0;
  746. }
  747. static int else_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state,
  748. struct strbuf *err)
  749. {
  750. struct ref_formatting_stack *prev = state->stack;
  751. struct if_then_else *if_then_else = NULL;
  752. if (prev->at_end == if_then_else_handler)
  753. if_then_else = (struct if_then_else *)prev->at_end_data;
  754. if (!if_then_else)
  755. return strbuf_addf_ret(err, -1, _("format: %%(else) atom used without an %%(if) atom"));
  756. if (!if_then_else->then_atom_seen)
  757. return strbuf_addf_ret(err, -1, _("format: %%(else) atom used without a %%(then) atom"));
  758. if (if_then_else->else_atom_seen)
  759. return strbuf_addf_ret(err, -1, _("format: %%(else) atom used more than once"));
  760. if_then_else->else_atom_seen = 1;
  761. push_stack_element(&state->stack);
  762. state->stack->at_end_data = prev->at_end_data;
  763. state->stack->at_end = prev->at_end;
  764. return 0;
  765. }
  766. static int end_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state,
  767. struct strbuf *err)
  768. {
  769. struct ref_formatting_stack *current = state->stack;
  770. struct strbuf s = STRBUF_INIT;
  771. if (!current->at_end)
  772. return strbuf_addf_ret(err, -1, _("format: %%(end) atom used without corresponding atom"));
  773. current->at_end(&state->stack);
  774. /* Stack may have been popped within at_end(), hence reset the current pointer */
  775. current = state->stack;
  776. /*
  777. * Perform quote formatting when the stack element is that of
  778. * a supporting atom. If nested then perform quote formatting
  779. * only on the topmost supporting atom.
  780. */
  781. if (!current->prev->prev) {
  782. quote_formatting(&s, current->output.buf, state->quote_style);
  783. strbuf_swap(&current->output, &s);
  784. }
  785. strbuf_release(&s);
  786. pop_stack_element(&state->stack);
  787. return 0;
  788. }
  789. /*
  790. * In a format string, find the next occurrence of %(atom).
  791. */
  792. static const char *find_next(const char *cp)
  793. {
  794. while (*cp) {
  795. if (*cp == '%') {
  796. /*
  797. * %( is the start of an atom;
  798. * %% is a quoted per-cent.
  799. */
  800. if (cp[1] == '(')
  801. return cp;
  802. else if (cp[1] == '%')
  803. cp++; /* skip over two % */
  804. /* otherwise this is a singleton, literal % */
  805. }
  806. cp++;
  807. }
  808. return NULL;
  809. }
  810. /*
  811. * Make sure the format string is well formed, and parse out
  812. * the used atoms.
  813. */
  814. int verify_ref_format(struct ref_format *format)
  815. {
  816. const char *cp, *sp;
  817. format->need_color_reset_at_eol = 0;
  818. for (cp = format->format; *cp && (sp = find_next(cp)); ) {
  819. struct strbuf err = STRBUF_INIT;
  820. const char *color, *ep = strchr(sp, ')');
  821. int at;
  822. if (!ep)
  823. return error(_("malformed format string %s"), sp);
  824. /* sp points at "%(" and ep points at the closing ")" */
  825. at = parse_ref_filter_atom(format, sp + 2, ep, &err);
  826. if (at < 0)
  827. die("%s", err.buf);
  828. cp = ep + 1;
  829. if (skip_prefix(used_atom[at].name, "color:", &color))
  830. format->need_color_reset_at_eol = !!strcmp(color, "reset");
  831. strbuf_release(&err);
  832. }
  833. if (format->need_color_reset_at_eol && !want_color(format->use_color))
  834. format->need_color_reset_at_eol = 0;
  835. return 0;
  836. }
  837. static const char *do_grab_oid(const char *field, const struct object_id *oid,
  838. struct used_atom *atom)
  839. {
  840. switch (atom->u.oid.option) {
  841. case O_FULL:
  842. return oid_to_hex(oid);
  843. case O_LENGTH:
  844. return find_unique_abbrev(oid, atom->u.oid.length);
  845. case O_SHORT:
  846. return find_unique_abbrev(oid, DEFAULT_ABBREV);
  847. default:
  848. BUG("unknown %%(%s) option", field);
  849. }
  850. }
  851. static int grab_oid(const char *name, const char *field, const struct object_id *oid,
  852. struct atom_value *v, struct used_atom *atom)
  853. {
  854. if (starts_with(name, field)) {
  855. v->s = xstrdup(do_grab_oid(field, oid, atom));
  856. return 1;
  857. }
  858. return 0;
  859. }
  860. /* See grab_values */
  861. static void grab_common_values(struct atom_value *val, int deref, struct expand_data *oi)
  862. {
  863. int i;
  864. for (i = 0; i < used_atom_cnt; i++) {
  865. const char *name = used_atom[i].name;
  866. struct atom_value *v = &val[i];
  867. if (!!deref != (*name == '*'))
  868. continue;
  869. if (deref)
  870. name++;
  871. if (!strcmp(name, "objecttype"))
  872. v->s = xstrdup(type_name(oi->type));
  873. else if (!strcmp(name, "objectsize:disk")) {
  874. v->value = oi->disk_size;
  875. v->s = xstrfmt("%"PRIuMAX, (uintmax_t)oi->disk_size);
  876. } else if (!strcmp(name, "objectsize")) {
  877. v->value = oi->size;
  878. v->s = xstrfmt("%"PRIuMAX , (uintmax_t)oi->size);
  879. } else if (!strcmp(name, "deltabase"))
  880. v->s = xstrdup(oid_to_hex(&oi->delta_base_oid));
  881. else if (deref)
  882. grab_oid(name, "objectname", &oi->oid, v, &used_atom[i]);
  883. }
  884. }
  885. /* See grab_values */
  886. static void grab_tag_values(struct atom_value *val, int deref, struct object *obj)
  887. {
  888. int i;
  889. struct tag *tag = (struct tag *) obj;
  890. for (i = 0; i < used_atom_cnt; i++) {
  891. const char *name = used_atom[i].name;
  892. struct atom_value *v = &val[i];
  893. if (!!deref != (*name == '*'))
  894. continue;
  895. if (deref)
  896. name++;
  897. if (!strcmp(name, "tag"))
  898. v->s = xstrdup(tag->tag);
  899. else if (!strcmp(name, "type") && tag->tagged)
  900. v->s = xstrdup(type_name(tag->tagged->type));
  901. else if (!strcmp(name, "object") && tag->tagged)
  902. v->s = xstrdup(oid_to_hex(&tag->tagged->oid));
  903. }
  904. }
  905. /* See grab_values */
  906. static void grab_commit_values(struct atom_value *val, int deref, struct object *obj)
  907. {
  908. int i;
  909. struct commit *commit = (struct commit *) obj;
  910. for (i = 0; i < used_atom_cnt; i++) {
  911. const char *name = used_atom[i].name;
  912. struct atom_value *v = &val[i];
  913. if (!!deref != (*name == '*'))
  914. continue;
  915. if (deref)
  916. name++;
  917. if (grab_oid(name, "tree", get_commit_tree_oid(commit), v, &used_atom[i]))
  918. continue;
  919. if (!strcmp(name, "numparent")) {
  920. v->value = commit_list_count(commit->parents);
  921. v->s = xstrfmt("%lu", (unsigned long)v->value);
  922. }
  923. else if (starts_with(name, "parent")) {
  924. struct commit_list *parents;
  925. struct strbuf s = STRBUF_INIT;
  926. for (parents = commit->parents; parents; parents = parents->next) {
  927. struct object_id *oid = &parents->item->object.oid;
  928. if (parents != commit->parents)
  929. strbuf_addch(&s, ' ');
  930. strbuf_addstr(&s, do_grab_oid("parent", oid, &used_atom[i]));
  931. }
  932. v->s = strbuf_detach(&s, NULL);
  933. }
  934. }
  935. }
  936. static const char *find_wholine(const char *who, int wholen, const char *buf)
  937. {
  938. const char *eol;
  939. while (*buf) {
  940. if (!strncmp(buf, who, wholen) &&
  941. buf[wholen] == ' ')
  942. return buf + wholen + 1;
  943. eol = strchr(buf, '\n');
  944. if (!eol)
  945. return "";
  946. eol++;
  947. if (*eol == '\n')
  948. return ""; /* end of header */
  949. buf = eol;
  950. }
  951. return "";
  952. }
  953. static const char *copy_line(const char *buf)
  954. {
  955. const char *eol = strchrnul(buf, '\n');
  956. return xmemdupz(buf, eol - buf);
  957. }
  958. static const char *copy_name(const char *buf)
  959. {
  960. const char *cp;
  961. for (cp = buf; *cp && *cp != '\n'; cp++) {
  962. if (!strncmp(cp, " <", 2))
  963. return xmemdupz(buf, cp - buf);
  964. }
  965. return xstrdup("");
  966. }
  967. static const char *copy_email(const char *buf, struct used_atom *atom)
  968. {
  969. const char *email = strchr(buf, '<');
  970. const char *eoemail;
  971. if (!email)
  972. return xstrdup("");
  973. switch (atom->u.email_option.option) {
  974. case EO_RAW:
  975. eoemail = strchr(email, '>');
  976. if (eoemail)
  977. eoemail++;
  978. break;
  979. case EO_TRIM:
  980. email++;
  981. eoemail = strchr(email, '>');
  982. break;
  983. case EO_LOCALPART:
  984. email++;
  985. eoemail = strchr(email, '@');
  986. if (!eoemail)
  987. eoemail = strchr(email, '>');
  988. break;
  989. default:
  990. BUG("unknown email option");
  991. }
  992. if (!eoemail)
  993. return xstrdup("");
  994. return xmemdupz(email, eoemail - email);
  995. }
  996. static char *copy_subject(const char *buf, unsigned long len)
  997. {
  998. char *r = xmemdupz(buf, len);
  999. int i;
  1000. for (i = 0; i < len; i++)
  1001. if (r[i] == '\n')
  1002. r[i] = ' ';
  1003. return r;
  1004. }
  1005. static void grab_date(const char *buf, struct atom_value *v, const char *atomname)
  1006. {
  1007. const char *eoemail = strstr(buf, "> ");
  1008. char *zone;
  1009. timestamp_t timestamp;
  1010. long tz;
  1011. struct date_mode date_mode = { DATE_NORMAL };
  1012. const char *formatp;
  1013. /*
  1014. * We got here because atomname ends in "date" or "date<something>";
  1015. * it's not possible that <something> is not ":<format>" because
  1016. * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
  1017. * ":" means no format is specified, and use the default.
  1018. */
  1019. formatp = strchr(atomname, ':');
  1020. if (formatp != NULL) {
  1021. formatp++;
  1022. parse_date_format(formatp, &date_mode);
  1023. }
  1024. if (!eoemail)
  1025. goto bad;
  1026. timestamp = parse_timestamp(eoemail + 2, &zone, 10);
  1027. if (timestamp == TIME_MAX)
  1028. goto bad;
  1029. tz = strtol(zone, NULL, 10);
  1030. if ((tz == LONG_MIN || tz == LONG_MAX) && errno == ERANGE)
  1031. goto bad;
  1032. v->s = xstrdup(show_date(timestamp, tz, &date_mode));
  1033. v->value = timestamp;
  1034. return;
  1035. bad:
  1036. v->s = xstrdup("");
  1037. v->value = 0;
  1038. }
  1039. /* See grab_values */
  1040. static void grab_person(const char *who, struct atom_value *val, int deref, void *buf)
  1041. {
  1042. int i;
  1043. int wholen = strlen(who);
  1044. const char *wholine = NULL;
  1045. for (i = 0; i < used_atom_cnt; i++) {
  1046. const char *name = used_atom[i].name;
  1047. struct atom_value *v = &val[i];
  1048. if (!!deref != (*name == '*'))
  1049. continue;
  1050. if (deref)
  1051. name++;
  1052. if (strncmp(who, name, wholen))
  1053. continue;
  1054. if (name[wholen] != 0 &&
  1055. strcmp(name + wholen, "name") &&
  1056. !starts_with(name + wholen, "email") &&
  1057. !starts_with(name + wholen, "date"))
  1058. continue;
  1059. if (!wholine)
  1060. wholine = find_wholine(who, wholen, buf);
  1061. if (!wholine)
  1062. return; /* no point looking for it */
  1063. if (name[wholen] == 0)
  1064. v->s = copy_line(wholine);
  1065. else if (!strcmp(name + wholen, "name"))
  1066. v->s = copy_name(wholine);
  1067. else if (starts_with(name + wholen, "email"))
  1068. v->s = copy_email(wholine, &used_atom[i]);
  1069. else if (starts_with(name + wholen, "date"))
  1070. grab_date(wholine, v, name);
  1071. }
  1072. /*
  1073. * For a tag or a commit object, if "creator" or "creatordate" is
  1074. * requested, do something special.
  1075. */
  1076. if (strcmp(who, "tagger") && strcmp(who, "committer"))
  1077. return; /* "author" for commit object is not wanted */
  1078. if (!wholine)
  1079. wholine = find_wholine(who, wholen, buf);
  1080. if (!wholine)
  1081. return;
  1082. for (i = 0; i < used_atom_cnt; i++) {
  1083. const char *name = used_atom[i].name;
  1084. struct atom_value *v = &val[i];
  1085. if (!!deref != (*name == '*'))
  1086. continue;
  1087. if (deref)
  1088. name++;
  1089. if (starts_with(name, "creatordate"))
  1090. grab_date(wholine, v, name);
  1091. else if (!strcmp(name, "creator"))
  1092. v->s = copy_line(wholine);
  1093. }
  1094. }
  1095. static void find_subpos(const char *buf,
  1096. const char **sub, unsigned long *sublen,
  1097. const char **body, unsigned long *bodylen,
  1098. unsigned long *nonsiglen,
  1099. const char **sig, unsigned long *siglen)
  1100. {
  1101. const char *eol;
  1102. /* skip past header until we hit empty line */
  1103. while (*buf && *buf != '\n') {
  1104. eol = strchrnul(buf, '\n');
  1105. if (*eol)
  1106. eol++;
  1107. buf = eol;
  1108. }
  1109. /* skip any empty lines */
  1110. while (*buf == '\n')
  1111. buf++;
  1112. /* parse signature first; we might not even have a subject line */
  1113. *sig = buf + parse_signature(buf, strlen(buf));
  1114. *siglen = strlen(*sig);
  1115. /* subject is first non-empty line */
  1116. *sub = buf;
  1117. /* subject goes to first empty line */
  1118. while (buf < *sig && *buf && *buf != '\n') {
  1119. eol = strchrnul(buf, '\n');
  1120. if (*eol)
  1121. eol++;
  1122. buf = eol;
  1123. }
  1124. *sublen = buf - *sub;
  1125. /* drop trailing newline, if present */
  1126. if (*sublen && (*sub)[*sublen - 1] == '\n')
  1127. *sublen -= 1;
  1128. /* skip any empty lines */
  1129. while (*buf == '\n')
  1130. buf++;
  1131. *body = buf;
  1132. *bodylen = strlen(buf);
  1133. *nonsiglen = *sig - buf;
  1134. }
  1135. /*
  1136. * If 'lines' is greater than 0, append that many lines from the given
  1137. * 'buf' of length 'size' to the given strbuf.
  1138. */
  1139. static void append_lines(struct strbuf *out, const char *buf, unsigned long size, int lines)
  1140. {
  1141. int i;
  1142. const char *sp, *eol;
  1143. size_t len;
  1144. sp = buf;
  1145. for (i = 0; i < lines && sp < buf + size; i++) {
  1146. if (i)
  1147. strbuf_addstr(out, "\n ");
  1148. eol = memchr(sp, '\n', size - (sp - buf));
  1149. len = eol ? eol - sp : size - (sp - buf);
  1150. strbuf_add(out, sp, len);
  1151. if (!eol)
  1152. break;
  1153. sp = eol + 1;
  1154. }
  1155. }
  1156. /* See grab_values */
  1157. static void grab_sub_body_contents(struct atom_value *val, int deref, void *buf)
  1158. {
  1159. int i;
  1160. const char *subpos = NULL, *bodypos = NULL, *sigpos = NULL;
  1161. unsigned long sublen = 0, bodylen = 0, nonsiglen = 0, siglen = 0;
  1162. for (i = 0; i < used_atom_cnt; i++) {
  1163. struct used_atom *atom = &used_atom[i];
  1164. const char *name = atom->name;
  1165. struct atom_value *v = &val[i];
  1166. if (!!deref != (*name == '*'))
  1167. continue;
  1168. if (deref)
  1169. name++;
  1170. if (strcmp(name, "body") &&
  1171. !starts_with(name, "subject") &&
  1172. !starts_with(name, "trailers") &&
  1173. !starts_with(name, "contents"))
  1174. continue;
  1175. if (!subpos)
  1176. find_subpos(buf,
  1177. &subpos, &sublen,
  1178. &bodypos, &bodylen, &nonsiglen,
  1179. &sigpos, &siglen);
  1180. if (atom->u.contents.option == C_SUB)
  1181. v->s = copy_subject(subpos, sublen);
  1182. else if (atom->u.contents.option == C_SUB_SANITIZE) {
  1183. struct strbuf sb = STRBUF_INIT;
  1184. format_sanitized_subject(&sb, subpos, sublen);
  1185. v->s = strbuf_detach(&sb, NULL);
  1186. } else if (atom->u.contents.option == C_BODY_DEP)
  1187. v->s = xmemdupz(bodypos, bodylen);
  1188. else if (atom->u.contents.option == C_LENGTH)
  1189. v->s = xstrfmt("%"PRIuMAX, (uintmax_t)strlen(subpos));
  1190. else if (atom->u.contents.option == C_BODY)
  1191. v->s = xmemdupz(bodypos, nonsiglen);
  1192. else if (atom->u.contents.option == C_SIG)
  1193. v->s = xmemdupz(sigpos, siglen);
  1194. else if (atom->u.contents.option == C_LINES) {
  1195. struct strbuf s = STRBUF_INIT;
  1196. const char *contents_end = bodylen + bodypos - siglen;
  1197. /* Size is the length of the message after removing the signature */
  1198. append_lines(&s, subpos, contents_end - subpos, atom->u.contents.nlines);
  1199. v->s = strbuf_detach(&s, NULL);
  1200. } else if (atom->u.contents.option == C_TRAILERS) {
  1201. struct strbuf s = STRBUF_INIT;
  1202. /* Format the trailer info according to the trailer_opts given */
  1203. format_trailers_from_commit(&s, subpos, &atom->u.contents.trailer_opts);
  1204. v->s = strbuf_detach(&s, NULL);
  1205. } else if (atom->u.contents.option == C_BARE)
  1206. v->s = xstrdup(subpos);
  1207. }
  1208. }
  1209. /*
  1210. * We want to have empty print-string for field requests
  1211. * that do not apply (e.g. "authordate" for a tag object)
  1212. */
  1213. static void fill_missing_values(struct atom_value *val)
  1214. {
  1215. int i;
  1216. for (i = 0; i < used_atom_cnt; i++) {
  1217. struct atom_value *v = &val[i];
  1218. if (v->s == NULL)
  1219. v->s = xstrdup("");
  1220. }
  1221. }
  1222. /*
  1223. * val is a list of atom_value to hold returned values. Extract
  1224. * the values for atoms in used_atom array out of (obj, buf, sz).
  1225. * when deref is false, (obj, buf, sz) is the object that is
  1226. * pointed at by the ref itself; otherwise it is the object the
  1227. * ref (which is a tag) refers to.
  1228. */
  1229. static void grab_values(struct atom_value *val, int deref, struct object *obj, void *buf)
  1230. {
  1231. switch (obj->type) {
  1232. case OBJ_TAG:
  1233. grab_tag_values(val, deref, obj);
  1234. grab_sub_body_contents(val, deref, buf);
  1235. grab_person("tagger", val, deref, buf);
  1236. break;
  1237. case OBJ_COMMIT:
  1238. grab_commit_values(val, deref, obj);
  1239. grab_sub_body_contents(val, deref, buf);
  1240. grab_person("author", val, deref, buf);
  1241. grab_person("committer", val, deref, buf);
  1242. break;
  1243. case OBJ_TREE:
  1244. /* grab_tree_values(val, deref, obj, buf, sz); */
  1245. break;
  1246. case OBJ_BLOB:
  1247. /* grab_blob_values(val, deref, obj, buf, sz); */
  1248. break;
  1249. default:
  1250. die("Eh? Object of type %d?", obj->type);
  1251. }
  1252. }
  1253. static inline char *copy_advance(char *dst, const char *src)
  1254. {
  1255. while (*src)
  1256. *dst++ = *src++;
  1257. return dst;
  1258. }
  1259. static const char *lstrip_ref_components(const char *refname, int len)
  1260. {
  1261. long remaining = len;
  1262. const char *start = xstrdup(refname);
  1263. const char *to_free = start;
  1264. if (len < 0) {
  1265. int i;
  1266. const char *p = refname;
  1267. /* Find total no of '/' separated path-components */
  1268. for (i = 0; p[i]; p[i] == '/' ? i++ : *p++)
  1269. ;
  1270. /*
  1271. * The number of components we need to strip is now
  1272. * the total minus the components to be left (Plus one
  1273. * because we count the number of '/', but the number
  1274. * of components is one more than the no of '/').
  1275. */
  1276. remaining = i + len + 1;
  1277. }
  1278. while (remaining > 0) {
  1279. switch (*start++) {
  1280. case '\0':
  1281. free((char *)to_free);
  1282. return xstrdup("");
  1283. case '/':
  1284. remaining--;
  1285. break;
  1286. }
  1287. }
  1288. start = xstrdup(start);
  1289. free((char *)to_free);
  1290. return start;
  1291. }
  1292. static const char *rstrip_ref_components(const char *refname, int len)
  1293. {
  1294. long remaining = len;
  1295. const char *start = xstrdup(refname);
  1296. const char *to_free = start;
  1297. if (len < 0) {
  1298. int i;
  1299. const char *p = refname;
  1300. /* Find total no of '/' separated path-components */
  1301. for (i = 0; p[i]; p[i] == '/' ? i++ : *p++)
  1302. ;
  1303. /*
  1304. * The number of components we need to strip is now
  1305. * the total minus the components to be left (Plus one
  1306. * because we count the number of '/', but the number
  1307. * of components is one more than the no of '/').
  1308. */
  1309. remaining = i + len + 1;
  1310. }
  1311. while (remaining-- > 0) {
  1312. char *p = strrchr(start, '/');
  1313. if (p == NULL) {
  1314. free((char *)to_free);
  1315. return xstrdup("");
  1316. } else
  1317. p[0] = '\0';
  1318. }
  1319. return start;
  1320. }
  1321. static const char *show_ref(struct refname_atom *atom, const char *refname)
  1322. {
  1323. if (atom->option == R_SHORT)
  1324. return shorten_unambiguous_ref(refname, warn_ambiguous_refs);
  1325. else if (atom->option == R_LSTRIP)
  1326. return lstrip_ref_components(refname, atom->lstrip);
  1327. else if (atom->option == R_RSTRIP)
  1328. return rstrip_ref_components(refname, atom->rstrip);
  1329. else
  1330. return xstrdup(refname);
  1331. }
  1332. static void fill_remote_ref_details(struct used_atom *atom, const char *refname,
  1333. struct branch *branch, const char **s)
  1334. {
  1335. int num_ours, num_theirs;
  1336. if (atom->u.remote_ref.option == RR_REF)
  1337. *s = show_ref(&atom->u.remote_ref.refname, refname);
  1338. else if (atom->u.remote_ref.option == RR_TRACK) {
  1339. if (stat_tracking_info(branch, &num_ours, &num_theirs,
  1340. NULL, atom->u.remote_ref.push,
  1341. AHEAD_BEHIND_FULL) < 0) {
  1342. *s = xstrdup(msgs.gone);
  1343. } else if (!num_ours && !num_theirs)
  1344. *s = xstrdup("");
  1345. else if (!num_ours)
  1346. *s = xstrfmt(msgs.behind, num_theirs);
  1347. else if (!num_theirs)
  1348. *s = xstrfmt(msgs.ahead, num_ours);
  1349. else
  1350. *s = xstrfmt(msgs.ahead_behind,
  1351. num_ours, num_theirs);
  1352. if (!atom->u.remote_ref.nobracket && *s[0]) {
  1353. const char *to_free = *s;
  1354. *s = xstrfmt("[%s]", *s);
  1355. free((void *)to_free);
  1356. }
  1357. } else if (atom->u.remote_ref.option == RR_TRACKSHORT) {
  1358. if (stat_tracking_info(branch, &num_ours, &num_theirs,
  1359. NULL, atom->u.remote_ref.push,
  1360. AHEAD_BEHIND_FULL) < 0) {
  1361. *s = xstrdup("");
  1362. return;
  1363. }
  1364. if (!num_ours && !num_theirs)
  1365. *s = xstrdup("=");
  1366. else if (!num_ours)
  1367. *s = xstrdup("<");
  1368. else if (!num_theirs)
  1369. *s = xstrdup(">");
  1370. else
  1371. *s = xstrdup("<>");
  1372. } else if (atom->u.remote_ref.option == RR_REMOTE_NAME) {
  1373. int explicit;
  1374. const char *remote = atom->u.remote_ref.push ?
  1375. pushremote_for_branch(branch, &explicit) :
  1376. remote_for_branch(branch, &explicit);
  1377. *s = xstrdup(explicit ? remote : "");
  1378. } else if (atom->u.remote_ref.option == RR_REMOTE_REF) {
  1379. const char *merge;
  1380. merge = remote_ref_for_branch(branch, atom->u.remote_ref.push);
  1381. *s = xstrdup(merge ? merge : "");
  1382. } else
  1383. BUG("unhandled RR_* enum");
  1384. }
  1385. char *get_head_description(void)
  1386. {
  1387. struct strbuf desc = STRBUF_INIT;
  1388. struct wt_status_state state;
  1389. memset(&state, 0, sizeof(state));
  1390. wt_status_get_state(the_repository, &state, 1);
  1391. /*
  1392. * The ( character must be hard-coded and not part of a localizable
  1393. * string, since the description is used as a sort key and compared
  1394. * with ref names.
  1395. */
  1396. strbuf_addch(&desc, '(');
  1397. if (state.rebase_in_progress ||
  1398. state.rebase_interactive_in_progress) {
  1399. if (state.branch)
  1400. strbuf_addf(&desc, _("no branch, rebasing %s"),
  1401. state.branch);
  1402. else
  1403. strbuf_addf(&desc, _("no branch, rebasing detached HEAD %s"),
  1404. state.detached_from);
  1405. } else if (state.bisect_in_progress)
  1406. strbuf_addf(&desc, _("no branch, bisect started on %s"),
  1407. state.branch);
  1408. else if (state.detached_from) {
  1409. if (state.detached_at)
  1410. strbuf_addstr(&desc, HEAD_DETACHED_AT);
  1411. else
  1412. strbuf_addstr(&desc, HEAD_DETACHED_FROM);
  1413. strbuf_addstr(&desc, state.detached_from);
  1414. }
  1415. else
  1416. strbuf_addstr(&desc, _("no branch"));
  1417. strbuf_addch(&desc, ')');
  1418. wt_status_state_free_buffers(&state);
  1419. return strbuf_detach(&desc, NULL);
  1420. }
  1421. static const char *get_symref(struct used_atom *atom, struct ref_array_item *ref)
  1422. {
  1423. if (!ref->symref)
  1424. return xstrdup("");
  1425. else
  1426. return show_ref(&atom->u.refname, ref->symref);
  1427. }
  1428. static const char *get_refname(struct used_atom *atom, struct ref_array_item *ref)
  1429. {
  1430. if (ref->kind & FILTER_REFS_DETACHED_HEAD)
  1431. return get_head_description();
  1432. return show_ref(&atom->u.refname, ref->refname);
  1433. }
  1434. static int get_object(struct ref_array_item *ref, int deref, struct object **obj,
  1435. struct expand_data *oi, struct strbuf *err)
  1436. {
  1437. /* parse_object_buffer() will set eaten to 0 if free() will be needed */
  1438. int eaten = 1;
  1439. if (oi->info.contentp) {
  1440. /* We need to know that to use parse_object_buffer properly */
  1441. oi->info.sizep = &oi->size;
  1442. oi->info.typep = &oi->type;
  1443. }
  1444. if (oid_object_info_extended(the_repository, &oi->oid, &oi->info,
  1445. OBJECT_INFO_LOOKUP_REPLACE))
  1446. return strbuf_addf_ret(err, -1, _("missing object %s for %s"),
  1447. oid_to_hex(&oi->oid), ref->refname);
  1448. if (oi->info.disk_sizep && oi->disk_size < 0)
  1449. BUG("Object size is less than zero.");
  1450. if (oi->info.contentp) {
  1451. *obj = parse_object_buffer(the_repository, &oi->oid, oi->type, oi->size, oi->content, &eaten);
  1452. if (!obj) {
  1453. if (!eaten)
  1454. free(oi->content);
  1455. return strbuf_addf_ret(err, -1, _("parse_object_buffer failed on %s for %s"),
  1456. oid_to_hex(&oi->oid), ref->refname);
  1457. }
  1458. grab_values(ref->value, deref, *obj, oi->content);
  1459. }
  1460. grab_common_values(ref->value, deref, oi);
  1461. if (!eaten)
  1462. free(oi->content);
  1463. return 0;
  1464. }
  1465. static void populate_worktree_map(struct hashmap *map, struct worktree **worktrees)
  1466. {
  1467. int i;
  1468. for (i = 0; worktrees[i]; i++) {
  1469. if (worktrees[i]->head_ref) {
  1470. struct ref_to_worktree_entry *entry;
  1471. entry = xmalloc(sizeof(*entry));
  1472. entry->wt = worktrees[i];
  1473. hashmap_entry_init(&entry->ent,
  1474. strhash(worktrees[i]->head_ref));
  1475. hashmap_add(map, &entry->ent);
  1476. }
  1477. }
  1478. }
  1479. static void lazy_init_worktree_map(void)
  1480. {
  1481. if (ref_to_worktree_map.worktrees)
  1482. return;
  1483. ref_to_worktree_map.worktrees = get_worktrees();
  1484. hashmap_init(&(ref_to_worktree_map.map), ref_to_worktree_map_cmpfnc, NULL, 0);
  1485. populate_worktree_map(&(ref_to_worktree_map.map), ref_to_worktree_map.worktrees);
  1486. }
  1487. static char *get_worktree_path(const struct used_atom *atom, const struct ref_array_item *ref)
  1488. {
  1489. struct hashmap_entry entry, *e;
  1490. struct ref_to_worktree_entry *lookup_result;
  1491. lazy_init_worktree_map();
  1492. hashmap_entry_init(&entry, strhash(ref->refname));
  1493. e = hashmap_get(&(ref_to_worktree_map.map), &entry, ref->refname);
  1494. if (!e)
  1495. return xstrdup("");
  1496. lookup_result = container_of(e, struct ref_to_worktree_entry, ent);
  1497. return xstrdup(lookup_result->wt->path);
  1498. }
  1499. /*
  1500. * Parse the object referred by ref, and grab needed value.
  1501. */
  1502. static int populate_value(struct ref_array_item *ref, struct strbuf *err)
  1503. {
  1504. struct object *obj;
  1505. int i;
  1506. struct object_info empty = OBJECT_INFO_INIT;
  1507. ref->value = xcalloc(used_atom_cnt, sizeof(struct atom_value));
  1508. if (need_symref && (ref->flag & REF_ISSYMREF) && !ref->symref) {
  1509. ref->symref = resolve_refdup(ref->refname, RESOLVE_REF_READING,
  1510. NULL, NULL);
  1511. if (!ref->symref)
  1512. ref->symref = xstrdup("");
  1513. }
  1514. /* Fill in specials first */
  1515. for (i = 0; i < used_atom_cnt; i++) {
  1516. struct used_atom *atom = &used_atom[i];
  1517. const char *name = used_atom[i].name;
  1518. struct atom_value *v = &ref->value[i];
  1519. int deref = 0;
  1520. const char *refname;
  1521. struct branch *branch = NULL;
  1522. v->handler = append_atom;
  1523. v->atom = atom;
  1524. if (*name == '*') {
  1525. deref = 1;
  1526. name++;
  1527. }
  1528. if (starts_with(name, "refname"))
  1529. refname = get_refname(atom, ref);
  1530. else if (!strcmp(name, "worktreepath")) {
  1531. if (ref->kind == FILTER_REFS_BRANCHES)
  1532. v->s = get_worktree_path(atom, ref);
  1533. else
  1534. v->s = xstrdup("");
  1535. continue;
  1536. }
  1537. else if (starts_with(name, "symref"))
  1538. refname = get_symref(atom, ref);
  1539. else if (starts_with(name, "upstream")) {
  1540. const char *branch_name;
  1541. /* only local branches may have an upstream */
  1542. if (!skip_prefix(ref->refname, "refs/heads/",
  1543. &branch_name)) {
  1544. v->s = xstrdup("");
  1545. continue;
  1546. }
  1547. branch = branch_get(branch_name);
  1548. refname = branch_get_upstream(branch, NULL);
  1549. if (refname)
  1550. fill_remote_ref_details(atom, refname, branch, &v->s);
  1551. else
  1552. v->s = xstrdup("");
  1553. continue;
  1554. } else if (atom->u.remote_ref.push) {
  1555. const char *branch_name;
  1556. v->s = xstrdup("");
  1557. if (!skip_prefix(ref->refname, "refs/heads/",
  1558. &branch_name))
  1559. continue;
  1560. branch = branch_get(branch_name);
  1561. if (atom->u.remote_ref.push_remote)
  1562. refname = NULL;
  1563. else {
  1564. refname = branch_get_push(branch, NULL);
  1565. if (!refname)
  1566. continue;
  1567. }
  1568. /* We will definitely re-init v->s on the next line. */
  1569. free((char *)v->s);
  1570. fill_remote_ref_details(atom, refname, branch, &v->s);
  1571. continue;
  1572. } else if (starts_with(name, "color:")) {
  1573. v->s = xstrdup(atom->u.color);
  1574. continue;
  1575. } else if (!strcmp(name, "flag")) {
  1576. char buf[256], *cp = buf;
  1577. if (ref->flag & REF_ISSYMREF)
  1578. cp = copy_advance(cp, ",symref");
  1579. if (ref->flag & REF_ISPACKED)
  1580. cp = copy_advance(cp, ",packed");
  1581. if (cp == buf)
  1582. v->s = xstrdup("");
  1583. else {
  1584. *cp = '\0';
  1585. v->s = xstrdup(buf + 1);
  1586. }
  1587. continue;
  1588. } else if (!deref && grab_oid(name, "objectname", &ref->objectname, v, atom)) {
  1589. continue;
  1590. } else if (!strcmp(name, "HEAD")) {
  1591. if (atom->u.head && !strcmp(ref->refname, atom->u.head))
  1592. v->s = xstrdup("*");
  1593. else
  1594. v->s = xstrdup(" ");
  1595. continue;
  1596. } else if (starts_with(name, "align")) {
  1597. v->handler = align_atom_handler;
  1598. v->s = xstrdup("");
  1599. continue;
  1600. } else if (!strcmp(name, "end")) {
  1601. v->handler = end_atom_handler;
  1602. v->s = xstrdup("");
  1603. continue;
  1604. } else if (starts_with(name, "if")) {
  1605. const char *s;
  1606. if (skip_prefix(name, "if:", &s))
  1607. v->s = xstrdup(s);
  1608. else
  1609. v->s = xstrdup("");
  1610. v->handler = if_atom_handler;
  1611. continue;
  1612. } else if (!strcmp(name, "then")) {
  1613. v->handler = then_atom_handler;
  1614. v->s = xstrdup("");
  1615. continue;
  1616. } else if (!strcmp(name, "else")) {
  1617. v->handler = else_atom_handler;
  1618. v->s = xstrdup("");
  1619. continue;
  1620. } else
  1621. continue;
  1622. if (!deref)
  1623. v->s = xstrdup(refname);
  1624. else
  1625. v->s = xstrfmt("%s^{}", refname);
  1626. free((char *)refname);
  1627. }
  1628. for (i = 0; i < used_atom_cnt; i++) {
  1629. struct atom_value *v = &ref->value[i];
  1630. if (v->s == NULL && used_atom[i].source == SOURCE_NONE)
  1631. return strbuf_addf_ret(err, -1, _("missing object %s for %s"),
  1632. oid_to_hex(&ref->objectname), ref->refname);
  1633. }
  1634. if (need_tagged)
  1635. oi.info.contentp = &oi.content;
  1636. if (!memcmp(&oi.info, &empty, sizeof(empty)) &&
  1637. !memcmp(&oi_deref.info, &empty, sizeof(empty)))
  1638. return 0;
  1639. oi.oid = ref->objectname;
  1640. if (get_object(ref, 0, &obj, &oi, err))
  1641. return -1;
  1642. /*
  1643. * If there is no atom that wants to know about tagged
  1644. * object, we are done.
  1645. */
  1646. if (!need_tagged || (obj->type != OBJ_TAG))
  1647. return 0;
  1648. /*
  1649. * If it is a tag object, see if we use a value that derefs
  1650. * the object, and if we do grab the object it refers to.
  1651. */
  1652. oi_deref.oid = *get_tagged_oid((struct tag *)obj);
  1653. /*
  1654. * NEEDSWORK: This derefs tag only once, which
  1655. * is good to deal with chains of trust, but
  1656. * is not consistent with what deref_tag() does
  1657. * which peels the onion to the core.
  1658. */
  1659. return get_object(ref, 1, &obj, &oi_deref, err);
  1660. }
  1661. /*
  1662. * Given a ref, return the value for the atom. This lazily gets value
  1663. * out of the object by calling populate value.
  1664. */
  1665. static int get_ref_atom_value(struct ref_array_item *ref, int atom,
  1666. struct atom_value **v, struct strbuf *err)
  1667. {
  1668. if (!ref->value) {
  1669. if (populate_value(ref, err))
  1670. return -1;
  1671. fill_missing_values(ref->value);
  1672. }
  1673. *v = &ref->value[atom];
  1674. return 0;
  1675. }
  1676. /*
  1677. * Return 1 if the refname matches one of the patterns, otherwise 0.
  1678. * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
  1679. * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
  1680. * matches "refs/heads/mas*", too).
  1681. */
  1682. static int match_pattern(const struct ref_filter *filter, const char *refname)
  1683. {
  1684. const char **patterns = filter->name_patterns;
  1685. unsigned flags = 0;
  1686. if (filter->ignore_case)
  1687. flags |= WM_CASEFOLD;
  1688. /*
  1689. * When no '--format' option is given we need to skip the prefix
  1690. * for matching refs of tags and branches.
  1691. */
  1692. (void)(skip_prefix(refname, "refs/tags/", &refname) ||
  1693. skip_prefix(refname, "refs/heads/", &refname) ||
  1694. skip_prefix(refname, "refs/remotes/", &refname) ||
  1695. skip_prefix(refname, "refs/", &refname));
  1696. for (; *patterns; patterns++) {
  1697. if (!wildmatch(*patterns, refname, flags))
  1698. return 1;
  1699. }
  1700. return 0;
  1701. }
  1702. /*
  1703. * Return 1 if the refname matches one of the patterns, otherwise 0.
  1704. * A pattern can be path prefix (e.g. a refname "refs/heads/master"
  1705. * matches a pattern "refs/heads/" but not "refs/heads/m") or a
  1706. * wildcard (e.g. the same ref matches "refs/heads/m*", too).
  1707. */
  1708. static int match_name_as_path(const struct ref_filter *filter, const char *refname)
  1709. {
  1710. const char **pattern = filter->name_patterns;
  1711. int namelen = strlen(refname);
  1712. unsigned flags = WM_PATHNAME;
  1713. if (filter->ignore_case)
  1714. flags |= WM_CASEFOLD;
  1715. for (; *pattern; pattern++) {
  1716. const char *p = *pattern;
  1717. int plen = strlen(p);
  1718. if ((plen <= namelen) &&
  1719. !strncmp(refname, p, plen) &&
  1720. (refname[plen] == '\0' ||
  1721. refname[plen] == '/' ||
  1722. p[plen-1] == '/'))
  1723. return 1;
  1724. if (!wildmatch(p, refname, flags))
  1725. return 1;
  1726. }
  1727. return 0;
  1728. }
  1729. /* Return 1 if the refname matches one of the patterns, otherwise 0. */
  1730. static int filter_pattern_match(struct ref_filter *filter, const char *refname)
  1731. {
  1732. if (!*filter->name_patterns)
  1733. return 1; /* No pattern always matches */
  1734. if (filter->match_as_path)
  1735. return match_name_as_path(filter, refname);
  1736. return match_pattern(filter, refname);
  1737. }
  1738. static int qsort_strcmp(const void *va, const void *vb)
  1739. {
  1740. const char *a = *(const char **)va;
  1741. const char *b = *(const char **)vb;
  1742. return strcmp(a, b);
  1743. }
  1744. static void find_longest_prefixes_1(struct string_list *out,
  1745. struct strbuf *prefix,
  1746. const char **patterns, size_t nr)
  1747. {
  1748. size_t i;
  1749. for (i = 0; i < nr; i++) {
  1750. char c = patterns[i][prefix->len];
  1751. if (!c || is_glob_special(c)) {
  1752. string_list_append(out, prefix->buf);
  1753. return;
  1754. }
  1755. }
  1756. i = 0;
  1757. while (i < nr) {
  1758. size_t end;
  1759. /*
  1760. * Set "end" to the index of the element _after_ the last one
  1761. * in our group.
  1762. */
  1763. for (end = i + 1; end < nr; end++) {
  1764. if (patterns[i][prefix->len] != patterns[end][prefix->len])
  1765. break;
  1766. }
  1767. strbuf_addch(prefix, patterns[i][prefix->len]);
  1768. find_longest_prefixes_1(out, prefix, patterns + i, end - i);
  1769. strbuf_setlen(prefix, prefix->len - 1);
  1770. i = end;
  1771. }
  1772. }
  1773. static void find_longest_prefixes(struct string_list *out,
  1774. const char **patterns)
  1775. {
  1776. struct strvec sorted = STRVEC_INIT;
  1777. struct strbuf prefix = STRBUF_INIT;
  1778. strvec_pushv(&sorted, patterns);
  1779. QSORT(sorted.v, sorted.nr, qsort_strcmp);
  1780. find_longest_prefixes_1(out, &prefix, sorted.v, sorted.nr);
  1781. strvec_clear(&sorted);
  1782. strbuf_release(&prefix);
  1783. }
  1784. /*
  1785. * This is the same as for_each_fullref_in(), but it tries to iterate
  1786. * only over the patterns we'll care about. Note that it _doesn't_ do a full
  1787. * pattern match, so the callback still has to match each ref individually.
  1788. */
  1789. static int for_each_fullref_in_pattern(struct ref_filter *filter,
  1790. each_ref_fn cb,
  1791. void *cb_data,
  1792. int broken)
  1793. {
  1794. struct string_list prefixes = STRING_LIST_INIT_DUP;
  1795. struct string_list_item *prefix;
  1796. int ret;
  1797. if (!filter->match_as_path) {
  1798. /*
  1799. * in this case, the patterns are applied after
  1800. * prefixes like "refs/heads/" etc. are stripped off,
  1801. * so we have to look at everything:
  1802. */
  1803. return for_each_fullref_in("", cb, cb_data, broken);
  1804. }
  1805. if (filter->ignore_case) {
  1806. /*
  1807. * we can't handle case-insensitive comparisons,
  1808. * so just return everything and let the caller
  1809. * sort it out.
  1810. */
  1811. return for_each_fullref_in("", cb, cb_data, broken);
  1812. }
  1813. if (!filter->name_patterns[0]) {
  1814. /* no patterns; we have to look at everything */
  1815. return for_each_fullref_in("", cb, cb_data, broken);
  1816. }
  1817. find_longest_prefixes(&prefixes, filter->name_patterns);
  1818. for_each_string_list_item(prefix, &prefixes) {
  1819. ret = for_each_fullref_in(prefix->string, cb, cb_data, broken);
  1820. if (ret)
  1821. break;
  1822. }
  1823. string_list_clear(&prefixes, 0);
  1824. return ret;
  1825. }
  1826. /*
  1827. * Given a ref (oid, refname), check if the ref belongs to the array
  1828. * of oids. If the given ref is a tag, check if the given tag points
  1829. * at one of the oids in the given oid array.
  1830. * NEEDSWORK:
  1831. * 1. Only a single level of indirection is obtained, we might want to
  1832. * change this to account for multiple levels (e.g. annotated tags
  1833. * pointing to annotated tags pointing to a commit.)
  1834. * 2. As the refs are cached we might know what refname peels to without
  1835. * the need to parse the object via parse_object(). peel_ref() might be a
  1836. * more efficient alternative to obtain the pointee.
  1837. */
  1838. static const struct object_id *match_points_at(struct oid_array *points_at,
  1839. const struct object_id *oid,
  1840. const char *refname)
  1841. {
  1842. const struct object_id *tagged_oid = NULL;
  1843. struct object *obj;
  1844. if (oid_array_lookup(points_at, oid) >= 0)
  1845. return oid;
  1846. obj = parse_object(the_repository, oid);
  1847. if (!obj)
  1848. die(_("malformed object at '%s'"), refname);
  1849. if (obj->type == OBJ_TAG)
  1850. tagged_oid = get_tagged_oid((struct tag *)obj);
  1851. if (tagged_oid && oid_array_lookup(points_at, tagged_oid) >= 0)
  1852. return tagged_oid;
  1853. return NULL;
  1854. }
  1855. /*
  1856. * Allocate space for a new ref_array_item and copy the name and oid to it.
  1857. *
  1858. * Callers can then fill in other struct members at their leisure.
  1859. */
  1860. static struct ref_array_item *new_ref_array_item(const char *refname,
  1861. const struct object_id *oid)
  1862. {
  1863. struct ref_array_item *ref;
  1864. FLEX_ALLOC_STR(ref, refname, refname);
  1865. oidcpy(&ref->objectname, oid);
  1866. return ref;
  1867. }
  1868. struct ref_array_item *ref_array_push(struct ref_array *array,
  1869. const char *refname,
  1870. const struct object_id *oid)
  1871. {
  1872. struct ref_array_item *ref = new_ref_array_item(refname, oid);
  1873. ALLOC_GROW(array->items, array->nr + 1, array->alloc);
  1874. array->items[array->nr++] = ref;
  1875. return ref;
  1876. }
  1877. static int ref_kind_from_refname(const char *refname)
  1878. {
  1879. unsigned int i;
  1880. static struct {
  1881. const char *prefix;
  1882. unsigned int kind;
  1883. } ref_kind[] = {
  1884. { "refs/heads/" , FILTER_REFS_BRANCHES },
  1885. { "refs/remotes/" , FILTER_REFS_REMOTES },
  1886. { "refs/tags/", FILTER_REFS_TAGS}
  1887. };
  1888. if (!strcmp(refname, "HEAD"))
  1889. return FILTER_REFS_DETACHED_HEAD;
  1890. for (i = 0; i < ARRAY_SIZE(ref_kind); i++) {
  1891. if (starts_with(refname, ref_kind[i].prefix))
  1892. return ref_kind[i].kind;
  1893. }
  1894. return FILTER_REFS_OTHERS;
  1895. }
  1896. static int filter_ref_kind(struct ref_filter *filter, const char *refname)
  1897. {
  1898. if (filter->kind == FILTER_REFS_BRANCHES ||
  1899. filter->kind == FILTER_REFS_REMOTES ||
  1900. filter->kind == FILTER_REFS_TAGS)
  1901. return filter->kind;
  1902. return ref_kind_from_refname(refname);
  1903. }
  1904. struct ref_filter_cbdata {
  1905. struct ref_array *array;
  1906. struct ref_filter *filter;
  1907. struct contains_cache contains_cache;
  1908. struct contains_cache no_contains_cache;
  1909. };
  1910. /*
  1911. * A call-back given to for_each_ref(). Filter refs and keep them for
  1912. * later object processing.
  1913. */
  1914. static int ref_filter_handler(const char *refname, const struct object_id *oid, int flag, void *cb_data)
  1915. {
  1916. struct ref_filter_cbdata *ref_cbdata = cb_data;
  1917. struct ref_filter *filter = ref_cbdata->filter;
  1918. struct ref_array_item *ref;
  1919. struct commit *commit = NULL;
  1920. unsigned int kind;
  1921. if (flag & REF_BAD_NAME) {
  1922. warning(_("ignoring ref with broken name %s"), refname);
  1923. return 0;
  1924. }
  1925. if (flag & REF_ISBROKEN) {
  1926. warning(_("ignoring broken ref %s"), refname);
  1927. return 0;
  1928. }
  1929. /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
  1930. kind = filter_ref_kind(filter, refname);
  1931. if (!(kind & filter->kind))
  1932. return 0;
  1933. if (!filter_pattern_match(filter, refname))
  1934. return 0;
  1935. if (filter->points_at.nr && !match_points_at(&filter->points_at, oid, refname))
  1936. return 0;
  1937. /*
  1938. * A merge filter is applied on refs pointing to commits. Hence
  1939. * obtain the commit using the 'oid' available and discard all
  1940. * non-commits early. The actual filtering is done later.
  1941. */
  1942. if (filter->reachable_from || filter->unreachable_from ||
  1943. filter->with_commit || filter->no_commit || filter->verbose) {
  1944. commit = lookup_commit_reference_gently(the_repository, oid, 1);
  1945. if (!commit)
  1946. return 0;
  1947. /* We perform the filtering for the '--contains' option... */
  1948. if (filter->with_commit &&
  1949. !commit_contains(filter, commit, filter->with_commit, &ref_cbdata->contains_cache))
  1950. return 0;
  1951. /* ...or for the `--no-contains' option */
  1952. if (filter->no_commit &&
  1953. commit_contains(filter, commit, filter->no_commit, &ref_cbdata->no_contains_cache))
  1954. return 0;
  1955. }
  1956. /*
  1957. * We do not open the object yet; sort may only need refname
  1958. * to do its job and the resulting list may yet to be pruned
  1959. * by maxcount logic.
  1960. */
  1961. ref = ref_array_push(ref_cbdata->array, refname, oid);
  1962. ref->commit = commit;
  1963. ref->flag = flag;
  1964. ref->kind = kind;
  1965. return 0;
  1966. }
  1967. /* Free memory allocated for a ref_array_item */
  1968. static void free_array_item(struct ref_array_item *item)
  1969. {
  1970. free((char *)item->symref);
  1971. if (item->value) {
  1972. int i;
  1973. for (i = 0; i < used_atom_cnt; i++)
  1974. free((char *)item->value[i].s);
  1975. free(item->value);
  1976. }
  1977. free(item);
  1978. }
  1979. /* Free all memory allocated for ref_array */
  1980. void ref_array_clear(struct ref_array *array)
  1981. {
  1982. int i;
  1983. for (i = 0; i < array->nr; i++)
  1984. free_array_item(array->items[i]);
  1985. FREE_AND_NULL(array->items);
  1986. array->nr = array->alloc = 0;
  1987. for (i = 0; i < used_atom_cnt; i++)
  1988. free((char *)used_atom[i].name);
  1989. FREE_AND_NULL(used_atom);
  1990. used_atom_cnt = 0;
  1991. if (ref_to_worktree_map.worktrees) {
  1992. hashmap_free_entries(&(ref_to_worktree_map.map),
  1993. struct ref_to_worktree_entry, ent);
  1994. free_worktrees(ref_to_worktree_map.worktrees);
  1995. ref_to_worktree_map.worktrees = NULL;
  1996. }
  1997. }
  1998. #define EXCLUDE_REACHED 0
  1999. #define INCLUDE_REACHED 1
  2000. static void reach_filter(struct ref_array *array,
  2001. struct commit_list *check_reachable,
  2002. int include_reached)
  2003. {
  2004. struct rev_info revs;
  2005. int i, old_nr;
  2006. struct commit **to_clear;
  2007. struct commit_list *cr;
  2008. if (!check_reachable)
  2009. return;
  2010. to_clear = xcalloc(sizeof(struct commit *), array->nr);
  2011. repo_init_revisions(the_repository, &revs, NULL);
  2012. for (i = 0; i < array->nr; i++) {
  2013. struct ref_array_item *item = array->items[i];
  2014. add_pending_object(&revs, &item->commit->object, item->refname);
  2015. to_clear[i] = item->commit;
  2016. }
  2017. for (cr = check_reachable; cr; cr = cr->next) {
  2018. struct commit *merge_commit = cr->item;
  2019. merge_commit->object.flags |= UNINTERESTING;
  2020. add_pending_object(&revs, &merge_commit->object, "");
  2021. }
  2022. revs.limited = 1;
  2023. if (prepare_revision_walk(&revs))
  2024. die(_("revision walk setup failed"));
  2025. old_nr = array->nr;
  2026. array->nr = 0;
  2027. for (i = 0; i < old_nr; i++) {
  2028. struct ref_array_item *item = array->items[i];
  2029. struct commit *commit = item->commit;
  2030. int is_merged = !!(commit->object.flags & UNINTERESTING);
  2031. if (is_merged == include_reached)
  2032. array->items[array->nr++] = array->items[i];
  2033. else
  2034. free_array_item(item);
  2035. }
  2036. clear_commit_marks_many(old_nr, to_clear, ALL_REV_FLAGS);
  2037. while (check_reachable) {
  2038. struct commit *merge_commit = pop_commit(&check_reachable);
  2039. clear_commit_marks(merge_commit, ALL_REV_FLAGS);
  2040. }
  2041. free(to_clear);
  2042. }
  2043. /*
  2044. * API for filtering a set of refs. Based on the type of refs the user
  2045. * has requested, we iterate through those refs and apply filters
  2046. * as per the given ref_filter structure and finally store the
  2047. * filtered refs in the ref_array structure.
  2048. */
  2049. int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int type)
  2050. {
  2051. struct ref_filter_cbdata ref_cbdata;
  2052. int ret = 0;
  2053. unsigned int broken = 0;
  2054. ref_cbdata.array = array;
  2055. ref_cbdata.filter = filter;
  2056. if (type & FILTER_REFS_INCLUDE_BROKEN)
  2057. broken = 1;
  2058. filter->kind = type & FILTER_REFS_KIND_MASK;
  2059. init_contains_cache(&ref_cbdata.contains_cache);
  2060. init_contains_cache(&ref_cbdata.no_contains_cache);
  2061. /* Simple per-ref filtering */
  2062. if (!filter->kind)
  2063. die("filter_refs: invalid type");
  2064. else {
  2065. /*
  2066. * For common cases where we need only branches or remotes or tags,
  2067. * we only iterate through those refs. If a mix of refs is needed,
  2068. * we iterate over all refs and filter out required refs with the help
  2069. * of filter_ref_kind().
  2070. */
  2071. if (filter->kind == FILTER_REFS_BRANCHES)
  2072. ret = for_each_fullref_in("refs/heads/", ref_filter_handler, &ref_cbdata, broken);
  2073. else if (filter->kind == FILTER_REFS_REMOTES)
  2074. ret = for_each_fullref_in("refs/remotes/", ref_filter_handler, &ref_cbdata, broken);
  2075. else if (filter->kind == FILTER_REFS_TAGS)
  2076. ret = for_each_fullref_in("refs/tags/", ref_filter_handler, &ref_cbdata, broken);
  2077. else if (filter->kind & FILTER_REFS_ALL)
  2078. ret = for_each_fullref_in_pattern(filter, ref_filter_handler, &ref_cbdata, broken);
  2079. if (!ret && (filter->kind & FILTER_REFS_DETACHED_HEAD))
  2080. head_ref(ref_filter_handler, &ref_cbdata);
  2081. }
  2082. clear_contains_cache(&ref_cbdata.contains_cache);
  2083. clear_contains_cache(&ref_cbdata.no_contains_cache);
  2084. /* Filters that need revision walking */
  2085. reach_filter(array, filter->reachable_from, INCLUDE_REACHED);
  2086. reach_filter(array, filter->unreachable_from, EXCLUDE_REACHED);
  2087. return ret;
  2088. }
  2089. static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, struct ref_array_item *b)
  2090. {
  2091. struct atom_value *va, *vb;
  2092. int cmp;
  2093. cmp_type cmp_type = used_atom[s->atom].type;
  2094. int (*cmp_fn)(const char *, const char *);
  2095. struct strbuf err = STRBUF_INIT;
  2096. if (get_ref_atom_value(a, s->atom, &va, &err))
  2097. die("%s", err.buf);
  2098. if (get_ref_atom_value(b, s->atom, &vb, &err))
  2099. die("%s", err.buf);
  2100. strbuf_release(&err);
  2101. cmp_fn = s->ignore_case ? strcasecmp : strcmp;
  2102. if (s->version)
  2103. cmp = versioncmp(va->s, vb->s);
  2104. else if (cmp_type == FIELD_STR)
  2105. cmp = cmp_fn(va->s, vb->s);
  2106. else {
  2107. if (va->value < vb->value)
  2108. cmp = -1;
  2109. else if (va->value == vb->value)
  2110. cmp = 0;
  2111. else
  2112. cmp = 1;
  2113. }
  2114. return (s->reverse) ? -cmp : cmp;
  2115. }
  2116. static int compare_refs(const void *a_, const void *b_, void *ref_sorting)
  2117. {
  2118. struct ref_array_item *a = *((struct ref_array_item **)a_);
  2119. struct ref_array_item *b = *((struct ref_array_item **)b_);
  2120. struct ref_sorting *s;
  2121. for (s = ref_sorting; s; s = s->next) {
  2122. int cmp = cmp_ref_sorting(s, a, b);
  2123. if (cmp)
  2124. return cmp;
  2125. }
  2126. s = ref_sorting;
  2127. return s && s->ignore_case ?
  2128. strcasecmp(a->refname, b->refname) :
  2129. strcmp(a->refname, b->refname);
  2130. }
  2131. void ref_sorting_icase_all(struct ref_sorting *sorting, int flag)
  2132. {
  2133. for (; sorting; sorting = sorting->next)
  2134. sorting->ignore_case = !!flag;
  2135. }
  2136. void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array)
  2137. {
  2138. QSORT_S(array->items, array->nr, compare_refs, sorting);
  2139. }
  2140. static void append_literal(const char *cp, const char *ep, struct ref_formatting_state *state)
  2141. {
  2142. struct strbuf *s = &state->stack->output;
  2143. while (*cp && (!ep || cp < ep)) {
  2144. if (*cp == '%') {
  2145. if (cp[1] == '%')
  2146. cp++;
  2147. else {
  2148. int ch = hex2chr(cp + 1);
  2149. if (0 <= ch) {
  2150. strbuf_addch(s, ch);
  2151. cp += 3;
  2152. continue;
  2153. }
  2154. }
  2155. }
  2156. strbuf_addch(s, *cp);
  2157. cp++;
  2158. }
  2159. }
  2160. int format_ref_array_item(struct ref_array_item *info,
  2161. const struct ref_format *format,
  2162. struct strbuf *final_buf,
  2163. struct strbuf *error_buf)
  2164. {
  2165. const char *cp, *sp, *ep;
  2166. struct ref_formatting_state state = REF_FORMATTING_STATE_INIT;
  2167. state.quote_style = format->quote_style;
  2168. push_stack_element(&state.stack);
  2169. for (cp = format->format; *cp && (sp = find_next(cp)); cp = ep + 1) {
  2170. struct atom_value *atomv;
  2171. int pos;
  2172. ep = strchr(sp, ')');
  2173. if (cp < sp)
  2174. append_literal(cp, sp, &state);
  2175. pos = parse_ref_filter_atom(format, sp + 2, ep, error_buf);
  2176. if (pos < 0 || get_ref_atom_value(info, pos, &atomv, error_buf) ||
  2177. atomv->handler(atomv, &state, error_buf)) {
  2178. pop_stack_element(&state.stack);
  2179. return -1;
  2180. }
  2181. }
  2182. if (*cp) {
  2183. sp = cp + strlen(cp);
  2184. append_literal(cp, sp, &state);
  2185. }
  2186. if (format->need_color_reset_at_eol) {
  2187. struct atom_value resetv;
  2188. resetv.s = GIT_COLOR_RESET;
  2189. if (append_atom(&resetv, &state, error_buf)) {
  2190. pop_stack_element(&state.stack);
  2191. return -1;
  2192. }
  2193. }
  2194. if (state.stack->prev) {
  2195. pop_stack_element(&state.stack);
  2196. return strbuf_addf_ret(error_buf, -1, _("format: %%(end) atom missing"));
  2197. }
  2198. strbuf_addbuf(final_buf, &state.stack->output);
  2199. pop_stack_element(&state.stack);
  2200. return 0;
  2201. }
  2202. void show_ref_array_item(struct ref_array_item *info,
  2203. const struct ref_format *format)
  2204. {
  2205. struct strbuf final_buf = STRBUF_INIT;
  2206. struct strbuf error_buf = STRBUF_INIT;
  2207. if (format_ref_array_item(info, format, &final_buf, &error_buf))
  2208. die("%s", error_buf.buf);
  2209. fwrite(final_buf.buf, 1, final_buf.len, stdout);
  2210. strbuf_release(&error_buf);
  2211. strbuf_release(&final_buf);
  2212. putchar('\n');
  2213. }
  2214. void pretty_print_ref(const char *name, const struct object_id *oid,
  2215. const struct ref_format *format)
  2216. {
  2217. struct ref_array_item *ref_item;
  2218. ref_item = new_ref_array_item(name, oid);
  2219. ref_item->kind = ref_kind_from_refname(name);
  2220. show_ref_array_item(ref_item, format);
  2221. free_array_item(ref_item);
  2222. }
  2223. static int parse_sorting_atom(const char *atom)
  2224. {
  2225. /*
  2226. * This parses an atom using a dummy ref_format, since we don't
  2227. * actually care about the formatting details.
  2228. */
  2229. struct ref_format dummy = REF_FORMAT_INIT;
  2230. const char *end = atom + strlen(atom);
  2231. struct strbuf err = STRBUF_INIT;
  2232. int res = parse_ref_filter_atom(&dummy, atom, end, &err);
  2233. if (res < 0)
  2234. die("%s", err.buf);
  2235. strbuf_release(&err);
  2236. return res;
  2237. }
  2238. /* If no sorting option is given, use refname to sort as default */
  2239. struct ref_sorting *ref_default_sorting(void)
  2240. {
  2241. static const char cstr_name[] = "refname";
  2242. struct ref_sorting *sorting = xcalloc(1, sizeof(*sorting));
  2243. sorting->next = NULL;
  2244. sorting->atom = parse_sorting_atom(cstr_name);
  2245. return sorting;
  2246. }
  2247. void parse_ref_sorting(struct ref_sorting **sorting_tail, const char *arg)
  2248. {
  2249. struct ref_sorting *s;
  2250. s = xcalloc(1, sizeof(*s));
  2251. s->next = *sorting_tail;
  2252. *sorting_tail = s;
  2253. if (*arg == '-') {
  2254. s->reverse = 1;
  2255. arg++;
  2256. }
  2257. if (skip_prefix(arg, "version:", &arg) ||
  2258. skip_prefix(arg, "v:", &arg))
  2259. s->version = 1;
  2260. s->atom = parse_sorting_atom(arg);
  2261. }
  2262. int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset)
  2263. {
  2264. /*
  2265. * NEEDSWORK: We should probably clear the list in this case, but we've
  2266. * already munged the global used_atoms list, which would need to be
  2267. * undone.
  2268. */
  2269. BUG_ON_OPT_NEG(unset);
  2270. parse_ref_sorting(opt->value, arg);
  2271. return 0;
  2272. }
  2273. int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset)
  2274. {
  2275. struct ref_filter *rf = opt->value;
  2276. struct object_id oid;
  2277. struct commit *merge_commit;
  2278. BUG_ON_OPT_NEG(unset);
  2279. if (get_oid(arg, &oid))
  2280. die(_("malformed object name %s"), arg);
  2281. merge_commit = lookup_commit_reference_gently(the_repository, &oid, 0);
  2282. if (!merge_commit)
  2283. return error(_("option `%s' must point to a commit"), opt->long_name);
  2284. if (starts_with(opt->long_name, "no"))
  2285. commit_list_insert(merge_commit, &rf->unreachable_from);
  2286. else
  2287. commit_list_insert(merge_commit, &rf->reachable_from);
  2288. return 0;
  2289. }