| 706 | } |
| 707 | |
| 708 | const char *repo_logmsg_reencode(struct repository *r, |
| 709 | const struct commit *commit, |
| 710 | char **commit_encoding, |
| 711 | const char *output_encoding) |
| 712 | { |
| 713 | static const char *utf8 = "UTF-8"; |
| 714 | const char *use_encoding; |
| 715 | char *encoding; |
| 716 | const char *msg = repo_get_commit_buffer(r, commit, NULL); |
| 717 | char *out; |
| 718 | |
| 719 | if (!output_encoding || !*output_encoding) { |
| 720 | if (commit_encoding) |
| 721 | *commit_encoding = get_header(msg, "encoding"); |
| 722 | return msg; |
| 723 | } |
| 724 | encoding = get_header(msg, "encoding"); |
| 725 | if (commit_encoding) |
| 726 | *commit_encoding = encoding; |
| 727 | use_encoding = encoding ? encoding : utf8; |
| 728 | if (same_encoding(use_encoding, output_encoding)) { |
| 729 | /* |
| 730 | * No encoding work to be done. If we have no encoding header |
| 731 | * at all, then there's nothing to do, and we can return the |
| 732 | * message verbatim (whether newly allocated or not). |
| 733 | */ |
| 734 | if (!encoding) |
| 735 | return msg; |
| 736 | |
| 737 | /* |
| 738 | * Otherwise, we still want to munge the encoding header in the |
| 739 | * result, which will be done by modifying the buffer. If we |
| 740 | * are using a fresh copy, we can reuse it. But if we are using |
| 741 | * the cached copy from repo_get_commit_buffer, we need to duplicate it |
| 742 | * to avoid munging the cached copy. |
| 743 | */ |
| 744 | if (msg == get_cached_commit_buffer(r, commit, NULL)) |
| 745 | out = xstrdup(msg); |
| 746 | else |
| 747 | out = (char *)msg; |
| 748 | } |
| 749 | else { |
| 750 | /* |
| 751 | * There's actual encoding work to do. Do the reencoding, which |
| 752 | * still leaves the header to be replaced in the next step. At |
| 753 | * this point, we are done with msg. If we allocated a fresh |
| 754 | * copy, we can free it. |
| 755 | */ |
| 756 | out = reencode_string(msg, output_encoding, use_encoding); |
| 757 | if (out) |
| 758 | repo_unuse_commit_buffer(r, commit, msg); |
| 759 | } |
| 760 | |
| 761 | /* |
| 762 | * This replacement actually consumes the buffer we hand it, so we do |
| 763 | * not have to worry about freeing the old "out" here. |
| 764 | */ |
| 765 | if (out) |
no test coverage detected