sequencer: use logmsg_reencode in get_message

This simplifies the code, as logmsg_reencode handles the
reencoding for us in a single call. It also means we learn
logmsg_reencode's trick of pulling the buffer from disk when
commit->buffer is NULL (we currently just silently return!).
It is doubtful this matters in practice, though, as
sequencer operations would not generally turn off
save_commit_buffer.

Note that we may be fixing a bug here. The existing code
does:

  if (same_encoding(to, from))
	  reencode_string(buf, to, from);

That probably should have been "!same_encoding".

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Jeff King 2014-06-10 17:39:35 -04:00 коммит произвёл Junio C Hamano
Родитель b000c59b0c
Коммит d74a4e57d2
1 изменённых файлов: 5 добавлений и 40 удалений

Просмотреть файл

@ -116,39 +116,23 @@ static const char *action_name(const struct replay_opts *opts)
return opts->action == REPLAY_REVERT ? "revert" : "cherry-pick"; return opts->action == REPLAY_REVERT ? "revert" : "cherry-pick";
} }
static char *get_encoding(const char *message);
struct commit_message { struct commit_message {
char *parent_label; char *parent_label;
const char *label; const char *label;
const char *subject; const char *subject;
char *reencoded_message;
const char *message; const char *message;
}; };
static int get_message(struct commit *commit, struct commit_message *out) static int get_message(struct commit *commit, struct commit_message *out)
{ {
const char *encoding;
const char *abbrev, *subject; const char *abbrev, *subject;
int abbrev_len, subject_len; int abbrev_len, subject_len;
char *q; char *q;
if (!commit->buffer)
return -1;
encoding = get_encoding(commit->buffer);
if (!encoding)
encoding = "UTF-8";
if (!git_commit_encoding) if (!git_commit_encoding)
git_commit_encoding = "UTF-8"; git_commit_encoding = "UTF-8";
out->reencoded_message = NULL; out->message = logmsg_reencode(commit, NULL, git_commit_encoding);
out->message = commit->buffer;
if (same_encoding(encoding, git_commit_encoding))
out->reencoded_message = reencode_string(commit->buffer,
git_commit_encoding, encoding);
if (out->reencoded_message)
out->message = out->reencoded_message;
abbrev = find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV); abbrev = find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV);
abbrev_len = strlen(abbrev); abbrev_len = strlen(abbrev);
@ -167,29 +151,10 @@ static int get_message(struct commit *commit, struct commit_message *out)
return 0; return 0;
} }
static void free_message(struct commit_message *msg) static void free_message(struct commit *commit, struct commit_message *msg)
{ {
free(msg->parent_label); free(msg->parent_label);
free(msg->reencoded_message); logmsg_free(msg->message, commit);
}
static char *get_encoding(const char *message)
{
const char *p = message, *eol;
while (*p && *p != '\n') {
for (eol = p + 1; *eol && *eol != '\n'; eol++)
; /* do nothing */
if (starts_with(p, "encoding ")) {
char *result = xmalloc(eol - 8 - p);
strlcpy(result, p + 9, eol - 8 - p);
return result;
}
p = eol;
if (*p == '\n')
p++;
}
return NULL;
} }
static void write_cherry_pick_head(struct commit *commit, const char *pseudoref) static void write_cherry_pick_head(struct commit *commit, const char *pseudoref)
@ -485,7 +450,7 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
unsigned char head[20]; unsigned char head[20];
struct commit *base, *next, *parent; struct commit *base, *next, *parent;
const char *base_label, *next_label; const char *base_label, *next_label;
struct commit_message msg = { NULL, NULL, NULL, NULL, NULL }; struct commit_message msg = { NULL, NULL, NULL, NULL };
char *defmsg = NULL; char *defmsg = NULL;
struct strbuf msgbuf = STRBUF_INIT; struct strbuf msgbuf = STRBUF_INIT;
int res, unborn = 0, allow; int res, unborn = 0, allow;
@ -650,7 +615,7 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
res = run_git_commit(defmsg, opts, allow); res = run_git_commit(defmsg, opts, allow);
leave: leave:
free_message(&msg); free_message(commit, &msg);
free(defmsg); free(defmsg);
return res; return res;