convert_to_git(): safe_crlf/checksafe becomes int conv_flags

When calling convert_to_git(), the checksafe parameter defined what should happen if the EOL conversion (CRLF --> LF --> CRLF) does not roundtrip cleanly. In addition, it also defined if line endings should be renormalized (CRLF --> LF) or kept as they are. checksafe was an safe_crlf enum with these values: SAFE_CRLF_FALSE: do nothing in case of EOL roundtrip errors SAFE_CRLF_FAIL: die in case of EOL roundtrip errors SAFE_CRLF_WARN: print a warning in case of EOL roundtrip errors SAFE_CRLF_RENORMALIZE: change CRLF to LF SAFE_CRLF_KEEP_CRLF: keep all line endings as they are In some cases the integer value 0 was passed as checksafe parameter instead of the correct enum value SAFE_CRLF_FALSE. That was no problem because SAFE_CRLF_FALSE is defined as 0. FALSE/FAIL/WARN are different from RENORMALIZE and KEEP_CRLF. Therefore, an enum is not ideal. Let's use a integer bit pattern instead and rename the parameter to conv_flags to make it more generically usable. This allows us to extend the bit pattern in a subsequent commit. Reported-By: Randall S. Becker <rsbecker@nexbridge.com> Helped-By: Lars Schneider <larsxschneider@gmail.com> Signed-off-by: Torsten Bögershausen <tboegi@web.de> Signed-off-by: Lars Schneider <larsxschneider@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Torsten Bögershausen committed Jan 13, 2018 at 23:49 UTC 8462ff43e42ab67cecd16fdfb59451a53cc8a945
8 files changed +46 -46
apply.c
+3 -3
@@ -2263,8 +2263,8 @@ static void show_stats(struct apply_state *state, struct patch *patch)
2263 static int read_old_data(struct stat *st, struct patch *patch,
2264 const char *path, struct strbuf *buf)
2265 {
2266 - enum safe_crlf safe_crlf = patch->crlf_in_old ?
2267 - SAFE_CRLF_KEEP_CRLF : SAFE_CRLF_RENORMALIZE;
2266 + int conv_flags = patch->crlf_in_old ?
2267 + CONV_EOL_KEEP_CRLF : CONV_EOL_RENORMALIZE;
2268 switch (st->st_mode & S_IFMT) {
2269 case S_IFLNK:
2270 if (strbuf_readlink(buf, path, st->st_size) < 0)
@@ -2281,7 +2281,7 @@ static int read_old_data(struct stat *st, struct patch *patch,
2281 * should never look at the index when explicit crlf option
2282 * is given.
2283 */
2284 - convert_to_git(NULL, path, buf->buf, buf->len, buf, safe_crlf);
2284 + convert_to_git(NULL, path, buf->buf, buf->len, buf, conv_flags);
2285 return 0;
2286 default:
2287 return -1;
combine-diff.c
+1 -1
@@ -1053,7 +1053,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
1053 if (is_file) {
1054 struct strbuf buf = STRBUF_INIT;
1055
1056 - if (convert_to_git(&the_index, elem->path, result, len, &buf, safe_crlf)) {
1056 + if (convert_to_git(&the_index, elem->path, result, len, &buf, global_conv_flags_eol)) {
1057 free(result);
1058 result = strbuf_detach(&buf, &len);
1059 result_size = len;
config.c
+5 -2
@@ -1149,11 +1149,14 @@ static int git_default_core_config(const char *var, const char *value)
1149 }
1150
1151 if (!strcmp(var, "core.safecrlf")) {
1152 + int eol_rndtrp_die;
1153 if (value && !strcasecmp(value, "warn")) {
1153 - safe_crlf = SAFE_CRLF_WARN;
1154 + global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
1155 return 0;
1156 }
1156 - safe_crlf = git_config_bool(var, value);
1157 + eol_rndtrp_die = git_config_bool(var, value);
1158 + global_conv_flags_eol = eol_rndtrp_die ?
1159 + CONV_EOL_RNDTRP_DIE : CONV_EOL_RNDTRP_WARN;
1160 return 0;
1161 }
1162
convert.c
+19 -19
@@ -193,30 +193,30 @@ static enum eol output_eol(enum crlf_action crlf_action)
193 return core_eol;
194 }
195
196 -static void check_safe_crlf(const char *path, enum crlf_action crlf_action,
196 +static void check_global_conv_flags_eol(const char *path, enum crlf_action crlf_action,
197 struct text_stat *old_stats, struct text_stat *new_stats,
198 - enum safe_crlf checksafe)
198 + int conv_flags)
199 {
200 if (old_stats->crlf && !new_stats->crlf ) {
201 /*
202 * CRLFs would not be restored by checkout
203 */
204 - if (checksafe == SAFE_CRLF_WARN)
204 + if (conv_flags & CONV_EOL_RNDTRP_DIE)
205 + die(_("CRLF would be replaced by LF in %s."), path);
206 + else if (conv_flags & CONV_EOL_RNDTRP_WARN)
207 warning(_("CRLF will be replaced by LF in %s.\n"
208 "The file will have its original line"
209 " endings in your working directory."), path);
208 - else /* i.e. SAFE_CRLF_FAIL */
209 - die(_("CRLF would be replaced by LF in %s."), path);
210 } else if (old_stats->lonelf && !new_stats->lonelf ) {
211 /*
212 * CRLFs would be added by checkout
213 */
214 - if (checksafe == SAFE_CRLF_WARN)
214 + if (conv_flags & CONV_EOL_RNDTRP_DIE)
215 + die(_("LF would be replaced by CRLF in %s"), path);
216 + else if (conv_flags & CONV_EOL_RNDTRP_WARN)
217 warning(_("LF will be replaced by CRLF in %s.\n"
218 "The file will have its original line"
219 " endings in your working directory."), path);
218 - else /* i.e. SAFE_CRLF_FAIL */
219 - die(_("LF would be replaced by CRLF in %s"), path);
220 }
221 }
222
@@ -268,7 +268,7 @@ static int will_convert_lf_to_crlf(size_t len, struct text_stat *stats,
268 static int crlf_to_git(const struct index_state *istate,
269 const char *path, const char *src, size_t len,
270 struct strbuf *buf,
271 - enum crlf_action crlf_action, enum safe_crlf checksafe)
271 + enum crlf_action crlf_action, int conv_flags)
272 {
273 struct text_stat stats;
274 char *dst;
@@ -298,12 +298,12 @@ static int crlf_to_git(const struct index_state *istate,
298 * unless we want to renormalize in a merge or
299 * cherry-pick.
300 */
301 - if ((checksafe != SAFE_CRLF_RENORMALIZE) &&
301 + if ((!(conv_flags & CONV_EOL_RENORMALIZE)) &&
302 has_crlf_in_index(istate, path))
303 convert_crlf_into_lf = 0;
304 }
305 - if ((checksafe == SAFE_CRLF_WARN ||
306 - (checksafe == SAFE_CRLF_FAIL)) && len) {
305 + if (((conv_flags & CONV_EOL_RNDTRP_WARN) ||
306 + ((conv_flags & CONV_EOL_RNDTRP_DIE) && len))) {
307 struct text_stat new_stats;
308 memcpy(&new_stats, &stats, sizeof(new_stats));
309 /* simulate "git add" */
@@ -316,7 +316,7 @@ static int crlf_to_git(const struct index_state *istate,
316 new_stats.crlf += new_stats.lonelf;
317 new_stats.lonelf = 0;
318 }
319 - check_safe_crlf(path, crlf_action, &stats, &new_stats, checksafe);
319 + check_global_conv_flags_eol(path, crlf_action, &stats, &new_stats, conv_flags);
320 }
321 if (!convert_crlf_into_lf)
322 return 0;
@@ -1129,7 +1129,7 @@ const char *get_convert_attr_ascii(const char *path)
1129
1130 int convert_to_git(const struct index_state *istate,
1131 const char *path, const char *src, size_t len,
1132 - struct strbuf *dst, enum safe_crlf checksafe)
1132 + struct strbuf *dst, int conv_flags)
1133 {
1134 int ret = 0;
1135 struct conv_attrs ca;
@@ -1144,8 +1144,8 @@ int convert_to_git(const struct index_state *istate,
1144 src = dst->buf;
1145 len = dst->len;
1146 }
1147 - if (checksafe != SAFE_CRLF_KEEP_CRLF) {
1148 - ret |= crlf_to_git(istate, path, src, len, dst, ca.crlf_action, checksafe);
1147 + if (!(conv_flags & CONV_EOL_KEEP_CRLF)) {
1148 + ret |= crlf_to_git(istate, path, src, len, dst, ca.crlf_action, conv_flags);
1149 if (ret && dst) {
1150 src = dst->buf;
1151 len = dst->len;
@@ -1156,7 +1156,7 @@ int convert_to_git(const struct index_state *istate,
1156
1157 void convert_to_git_filter_fd(const struct index_state *istate,
1158 const char *path, int fd, struct strbuf *dst,
1159 - enum safe_crlf checksafe)
1159 + int conv_flags)
1160 {
1161 struct conv_attrs ca;
1162 convert_attrs(&ca, path);
@@ -1167,7 +1167,7 @@ void convert_to_git_filter_fd(const struct index_state *istate,
1167 if (!apply_filter(path, NULL, 0, fd, dst, ca.drv, CAP_CLEAN, NULL))
1168 die("%s: clean filter '%s' failed", path, ca.drv->name);
1169
1170 - crlf_to_git(istate, path, dst->buf, dst->len, dst, ca.crlf_action, checksafe);
1170 + crlf_to_git(istate, path, dst->buf, dst->len, dst, ca.crlf_action, conv_flags);
1171 ident_to_git(path, dst->buf, dst->len, dst, ca.ident);
1172 }
1173
@@ -1226,7 +1226,7 @@ int renormalize_buffer(const struct index_state *istate, const char *path,
1226 src = dst->buf;
1227 len = dst->len;
1228 }
1229 - return ret | convert_to_git(istate, path, src, len, dst, SAFE_CRLF_RENORMALIZE);
1229 + return ret | convert_to_git(istate, path, src, len, dst, CONV_EOL_RENORMALIZE);
1230 }
1231
1232 /*****************************************************************
convert.h
+7 -10
@@ -8,15 +8,12 @@
8
9 struct index_state;
10
11 -enum safe_crlf {
12 - SAFE_CRLF_FALSE = 0,
13 - SAFE_CRLF_FAIL = 1,
14 - SAFE_CRLF_WARN = 2,
15 - SAFE_CRLF_RENORMALIZE = 3,
16 - SAFE_CRLF_KEEP_CRLF = 4
17 -};
11 +#define CONV_EOL_RNDTRP_DIE (1<<0) /* Die if CRLF to LF to CRLF is different */
12 +#define CONV_EOL_RNDTRP_WARN (1<<1) /* Warn if CRLF to LF to CRLF is different */
13 +#define CONV_EOL_RENORMALIZE (1<<2) /* Convert CRLF to LF */
14 +#define CONV_EOL_KEEP_CRLF (1<<3) /* Keep CRLF line endings as is */
15
19 -extern enum safe_crlf safe_crlf;
16 +extern int global_conv_flags_eol;
17
18 enum auto_crlf {
19 AUTO_CRLF_FALSE = 0,
@@ -66,7 +63,7 @@ extern const char *get_convert_attr_ascii(const char *path);
63 /* returns 1 if *dst was used */
64 extern int convert_to_git(const struct index_state *istate,
65 const char *path, const char *src, size_t len,
69 - struct strbuf *dst, enum safe_crlf checksafe);
66 + struct strbuf *dst, int conv_flags);
67 extern int convert_to_working_tree(const char *path, const char *src,
68 size_t len, struct strbuf *dst);
69 extern int async_convert_to_working_tree(const char *path, const char *src,
@@ -85,7 +82,7 @@ static inline int would_convert_to_git(const struct index_state *istate,
82 extern void convert_to_git_filter_fd(const struct index_state *istate,
83 const char *path, int fd,
84 struct strbuf *dst,
88 - enum safe_crlf checksafe);
85 + int conv_flags);
86 extern int would_convert_to_git_filter_fd(const char *path);
87
88 /*****************************************************************
diff.c
+4 -4
@@ -3520,13 +3520,13 @@ int diff_populate_filespec(struct diff_filespec *s, unsigned int flags)
3520 {
3521 int size_only = flags & CHECK_SIZE_ONLY;
3522 int err = 0;
3523 + int conv_flags = global_conv_flags_eol;
3524 /*
3525 * demote FAIL to WARN to allow inspecting the situation
3526 * instead of refusing.
3527 */
3527 - enum safe_crlf crlf_warn = (safe_crlf == SAFE_CRLF_FAIL
3528 - ? SAFE_CRLF_WARN
3529 - : safe_crlf);
3528 + if (conv_flags & CONV_EOL_RNDTRP_DIE)
3529 + conv_flags = CONV_EOL_RNDTRP_WARN;
3530
3531 if (!DIFF_FILE_VALID(s))
3532 die("internal error: asking to populate invalid file.");
@@ -3603,7 +3603,7 @@ int diff_populate_filespec(struct diff_filespec *s, unsigned int flags)
3603 /*
3604 * Convert from working tree format to canonical git format
3605 */
3606 - if (convert_to_git(&the_index, s->path, s->data, s->size, &buf, crlf_warn)) {
3606 + if (convert_to_git(&the_index, s->path, s->data, s->size, &buf, conv_flags)) {
3607 size_t size = 0;
3608 munmap(s->data, s->size);
3609 s->should_munmap = 0;
environment.c
+1 -1
@@ -49,7 +49,7 @@ enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
49 int check_replace_refs = 1;
50 char *git_replace_ref_base;
51 enum eol core_eol = EOL_UNSET;
52 -enum safe_crlf safe_crlf = SAFE_CRLF_WARN;
52 +int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
53 unsigned whitespace_rule_cfg = WS_DEFAULT_RULE;
54 enum branch_track git_branch_track = BRANCH_TRACK_REMOTE;
55 enum rebase_setup_type autorebase = AUTOREBASE_NEVER;
sha1_file.c
+6 -6
@@ -133,14 +133,14 @@ static struct cached_object *find_cached_object(const unsigned char *sha1)
133 }
134
135
136 -static enum safe_crlf get_safe_crlf(unsigned flags)
136 +static int get_conv_flags(unsigned flags)
137 {
138 if (flags & HASH_RENORMALIZE)
139 - return SAFE_CRLF_RENORMALIZE;
139 + return CONV_EOL_RENORMALIZE;
140 else if (flags & HASH_WRITE_OBJECT)
141 - return safe_crlf;
141 + return global_conv_flags_eol;
142 else
143 - return SAFE_CRLF_FALSE;
143 + return 0;
144 }
145
146
@@ -1752,7 +1752,7 @@ static int index_mem(struct object_id *oid, void *buf, size_t size,
1752 if ((type == OBJ_BLOB) && path) {
1753 struct strbuf nbuf = STRBUF_INIT;
1754 if (convert_to_git(&the_index, path, buf, size, &nbuf,
1755 - get_safe_crlf(flags))) {
1755 + get_conv_flags(flags))) {
1756 buf = strbuf_detach(&nbuf, &size);
1757 re_allocated = 1;
1758 }
@@ -1786,7 +1786,7 @@ static int index_stream_convert_blob(struct object_id *oid, int fd,
1786 assert(would_convert_to_git_filter_fd(path));
1787
1788 convert_to_git_filter_fd(&the_index, path, fd, &sbuf,
1789 - get_safe_crlf(flags));
1789 + get_conv_flags(flags));
1790
1791 if (write_object)
1792 ret = write_sha1_file(sbuf.buf, sbuf.len, typename(OBJ_BLOB),