Skip to content

Commit 8462ff4

Browse files
tboegigitster
authored andcommitted
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 <[email protected]> Helped-By: Lars Schneider <[email protected]> Signed-off-by: Torsten Bögershausen <[email protected]> Signed-off-by: Lars Schneider <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1eaabe3 commit 8462ff4

File tree

8 files changed

+46
-46
lines changed

8 files changed

+46
-46
lines changed

apply.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2263,8 +2263,8 @@ static void show_stats(struct apply_state *state, struct patch *patch)
22632263
static int read_old_data(struct stat *st, struct patch *patch,
22642264
const char *path, struct strbuf *buf)
22652265
{
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;
22682268
switch (st->st_mode & S_IFMT) {
22692269
case S_IFLNK:
22702270
if (strbuf_readlink(buf, path, st->st_size) < 0)
@@ -2281,7 +2281,7 @@ static int read_old_data(struct stat *st, struct patch *patch,
22812281
* should never look at the index when explicit crlf option
22822282
* is given.
22832283
*/
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);
22852285
return 0;
22862286
default:
22872287
return -1;

combine-diff.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1053,7 +1053,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
10531053
if (is_file) {
10541054
struct strbuf buf = STRBUF_INIT;
10551055

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)) {
10571057
free(result);
10581058
result = strbuf_detach(&buf, &len);
10591059
result_size = len;

config.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,11 +1149,14 @@ static int git_default_core_config(const char *var, const char *value)
11491149
}
11501150

11511151
if (!strcmp(var, "core.safecrlf")) {
1152+
int eol_rndtrp_die;
11521153
if (value && !strcasecmp(value, "warn")) {
1153-
safe_crlf = SAFE_CRLF_WARN;
1154+
global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
11541155
return 0;
11551156
}
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;
11571160
return 0;
11581161
}
11591162

convert.c

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -193,30 +193,30 @@ static enum eol output_eol(enum crlf_action crlf_action)
193193
return core_eol;
194194
}
195195

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,
197197
struct text_stat *old_stats, struct text_stat *new_stats,
198-
enum safe_crlf checksafe)
198+
int conv_flags)
199199
{
200200
if (old_stats->crlf && !new_stats->crlf ) {
201201
/*
202202
* CRLFs would not be restored by checkout
203203
*/
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)
205207
warning(_("CRLF will be replaced by LF in %s.\n"
206208
"The file will have its original line"
207209
" endings in your working directory."), path);
208-
else /* i.e. SAFE_CRLF_FAIL */
209-
die(_("CRLF would be replaced by LF in %s."), path);
210210
} else if (old_stats->lonelf && !new_stats->lonelf ) {
211211
/*
212212
* CRLFs would be added by checkout
213213
*/
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)
215217
warning(_("LF will be replaced by CRLF in %s.\n"
216218
"The file will have its original line"
217219
" endings in your working directory."), path);
218-
else /* i.e. SAFE_CRLF_FAIL */
219-
die(_("LF would be replaced by CRLF in %s"), path);
220220
}
221221
}
222222

@@ -268,7 +268,7 @@ static int will_convert_lf_to_crlf(size_t len, struct text_stat *stats,
268268
static int crlf_to_git(const struct index_state *istate,
269269
const char *path, const char *src, size_t len,
270270
struct strbuf *buf,
271-
enum crlf_action crlf_action, enum safe_crlf checksafe)
271+
enum crlf_action crlf_action, int conv_flags)
272272
{
273273
struct text_stat stats;
274274
char *dst;
@@ -298,12 +298,12 @@ static int crlf_to_git(const struct index_state *istate,
298298
* unless we want to renormalize in a merge or
299299
* cherry-pick.
300300
*/
301-
if ((checksafe != SAFE_CRLF_RENORMALIZE) &&
301+
if ((!(conv_flags & CONV_EOL_RENORMALIZE)) &&
302302
has_crlf_in_index(istate, path))
303303
convert_crlf_into_lf = 0;
304304
}
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))) {
307307
struct text_stat new_stats;
308308
memcpy(&new_stats, &stats, sizeof(new_stats));
309309
/* simulate "git add" */
@@ -316,7 +316,7 @@ static int crlf_to_git(const struct index_state *istate,
316316
new_stats.crlf += new_stats.lonelf;
317317
new_stats.lonelf = 0;
318318
}
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);
320320
}
321321
if (!convert_crlf_into_lf)
322322
return 0;
@@ -1129,7 +1129,7 @@ const char *get_convert_attr_ascii(const char *path)
11291129

