From fd6cce9e89ab5ac1125a3b5f5611048ad22379e7 Mon Sep 17 00:00:00 2001 From: Eyvind Bernhardsen Date: Wed, 19 May 2010 22:43:10 +0200 Subject: Add per-repository eol normalization Change the semantics of the "crlf" attribute so that it enables end-of-line normalization when it is set, regardless of "core.autocrlf". Add a new setting for "crlf": "auto", which enables end-of-line conversion but does not override the automatic text file detection. Add a new attribute "eol" with possible values "crlf" and "lf". When set, this attribute enables normalization and forces git to use CRLF or LF line endings in the working directory, respectively. The line ending style to be used for normalized text files in the working directory is set using "core.autocrlf". When it is set to "true", CRLFs are used in the working directory; when set to "input" or "false", LFs are used. Signed-off-by: Eyvind Bernhardsen Signed-off-by: Junio C Hamano --- convert.c | 117 ++++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 84 insertions(+), 33 deletions(-) (limited to 'convert.c') diff --git a/convert.c b/convert.c index a54c5fc4a..1144e0b4f 100644 --- a/convert.c +++ b/convert.c @@ -8,13 +8,23 @@ * This should use the pathname to decide on whether it wants to do some * more interesting conversions (automatic gzip/unzip, general format * conversions etc etc), but by default it just does automatic CRLF<->LF - * translation when the "auto_crlf" option is set. + * translation when the "crlf" attribute or "auto_crlf" option is set. */ -#define CRLF_GUESS (-1) -#define CRLF_BINARY 0 -#define CRLF_TEXT 1 -#define CRLF_INPUT 2 +enum action { + CRLF_GUESS = -1, + CRLF_BINARY = 0, + CRLF_TEXT, + CRLF_INPUT, + CRLF_CRLF, + CRLF_AUTO, +}; + +enum eol { + EOL_UNSET, + EOL_LF, + EOL_CRLF, +}; struct text_stat { /* NUL, CR, LF and CRLF counts */ @@ -89,13 +99,14 @@ static int is_binary(unsigned long size, struct text_stat *stats) return 0; } -static void check_safe_crlf(const char *path, int action, +static void check_safe_crlf(const char *path, enum action action, struct text_stat *stats, enum safe_crlf checksafe) { if (!checksafe) return; - if (action == CRLF_INPUT || auto_crlf <= 0) { + if (action == CRLF_INPUT || + (action == CRLF_GUESS && auto_crlf == AUTO_CRLF_INPUT)) { /* * CRLFs would not be restored by checkout: * check if we'd remove CRLFs @@ -106,7 +117,8 @@ static void check_safe_crlf(const char *path, int action, else /* i.e. SAFE_CRLF_FAIL */ die("CRLF would be replaced by LF in %s.", path); } - } else if (auto_crlf > 0) { + } else if (action == CRLF_CRLF || + (action == CRLF_GUESS && auto_crlf == AUTO_CRLF_TRUE)) { /* * CRLFs would be added by checkout: * check if we have "naked" LFs @@ -158,17 +170,18 @@ static int has_cr_in_index(const char *path) } static int crlf_to_git(const char *path, const char *src, size_t len, - struct strbuf *buf, int action, enum safe_crlf checksafe) + struct strbuf *buf, enum action action, enum safe_crlf checksafe) { struct text_stat stats; char *dst; - if ((action == CRLF_BINARY) || !auto_crlf || !len) + if (action == CRLF_BINARY || + (action == CRLF_GUESS && auto_crlf == AUTO_CRLF_FALSE) || !len) return 0; gather_stats(src, len, &stats); - if (action == CRLF_GUESS) { + if (action == CRLF_AUTO || action == CRLF_GUESS) { /* * We're currently not going to even try to convert stuff * that has bare CR characters. Does anybody do that crazy @@ -183,12 +196,14 @@ static int crlf_to_git(const char *path, const char *src, size_t len, if (is_binary(len, &stats)) return 0; - /* - * If the file in the index has any CR in it, do not convert. - * This is the new safer autocrlf handling. - */ - if (has_cr_in_index(path)) - return 0; + if (action == CRLF_GUESS) { + /* + * If the file in the index has any CR in it, do not convert. + * This is the new safer autocrlf handling. + */ + if (has_cr_in_index(path)) + return 0; + } } check_safe_crlf(path, action, &stats, checksafe); @@ -201,7 +216,7 @@ static int crlf_to_git(const char *path, const char *src, size_t len, if (strbuf_avail(buf) + buf->len < len) strbuf_grow(buf, len - buf->len); dst = buf->buf; - if (action == CRLF_GUESS) { + if (action == CRLF_AUTO || action == CRLF_GUESS) { /* * If we guessed, we already know we rejected a file with * lone CR, and we can strip a CR without looking at what @@ -224,13 +239,13 @@ static int crlf_to_git(const char *path, const char *src, size_t len, } static int crlf_to_worktree(const char *path, const char *src, size_t len, - struct strbuf *buf, int action) + struct strbuf *buf, enum action action) { char *to_free = NULL; struct text_stat stats; if ((action == CRLF_BINARY) || (action == CRLF_INPUT) || - auto_crlf <= 0) + (action != CRLF_CRLF && auto_crlf != AUTO_CRLF_TRUE)) return 0; if (!len) @@ -246,11 +261,13 @@ static int crlf_to_worktree(const char *path, const char *src, size_t len, if (stats.lf == stats.crlf) return 0; - if (action == CRLF_GUESS) { - /* If we have any CR or CRLF line endings, we do not touch it */ - /* This is the new safer autocrlf-handling */ - if (stats.cr > 0 || stats.crlf > 0) - return 0; + if (action == CRLF_AUTO || action == CRLF_GUESS) { + if (action == CRLF_GUESS) { + /* If we have any CR or CRLF line endings, we do not touch it */ + /* This is the new safer autocrlf-handling */ + if (stats.cr > 0 || stats.crlf > 0) + return 0; + } /* If we have any bare CR characters, we're not going to touch it */ if (stats.cr != stats.crlf) @@ -423,11 +440,13 @@ static int read_convert_config(const char *var, const char *value, void *cb) static void setup_convert_check(struct git_attr_check *check) { static struct git_attr *attr_crlf; + static struct git_attr *attr_eol; static struct git_attr *attr_ident; static struct git_attr *attr_filter; if (!attr_crlf) { attr_crlf = git_attr("crlf"); + attr_eol = git_attr("eol"); attr_ident = git_attr("ident"); attr_filter = git_attr("filter"); user_convert_tail = &user_convert; @@ -436,6 +455,7 @@ static void setup_convert_check(struct git_attr_check *check) check[0].attr = attr_crlf; check[1].attr = attr_ident; check[2].attr = attr_filter; + check[3].attr = attr_eol; } static int count_ident(const char *cp, unsigned long size) @@ -592,9 +612,24 @@ static int git_path_check_crlf(const char *path, struct git_attr_check *check) ; else if (!strcmp(value, "input")) return CRLF_INPUT; + else if (!strcmp(value, "auto")) + return CRLF_AUTO; return CRLF_GUESS; } +static int git_path_check_eol(const char *path, struct git_attr_check *check) +{ + const char *value = check->value; + + if (ATTR_UNSET(value)) + ; + else if (!strcmp(value, "lf")) + return EOL_LF; + else if (!strcmp(value, "crlf")) + return EOL_CRLF; + return EOL_UNSET; +} + static struct convert_driver *git_path_check_convert(const char *path, struct git_attr_check *check) { @@ -616,20 +651,32 @@ static int git_path_check_ident(const char *path, struct git_attr_check *check) return !!ATTR_TRUE(value); } +enum action determine_action(enum action crlf_attr, enum eol eol_attr) { + if (crlf_attr == CRLF_BINARY) + return CRLF_BINARY; + if (eol_attr == EOL_LF) + return CRLF_INPUT; + if (eol_attr == EOL_CRLF) + return CRLF_CRLF; + return crlf_attr; +} + int convert_to_git(const char *path, const char *src, size_t len, struct strbuf *dst, enum safe_crlf checksafe) { - struct git_attr_check check[3]; - int crlf = CRLF_GUESS; + struct git_attr_check check[4]; + enum action action = CRLF_GUESS; + enum eol eol = EOL_UNSET; int ident = 0, ret = 0; const char *filter = NULL; setup_convert_check(check); if (!git_checkattr(path, ARRAY_SIZE(check), check)) { struct convert_driver *drv; - crlf = git_path_check_crlf(path, check + 0); + action = git_path_check_crlf(path, check + 0); ident = git_path_check_ident(path, check + 1); drv = git_path_check_convert(path, check + 2); + eol = git_path_check_eol(path, check + 3); if (drv && drv->clean) filter = drv->clean; } @@ -639,7 +686,8 @@ int convert_to_git(const char *path, const char *src, size_t len, src = dst->buf; len = dst->len; } - ret |= crlf_to_git(path, src, len, dst, crlf, checksafe); + action = determine_action(action, eol); + ret |= crlf_to_git(path, src, len, dst, action, checksafe); if (ret) { src = dst->buf; len = dst->len; @@ -649,17 +697,19 @@ int convert_to_git(const char *path, const char *src, size_t len, int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst) { - struct git_attr_check check[3]; - int crlf = CRLF_GUESS; + struct git_attr_check check[4]; + enum action action = CRLF_GUESS; + enum eol eol = EOL_UNSET; int ident = 0, ret = 0; const char *filter = NULL; setup_convert_check(check); if (!git_checkattr(path, ARRAY_SIZE(check), check)) { struct convert_driver *drv; - crlf = git_path_check_crlf(path, check + 0); + action = git_path_check_crlf(path, check + 0); ident = git_path_check_ident(path, check + 1); drv = git_path_check_convert(path, check + 2); + eol = git_path_check_eol(path, check + 3); if (drv && drv->smudge) filter = drv->smudge; } @@ -669,7 +719,8 @@ int convert_to_working_tree(const char *path, const char *src, size_t len, struc src = dst->buf; len = dst->len; } - ret |= crlf_to_worktree(path, src, len, dst, crlf); + action = determine_action(action, eol); + ret |= crlf_to_worktree(path, src, len, dst, action); if (ret) { src = dst->buf; len = dst->len; -- cgit v1.2.1 From 5ec3e67052289217c84e53d2cda90d939ac5725b Mon Sep 17 00:00:00 2001 From: Eyvind Bernhardsen Date: Wed, 19 May 2010 22:43:11 +0200 Subject: Rename the "crlf" attribute "text" As discussed on the list, "crlf" is not an optimal name. Linus suggested "text", which is much better. Signed-off-by: Eyvind Bernhardsen Signed-off-by: Junio C Hamano --- convert.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'convert.c') diff --git a/convert.c b/convert.c index 1144e0b4f..c61c02b19 100644 --- a/convert.c +++ b/convert.c @@ -439,12 +439,14 @@ static int read_convert_config(const char *var, const char *value, void *cb) static void setup_convert_check(struct git_attr_check *check) { + static struct git_attr *attr_text; static struct git_attr *attr_crlf; static struct git_attr *attr_eol; static struct git_attr *attr_ident; static struct git_attr *attr_filter; - if (!attr_crlf) { + if (!attr_text) { + attr_text = git_attr("text"); attr_crlf = git_attr("crlf"); attr_eol = git_attr("eol"); attr_ident = git_attr("ident"); @@ -456,6 +458,7 @@ static void setup_convert_check(struct git_attr_check *check) check[1].attr = attr_ident; check[2].attr = attr_filter; check[3].attr = attr_eol; + check[4].attr = attr_text; } static int count_ident(const char *cp, unsigned long size) @@ -651,20 +654,20 @@ static int git_path_check_ident(const char *path, struct git_attr_check *check) return !!ATTR_TRUE(value); } -enum action determine_action(enum action crlf_attr, enum eol eol_attr) { - if (crlf_attr == CRLF_BINARY) +enum action determine_action(enum action text_attr, enum eol eol_attr) { + if (text_attr == CRLF_BINARY) return CRLF_BINARY; if (eol_attr == EOL_LF) return CRLF_INPUT; if (eol_attr == EOL_CRLF) return CRLF_CRLF; - return crlf_attr; + return text_attr; } int convert_to_git(const char *path, const char *src, size_t len, struct strbuf *dst, enum safe_crlf checksafe) { - struct git_attr_check check[4]; + struct git_attr_check check[5]; enum action action = CRLF_GUESS; enum eol eol = EOL_UNSET; int ident = 0, ret = 0; @@ -673,7 +676,9 @@ int convert_to_git(const char *path, const char *src, size_t len, setup_convert_check(check); if (!git_checkattr(path, ARRAY_SIZE(check), check)) { struct convert_driver *drv; - action = git_path_check_crlf(path, check + 0); + action = git_path_check_crlf(path, check + 4); + if (action == CRLF_GUESS) + action = git_path_check_crlf(path, check + 0); ident = git_path_check_ident(path, check + 1); drv = git_path_check_convert(path, check + 2); eol = git_path_check_eol(path, check + 3); @@ -697,7 +702,7 @@ int convert_to_git(const char *path, const char *src, size_t len, int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst) { - struct git_attr_check check[4]; + struct git_attr_check check[5]; enum action action = CRLF_GUESS; enum eol eol = EOL_UNSET; int ident = 0, ret = 0; @@ -706,7 +711,9 @@ int convert_to_working_tree(const char *path, const char *src, size_t len, struc setup_convert_check(check); if (!git_checkattr(path, ARRAY_SIZE(check), check)) { struct convert_driver *drv; - action = git_path_check_crlf(path, check + 0); + action = git_path_check_crlf(path, check + 4); + if (action == CRLF_GUESS) + action = git_path_check_crlf(path, check + 0); ident = git_path_check_ident(path, check + 1); drv = git_path_check_convert(path, check + 2); eol = git_path_check_eol(path, check + 3); -- cgit v1.2.1 From 942e7747678ecf5f118ea5b2d0c763166de21f3a Mon Sep 17 00:00:00 2001 From: Eyvind Bernhardsen Date: Fri, 4 Jun 2010 21:29:08 +0200 Subject: Add "core.eol" config variable Introduce a new configuration variable, "core.eol", that allows the user to set which line endings to use for end-of-line-normalized files in the working directory. It defaults to "native", which means CRLF on Windows and LF everywhere else. Note that "core.autocrlf" overrides core.eol. This means that [core] autocrlf = true puts CRLFs in the working directory even if core.eol is set to "lf". Signed-off-by: Eyvind Bernhardsen Signed-off-by: Junio C Hamano --- convert.c | 60 ++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 36 insertions(+), 24 deletions(-) (limited to 'convert.c') diff --git a/convert.c b/convert.c index c61c02b19..061fb23d6 100644 --- a/convert.c +++ b/convert.c @@ -8,7 +8,7 @@ * This should use the pathname to decide on whether it wants to do some * more interesting conversions (automatic gzip/unzip, general format * conversions etc etc), but by default it just does automatic CRLF<->LF - * translation when the "crlf" attribute or "auto_crlf" option is set. + * translation when the "text" attribute or "auto_crlf" option is set. */ enum action { @@ -20,12 +20,6 @@ enum action { CRLF_AUTO, }; -enum eol { - EOL_UNSET, - EOL_LF, - EOL_CRLF, -}; - struct text_stat { /* NUL, CR, LF and CRLF counts */ unsigned nul, cr, lf, crlf; @@ -99,33 +93,55 @@ static int is_binary(unsigned long size, struct text_stat *stats) return 0; } +static enum eol determine_output_conversion(enum action action) { + switch (action) { + case CRLF_BINARY: + return EOL_UNSET; + case CRLF_CRLF: + return EOL_CRLF; + case CRLF_INPUT: + return EOL_LF; + case CRLF_GUESS: + if (!auto_crlf) + return EOL_UNSET; + /* fall through */ + case CRLF_TEXT: + case CRLF_AUTO: + if (auto_crlf == AUTO_CRLF_TRUE) + return EOL_CRLF; + else if (auto_crlf == AUTO_CRLF_INPUT) + return EOL_LF; + else if (eol == EOL_UNSET) + return EOL_NATIVE; + } + return eol; +} + static void check_safe_crlf(const char *path, enum action action, struct text_stat *stats, enum safe_crlf checksafe) { if (!checksafe) return; - if (action == CRLF_INPUT || - (action == CRLF_GUESS && auto_crlf == AUTO_CRLF_INPUT)) { + if (determine_output_conversion(action) == EOL_LF) { /* * CRLFs would not be restored by checkout: * check if we'd remove CRLFs */ if (stats->crlf) { if (checksafe == SAFE_CRLF_WARN) - warning("CRLF will be replaced by LF in %s.", path); + warning("CRLF will be replaced by LF in %s.\nThe file will have its original line endings in your working directory.", path); else /* i.e. SAFE_CRLF_FAIL */ die("CRLF would be replaced by LF in %s.", path); } - } else if (action == CRLF_CRLF || - (action == CRLF_GUESS && auto_crlf == AUTO_CRLF_TRUE)) { + } else if (determine_output_conversion(action) == EOL_CRLF) { /* * CRLFs would be added by checkout: * check if we have "naked" LFs */ if (stats->lf != stats->crlf) { if (checksafe == SAFE_CRLF_WARN) - warning("LF will be replaced by CRLF in %s", path); + warning("LF will be replaced by CRLF in %s.\nThe file will have its original line endings in your working directory.", path); else /* i.e. SAFE_CRLF_FAIL */ die("LF would be replaced by CRLF in %s", path); } @@ -244,11 +260,7 @@ static int crlf_to_worktree(const char *path, const char *src, size_t len, char *to_free = NULL; struct text_stat stats; - if ((action == CRLF_BINARY) || (action == CRLF_INPUT) || - (action != CRLF_CRLF && auto_crlf != AUTO_CRLF_TRUE)) - return 0; - - if (!len) + if (!len || determine_output_conversion(action) != EOL_CRLF) return 0; gather_stats(src, len, &stats); @@ -669,7 +681,7 @@ int convert_to_git(const char *path, const char *src, size_t len, { struct git_attr_check check[5]; enum action action = CRLF_GUESS; - enum eol eol = EOL_UNSET; + enum eol eol_attr = EOL_UNSET; int ident = 0, ret = 0; const char *filter = NULL; @@ -681,7 +693,7 @@ int convert_to_git(const char *path, const char *src, size_t len, action = git_path_check_crlf(path, check + 0); ident = git_path_check_ident(path, check + 1); drv = git_path_check_convert(path, check + 2); - eol = git_path_check_eol(path, check + 3); + eol_attr = git_path_check_eol(path, check + 3); if (drv && drv->clean) filter = drv->clean; } @@ -691,7 +703,7 @@ int convert_to_git(const char *path, const char *src, size_t len, src = dst->buf; len = dst->len; } - action = determine_action(action, eol); + action = determine_action(action, eol_attr); ret |= crlf_to_git(path, src, len, dst, action, checksafe); if (ret) { src = dst->buf; @@ -704,7 +716,7 @@ int convert_to_working_tree(const char *path, const char *src, size_t len, struc { struct git_attr_check check[5]; enum action action = CRLF_GUESS; - enum eol eol = EOL_UNSET; + enum eol eol_attr = EOL_UNSET; int ident = 0, ret = 0; const char *filter = NULL; @@ -716,7 +728,7 @@ int convert_to_working_tree(const char *path, const char *src, size_t len, struc action = git_path_check_crlf(path, check + 0); ident = git_path_check_ident(path, check + 1); drv = git_path_check_convert(path, check + 2); - eol = git_path_check_eol(path, check + 3); + eol_attr = git_path_check_eol(path, check + 3); if (drv && drv->smudge) filter = drv->smudge; } @@ -726,7 +738,7 @@ int convert_to_working_tree(const char *path, const char *src, size_t len, struc src = dst->buf; len = dst->len; } - action = determine_action(action, eol); + action = determine_action(action, eol_attr); ret |= crlf_to_worktree(path, src, len, dst, action); if (ret) { src = dst->buf; -- cgit v1.2.1