]> git.proxmox.com Git - libgit2.git/blob - tests/resources/generate_crlf.sh
New upstream version 0.28.1+dfsg.1
[libgit2.git] / tests / resources / generate_crlf.sh
1 #!/usr/bin/env bash
2 #
3 # This script will generate the test corpus for CR/LF data using git;
4 # we create files with all possible line ending varieties (all LF, all
5 # CRLF, mixed, etc) on all the possible line ending configurations
6 # (`core.autocrlf=true`, `text=auto` in gitattributes, etc). This
7 # allows us to validate that our configuration will match byte-for-byte
8 # the configuration that git produces.
9 #
10 # To update the test resource data, from the test resource directory:
11 # git rm -r ./crlf_data/{posix,windows}
12 # sh ./generate_crlf.sh ./crlf ./crlf_data /tmp/crlf_gitdirs
13 # git add ./crlf_data/{posix,windows}
14
15 set -e
16
17 if [ "$1" == "" -o "$2" == "" ]; then
18 echo "usage: $0 crlfrepo directory [tempdir]"
19 exit 1
20 fi
21
22 input=$1
23 output=$2
24 tempdir=$3
25
26 set -u
27
28 create_to_workdir_data() {
29 local input=$1
30 local output=$2
31 local tempdir=$3
32 local systype=$4
33 local autocrlf=$5
34 local attr=$6
35
36 local worktree="${output}/${systype}_to_workdir/autocrlf_${autocrlf}"
37
38 if [ "$attr" != "" ]; then
39 local attrdir=`echo $attr | sed -e "s/ /,/g" | sed -e "s/=/_/g"`
40 worktree="${worktree},${attrdir}"
41 fi
42
43 if [ "$tempdir" = "" ]; then
44 local gitdir="${worktree}/.git"
45 else
46 local gitdir="${tempdir}/generate_crlf_${RANDOM}"
47 fi
48
49 echo "Creating ${worktree}"
50 mkdir -p "${worktree}"
51
52 git clone --no-checkout --quiet --bare "${input}/.gitted" "${gitdir}"
53 git --work-tree="${worktree}" --git-dir="${gitdir}" config core.autocrlf ${autocrlf}
54
55 if [ "$attr" != "" ]; then
56 echo "* ${attr}" >> "${worktree}/.gitattributes"
57 fi
58
59 git --work-tree="${worktree}" --git-dir="${gitdir}" checkout HEAD
60
61 if [ "$attr" != "" ]; then
62 rm "${worktree}/.gitattributes"
63 fi
64
65 if [ "$tempdir" != "" ]; then
66 rm -rf "${gitdir}"
67 fi
68 }
69
70 create_to_odb_data() {
71 local input=$1
72 local output=$2
73 local tempdir=$3
74 local systype=$4
75 local autocrlf=$5
76 local safecrlf=$6
77 local attr=$7
78
79 local destdir="${output}/${systype}_to_odb/autocrlf_${autocrlf},safecrlf_${safecrlf}"
80
81 if [ "$attr" != "" ]; then
82 local attrdir=`echo $attr | sed -e "s/ /,/g" | sed -e "s/=/_/g"`
83 destdir="${destdir},${attrdir}"
84 fi
85
86 if [ "$tempdir" = "" ]; then
87 local workdir="${destdir}/_workdir"
88 else
89 local workdir="${tempdir}/generate_crlf_${RANDOM}"
90 fi
91
92 echo "Creating ${destdir}"
93 mkdir -p "${destdir}"
94
95 git init "${workdir}" >/dev/null
96 git --work-tree="${workdir}" --git-dir="${workdir}/.git" config core.autocrlf "${autocrlf}"
97 git --work-tree="${workdir}" --git-dir="${workdir}/.git" config core.safecrlf "${safecrlf}"
98
99 if [ "$attr" != "" ]; then
100 echo "* ${attr}" > "${workdir}/.gitattributes"
101 fi
102
103 cp ${input}/* ${workdir}
104
105 for path in ${workdir}/*; do
106 filename=$(basename $path)
107 failed=""
108 output=$(git --work-tree="${workdir}" --git-dir="${workdir}/.git" add ${filename} 2>&1) || failed=1
109
110 if [ ! -z "${failed}" -a "${output:0:35}" == "fatal: LF would be replaced by CRLF" ]; then
111 echo "LF would be replaced by CRLF in '${filename}'" > "${destdir}/${filename}.fail"
112 elif [ ! -z "${failed}" -a "${output:0:35}" == "fatal: CRLF would be replaced by LF" ]; then
113 echo "CRLF would be replaced by LF in '${filename}'" > "${destdir}/${filename}.fail"
114 elif [ ! -z "${failed}" ]; then
115 echo "failed to add ${filename}: ${output}" 1>&2
116 exit 1
117 else
118 git --work-tree="${workdir}" --git-dir="${workdir}/.git" cat-file blob ":${filename}" > "${destdir}/${filename}"
119 fi
120 done
121
122 if [ "$tempdir" != "" ]; then
123 rm -rf "${workdir}"
124 fi
125 }
126
127 if [[ `uname -s` == MINGW* ]]; then
128 systype="windows"
129 else
130 systype="posix"
131 fi
132
133 for autocrlf in true false input; do
134 for attr in "" text text=auto -text crlf -crlf eol=lf eol=crlf \
135 "text eol=lf" "text eol=crlf" \
136 "text=auto eol=lf" "text=auto eol=crlf"; do
137
138 create_to_workdir_data "${input}" "${output}" "${tempdir}" \
139 "${systype}" "${autocrlf}" "${attr}"
140
141 for safecrlf in true false warn; do
142 create_to_odb_data "${input}" "${output}" "${tempdir}" \
143 "${systype}" "${autocrlf}" "${safecrlf}" "${attr}"
144 done
145 done
146 done
147