11301130
int convert_to_git(const struct index_state *istate,
11311131
const char *path, const char *src, size_t len,
1132-
struct strbuf *dst, enum safe_crlf checksafe)
1132+
struct strbuf *dst, int conv_flags)
11331133
{
11341134
int ret = 0;
11351135
struct conv_attrs ca;
@@ -1144,8 +1144,8 @@ int convert_to_git(const struct index_state *istate,
11441144
src = dst->buf;
11451145
len = dst->len;
11461146
}
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);
11491149
if (ret && dst) {
11501150
src = dst->buf;
11511151
len = dst->len;
@@ -1156,7 +1156,7 @@ int convert_to_git(const struct index_state *istate,
11561156

11571157
void convert_to_git_filter_fd(const struct index_state *istate,
11581158
const char *path, int fd, struct strbuf *dst,
1159-
enum safe_crlf checksafe)
1159+
int conv_flags)
11601160
{
11611161
struct conv_attrs ca;
11621162
convert_attrs(&ca, path);
@@ -1167,7 +1167,7 @@ void convert_to_git_filter_fd(const struct index_state *istate,
11671167
if (!apply_filter(path, NULL, 0, fd, dst, ca.drv, CAP_CLEAN, NULL))
11681168
die("%s: clean filter '%s' failed", path, ca.drv->name);
11691169

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);
11711171
ident_to_git(path, dst->buf, dst->len, dst, ca.ident);
11721172
}
11731173

@@ -1226,7 +1226,7 @@ int renormalize_buffer(const struct index_state *istate, const char *path,
12261226
src = dst->buf;
12271227
len = dst->len;
12281228
}
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);
12301230
}
12311231

12321232
/*****************************************************************

convert.h

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,12 @@
88

99
struct index_state;
1010

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 */
1815

19-
extern enum safe_crlf safe_crlf;
16+
extern int global_conv_flags_eol;
2017

2118
enum auto_crlf {
2219
AUTO_CRLF_FALSE = 0,
@@ -66,7 +63,7 @@ extern const char *get_convert_attr_ascii(const char *path);
6663
/* returns 1 if *dst was used */
6764
extern int convert_to_git(const struct index_state *istate,
6865
const char *path, const char *src, size_t len,
69-
struct strbuf *dst, enum safe_crlf checksafe);
66+
struct strbuf *dst, int conv_flags);
7067
extern int convert_to_working_tree(const char *path, const char *src,
7168
size_t len, struct strbuf *dst);
7269
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,
8582
extern void convert_to_git_filter_fd(const struct index_state *istate,
8683
const char *path, int fd,
8784
struct strbuf *dst,
88-
enum safe_crlf checksafe);
85+
int conv_flags);
8986
extern int would_convert_to_git_filter_fd(const char *path);
9087

9188
/*****************************************************************

diff.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3520,13 +3520,13 @@ int diff_populate_filespec(struct diff_filespec *s, unsigned int flags)
35203520
{
35213521
int size_only = flags & CHECK_SIZE_ONLY;
35223522
int err = 0;
3523+
int conv_flags = global_conv_flags_eol;
35233524
/*
35243525
* demote FAIL to WARN to allow inspecting the situation
35253526
* instead of refusing.
35263527
*/
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;
35303530

35313531
if (!DIFF_FILE_VALID(s))
35323532
die("internal error: asking to populate invalid file.");
@@ -3603,7 +3603,7 @@ int diff_populate_filespec(struct diff_filespec *s, unsigned int flags)
36033603
/*
36043604
* Convert from working tree format to canonical git format
36053605
*/
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)) {
36073607
size_t size = 0;
36083608
munmap(s->data, s->size);
36093609
s->should_munmap = 0;

environment.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
4949
int check_replace_refs = 1;
5050
char *git_replace_ref_base;
5151
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;
5353
unsigned whitespace_rule_cfg = WS_DEFAULT_RULE;
5454
enum branch_track git_branch_track = BRANCH_TRACK_REMOTE;
5555
enum rebase_setup_type autorebase = AUTOREBASE_NEVER;

sha1_file.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,14 @@ static struct cached_object *find_cached_object(const unsigned char *sha1)
133133
}
134134

135135

136-
static enum safe_crlf get_safe_crlf(unsigned flags)
136+
static int get_conv_flags(unsigned flags)
137137
{
138138
if (flags & HASH_RENORMALIZE)
139-
return SAFE_CRLF_RENORMALIZE;
139+
return CONV_EOL_RENORMALIZE;
140140
else if (flags & HASH_WRITE_OBJECT)
141-
return safe_crlf;
141+
return global_conv_flags_eol;
142142
else
143-
return SAFE_CRLF_FALSE;
143+
return 0;
144144
}
145145

146146

@@ -1752,7 +1752,7 @@ static int index_mem(struct object_id *oid, void *buf, size_t size,
17521752
if ((type == OBJ_BLOB) && path) {
17531753
struct strbuf nbuf = STRBUF_INIT;
17541754
if (convert_to_git(&the_index, path, buf, size, &nbuf,
1755-
get_safe_crlf(flags))) {
1755+
get_conv_flags(flags))) {
17561756
buf = strbuf_detach(&nbuf, &size);
17571757
re_allocated = 1;
17581758
}
@@ -1786,7 +1786,7 @@ static int index_stream_convert_blob(struct object_id *oid, int fd,
17861786
assert(would_convert_to_git_filter_fd(path));
17871787

17881788
convert_to_git_filter_fd(&the_index, path, fd, &sbuf,
1789-
get_safe_crlf(flags));
1789+
get_conv_flags(flags));
17901790

17911791
if (write_object)
17921792
ret = write_sha1_file(sbuf.buf, sbuf.len, typename(OBJ_BLOB),

0 commit comments

Comments
 (0)