Skip to content

Commit c72c8a9

Browse files
committed
astextplain: use mimetype to decide helper
We use the file extension to determine which helper to call to convert a file to text. We currently match an all upper case version and an all lower case version of each extension, but sometimes variations like 'foo.Rtf' fall though our raster. To cover all variations in this established way we'd have to add every permutation of upper and lower case for each extension to the script. That's a big maintainance burden. We could also convert the passed filename to a known case and match against that. Since POSIX has no easy way for sh to do case conversion in-process we'd have to shell out to tr, awk or similar. For the antiword helper we already need to make sure the file is of the type it claims by it's extension. We use file to tell us the mimetype for this. file can also tell us the types of other files we care about for astextplain, so we can use it for the decision which helper to use in the first place. This fixes git-for-windows/git#5641 Signed-off-by: Matthias Aßhauer <mha1993@live.de>
1 parent 684e552 commit c72c8a9

File tree

2 files changed

+8
-15
lines changed

2 files changed

+8
-15
lines changed

git-extra/PKGBUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ sha256sums=('8ed76d1cb069ac8568f21c431f5e23caebea502d932ab4cdff71396f4f0d5b72'
7979
'dcbd1b981d5b899afe30151a5f5a27ea52025ff1335f131af1b5891f62ddd55b'
8080
'683ab066be19cb4defec470ebd53f165ca5dbf761fd40c13aee8abe31ba42803'
8181
'1cf2c13fb97c51375a76ed479362c3cbcdb51ab4d3a745e8d2c3a780badd8d46'
82-
'201fc40c1aadadceccc617511a4add05536b3638bff34456cc334e3b21c130e7'
82+
'47a933f99a368d94e532e8de356998fc20bb9ae9ef6fe729dcd6ed029e3e6073'
8383
'180e05c1b8afa34d59cb2f6635bc75fece10474f37e164ea5916a6b3b56e20dd'
8484
'22f41610dea842890955032af30efdb60e80f310e95a04e57ab36b10e0376923'
8585
'38c0cf57e03d275cdd42984f102bee8352fac672f875ab23c46b9625eefc49f3'

git-extra/astextplain

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,21 @@ if test "$#" != 1 ; then
77
fi
88

99
# XXX output encoding (UTF-8) hardcoded
10-
case "$1" in
11-
*.ods | *.ODS | *.odf |*.ODF | *.odt | *.ODT)
10+
case "$(file --brief --mime-type "$1")" in
11+
application/vnd.oasis.opendocument.spreadsheet | application/vnd.oasis.opendocument.formula | application/vnd.oasis.opendocument.text)
1212
odt2txt "$1" || cat "$1"
1313
;;
14-
*.doc | *.DOC | *.dot | *.DOT)
15-
case "$(file --brief --mime-type "$1")" in
16-
application/msword)
17-
out=$(antiword -m UTF-8 "$1") && sed "s/\^M$//" <<<$out || cat "$1"
18-
;;
19-
*)
20-
cat "$1"
21-
;;
22-
esac
14+
application/msword)
15+
out=$(antiword -m UTF-8 "$1") && sed "s/\^M$//" <<<$out || cat "$1"
2316
;;
24-
*.docx | *.DOCX | *.dotx | *.DOTX | *.docm | *.DOCM | *.dotm | *.DOTM)
17+
application/vnd.openxmlformats-officedocument.wordprocessingml.* | application/vnd.ms-word.*.macroenabled.12)
2518
docx2txt.pl "$1" - || cat "$1"
2619
;;
27-
*.pdf | *.PDF)
20+
application/pdf)
2821
out=$(pdftotext -q -layout -enc UTF-8 "$1" -) && sed "s/(\^M$)|(^\^L)//" <<<$out || cat "$1"
2922
;;
3023
# TODO add rtf support
31-
*.rtf | *.RTF)
24+
application/rtf)
3225
cat "$1"
3326
;;
3427
*)

0 commit comments

Comments
 (0)