convert.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028
  1. #include "cache.h"
  2. #include "config.h"
  3. #include "object-store.h"
  4. #include "attr.h"
  5. #include "run-command.h"
  6. #include "quote.h"
  7. #include "sigchain.h"
  8. #include "pkt-line.h"
  9. #include "sub-process.h"
  10. #include "utf8.h"
  11. #include "ll-merge.h"
  12. /*
  13. * convert.c - convert a file when checking it out and checking it in.
  14. *
  15. * This should use the pathname to decide on whether it wants to do some
  16. * more interesting conversions (automatic gzip/unzip, general format
  17. * conversions etc etc), but by default it just does automatic CRLF<->LF
  18. * translation when the "text" attribute or "auto_crlf" option is set.
  19. */
  20. /* Stat bits: When BIN is set, the txt bits are unset */
  21. #define CONVERT_STAT_BITS_TXT_LF 0x1
  22. #define CONVERT_STAT_BITS_TXT_CRLF 0x2
  23. #define CONVERT_STAT_BITS_BIN 0x4
  24. enum crlf_action {
  25. CRLF_UNDEFINED,
  26. CRLF_BINARY,
  27. CRLF_TEXT,
  28. CRLF_TEXT_INPUT,
  29. CRLF_TEXT_CRLF,
  30. CRLF_AUTO,
  31. CRLF_AUTO_INPUT,
  32. CRLF_AUTO_CRLF
  33. };
  34. struct text_stat {
  35. /* NUL, CR, LF and CRLF counts */
  36. unsigned nul, lonecr, lonelf, crlf;
  37. /* These are just approximations! */
  38. unsigned printable, nonprintable;
  39. };
  40. static void gather_stats(const char *buf, unsigned long size, struct text_stat *stats)
  41. {
  42. unsigned long i;
  43. memset(stats, 0, sizeof(*stats));
  44. for (i = 0; i < size; i++) {
  45. unsigned char c = buf[i];
  46. if (c == '\r') {
  47. if (i+1 < size && buf[i+1] == '\n') {
  48. stats->crlf++;
  49. i++;
  50. } else
  51. stats->lonecr++;
  52. continue;
  53. }
  54. if (c == '\n') {
  55. stats->lonelf++;
  56. continue;
  57. }
  58. if (c == 127)
  59. /* DEL */
  60. stats->nonprintable++;
  61. else if (c < 32) {
  62. switch (c) {
  63. /* BS, HT, ESC and FF */
  64. case '\b': case '\t': case '\033': case '\014':
  65. stats->printable++;
  66. break;
  67. case 0:
  68. stats->nul++;
  69. /* fall through */
  70. default:
  71. stats->nonprintable++;
  72. }
  73. }
  74. else
  75. stats->printable++;
  76. }
  77. /* If file ends with EOF then don't count this EOF as non-printable. */
  78. if (size >= 1 && buf[size-1] == '\032')
  79. stats->nonprintable--;
  80. }
  81. /*
  82. * The same heuristics as diff.c::mmfile_is_binary()
  83. * We treat files with bare CR as binary
  84. */
  85. static int convert_is_binary(const struct text_stat *stats)
  86. {
  87. if (stats->lonecr)
  88. return 1;
  89. if (stats->nul)
  90. return 1;
  91. if ((stats->printable >> 7) < stats->nonprintable)
  92. return 1;
  93. return 0;
  94. }
  95. static unsigned int gather_convert_stats(const char *data, unsigned long size)
  96. {
  97. struct text_stat stats;
  98. int ret = 0;
  99. if (!data || !size)
  100. return 0;
  101. gather_stats(data, size, &stats);
  102. if (convert_is_binary(&stats))
  103. ret |= CONVERT_STAT_BITS_BIN;
  104. if (stats.crlf)
  105. ret |= CONVERT_STAT_BITS_TXT_CRLF;
  106. if (stats.lonelf)
  107. ret |= CONVERT_STAT_BITS_TXT_LF;
  108. return ret;
  109. }
  110. static const char *gather_convert_stats_ascii(const char *data, unsigned long size)
  111. {
  112. unsigned int convert_stats = gather_convert_stats(data, size);
  113. if (convert_stats & CONVERT_STAT_BITS_BIN)
  114. return "-text";
  115. switch (convert_stats) {
  116. case CONVERT_STAT_BITS_TXT_LF:
  117. return "lf";
  118. case CONVERT_STAT_BITS_TXT_CRLF:
  119. return "crlf";
  120. case CONVERT_STAT_BITS_TXT_LF | CONVERT_STAT_BITS_TXT_CRLF:
  121. return "mixed";
  122. default:
  123. return "none";
  124. }
  125. }
  126. const char *get_cached_convert_stats_ascii(const struct index_state *istate,
  127. const char *path)
  128. {
  129. const char *ret;
  130. unsigned long sz;
  131. void *data = read_blob_data_from_index(istate, path, &sz);
  132. ret = gather_convert_stats_ascii(data, sz);
  133. free(data);
  134. return ret;
  135. }
  136. const char *get_wt_convert_stats_ascii(const char *path)
  137. {
  138. const char *ret = "";
  139. struct strbuf sb = STRBUF_INIT;
  140. if (strbuf_read_file(&sb, path, 0) >= 0)
  141. ret = gather_convert_stats_ascii(sb.buf, sb.len);
  142. strbuf_release(&sb);
  143. return ret;
  144. }
  145. static int text_eol_is_crlf(void)
  146. {
  147. if (auto_crlf == AUTO_CRLF_TRUE)
  148. return 1;
  149. else if (auto_crlf == AUTO_CRLF_INPUT)
  150. return 0;
  151. if (core_eol == EOL_CRLF)
  152. return 1;
  153. if (core_eol == EOL_UNSET && EOL_NATIVE == EOL_CRLF)
  154. return 1;
  155. return 0;
  156. }
  157. static enum eol output_eol(enum crlf_action crlf_action)
  158. {
  159. switch (crlf_action) {
  160. case CRLF_BINARY:
  161. return EOL_UNSET;
  162. case CRLF_TEXT_CRLF:
  163. return EOL_CRLF;
  164. case CRLF_TEXT_INPUT:
  165. return EOL_LF;
  166. case CRLF_UNDEFINED:
  167. case CRLF_AUTO_CRLF:
  168. return EOL_CRLF;
  169. case CRLF_AUTO_INPUT:
  170. return EOL_LF;
  171. case CRLF_TEXT:
  172. case CRLF_AUTO:
  173. /* fall through */
  174. return text_eol_is_crlf() ? EOL_CRLF : EOL_LF;
  175. }
  176. warning(_("illegal crlf_action %d"), (int)crlf_action);
  177. return core_eol;
  178. }
  179. static void check_global_conv_flags_eol(const char *path,
  180. struct text_stat *old_stats, struct text_stat *new_stats,
  181. int conv_flags)
  182. {
  183. if (old_stats->crlf && !new_stats->crlf ) {
  184. /*
  185. * CRLFs would not be restored by checkout
  186. */
  187. if (conv_flags & CONV_EOL_RNDTRP_DIE)
  188. die(_("CRLF would be replaced by LF in %s"), path);
  189. else if (conv_flags & CONV_EOL_RNDTRP_WARN)
  190. warning(_("CRLF will be replaced by LF in %s.\n"
  191. "The file will have its original line"
  192. " endings in your working directory"), path);
  193. } else if (old_stats->lonelf && !new_stats->lonelf ) {
  194. /*
  195. * CRLFs would be added by checkout
  196. */
  197. if (conv_flags & CONV_EOL_RNDTRP_DIE)
  198. die(_("LF would be replaced by CRLF in %s"), path);
  199. else if (conv_flags & CONV_EOL_RNDTRP_WARN)
  200. warning(_("LF will be replaced by CRLF in %s.\n"
  201. "The file will have its original line"
  202. " endings in your working directory"), path);
  203. }
  204. }
  205. static int has_crlf_in_index(const struct index_state *istate, const char *path)
  206. {
  207. unsigned long sz;
  208. void *data;
  209. const char *crp;
  210. int has_crlf = 0;
  211. data = read_blob_data_from_index(istate, path, &sz);
  212. if (!data)
  213. return 0;
  214. crp = memchr(data, '\r', sz);
  215. if (crp) {
  216. unsigned int ret_stats;
  217. ret_stats = gather_convert_stats(data, sz);
  218. if (!(ret_stats & CONVERT_STAT_BITS_BIN) &&
  219. (ret_stats & CONVERT_STAT_BITS_TXT_CRLF))
  220. has_crlf = 1;
  221. }
  222. free(data);
  223. return has_crlf;
  224. }
  225. static int will_convert_lf_to_crlf(struct text_stat *stats,
  226. enum crlf_action crlf_action)
  227. {
  228. if (output_eol(crlf_action) != EOL_CRLF)
  229. return 0;
  230. /* No "naked" LF? Nothing to convert, regardless. */
  231. if (!stats->lonelf)
  232. return 0;
  233. if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
  234. /* If we have any CR or CRLF line endings, we do not touch it */
  235. /* This is the new safer autocrlf-handling */
  236. if (stats->lonecr || stats->crlf)
  237. return 0;
  238. if (convert_is_binary(stats))
  239. return 0;
  240. }
  241. return 1;
  242. }
  243. static int validate_encoding(const char *path, const char *enc,
  244. const char *data, size_t len, int die_on_error)
  245. {
  246. const char *stripped;
  247. /* We only check for UTF here as UTF?? can be an alias for UTF-?? */
  248. if (skip_iprefix(enc, "UTF", &stripped)) {
  249. skip_prefix(stripped, "-", &stripped);
  250. /*
  251. * Check for detectable errors in UTF encodings
  252. */
  253. if (has_prohibited_utf_bom(enc, data, len)) {
  254. const char *error_msg = _(
  255. "BOM is prohibited in '%s' if encoded as %s");
  256. /*
  257. * This advice is shown for UTF-??BE and UTF-??LE encodings.
  258. * We cut off the last two characters of the encoding name
  259. * to generate the encoding name suitable for BOMs.
  260. */
  261. const char *advise_msg = _(
  262. "The file '%s' contains a byte order "
  263. "mark (BOM). Please use UTF-%.*s as "
  264. "working-tree-encoding.");
  265. int stripped_len = strlen(stripped) - strlen("BE");
  266. advise(advise_msg, path, stripped_len, stripped);
  267. if (die_on_error)
  268. die(error_msg, path, enc);
  269. else {
  270. return error(error_msg, path, enc);
  271. }
  272. } else if (is_missing_required_utf_bom(enc, data, len)) {
  273. const char *error_msg = _(
  274. "BOM is required in '%s' if encoded as %s");
  275. const char *advise_msg = _(
  276. "The file '%s' is missing a byte order "
  277. "mark (BOM). Please use UTF-%sBE or UTF-%sLE "
  278. "(depending on the byte order) as "
  279. "working-tree-encoding.");
  280. advise(advise_msg, path, stripped, stripped);
  281. if (die_on_error)
  282. die(error_msg, path, enc);
  283. else {
  284. return error(error_msg, path, enc);
  285. }
  286. }
  287. }
  288. return 0;
  289. }
  290. static void trace_encoding(const char *context, const char *path,
  291. const char *encoding, const char *buf, size_t len)
  292. {
  293. static struct trace_key coe = TRACE_KEY_INIT(WORKING_TREE_ENCODING);
  294. struct strbuf trace = STRBUF_INIT;
  295. int i;
  296. strbuf_addf(&trace, "%s (%s, considered %s):\n", context, path, encoding);
  297. for (i = 0; i < len && buf; ++i) {
  298. strbuf_addf(
  299. &trace, "| \033[2m%2i:\033[0m %2x \033[2m%c\033[0m%c",
  300. i,
  301. (unsigned char) buf[i],
  302. (buf[i] > 32 && buf[i] < 127 ? buf[i] : ' '),
  303. ((i+1) % 8 && (i+1) < len ? ' ' : '\n')
  304. );
  305. }
  306. strbuf_addchars(&trace, '\n', 1);
  307. trace_strbuf(&coe, &trace);
  308. strbuf_release(&trace);
  309. }
  310. static int check_roundtrip(const char *enc_name)
  311. {
  312. /*
  313. * check_roundtrip_encoding contains a string of comma and/or
  314. * space separated encodings (eg. "UTF-16, ASCII, CP1125").
  315. * Search for the given encoding in that string.
  316. */
  317. const char *found = strcasestr(check_roundtrip_encoding, enc_name);
  318. const char *next;
  319. int len;
  320. if (!found)
  321. return 0;
  322. next = found + strlen(enc_name);
  323. len = strlen(check_roundtrip_encoding);
  324. return (found && (
  325. /*
  326. * check that the found encoding is at the
  327. * beginning of check_roundtrip_encoding or
  328. * that it is prefixed with a space or comma
  329. */
  330. found == check_roundtrip_encoding || (
  331. (isspace(found[-1]) || found[-1] == ',')
  332. )
  333. ) && (
  334. /*
  335. * check that the found encoding is at the
  336. * end of check_roundtrip_encoding or
  337. * that it is suffixed with a space or comma
  338. */
  339. next == check_roundtrip_encoding + len || (
  340. next < check_roundtrip_encoding + len &&
  341. (isspace(next[0]) || next[0] == ',')
  342. )
  343. ));
  344. }
  345. static const char *default_encoding = "UTF-8";
  346. static int encode_to_git(const char *path, const char *src, size_t src_len,
  347. struct strbuf *buf, const char *enc, int conv_flags)
  348. {
  349. char *dst;
  350. size_t dst_len;
  351. int die_on_error = conv_flags & CONV_WRITE_OBJECT;
  352. /*
  353. * No encoding is specified or there is nothing to encode.
  354. * Tell the caller that the content was not modified.
  355. */
  356. if (!enc || (src && !src_len))
  357. return 0;
  358. /*
  359. * Looks like we got called from "would_convert_to_git()".
  360. * This means Git wants to know if it would encode (= modify!)
  361. * the content. Let's answer with "yes", since an encoding was
  362. * specified.
  363. */
  364. if (!buf && !src)
  365. return 1;
  366. if (validate_encoding(path, enc, src, src_len, die_on_error))
  367. return 0;
  368. trace_encoding("source", path, enc, src, src_len);
  369. dst = reencode_string_len(src, src_len, default_encoding, enc,
  370. &dst_len);
  371. if (!dst) {
  372. /*
  373. * We could add the blob "as-is" to Git. However, on checkout
  374. * we would try to re-encode to the original encoding. This
  375. * would fail and we would leave the user with a messed-up
  376. * working tree. Let's try to avoid this by screaming loud.
  377. */
  378. const char* msg = _("failed to encode '%s' from %s to %s");
  379. if (die_on_error)
  380. die(msg, path, enc, default_encoding);
  381. else {
  382. error(msg, path, enc, default_encoding);
  383. return 0;
  384. }
  385. }
  386. trace_encoding("destination", path, default_encoding, dst, dst_len);
  387. /*
  388. * UTF supports lossless conversion round tripping [1] and conversions
  389. * between UTF and other encodings are mostly round trip safe as
  390. * Unicode aims to be a superset of all other character encodings.
  391. * However, certain encodings (e.g. SHIFT-JIS) are known to have round
  392. * trip issues [2]. Check the round trip conversion for all encodings
  393. * listed in core.checkRoundtripEncoding.
  394. *
  395. * The round trip check is only performed if content is written to Git.
  396. * This ensures that no information is lost during conversion to/from
  397. * the internal UTF-8 representation.
  398. *
  399. * Please note, the code below is not tested because I was not able to
  400. * generate a faulty round trip without an iconv error. Iconv errors
  401. * are already caught above.
  402. *
  403. * [1] http://unicode.org/faq/utf_bom.html#gen2
  404. * [2] https://support.microsoft.com/en-us/help/170559/prb-conversion-problem-between-shift-jis-and-unicode
  405. */
  406. if (die_on_error && check_roundtrip(enc)) {
  407. char *re_src;
  408. size_t re_src_len;
  409. re_src = reencode_string_len(dst, dst_len,
  410. enc, default_encoding,
  411. &re_src_len);
  412. trace_printf("Checking roundtrip encoding for %s...\n", enc);
  413. trace_encoding("reencoded source", path, enc,
  414. re_src, re_src_len);
  415. if (!re_src || src_len != re_src_len ||
  416. memcmp(src, re_src, src_len)) {
  417. const char* msg = _("encoding '%s' from %s to %s and "
  418. "back is not the same");
  419. die(msg, path, enc, default_encoding);
  420. }
  421. free(re_src);
  422. }
  423. strbuf_attach(buf, dst, dst_len, dst_len + 1);
  424. return 1;
  425. }
  426. static int encode_to_worktree(const char *path, const char *src, size_t src_len,
  427. struct strbuf *buf, const char *enc)
  428. {
  429. char *dst;
  430. size_t dst_len;
  431. /*
  432. * No encoding is specified or there is nothing to encode.
  433. * Tell the caller that the content was not modified.
  434. */
  435. if (!enc || (src && !src_len))
  436. return 0;
  437. dst = reencode_string_len(src, src_len, enc, default_encoding,
  438. &dst_len);
  439. if (!dst) {
  440. error(_("failed to encode '%s' from %s to %s"),
  441. path, default_encoding, enc);
  442. return 0;
  443. }
  444. strbuf_attach(buf, dst, dst_len, dst_len + 1);
  445. return 1;
  446. }
  447. static int crlf_to_git(const struct index_state *istate,
  448. const char *path, const char *src, size_t len,
  449. struct strbuf *buf,
  450. enum crlf_action crlf_action, int conv_flags)
  451. {
  452. struct text_stat stats;
  453. char *dst;
  454. int convert_crlf_into_lf;
  455. if (crlf_action == CRLF_BINARY ||
  456. (src && !len))
  457. return 0;
  458. /*
  459. * If we are doing a dry-run and have no source buffer, there is
  460. * nothing to analyze; we must assume we would convert.
  461. */
  462. if (!buf && !src)
  463. return 1;
  464. gather_stats(src, len, &stats);
  465. /* Optimization: No CRLF? Nothing to convert, regardless. */
  466. convert_crlf_into_lf = !!stats.crlf;
  467. if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
  468. if (convert_is_binary(&stats))
  469. return 0;
  470. /*
  471. * If the file in the index has any CR in it, do not
  472. * convert. This is the new safer autocrlf handling,
  473. * unless we want to renormalize in a merge or
  474. * cherry-pick.
  475. */
  476. if ((!(conv_flags & CONV_EOL_RENORMALIZE)) &&
  477. has_crlf_in_index(istate, path))
  478. convert_crlf_into_lf = 0;
  479. }
  480. if (((conv_flags & CONV_EOL_RNDTRP_WARN) ||
  481. ((conv_flags & CONV_EOL_RNDTRP_DIE) && len))) {
  482. struct text_stat new_stats;
  483. memcpy(&new_stats, &stats, sizeof(new_stats));
  484. /* simulate "git add" */
  485. if (convert_crlf_into_lf) {
  486. new_stats.lonelf += new_stats.crlf;
  487. new_stats.crlf = 0;
  488. }
  489. /* simulate "git checkout" */
  490. if (will_convert_lf_to_crlf(&new_stats, crlf_action)) {
  491. new_stats.crlf += new_stats.lonelf;
  492. new_stats.lonelf = 0;
  493. }
  494. check_global_conv_flags_eol(path, &stats, &new_stats, conv_flags);
  495. }
  496. if (!convert_crlf_into_lf)
  497. return 0;
  498. /*
  499. * At this point all of our source analysis is done, and we are sure we
  500. * would convert. If we are in dry-run mode, we can give an answer.
  501. */
  502. if (!buf)
  503. return 1;
  504. /* only grow if not in place */
  505. if (strbuf_avail(buf) + buf->len < len)
  506. strbuf_grow(buf, len - buf->len);
  507. dst = buf->buf;
  508. if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
  509. /*
  510. * If we guessed, we already know we rejected a file with
  511. * lone CR, and we can strip a CR without looking at what
  512. * follow it.
  513. */
  514. do {
  515. unsigned char c = *src++;
  516. if (c != '\r')
  517. *dst++ = c;
  518. } while (--len);
  519. } else {
  520. do {
  521. unsigned char c = *src++;
  522. if (! (c == '\r' && (1 < len && *src == '\n')))
  523. *dst++ = c;
  524. } while (--len);
  525. }
  526. strbuf_setlen(buf, dst - buf->buf);
  527. return 1;
  528. }
  529. static int crlf_to_worktree(const char *src, size_t len,
  530. struct strbuf *buf, enum crlf_action crlf_action)
  531. {
  532. char *to_free = NULL;
  533. struct text_stat stats;
  534. if (!len || output_eol(crlf_action) != EOL_CRLF)
  535. return 0;
  536. gather_stats(src, len, &stats);
  537. if (!will_convert_lf_to_crlf(&stats, crlf_action))
  538. return 0;
  539. /* are we "faking" in place editing ? */
  540. if (src == buf->buf)
  541. to_free = strbuf_detach(buf, NULL);
  542. strbuf_grow(buf, len + stats.lonelf);
  543. for (;;) {
  544. const char *nl = memchr(src, '\n', len);
  545. if (!nl)
  546. break;
  547. if (nl > src && nl[-1] == '\r') {
  548. strbuf_add(buf, src, nl + 1 - src);
  549. } else {
  550. strbuf_add(buf, src, nl - src);
  551. strbuf_addstr(buf, "\r\n");
  552. }
  553. len -= nl + 1 - src;
  554. src = nl + 1;
  555. }
  556. strbuf_add(buf, src, len);
  557. free(to_free);
  558. return 1;
  559. }
  560. struct filter_params {
  561. const char *src;
  562. unsigned long size;
  563. int fd;
  564. const char *cmd;
  565. const char *path;
  566. };
  567. static int filter_buffer_or_fd(int in, int out, void *data)
  568. {
  569. /*
  570. * Spawn cmd and feed the buffer contents through its stdin.
  571. */
  572. struct child_process child_process = CHILD_PROCESS_INIT;
  573. struct filter_params *params = (struct filter_params *)data;
  574. int write_err, status;
  575. /* apply % substitution to cmd */
  576. struct strbuf cmd = STRBUF_INIT;
  577. struct strbuf path = STRBUF_INIT;
  578. struct strbuf_expand_dict_entry dict[] = {
  579. { "f", NULL, },
  580. { NULL, NULL, },
  581. };
  582. /* quote the path to preserve spaces, etc. */
  583. sq_quote_buf(&path, params->path);
  584. dict[0].value = path.buf;
  585. /* expand all %f with the quoted path */
  586. strbuf_expand(&cmd, params->cmd, strbuf_expand_dict_cb, &dict);
  587. strbuf_release(&path);
  588. strvec_push(&child_process.args, cmd.buf);
  589. child_process.use_shell = 1;
  590. child_process.in = -1;
  591. child_process.out = out;
  592. if (start_command(&child_process)) {
  593. strbuf_release(&cmd);
  594. return error(_("cannot fork to run external filter '%s'"),
  595. params->cmd);
  596. }
  597. sigchain_push(SIGPIPE, SIG_IGN);
  598. if (params->src) {
  599. write_err = (write_in_full(child_process.in,
  600. params->src, params->size) < 0);
  601. if (errno == EPIPE)
  602. write_err = 0;
  603. } else {
  604. write_err = copy_fd(params->fd, child_process.in);
  605. if (write_err == COPY_WRITE_ERROR && errno == EPIPE)
  606. write_err = 0;
  607. }
  608. if (close(child_process.in))
  609. write_err = 1;
  610. if (write_err)
  611. error(_("cannot feed the input to external filter '%s'"),
  612. params->cmd);
  613. sigchain_pop(SIGPIPE);
  614. status = finish_command(&child_process);
  615. if (status)
  616. error(_("external filter '%s' failed %d"), params->cmd, status);
  617. strbuf_release(&cmd);
  618. return (write_err || status);
  619. }
  620. static int apply_single_file_filter(const char *path, const char *src, size_t len, int fd,
  621. struct strbuf *dst, const char *cmd)
  622. {
  623. /*
  624. * Create a pipeline to have the command filter the buffer's
  625. * contents.
  626. *
  627. * (child --> cmd) --> us
  628. */
  629. int err = 0;
  630. struct strbuf nbuf = STRBUF_INIT;
  631. struct async async;
  632. struct filter_params params;
  633. memset(&async, 0, sizeof(async));
  634. async.proc = filter_buffer_or_fd;
  635. async.data = &params;
  636. async.out = -1;
  637. params.src = src;
  638. params.size = len;
  639. params.fd = fd;
  640. params.cmd = cmd;
  641. params.path = path;
  642. fflush(NULL);
  643. if (start_async(&async))
  644. return 0; /* error was already reported */
  645. if (strbuf_read(&nbuf, async.out, 0) < 0) {
  646. err = error(_("read from external filter '%s' failed"), cmd);
  647. }
  648. if (close(async.out)) {
  649. err = error(_("read from external filter '%s' failed"), cmd);
  650. }
  651. if (finish_async(&async)) {
  652. err = error(_("external filter '%s' failed"), cmd);
  653. }
  654. if (!err) {
  655. strbuf_swap(dst, &nbuf);
  656. }
  657. strbuf_release(&nbuf);
  658. return !err;
  659. }
  660. #define CAP_CLEAN (1u<<0)
  661. #define CAP_SMUDGE (1u<<1)
  662. #define CAP_DELAY (1u<<2)
  663. struct cmd2process {
  664. struct subprocess_entry subprocess; /* must be the first member! */
  665. unsigned int supported_capabilities;
  666. };
  667. static int subprocess_map_initialized;
  668. static struct hashmap subprocess_map;
  669. static int start_multi_file_filter_fn(struct subprocess_entry *subprocess)
  670. {
  671. static int versions[] = {2, 0};
  672. static struct subprocess_capability capabilities[] = {
  673. { "clean", CAP_CLEAN },
  674. { "smudge", CAP_SMUDGE },
  675. { "delay", CAP_DELAY },
  676. { NULL, 0 }
  677. };
  678. struct cmd2process *entry = (struct cmd2process *)subprocess;
  679. return subprocess_handshake(subprocess, "git-filter", versions, NULL,
  680. capabilities,
  681. &entry->supported_capabilities);
  682. }
  683. static void handle_filter_error(const struct strbuf *filter_status,
  684. struct cmd2process *entry,
  685. const unsigned int wanted_capability)
  686. {
  687. if (!strcmp(filter_status->buf, "error"))
  688. ; /* The filter signaled a problem with the file. */
  689. else if (!strcmp(filter_status->buf, "abort") && wanted_capability) {
  690. /*
  691. * The filter signaled a permanent problem. Don't try to filter
  692. * files with the same command for the lifetime of the current
  693. * Git process.
  694. */
  695. entry->supported_capabilities &= ~wanted_capability;
  696. } else {
  697. /*
  698. * Something went wrong with the protocol filter.
  699. * Force shutdown and restart if another blob requires filtering.
  700. */
  701. error(_("external filter '%s' failed"), entry->subprocess.cmd);
  702. subprocess_stop(&subprocess_map, &entry->subprocess);
  703. free(entry);
  704. }
  705. }
  706. static int apply_multi_file_filter(const char *path, const char *src, size_t len,
  707. int fd, struct strbuf *dst, const char *cmd,
  708. const unsigned int wanted_capability,
  709. const struct checkout_metadata *meta,
  710. struct delayed_checkout *dco)
  711. {
  712. int err;
  713. int can_delay = 0;
  714. struct cmd2process *entry;
  715. struct child_process *process;
  716. struct strbuf nbuf = STRBUF_INIT;
  717. struct strbuf filter_status = STRBUF_INIT;
  718. const char *filter_type;
  719. if (!subprocess_map_initialized) {
  720. subprocess_map_initialized = 1;
  721. hashmap_init(&subprocess_map, cmd2process_cmp, NULL, 0);
  722. entry = NULL;
  723. } else {
  724. entry = (struct cmd2process *)subprocess_find_entry(&subprocess_map, cmd);
  725. }
  726. fflush(NULL);
  727. if (!entry) {
  728. entry = xmalloc(sizeof(*entry));
  729. entry->supported_capabilities = 0;
  730. if (subprocess_start(&subprocess_map, &entry->subprocess, cmd, start_multi_file_filter_fn)) {
  731. free(entry);
  732. return 0;
  733. }
  734. }
  735. process = &entry->subprocess.process;
  736. if (!(entry->supported_capabilities & wanted_capability))
  737. return 0;
  738. if (wanted_capability & CAP_CLEAN)
  739. filter_type = "clean";
  740. else if (wanted_capability & CAP_SMUDGE)
  741. filter_type = "smudge";
  742. else
  743. die(_("unexpected filter type"));
  744. sigchain_push(SIGPIPE, SIG_IGN);
  745. assert(strlen(filter_type) < LARGE_PACKET_DATA_MAX - strlen("command=\n"));
  746. err = packet_write_fmt_gently(process->in, "command=%s\n", filter_type);
  747. if (err)
  748. goto done;
  749. err = strlen(path) > LARGE_PACKET_DATA_MAX - strlen("pathname=\n");
  750. if (err) {
  751. error(_("path name too long for external filter"));
  752. goto done;
  753. }
  754. err = packet_write_fmt_gently(process->in, "pathname=%s\n", path);
  755. if (err)
  756. goto done;
  757. if (meta && meta->refname) {
  758. err = packet_write_fmt_gently(process->in, "ref=%s\n", meta->refname);
  759. if (err)
  760. goto done;
  761. }
  762. if (meta && !is_null_oid(&meta->treeish)) {
  763. err = packet_write_fmt_gently(process->in, "treeish=%s\n", oid_to_hex(&meta->treeish));
  764. if (err)
  765. goto done;
  766. }
  767. if (meta && !is_null_oid(&meta->blob)) {
  768. err = packet_write_fmt_gently(process->in, "blob=%s\n", oid_to_hex(&meta->blob));
  769. if (err)
  770. goto done;
  771. }
  772. if ((entry->supported_capabilities & CAP_DELAY) &&
  773. dco && dco->state == CE_CAN_DELAY) {
  774. can_delay = 1;
  775. err = packet_write_fmt_gently(process->in, "can-delay=1\n");
  776. if (err)
  777. goto done;
  778. }
  779. err = packet_flush_gently(process->in);
  780. if (err)
  781. goto done;
  782. if (fd >= 0)
  783. err = write_packetized_from_fd(fd, process->in);
  784. else
  785. err = write_packetized_from_buf(src, len, process->in);
  786. if (err)
  787. goto done;
  788. err = subprocess_read_status(process->out, &filter_status);
  789. if (err)
  790. goto done;
  791. if (can_delay && !strcmp(filter_status.buf, "delayed")) {
  792. string_list_insert(&dco->filters, cmd);
  793. string_list_insert(&dco->paths, path);
  794. } else {
  795. /* The filter got the blob and wants to send us a response. */
  796. err = strcmp(filter_status.buf, "success");
  797. if (err)
  798. goto done;
  799. err = read_packetized_to_strbuf(process->out, &nbuf) < 0;
  800. if (err)
  801. goto done;
  802. err = subprocess_read_status(process->out, &filter_status);
  803. if (err)
  804. goto done;
  805. err = strcmp(filter_status.buf, "success");
  806. }
  807. done:
  808. sigchain_pop(SIGPIPE);
  809. if (err)
  810. handle_filter_error(&filter_status, entry, wanted_capability);
  811. else
  812. strbuf_swap(dst, &nbuf);
  813. strbuf_release(&nbuf);
  814. return !err;
  815. }
  816. int async_query_available_blobs(const char *cmd, struct string_list *available_paths)
  817. {
  818. int err;
  819. char *line;
  820. struct cmd2process *entry;
  821. struct child_process *process;
  822. struct strbuf filter_status = STRBUF_INIT;
  823. assert(subprocess_map_initialized);
  824. entry = (struct cmd2process *)subprocess_find_entry(&subprocess_map, cmd);
  825. if (!entry) {
  826. error(_("external filter '%s' is not available anymore although "
  827. "not all paths have been filtered"), cmd);
  828. return 0;
  829. }
  830. process = &entry->subprocess.process;
  831. sigchain_push(SIGPIPE, SIG_IGN);
  832. err = packet_write_fmt_gently(
  833. process->in, "command=list_available_blobs\n");
  834. if (err)
  835. goto done;
  836. err = packet_flush_gently(process->in);
  837. if (err)
  838. goto done;
  839. while ((line = packet_read_line(process->out, NULL))) {
  840. const char *path;
  841. if (skip_prefix(line, "pathname=", &path))
  842. string_list_insert(available_paths, xstrdup(path));
  843. else
  844. ; /* ignore unknown keys */
  845. }
  846. err = subprocess_read_status(process->out, &filter_status);
  847. if (err)
  848. goto done;
  849. err = strcmp(filter_status.buf, "success");
  850. done:
  851. sigchain_pop(SIGPIPE);
  852. if (err)
  853. handle_filter_error(&filter_status, entry, 0);
  854. return !err;
  855. }
  856. static struct convert_driver {
  857. const char *name;
  858. struct convert_driver *next;
  859. const char *smudge;
  860. const char *clean;
  861. const char *process;
  862. int required;
  863. } *user_convert, **user_convert_tail;
  864. static int apply_filter(const char *path, const char *src, size_t len,
  865. int fd, struct strbuf *dst, struct convert_driver *drv,
  866. const unsigned int wanted_capability,
  867. const struct checkout_metadata *meta,
  868. struct delayed_checkout *dco)
  869. {
  870. const char *cmd = NULL;
  871. if (!drv)
  872. return 0;
  873. if (!dst)
  874. return 1;
  875. if ((wanted_capability & CAP_CLEAN) && !drv->process && drv->clean)
  876. cmd = drv->clean;
  877. else if ((wanted_capability & CAP_SMUDGE) && !drv->process && drv->smudge)
  878. cmd = drv->smudge;
  879. if (cmd && *cmd)
  880. return apply_single_file_filter(path, src, len, fd, dst, cmd);
  881. else if (drv->process && *drv->process)
  882. return apply_multi_file_filter(path, src, len, fd, dst,
  883. drv->process, wanted_capability, meta, dco);
  884. return 0;
  885. }
  886. static int read_convert_config(const char *var, const char *value, void *cb)
  887. {
  888. const char *key, *name;
  889. size_t namelen;
  890. struct convert_driver *drv;
  891. /*
  892. * External conversion drivers are configured using
  893. * "filter.<name>.variable".
  894. */
  895. if (parse_config_key(var, "filter", &name, &namelen, &key) < 0 || !name)
  896. return 0;
  897. for (drv = user_convert; drv; drv = drv->next)
  898. if (!strncmp(drv->name, name, namelen) && !drv->name[namelen])
  899. break;
  900. if (!drv) {
  901. drv = xcalloc(1, sizeof(struct convert_driver));
  902. drv->name = xmemdupz(name, namelen);
  903. *user_convert_tail = drv;
  904. user_convert_tail = &(drv->next);
  905. }
  906. /*
  907. * filter.<name>.smudge and filter.<name>.clean specifies
  908. * the command line:
  909. *
  910. * command-line
  911. *
  912. * The command-line will not be interpolated in any way.
  913. */
  914. if (!strcmp("smudge", key))
  915. return git_config_string(&drv->smudge, var, value);
  916. if (!strcmp("clean", key))
  917. return git_config_string(&drv->clean, var, value);
  918. if (!strcmp("process", key))
  919. return git_config_string(&drv->process, var, value);
  920. if (!strcmp("required", key)) {
  921. drv->required = git_config_bool(var, value);
  922. return 0;
  923. }
  924. return 0;
  925. }
  926. static int count_ident(const char *cp, unsigned long size)
  927. {
  928. /*
  929. * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
  930. */
  931. int cnt = 0;
  932. char ch;
  933. while (size) {
  934. ch = *cp++;
  935. size--;
  936. if (ch != '$')
  937. continue;
  938. if (size < 3)
  939. break;
  940. if (memcmp("Id", cp, 2))
  941. continue;
  942. ch = cp[2];
  943. cp += 3;
  944. size -= 3;
  945. if (ch == '$')
  946. cnt++; /* $Id$ */
  947. if (ch != ':')
  948. continue;
  949. /*
  950. * "$Id: ... "; scan up to the closing dollar sign and discard.
  951. */
  952. while (size) {
  953. ch = *cp++;
  954. size--;
  955. if (ch == '$') {
  956. cnt++;
  957. break;
  958. }
  959. if (ch == '\n')
  960. break;
  961. }
  962. }
  963. return cnt;
  964. }
  965. static int ident_to_git(const char *src, size_t len,
  966. struct strbuf *buf, int ident)
  967. {
  968. char *dst, *dollar;
  969. if (!ident || (src && !count_ident(src, len)))
  970. return 0;
  971. if (!buf)
  972. return 1;
  973. /* only grow if not in place */
  974. if (strbuf_avail(buf) + buf->len < len)
  975. strbuf_grow(buf, len - buf->len);
  976. dst = buf->buf;
  977. for (;;) {
  978. dollar = memchr(src, '$', len);
  979. if (!dollar)
  980. break;
  981. memmove(dst, src, dollar + 1 - src);
  982. dst += dollar + 1 - src;
  983. len -= dollar + 1 - src;
  984. src = dollar + 1;
  985. if (len > 3 && !memcmp(src, "Id:", 3)) {
  986. dollar = memchr(src + 3, '$', len - 3);
  987. if (!dollar)
  988. break;
  989. if (memchr(src + 3, '\n', dollar - src - 3)) {
  990. /* Line break before the next dollar. */
  991. continue;
  992. }
  993. memcpy(dst, "Id$", 3);
  994. dst += 3;
  995. len -= dollar + 1 - src;
  996. src = dollar + 1;
  997. }
  998. }
  999. memmove(dst, src, len);
  1000. strbuf_setlen(buf, dst + len - buf->buf);
  1001. return 1;
  1002. }
  1003. static int ident_to_worktree(const char *src, size_t len,
  1004. struct strbuf *buf, int ident)
  1005. {
  1006. struct object_id oid;
  1007. char *to_free = NULL, *dollar, *spc;
  1008. int cnt;
  1009. if (!ident)
  1010. return 0;
  1011. cnt = count_ident(src, len);
  1012. if (!cnt)
  1013. return 0;
  1014. /* are we "faking" in place editing ? */
  1015. if (src == buf->buf)
  1016. to_free = strbuf_detach(buf, NULL);
  1017. hash_object_file(the_hash_algo, src, len, "blob", &oid);
  1018. strbuf_grow(buf, len + cnt * (the_hash_algo->hexsz + 3));
  1019. for (;;) {
  1020. /* step 1: run to the next '$' */
  1021. dollar = memchr(src, '$', len);
  1022. if (!dollar)
  1023. break;
  1024. strbuf_add(buf, src, dollar + 1 - src);
  1025. len -= dollar + 1 - src;
  1026. src = dollar + 1;
  1027. /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
  1028. if (len < 3 || memcmp("Id", src, 2))
  1029. continue;
  1030. /* step 3: skip over Id$ or Id:xxxxx$ */
  1031. if (src[2] == '$') {
  1032. src += 3;
  1033. len -= 3;
  1034. } else if (src[2] == ':') {
  1035. /*
  1036. * It's possible that an expanded Id has crept its way into the
  1037. * repository, we cope with that by stripping the expansion out.
  1038. * This is probably not a good idea, since it will cause changes
  1039. * on checkout, which won't go away by stash, but let's keep it
  1040. * for git-style ids.
  1041. */
  1042. dollar = memchr(src + 3, '$', len - 3);
  1043. if (!dollar) {
  1044. /* incomplete keyword, no more '$', so just quit the loop */
  1045. break;
  1046. }
  1047. if (memchr(src + 3, '\n', dollar - src - 3)) {
  1048. /* Line break before the next dollar. */
  1049. continue;
  1050. }
  1051. spc = memchr(src + 4, ' ', dollar - src - 4);
  1052. if (spc && spc < dollar-1) {
  1053. /* There are spaces in unexpected places.
  1054. * This is probably an id from some other
  1055. * versioning system. Keep it for now.
  1056. */
  1057. continue;
  1058. }
  1059. len -= dollar + 1 - src;
  1060. src = dollar + 1;
  1061. } else {
  1062. /* it wasn't a "Id$" or "Id:xxxx$" */
  1063. continue;
  1064. }
  1065. /* step 4: substitute */
  1066. strbuf_addstr(buf, "Id: ");
  1067. strbuf_addstr(buf, oid_to_hex(&oid));
  1068. strbuf_addstr(buf, " $");
  1069. }
  1070. strbuf_add(buf, src, len);
  1071. free(to_free);
  1072. return 1;
  1073. }
  1074. static const char *git_path_check_encoding(struct attr_check_item *check)
  1075. {
  1076. const char *value = check->value;
  1077. if (ATTR_UNSET(value) || !strlen(value))
  1078. return NULL;
  1079. if (ATTR_TRUE(value) || ATTR_FALSE(value)) {
  1080. die(_("true/false are no valid working-tree-encodings"));
  1081. }
  1082. /* Don't encode to the default encoding */
  1083. if (same_encoding(value, default_encoding))
  1084. return NULL;
  1085. return value;
  1086. }
  1087. static enum crlf_action git_path_check_crlf(struct attr_check_item *check)
  1088. {
  1089. const char *value = check->value;
  1090. if (ATTR_TRUE(value))
  1091. return CRLF_TEXT;
  1092. else if (ATTR_FALSE(value))
  1093. return CRLF_BINARY;
  1094. else if (ATTR_UNSET(value))
  1095. ;
  1096. else if (!strcmp(value, "input"))
  1097. return CRLF_TEXT_INPUT;
  1098. else if (!strcmp(value, "auto"))
  1099. return CRLF_AUTO;
  1100. return CRLF_UNDEFINED;
  1101. }
  1102. static enum eol git_path_check_eol(struct attr_check_item *check)
  1103. {
  1104. const char *value = check->value;
  1105. if (ATTR_UNSET(value))
  1106. ;
  1107. else if (!strcmp(value, "lf"))
  1108. return EOL_LF;
  1109. else if (!strcmp(value, "crlf"))
  1110. return EOL_CRLF;
  1111. return EOL_UNSET;
  1112. }
  1113. static struct convert_driver *git_path_check_convert(struct attr_check_item *check)
  1114. {
  1115. const char *value = check->value;
  1116. struct convert_driver *drv;
  1117. if (ATTR_TRUE(value) || ATTR_FALSE(value) || ATTR_UNSET(value))
  1118. return NULL;
  1119. for (drv = user_convert; drv; drv = drv->next)
  1120. if (!strcmp(value, drv->name))
  1121. return drv;
  1122. return NULL;
  1123. }
  1124. static int git_path_check_ident(struct attr_check_item *check)
  1125. {
  1126. const char *value = check->value;
  1127. return !!ATTR_TRUE(value);
  1128. }
  1129. struct conv_attrs {
  1130. struct convert_driver *drv;
  1131. enum crlf_action attr_action; /* What attr says */
  1132. enum crlf_action crlf_action; /* When no attr is set, use core.autocrlf */
  1133. int ident;
  1134. const char *working_tree_encoding; /* Supported encoding or default encoding if NULL */
  1135. };
  1136. static struct attr_check *check;
  1137. static void convert_attrs(const struct index_state *istate,
  1138. struct conv_attrs *ca, const char *path)
  1139. {
  1140. struct attr_check_item *ccheck = NULL;
  1141. if (!check) {
  1142. check = attr_check_initl("crlf", "ident", "filter",
  1143. "eol", "text", "working-tree-encoding",
  1144. NULL);
  1145. user_convert_tail = &user_convert;
  1146. git_config(read_convert_config, NULL);
  1147. }
  1148. git_check_attr(istate, path, check);
  1149. ccheck = check->items;
  1150. ca->crlf_action = git_path_check_crlf(ccheck + 4);
  1151. if (ca->crlf_action == CRLF_UNDEFINED)
  1152. ca->crlf_action = git_path_check_crlf(ccheck + 0);
  1153. ca->ident = git_path_check_ident(ccheck + 1);
  1154. ca->drv = git_path_check_convert(ccheck + 2);
  1155. if (ca->crlf_action != CRLF_BINARY) {
  1156. enum eol eol_attr = git_path_check_eol(ccheck + 3);
  1157. if (ca->crlf_action == CRLF_AUTO && eol_attr == EOL_LF)
  1158. ca->crlf_action = CRLF_AUTO_INPUT;
  1159. else if (ca->crlf_action == CRLF_AUTO && eol_attr == EOL_CRLF)
  1160. ca->crlf_action = CRLF_AUTO_CRLF;
  1161. else if (eol_attr == EOL_LF)
  1162. ca->crlf_action = CRLF_TEXT_INPUT;
  1163. else if (eol_attr == EOL_CRLF)
  1164. ca->crlf_action = CRLF_TEXT_CRLF;
  1165. }
  1166. ca->working_tree_encoding = git_path_check_encoding(ccheck + 5);
  1167. /* Save attr and make a decision for action */
  1168. ca->attr_action = ca->crlf_action;
  1169. if (ca->crlf_action == CRLF_TEXT)
  1170. ca->crlf_action = text_eol_is_crlf() ? CRLF_TEXT_CRLF : CRLF_TEXT_INPUT;
  1171. if (ca->crlf_action == CRLF_UNDEFINED && auto_crlf == AUTO_CRLF_FALSE)
  1172. ca->crlf_action = CRLF_BINARY;
  1173. if (ca->crlf_action == CRLF_UNDEFINED && auto_crlf == AUTO_CRLF_TRUE)
  1174. ca->crlf_action = CRLF_AUTO_CRLF;
  1175. if (ca->crlf_action == CRLF_UNDEFINED && auto_crlf == AUTO_CRLF_INPUT)
  1176. ca->crlf_action = CRLF_AUTO_INPUT;
  1177. }
  1178. void reset_parsed_attributes(void)
  1179. {
  1180. struct convert_driver *drv, *next;
  1181. attr_check_free(check);
  1182. check = NULL;
  1183. reset_merge_attributes();
  1184. for (drv = user_convert; drv; drv = next) {
  1185. next = drv->next;
  1186. free((void *)drv->name);
  1187. free(drv);
  1188. }
  1189. user_convert = NULL;
  1190. user_convert_tail = NULL;
  1191. }
  1192. int would_convert_to_git_filter_fd(const struct index_state *istate, const char *path)
  1193. {
  1194. struct conv_attrs ca;
  1195. convert_attrs(istate, &ca, path);
  1196. if (!ca.drv)
  1197. return 0;
  1198. /*
  1199. * Apply a filter to an fd only if the filter is required to succeed.
  1200. * We must die if the filter fails, because the original data before
  1201. * filtering is not available.
  1202. */
  1203. if (!ca.drv->required)
  1204. return 0;
  1205. return apply_filter(path, NULL, 0, -1, NULL, ca.drv, CAP_CLEAN, NULL, NULL);
  1206. }
  1207. const char *get_convert_attr_ascii(const struct index_state *istate, const char *path)
  1208. {
  1209. struct conv_attrs ca;
  1210. convert_attrs(istate, &ca, path);
  1211. switch (ca.attr_action) {
  1212. case CRLF_UNDEFINED:
  1213. return "";
  1214. case CRLF_BINARY:
  1215. return "-text";
  1216. case CRLF_TEXT:
  1217. return "text";
  1218. case CRLF_TEXT_INPUT:
  1219. return "text eol=lf";
  1220. case CRLF_TEXT_CRLF:
  1221. return "text eol=crlf";
  1222. case CRLF_AUTO:
  1223. return "text=auto";
  1224. case CRLF_AUTO_CRLF:
  1225. return "text=auto eol=crlf";
  1226. case CRLF_AUTO_INPUT:
  1227. return "text=auto eol=lf";
  1228. }
  1229. return "";
  1230. }
  1231. int convert_to_git(const struct index_state *istate,
  1232. const char *path, const char *src, size_t len,
  1233. struct strbuf *dst, int conv_flags)
  1234. {
  1235. int ret = 0;
  1236. struct conv_attrs ca;
  1237. convert_attrs(istate, &ca, path);
  1238. ret |= apply_filter(path, src, len, -1, dst, ca.drv, CAP_CLEAN, NULL, NULL);
  1239. if (!ret && ca.drv && ca.drv->required)
  1240. die(_("%s: clean filter '%s' failed"), path, ca.drv->name);
  1241. if (ret && dst) {
  1242. src = dst->buf;
  1243. len = dst->len;
  1244. }
  1245. ret |= encode_to_git(path, src, len, dst, ca.working_tree_encoding, conv_flags);
  1246. if (ret && dst) {
  1247. src = dst->buf;
  1248. len = dst->len;
  1249. }
  1250. if (!(conv_flags & CONV_EOL_KEEP_CRLF)) {
  1251. ret |= crlf_to_git(istate, path, src, len, dst, ca.crlf_action, conv_flags);
  1252. if (ret && dst) {
  1253. src = dst->buf;
  1254. len = dst->len;
  1255. }
  1256. }
  1257. return ret | ident_to_git(src, len, dst, ca.ident);
  1258. }
  1259. void convert_to_git_filter_fd(const struct index_state *istate,
  1260. const char *path, int fd, struct strbuf *dst,
  1261. int conv_flags)
  1262. {
  1263. struct conv_attrs ca;
  1264. convert_attrs(istate, &ca, path);
  1265. assert(ca.drv);
  1266. assert(ca.drv->clean || ca.drv->process);
  1267. if (!apply_filter(path, NULL, 0, fd, dst, ca.drv, CAP_CLEAN, NULL, NULL))
  1268. die(_("%s: clean filter '%s' failed"), path, ca.drv->name);
  1269. encode_to_git(path, dst->buf, dst->len, dst, ca.working_tree_encoding, conv_flags);
  1270. crlf_to_git(istate, path, dst->buf, dst->len, dst, ca.crlf_action, conv_flags);
  1271. ident_to_git(dst->buf, dst->len, dst, ca.ident);
  1272. }
  1273. static int convert_to_working_tree_internal(const struct index_state *istate,
  1274. const char *path, const char *src,
  1275. size_t len, struct strbuf *dst,
  1276. int normalizing,
  1277. const struct checkout_metadata *meta,
  1278. struct delayed_checkout *dco)
  1279. {
  1280. int ret = 0, ret_filter = 0;
  1281. struct conv_attrs ca;
  1282. convert_attrs(istate, &ca, path);
  1283. ret |= ident_to_worktree(src, len, dst, ca.ident);
  1284. if (ret) {
  1285. src = dst->buf;
  1286. len = dst->len;
  1287. }
  1288. /*
  1289. * CRLF conversion can be skipped if normalizing, unless there
  1290. * is a smudge or process filter (even if the process filter doesn't
  1291. * support smudge). The filters might expect CRLFs.
  1292. */
  1293. if ((ca.drv && (ca.drv->smudge || ca.drv->process)) || !normalizing) {
  1294. ret |= crlf_to_worktree(src, len, dst, ca.crlf_action);
  1295. if (ret) {
  1296. src = dst->buf;
  1297. len = dst->len;
  1298. }
  1299. }
  1300. ret |= encode_to_worktree(path, src, len, dst, ca.working_tree_encoding);
  1301. if (ret) {
  1302. src = dst->buf;
  1303. len = dst->len;
  1304. }
  1305. ret_filter = apply_filter(
  1306. path, src, len, -1, dst, ca.drv, CAP_SMUDGE, meta, dco);
  1307. if (!ret_filter && ca.drv && ca.drv->required)
  1308. die(_("%s: smudge filter %s failed"), path, ca.drv->name);
  1309. return ret | ret_filter;
  1310. }
  1311. int async_convert_to_working_tree(const struct index_state *istate,
  1312. const char *path, const char *src,
  1313. size_t len, struct strbuf *dst,
  1314. const struct checkout_metadata *meta,
  1315. void *dco)
  1316. {
  1317. return convert_to_working_tree_internal(istate, path, src, len, dst, 0, meta, dco);
  1318. }
  1319. int convert_to_working_tree(const struct index_state *istate,
  1320. const char *path, const char *src,
  1321. size_t len, struct strbuf *dst,
  1322. const struct checkout_metadata *meta)
  1323. {
  1324. return convert_to_working_tree_internal(istate, path, src, len, dst, 0, meta, NULL);
  1325. }
  1326. int renormalize_buffer(const struct index_state *istate, const char *path,
  1327. const char *src, size_t len, struct strbuf *dst)
  1328. {
  1329. int ret = convert_to_working_tree_internal(istate, path, src, len, dst, 1, NULL, NULL);
  1330. if (ret) {
  1331. src = dst->buf;
  1332. len = dst->len;
  1333. }
  1334. return ret | convert_to_git(istate, path, src, len, dst, CONV_EOL_RENORMALIZE);
  1335. }
  1336. /*****************************************************************
  1337. *
  1338. * Streaming conversion support
  1339. *
  1340. *****************************************************************/
  1341. typedef int (*filter_fn)(struct stream_filter *,
  1342. const char *input, size_t *isize_p,
  1343. char *output, size_t *osize_p);
  1344. typedef void (*free_fn)(struct stream_filter *);
  1345. struct stream_filter_vtbl {
  1346. filter_fn filter;
  1347. free_fn free;
  1348. };
  1349. struct stream_filter {
  1350. struct stream_filter_vtbl *vtbl;
  1351. };
  1352. static int null_filter_fn(struct stream_filter *filter,
  1353. const char *input, size_t *isize_p,
  1354. char *output, size_t *osize_p)
  1355. {
  1356. size_t count;
  1357. if (!input)
  1358. return 0; /* we do not keep any states */
  1359. count = *isize_p;
  1360. if (*osize_p < count)
  1361. count = *osize_p;
  1362. if (count) {
  1363. memmove(output, input, count);
  1364. *isize_p -= count;
  1365. *osize_p -= count;
  1366. }
  1367. return 0;
  1368. }
  1369. static void null_free_fn(struct stream_filter *filter)
  1370. {
  1371. ; /* nothing -- null instances are shared */
  1372. }
  1373. static struct stream_filter_vtbl null_vtbl = {
  1374. null_filter_fn,
  1375. null_free_fn,
  1376. };
  1377. static struct stream_filter null_filter_singleton = {
  1378. &null_vtbl,
  1379. };
  1380. int is_null_stream_filter(struct stream_filter *filter)
  1381. {
  1382. return filter == &null_filter_singleton;
  1383. }
  1384. /*
  1385. * LF-to-CRLF filter
  1386. */
  1387. struct lf_to_crlf_filter {
  1388. struct stream_filter filter;
  1389. unsigned has_held:1;
  1390. char held;
  1391. };
  1392. static int lf_to_crlf_filter_fn(struct stream_filter *filter,
  1393. const char *input, size_t *isize_p,
  1394. char *output, size_t *osize_p)
  1395. {
  1396. size_t count, o = 0;
  1397. struct lf_to_crlf_filter *lf_to_crlf = (struct lf_to_crlf_filter *)filter;
  1398. /*
  1399. * We may be holding onto the CR to see if it is followed by a
  1400. * LF, in which case we would need to go to the main loop.
  1401. * Otherwise, just emit it to the output stream.
  1402. */
  1403. if (lf_to_crlf->has_held && (lf_to_crlf->held != '\r' || !input)) {
  1404. output[o++] = lf_to_crlf->held;
  1405. lf_to_crlf->has_held = 0;
  1406. }
  1407. /* We are told to drain */
  1408. if (!input) {
  1409. *osize_p -= o;
  1410. return 0;
  1411. }
  1412. count = *isize_p;
  1413. if (count || lf_to_crlf->has_held) {
  1414. size_t i;
  1415. int was_cr = 0;
  1416. if (lf_to_crlf->has_held) {
  1417. was_cr = 1;
  1418. lf_to_crlf->has_held = 0;
  1419. }
  1420. for (i = 0; o < *osize_p && i < count; i++) {
  1421. char ch = input[i];
  1422. if (ch == '\n') {
  1423. output[o++] = '\r';
  1424. } else if (was_cr) {
  1425. /*
  1426. * Previous round saw CR and it is not followed
  1427. * by a LF; emit the CR before processing the
  1428. * current character.
  1429. */
  1430. output[o++] = '\r';
  1431. }
  1432. /*
  1433. * We may have consumed the last output slot,
  1434. * in which case we need to break out of this
  1435. * loop; hold the current character before
  1436. * returning.
  1437. */
  1438. if (*osize_p <= o) {
  1439. lf_to_crlf->has_held = 1;
  1440. lf_to_crlf->held = ch;
  1441. continue; /* break but increment i */
  1442. }
  1443. if (ch == '\r') {
  1444. was_cr = 1;
  1445. continue;
  1446. }
  1447. was_cr = 0;
  1448. output[o++] = ch;
  1449. }
  1450. *osize_p -= o;
  1451. *isize_p -= i;
  1452. if (!lf_to_crlf->has_held && was_cr) {
  1453. lf_to_crlf->has_held = 1;
  1454. lf_to_crlf->held = '\r';
  1455. }
  1456. }
  1457. return 0;
  1458. }
  1459. static void lf_to_crlf_free_fn(struct stream_filter *filter)
  1460. {
  1461. free(filter);
  1462. }
  1463. static struct stream_filter_vtbl lf_to_crlf_vtbl = {
  1464. lf_to_crlf_filter_fn,
  1465. lf_to_crlf_free_fn,
  1466. };
  1467. static struct stream_filter *lf_to_crlf_filter(void)
  1468. {
  1469. struct lf_to_crlf_filter *lf_to_crlf = xcalloc(1, sizeof(*lf_to_crlf));
  1470. lf_to_crlf->filter.vtbl = &lf_to_crlf_vtbl;
  1471. return (struct stream_filter *)lf_to_crlf;
  1472. }
  1473. /*
  1474. * Cascade filter
  1475. */
  1476. #define FILTER_BUFFER 1024
  1477. struct cascade_filter {
  1478. struct stream_filter filter;
  1479. struct stream_filter *one;
  1480. struct stream_filter *two;
  1481. char buf[FILTER_BUFFER];
  1482. int end, ptr;
  1483. };
  1484. static int cascade_filter_fn(struct stream_filter *filter,
  1485. const char *input, size_t *isize_p,
  1486. char *output, size_t *osize_p)
  1487. {
  1488. struct cascade_filter *cas = (struct cascade_filter *) filter;
  1489. size_t filled = 0;
  1490. size_t sz = *osize_p;
  1491. size_t to_feed, remaining;
  1492. /*
  1493. * input -- (one) --> buf -- (two) --> output
  1494. */
  1495. while (filled < sz) {
  1496. remaining = sz - filled;
  1497. /* do we already have something to feed two with? */
  1498. if (cas->ptr < cas->end) {
  1499. to_feed = cas->end - cas->ptr;
  1500. if (stream_filter(cas->two,
  1501. cas->buf + cas->ptr, &to_feed,
  1502. output + filled, &remaining))
  1503. return -1;
  1504. cas->ptr += (cas->end - cas->ptr) - to_feed;
  1505. filled = sz - remaining;
  1506. continue;
  1507. }
  1508. /* feed one from upstream and have it emit into our buffer */
  1509. to_feed = input ? *isize_p : 0;
  1510. if (input && !to_feed)
  1511. break;
  1512. remaining = sizeof(cas->buf);
  1513. if (stream_filter(cas->one,
  1514. input, &to_feed,
  1515. cas->buf, &remaining))
  1516. return -1;
  1517. cas->end = sizeof(cas->buf) - remaining;
  1518. cas->ptr = 0;
  1519. if (input) {
  1520. size_t fed = *isize_p - to_feed;
  1521. *isize_p -= fed;
  1522. input += fed;
  1523. }
  1524. /* do we know that we drained one completely? */
  1525. if (input || cas->end)
  1526. continue;
  1527. /* tell two to drain; we have nothing more to give it */
  1528. to_feed = 0;
  1529. remaining = sz - filled;
  1530. if (stream_filter(cas->two,
  1531. NULL, &to_feed,
  1532. output + filled, &remaining))
  1533. return -1;
  1534. if (remaining == (sz - filled))
  1535. break; /* completely drained two */
  1536. filled = sz - remaining;
  1537. }
  1538. *osize_p -= filled;
  1539. return 0;
  1540. }
  1541. static void cascade_free_fn(struct stream_filter *filter)
  1542. {
  1543. struct cascade_filter *cas = (struct cascade_filter *)filter;
  1544. free_stream_filter(cas->one);
  1545. free_stream_filter(cas->two);
  1546. free(filter);
  1547. }
  1548. static struct stream_filter_vtbl cascade_vtbl = {
  1549. cascade_filter_fn,
  1550. cascade_free_fn,
  1551. };
  1552. static struct stream_filter *cascade_filter(struct stream_filter *one,
  1553. struct stream_filter *two)
  1554. {
  1555. struct cascade_filter *cascade;
  1556. if (!one || is_null_stream_filter(one))
  1557. return two;
  1558. if (!two || is_null_stream_filter(two))
  1559. return one;
  1560. cascade = xmalloc(sizeof(*cascade));
  1561. cascade->one = one;
  1562. cascade->two = two;
  1563. cascade->end = cascade->ptr = 0;
  1564. cascade->filter.vtbl = &cascade_vtbl;
  1565. return (struct stream_filter *)cascade;
  1566. }
  1567. /*
  1568. * ident filter
  1569. */
  1570. #define IDENT_DRAINING (-1)
  1571. #define IDENT_SKIPPING (-2)
  1572. struct ident_filter {
  1573. struct stream_filter filter;
  1574. struct strbuf left;
  1575. int state;
  1576. char ident[GIT_MAX_HEXSZ + 5]; /* ": x40 $" */
  1577. };
  1578. static int is_foreign_ident(const char *str)
  1579. {
  1580. int i;
  1581. if (!skip_prefix(str, "$Id: ", &str))
  1582. return 0;
  1583. for (i = 0; str[i]; i++) {
  1584. if (isspace(str[i]) && str[i+1] != '$')
  1585. return 1;
  1586. }
  1587. return 0;
  1588. }
  1589. static void ident_drain(struct ident_filter *ident, char **output_p, size_t *osize_p)
  1590. {
  1591. size_t to_drain = ident->left.len;
  1592. if (*osize_p < to_drain)
  1593. to_drain = *osize_p;
  1594. if (to_drain) {
  1595. memcpy(*output_p, ident->left.buf, to_drain);
  1596. strbuf_remove(&ident->left, 0, to_drain);
  1597. *output_p += to_drain;
  1598. *osize_p -= to_drain;
  1599. }
  1600. if (!ident->left.len)
  1601. ident->state = 0;
  1602. }
  1603. static int ident_filter_fn(struct stream_filter *filter,
  1604. const char *input, size_t *isize_p,
  1605. char *output, size_t *osize_p)
  1606. {
  1607. struct ident_filter *ident = (struct ident_filter *)filter;
  1608. static const char head[] = "$Id";
  1609. if (!input) {
  1610. /* drain upon eof */
  1611. switch (ident->state) {
  1612. default:
  1613. strbuf_add(&ident->left, head, ident->state);
  1614. /* fallthrough */
  1615. case IDENT_SKIPPING:
  1616. /* fallthrough */
  1617. case IDENT_DRAINING:
  1618. ident_drain(ident, &output, osize_p);
  1619. }
  1620. return 0;
  1621. }
  1622. while (*isize_p || (ident->state == IDENT_DRAINING)) {
  1623. int ch;
  1624. if (ident->state == IDENT_DRAINING) {
  1625. ident_drain(ident, &output, osize_p);
  1626. if (!*osize_p)
  1627. break;
  1628. continue;
  1629. }
  1630. ch = *(input++);
  1631. (*isize_p)--;
  1632. if (ident->state == IDENT_SKIPPING) {
  1633. /*
  1634. * Skipping until '$' or LF, but keeping them
  1635. * in case it is a foreign ident.
  1636. */
  1637. strbuf_addch(&ident->left, ch);
  1638. if (ch != '\n' && ch != '$')
  1639. continue;
  1640. if (ch == '$' && !is_foreign_ident(ident->left.buf)) {
  1641. strbuf_setlen(&ident->left, sizeof(head) - 1);
  1642. strbuf_addstr(&ident->left, ident->ident);
  1643. }
  1644. ident->state = IDENT_DRAINING;
  1645. continue;
  1646. }
  1647. if (ident->state < sizeof(head) &&
  1648. head[ident->state] == ch) {
  1649. ident->state++;
  1650. continue;
  1651. }
  1652. if (ident->state)
  1653. strbuf_add(&ident->left, head, ident->state);
  1654. if (ident->state == sizeof(head) - 1) {
  1655. if (ch != ':' && ch != '$') {
  1656. strbuf_addch(&ident->left, ch);
  1657. ident->state = 0;
  1658. continue;
  1659. }
  1660. if (ch == ':') {
  1661. strbuf_addch(&ident->left, ch);
  1662. ident->state = IDENT_SKIPPING;
  1663. } else {
  1664. strbuf_addstr(&ident->left, ident->ident);
  1665. ident->state = IDENT_DRAINING;
  1666. }
  1667. continue;
  1668. }
  1669. strbuf_addch(&ident->left, ch);
  1670. ident->state = IDENT_DRAINING;
  1671. }
  1672. return 0;
  1673. }
  1674. static void ident_free_fn(struct stream_filter *filter)
  1675. {
  1676. struct ident_filter *ident = (struct ident_filter *)filter;
  1677. strbuf_release(&ident->left);
  1678. free(filter);
  1679. }
  1680. static struct stream_filter_vtbl ident_vtbl = {
  1681. ident_filter_fn,
  1682. ident_free_fn,
  1683. };
  1684. static struct stream_filter *ident_filter(const struct object_id *oid)
  1685. {
  1686. struct ident_filter *ident = xmalloc(sizeof(*ident));
  1687. xsnprintf(ident->ident, sizeof(ident->ident),
  1688. ": %s $", oid_to_hex(oid));
  1689. strbuf_init(&ident->left, 0);
  1690. ident->filter.vtbl = &ident_vtbl;
  1691. ident->state = 0;
  1692. return (struct stream_filter *)ident;
  1693. }
  1694. /*
  1695. * Return an appropriately constructed filter for the path, or NULL if
  1696. * the contents cannot be filtered without reading the whole thing
  1697. * in-core.
  1698. *
  1699. * Note that you would be crazy to set CRLF, smudge/clean or ident to a
  1700. * large binary blob you would want us not to slurp into the memory!
  1701. */
  1702. struct stream_filter *get_stream_filter(const struct index_state *istate,
  1703. const char *path,
  1704. const struct object_id *oid)
  1705. {
  1706. struct conv_attrs ca;
  1707. struct stream_filter *filter = NULL;
  1708. convert_attrs(istate, &ca, path);
  1709. if (ca.drv && (ca.drv->process || ca.drv->smudge || ca.drv->clean))
  1710. return NULL;
  1711. if (ca.working_tree_encoding)
  1712. return NULL;
  1713. if (ca.crlf_action == CRLF_AUTO || ca.crlf_action == CRLF_AUTO_CRLF)
  1714. return NULL;
  1715. if (ca.ident)
  1716. filter = ident_filter(oid);
  1717. if (output_eol(ca.crlf_action) == EOL_CRLF)
  1718. filter = cascade_filter(filter, lf_to_crlf_filter());
  1719. else
  1720. filter = cascade_filter(filter, &null_filter_singleton);
  1721. return filter;
  1722. }
  1723. void free_stream_filter(struct stream_filter *filter)
  1724. {
  1725. filter->vtbl->free(filter);
  1726. }
  1727. int stream_filter(struct stream_filter *filter,
  1728. const char *input, size_t *isize_p,
  1729. char *output, size_t *osize_p)
  1730. {
  1731. return filter->vtbl->filter(filter, input, isize_p, output, osize_p);
  1732. }
  1733. void init_checkout_metadata(struct checkout_metadata *meta, const char *refname,
  1734. const struct object_id *treeish,
  1735. const struct object_id *blob)
  1736. {
  1737. memset(meta, 0, sizeof(*meta));
  1738. if (refname)
  1739. meta->refname = refname;
  1740. if (treeish)
  1741. oidcpy(&meta->treeish, treeish);
  1742. if (blob)
  1743. oidcpy(&meta->blob, blob);
  1744. }
  1745. void clone_checkout_metadata(struct checkout_metadata *dst,
  1746. const struct checkout_metadata *src,
  1747. const struct object_id *blob)
  1748. {
  1749. memcpy(dst, src, sizeof(*dst));
  1750. if (blob)
  1751. oidcpy(&dst->blob, blob);
  1752. }