commit: helper methods to reduce redundant blocks of code
* builtin/commit.c: Replace block of code with a one-liner call to logmsg_reencode(). * commit.c: new function for looking up a comit by name * pretty.c: helper methods for getting output encodings Add helpers get_log_output_encoding() and get_commit_output_encoding() that eliminate some messy and duplicate if-blocks. Signed-off-by: Pat Notz <patnotz@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Pat Notz committed
Nov 2, 2010 at 13:59 UTC
a6fa59924d154f2dcfc331357bf553e043aa0242
8 files changed
+36
-32
builtin/commit.c
+4
-22
@@ -896,30 +896,14 @@ static int parse_and_validate_options(int argc, const char *argv[],
896
if (!use_message && renew_authorship)
897
die("--reset-author can be used only with -C, -c or --amend.");
898
if (use_message) {
899
- unsigned char sha1[20];
900
- static char utf8[] = "UTF-8";
899
const char *out_enc;
902
- char *enc, *end;
900
struct commit *commit;
901
905
- if (get_sha1(use_message, sha1))
902
+ commit = lookup_commit_reference_by_name(use_message);
903
+ if (!commit)
904
die("could not lookup commit %s", use_message);
907
- commit = lookup_commit_reference(sha1);
908
- if (!commit || parse_commit(commit))
909
- die("could not parse commit %s", use_message);
910
-
911
- enc = strstr(commit->buffer, "\nencoding");
912
- if (enc) {
913
- end = strchr(enc + 10, '\n');
914
- enc = xstrndup(enc + 10, end - (enc + 10));
915
- } else {
916
- enc = utf8;
917
- }
918
- out_enc = git_commit_encoding ? git_commit_encoding : utf8;
919
-
920
- if (strcmp(out_enc, enc))
921
- use_message_buffer =
922
- reencode_string(commit->buffer, out_enc, enc);
905
+ out_enc = get_commit_output_encoding();
906
+ use_message_buffer = logmsg_reencode(commit, out_enc);
907
908
/*
909
* If we failed to reencode the buffer, just copy it
@@ -929,8 +913,6 @@ static int parse_and_validate_options(int argc, const char *argv[],
913
*/
914
if (use_message_buffer == NULL)
915
use_message_buffer = xstrdup(commit->buffer);
932
- if (enc != utf8)
933
- free(enc);
916
}
917
918
if (!!also + !!only + !!all + !!interactive > 1)
builtin/log.c
+1
-2
@@ -329,8 +329,7 @@ static void show_tagger(char *buf, int len, struct rev_info *rev)
329
struct strbuf out = STRBUF_INIT;
330
331
pp_user_info("Tagger", rev->commit_format, &out, buf, rev->date_mode,
332
- git_log_output_encoding ?
333
- git_log_output_encoding: git_commit_encoding);
332
+ get_log_output_encoding());
333
printf("%s", out.buf);
334
strbuf_release(&out);
335
}
builtin/mailinfo.c
+1
-1
@@ -1032,7 +1032,7 @@ int cmd_mailinfo(int argc, const char **argv, const char *prefix)
1032
*/
1033
git_config(git_mailinfo_config, NULL);
1034
1035
- def_charset = (git_commit_encoding ? git_commit_encoding : "UTF-8");
1035
+ def_charset = get_commit_output_encoding();
1036
metainfo_charset = def_charset;
1037
1038
while (1 < argc && argv[1][0] == '-') {
cache.h
+3
@@ -1003,6 +1003,9 @@ extern int git_env_bool(const char *, int);
1003
extern int git_config_system(void);
1004
extern int git_config_global(void);
1005
extern int config_error_nonbool(const char *);
1006
+extern const char *get_log_output_encoding(void);
1007
+extern const char *get_commit_output_encoding(void);
1008
+
1009
extern const char *config_exclusive_filename;
1010
1011
#define MAX_GITNAME (1000)
commit.c
+13
@@ -49,6 +49,19 @@ struct commit *lookup_commit(const unsigned char *sha1)
49
return check_commit(obj, sha1, 0);
50
}
51
52
+struct commit *lookup_commit_reference_by_name(const char *name)
53
+{
54
+ unsigned char sha1[20];
55
+ struct commit *commit;
56
+
57
+ if (get_sha1(name, sha1))
58
+ return NULL;
59
+ commit = lookup_commit_reference(sha1);
60
+ if (!commit || parse_commit(commit))
61
+ return NULL;
62
+ return commit;
63
+}
64
+
65
static unsigned long parse_commit_date(const char *buf, const char *tail)
66
{
67
const char *dateptr;
commit.h
+1
@@ -36,6 +36,7 @@ struct commit *lookup_commit(const unsigned char *sha1);
36
struct commit *lookup_commit_reference(const unsigned char *sha1);
37
struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
38
int quiet);
39
+struct commit *lookup_commit_reference_by_name(const char *name);
40
41
int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size);
42
environment.c
+11
@@ -192,3 +192,14 @@ int set_git_dir(const char *path)
192
setup_git_env();
193
return 0;
194
}
195
+
196
+const char *get_log_output_encoding(void)
197
+{
198
+ return git_log_output_encoding ? git_log_output_encoding
199
+ : get_commit_output_encoding();
200
+}
201
+
202
+const char *get_commit_output_encoding(void)
203
+{
204
+ return git_commit_encoding ? git_commit_encoding : "UTF-8";
205
+}
pretty.c
+2
-7
@@ -886,8 +886,7 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
886
case 'N':
887
if (c->pretty_ctx->show_notes) {
888
format_display_notes(commit->object.sha1, sb,
889
- git_log_output_encoding ? git_log_output_encoding
890
- : git_commit_encoding, 0);
889
+ get_log_output_encoding(), 0);
890
return 1;
891
}
892
return 0;
@@ -1159,11 +1158,7 @@ char *reencode_commit_message(const struct commit *commit, const char **encoding
1158
{
1159
const char *encoding;
1160
1162
- encoding = (git_log_output_encoding
1163
- ? git_log_output_encoding
1164
- : git_commit_encoding);
1165
- if (!encoding)
1166
- encoding = "UTF-8";
1161
+ encoding = get_log_output_encoding();
1162
if (encoding_p)
1163
*encoding_p = encoding;
1164
return logmsg_reencode(commit, encoding);