MCPcopy Index your code
hub / github.com/git/git / encode_to_git

Function encode_to_git

convert.c:387–476  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

385static const char *default_encoding = "UTF-8";
386
387static int encode_to_git(const char *path, const char *src, size_t src_len,
388 struct strbuf *buf, const char *enc, int conv_flags)
389{
390 char *dst;
391 size_t dst_len;
392 int die_on_error = conv_flags & CONV_WRITE_OBJECT;
393
394 /*
395 * No encoding is specified or there is nothing to encode.
396 * Tell the caller that the content was not modified.
397 */
398 if (!enc || (src && !src_len))
399 return 0;
400
401 /*
402 * Looks like we got called from "would_convert_to_git()".
403 * This means Git wants to know if it would encode (= modify!)
404 * the content. Let's answer with "yes", since an encoding was
405 * specified.
406 */
407 if (!buf && !src)
408 return 1;
409
410 if (validate_encoding(path, enc, src, src_len, die_on_error))
411 return 0;
412
413 trace_encoding("source", path, enc, src, src_len);
414 dst = reencode_string_len(src, src_len, default_encoding, enc,
415 &dst_len);
416 if (!dst) {
417 /*
418 * We could add the blob "as-is" to Git. However, on checkout
419 * we would try to re-encode to the original encoding. This
420 * would fail and we would leave the user with a messed-up
421 * working tree. Let's try to avoid this by screaming loud.
422 */
423 const char* msg = _("failed to encode '%s' from %s to %s");
424 if (die_on_error)
425 die(msg, path, enc, default_encoding);
426 else {
427 error(msg, path, enc, default_encoding);
428 return 0;
429 }
430 }
431 trace_encoding("destination", path, default_encoding, dst, dst_len);
432
433 /*
434 * UTF supports lossless conversion round tripping [1] and conversions
435 * between UTF and other encodings are mostly round trip safe as
436 * Unicode aims to be a superset of all other character encodings.
437 * However, certain encodings (e.g. SHIFT-JIS) are known to have round
438 * trip issues [2]. Check the round trip conversion for all encodings
439 * listed in core.checkRoundtripEncoding.
440 *
441 * The round trip check is only performed if content is written to Git.
442 * This ensures that no information is lost during conversion to/from
443 * the internal UTF-8 representation.
444 *

Callers 2

convert_to_gitFunction · 0.85
convert_to_git_filter_fdFunction · 0.85

Calls 7

validate_encodingFunction · 0.85
trace_encodingFunction · 0.85
errorFunction · 0.85
check_roundtripFunction · 0.85
strbuf_attachFunction · 0.85
reencode_string_lenFunction · 0.70
dieFunction · 0.70

Tested by

no test coverage detected