ls-files: Add eol diagnostics
PATCH v6 1/1 ls-files: Add eol diagnostics
When working in a cross-platform environment, a user wants to check if text files are stored normalized in the repository and if .gitattributes are set appropriately.
Make it possible to let Git show the line endings in the index and in the working tree and the effective text/eol attributes.
The end of line ("eolinfo") are shown like this:
"binary" binary file "text-no-eol" text file without any EOL "text-lf" text file with LF "text-crlf" text file with CRLF
"text-crlf-lf" text file with mixed line endings.
The effective text/eol attribute is one of these:
"", "-text", "text", "text=auto", "eol=lf", "eol=crlf"
git ls-files --eol gives an output like this:
i/text-no-eol w/text-no-eol attr/text=auto t/t5100/empty i/binary w/binary attr/-text t/test-binary-2.png i/text-lf w/text-lf attr/eol=lf t/t5100/rfc2047-info-0007 i/text-lf w/text-crlf attr/eol=crlf doit.bat i/text-crlf-lf w/text-crlf-lf attr/ locale/XX.po
Note that the output is meant to be human-readable and may change.
---
Thanks everybody for contributing to this patch, expecially Eric Sunshine Changes since v5:
- shell here document without substitions (Thanks Eric)
Changes since v4:
- gather_convert_stats_ascii() is static (Thanks Ramsay)
Changes since v3:
- typos (thanks for careful review)
- Remove un-intended changes in check-attr.c (they should not have been there)
- Improved t0027 (Thanks Eric)
Changes since v2:
- Major rework
- New prefix, more common to what we use in Git (i/, w/ attr/)
- Only one option: git ls-files --eol (may be combined with -s or -o or -d
- Simplify the diagnostics: either the file is binary or text
As empty files are not binary, they are text-no-eol
- Test cases in t0027
Documentation/git-ls-files.txt | 22 +++++++++ builtin/ls-files.c | 19 +++++++ convert.c | 86 ++++++++++++++++++++++++++++++++ convert.h | 3 ++ t/t0027-auto-crlf.sh | 109 ++++++++++++++++++++++++++++++++++++----- 5 files changed, 227 insertions(+), 12 deletions(-)
Documentation/git-ls-files.txt +22 -0
@@ -12,6 +12,7 @@ SYNOPSIS 'git ls-files' [-z] [-t] [-v] (--[cached|deleted|others|ignored|stage|unmerged|killed|modified])* (-[c|d|o|i|s|u|k|m])* + [--eol] [-x <pattern>|--exclude=<pattern>] [-X <file>|--exclude-from=<file>] [--exclude-per-directory=<file>]
@@ -147,6 +148,18 @@ a space) at the start of each line: possible for manual inspection; the exact format may change at any time. +--eol:: + Show line endings ("eolinfo") and the text/eol attributes ("texteolattr") of files. + "eolinfo" is the file content identification used by Git when + the "text" attribute is "auto", or core.autocrlf != false. + + "eolinfo" is either "" (when the the info is not available"), or one of "binary", + "text-no-eol", "text-lf", "text-crlf" or "text-crlf-lf". + The "texteolattr" can be "", "-text", "text", "text=auto", "eol=lf", "eol=crlf". + + Both the content in the index ("i/") and the content in the working tree ("w/") + are shown for regular files, followed by the "texteolattr ("attr/"). + \--:: Do not interpret any more arguments as options.
@@ -161,6 +174,15 @@ which case it outputs: [<tag> ]<mode> <object> <stage> <file> +'git ls-files --eol' will show + i/<eolinfo> w/<eolinfo> attr/<eolattr> <file> + +'git ls-files --eol -o' will show + i/ w/<eolinfo> attr/<eolattr> <file> + +'git ls-files --eol -s' will show +[<tag> ]<mode> <object> <stage> i/<eolinfo> w/<eolinfo> attr/<eolattr> <file> + 'git ls-files --unmerged' and 'git ls-files --stage' can be used to examine detailed information on unmerged paths.
builtin/ls-files.c +19 -0
@@ -27,6 +27,7 @@ static int show_killed;static int show_valid_bit;static int line_terminator ='\n' ;static int debug_mode; +static int show_eol;static const char *prefix;static int max_prefix_len;
@@ -47,6 +48,21 @@ static const char *tag_modified = "";static const char *tag_skip_worktree ="" ;static const char *tag_resolve_undo ="" ; +static void write_eolinfo (const struct cache_entry *ce,const char *path) +{ +struct stat st; +const char *i_txt ="" ; +const char *w_txt ="" ; +if (!show_eol) +return ; +if (ce &&S_ISREG (ce->ce_mode)) + i_txt =get_cached_convert_stats_ascii (ce->name); +if (!lstat (path, &st) && (S_ISREG (st.st_mode))) + w_txt =get_wt_convert_stats_ascii (path); +printf ("i/%-13s w/%-13s attr/%-9s " , i_txt, w_txt, +get_convert_attr_ascii (path)); +} +static void write_name (const char *name) {/*
@@ -68,6 +84,7 @@ static void show_dir_entry(const char *tag, struct dir_entry *ent)return ;fputs (tag, stdout); +write_eolinfo (NULL , ent->name);write_name (ent->name); }
@@ -170,6 +187,7 @@ static void show_ce_entry(const char *tag, const struct cache_entry *ce)find_unique_abbrev (ce->sha1,abbrev), ce_stage(ce)); } +write_eolinfo (ce, ce->name);write_name (ce->name);if (debug_mode) {const struct stat_data *sd = &ce->ce_stat_data;
@@ -433,6 +451,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)OPT_BIT (0 ,"directory" , &dir.flags,N_ ("show 'other' directories' names only" ),DIR_SHOW_OTHER_DIRECTORIES ), +OPT_BOOL (0 ,"eol" , &show_eol,N_ ("show line endings of files" )),OPT_NEGBIT (0 ,"empty-directory" , &dir.flags,N_ ("don't show empty directories" ), DIR_HIDE_EMPTY_DIRECTORIES),
convert.c +86 -0
104 lines not on this page · raw message
convert.h +3 -0
@@ -32,6 +32,9 @@ enum eol { };extern enum eol core_eol; +extern const char *get_cached_convert_stats_ascii (const char *path); +extern const char *get_wt_convert_stats_ascii (const char *path); +extern const char *get_convert_attr_ascii (const char *path);/* returns 1 if *dst was used */ extern int convert_to_git(const char *path, const char *src, size_t len,
t/t0027-auto-crlf.sh +97 -12
148 lines not on this page · raw message
Signature · 1 line
2.6.2.403.gd7a84e3
Quoted 8 lines · Torsten Bögershausen
When working in a cross-platform environment, a user wants to check if text files are stored normalized in the repository and if .gitattributes are set appropriately.
[...]Signed-off-by Torsten Bögershausen <[email protected]>---
convert.c static int is_binary(unsigned long size, struct text_stat *stats) · line 105
Quoted 6 lines
convert.c +6
-0@@ -95,6 +100,63 @@ static int is_binary(unsigned long size, struct text_stat *stats) +const char *get_wt_convert_stats_ascii (const char *path) +{ +const char *ret; +struct strbuf sb =STRBUF_INIT ; +if (strbuf_read_file (&sb, path,0 ) <0 ) +return "" ;
Given the implementation of strbuf_read_file(), some memory may have been allocated to the strbuf even if it returns a failure, therefore this may be leaking.
convert.c static int is_binary(unsigned long size, struct text_stat *stats) · line 158
Quoted 4 lines
convert.c +4
-0+ ret =gather_convert_stats_ascii (sb.buf, sb.len); +strbuf_release (&sb); +return ret; +}
You may, therefore, want to restructure the code like this to avoid the leak:
const char *ret = "";
struct strbuf sb = STRBUF_INIT;
if (strbuf_read_file(...) >= 0)
ret = gather_convert_stats_ascii(...);
strbuf_release(&sb);
return ret;t/t0027-auto-crlf.sh checkout_files () { · line 216
Quoted 6 lines
t/t0027-auto-crlf.sh +0
-1@@ -213,7 +238,20 @@ checkout_files () { git -c core.eol=$eol checkout $src$f.txt fi done -
Was dropping this blank line intentional?
t/t0027-auto-crlf.sh checkout_files () { · line 253
Quoted 13 lines
t/t0027-auto-crlf.sh +13
-0+ test_expect_success "ls-files --eol $lfname ${pfx}LF.txt" " + cat >e <<-EOF && + i/text-crlf w/$(stats_ascii $crlfname) ${src}CRLF.txt + i/text-crlf-lf w/$(stats_ascii $lfmixcrlf) ${src}CRLF_mix_LF.txt + i/text-lf w/$(stats_ascii $lfname) ${src}LF.txt + i/binary w/$(stats_ascii $lfmixcr) ${src}LF_mix_CR.txt + i/binary w/$(stats_ascii $crlfnul) ${src}CRLF_nul.txt + i/binary w/$(stats_ascii $crlfnul) ${src}LF_nul.txt + EOF + sort <e >expect && + git ls-files --eol $src* | sed -e 's!attr/[=a-z-]*!!g' -e 's/ */ /g' | sort >actual && + test_cmp expect actual && + rm e expect actual
If it is indeed important to cleanup these files so that they don't muck up following tests, then you'd be better off using:
test_when_finished "rm -f e expect actual"
at the top of the test to ensure that the cleanup happens regardless of the whether the test succeeds or fails. On the other hand, if these files won't muck up subsequent tests, then it may be fine to skip cleanup altogether.
t/t0027-auto-crlf.sh checkout_files () { · line 272
Quoted 9 lines
t/t0027-auto-crlf.sh +5
-0+ " test_expect_success "checkout core.eol=$eol core.autocrlf=$crlf gitattributes=$attr file=LF" " compare_ws_file $pfx $lfname ${src}LF.txt "@@ -231,6 +269,37 @@ checkout_files () { +# Test control characters +# NUL SOH CR EOF==^Z +test_expect_success 'ls-files --eol -o Text/Binary' ' + test_when_finished "rm e expect actual TeBi_*" &&
It's probably not a big deal, but "rm -f" perhaps?
Quoted 14 lines
t/t0027-auto-crlf.sh +14
-0+ STRT=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA && + STR=$STRT$STRT$STRT$STRT && + printf "${STR}BBB\001" >TeBi_127_S && + printf "${STR}BBBB\001">TeBi_128_S && + printf "${STR}BBB\032" >TeBi_127_E && + printf "\032${STR}BBB" >TeBi_E_127 && + printf "${STR}BBBB\000">TeBi_128_N && + printf "${STR}BBB\012">TeBi_128_L && + printf "${STR}BBB\015">TeBi_127_C && + printf "${STR}BB\015\012" >TeBi_126_CL && + printf "${STR}BB\015\012\015" >TeBi_126_CLC && + cat >e <<-\EOF && + i/ w/binary TeBi_127_S + i/ w/text-no-eol TeBi_128_S
t/t0027-auto-crlf.sh checkout_files () { · line 299
Quoted 10 lines
t/t0027-auto-crlf.sh +10
-0+ i/ w/text-no-eol TeBi_127_E + i/ w/binary TeBi_E_127 + i/ w/binary TeBi_128_N + i/ w/text-lf TeBi_128_L + i/ w/binary TeBi_127_C + i/ w/text-crlf TeBi_126_CL + i/ w/binary TeBi_126_CLC + EOF + sort <e >expect && + git ls-files --eol -o | egrep "TeBi_" | sed -e 's!attr/[=a-z-]*!!g' -e "s/ */ /g" | sort >actual &&
Okay. "egrep" is used about as frequently in test scripts as "grep -E".
t/t0027-auto-crlf.sh checkout_files native true "lf" LF CRLF CRLF_mix_LF LF_mix_CR · line 551
Quoted 6 lines
t/t0027-auto-crlf.sh +5
-0+ test_cmp expect actual +'@@ -480,4 +549,20 @@ checkout_files native true "lf" LF CRLF CRLF_mix_LF LF_mix_CR +# Should be the last test case +test_expect_success 'ls-files --eol -d' " + rm crlf_false_attr__CRLF.txt crlf_false_attr__CRLF_mix_LF.txt crlf_false_attr__LF.txt .gitattributes &&
Did you mean to wrap this in test_when_finished()?
If not, is it cleaning up gunk left by some earlier test(s)? If so, what happens if one of those tests failed and didn't create one of these files? In that case, this 'rm' would fail, causing this entire test to fail. Better would be to use "rm -f".
t/t0027-auto-crlf.sh checkout_files native true "lf" LF CRLF CRLF_mix_LF LF_mix_CR · line 567
Quoted 12 lines
t/t0027-auto-crlf.sh +12
-0+ cat >expect <<-\EOF && + i/text-crlf w/ crlf_false_attr__CRLF.txt + i/text-crlf-lf w/ crlf_false_attr__CRLF_mix_LF.txt + i/text-lf w/ .gitattributes + i/text-lf w/ crlf_false_attr__LF.txt + EOF + git ls-files --eol -d | sed -e 's!attr/[=a-z-]*!!g' -e 's/ */ /g' | sort >actual && + test_cmp expect actual && + rm expect actual +" + +
Style: Drop the extra blank line.
Quoted 3 lines
t/t0027-auto-crlf.sh +0
-1test_done --2.6.2.403.gd7a84e3