1 |
On 02/06/2012 12:32 AM, HUjuice wrote: |
2 |
>>> .git/hooks/pre-commit |
3 |
>>> ------------------------------ |
4 |
>>> #!/bin/sh |
5 |
>>> for i in $(git diff --name-only); do |
6 |
>>> if ! xmllint --valid --noout $i; then |
7 |
>>> echo "Errori XML nel file $i" |
8 |
>>> exit 0 |
9 |
>>> fi |
10 |
>>> done |
11 |
>>> ------- |
12 |
>>> Secondo te si può migliorare? |
13 |
> |
14 |
>> |
15 |
>> exit 0 mi pare dica "tutto ok" |
16 |
> |
17 |
> Ovviamente hai ragione. E poi manca un --cached (dentro la |
18 |
> sostituzione di comando). |
19 |
> .git/hooks/pre-commit |
20 |
> ------------------------------ |
21 |
> #!/bin/sh |
22 |
> for i in $(git diff --cached --name-only); do |
23 |
> if ! xmllint --valid --noout $i; then |
24 |
> echo "Errori XML nel file $i" |
25 |
> exit 1 |
26 |
> fi |
27 |
> done |
28 |
> -------------------------------- |
29 |
> Un check globale, come dici tu, è anche meglio. |
30 |
|
31 |
|
32 |
Allora.. |
33 |
|
34 |
prendendo dall'esempio di hook che ha git lo cambierei così: |
35 |
|
36 |
|
37 |
#!/bin/sh |
38 |
# |
39 |
# XML validation of files that are about to be committed. |
40 |
# Called by "git commit" with no arguments. The hook should |
41 |
# exit with non-zero status after issuing an appropriate message if |
42 |
# it wants to stop the commit. |
43 |
|
44 |
if git rev-parse --verify HEAD >/dev/null 2>&1 |
45 |
then |
46 |
against=HEAD |
47 |
else |
48 |
# Initial commit: diff against an empty tree object |
49 |
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 |
50 |
fi |
51 |
|
52 |
# If you want to allow non-ascii filenames set this variable to true. |
53 |
allownonascii=$(git config hooks.allownonascii) |
54 |
|
55 |
# Cross platform projects tend to avoid non-ascii filenames; prevent |
56 |
# them from being added to the repository. We exploit the fact that the |
57 |
# printable range starts at the space character and ends with tilde. |
58 |
if [ "$allownonascii" != "true" ] && |
59 |
# Note that the use of brackets around a tr range is ok here, (it's |
60 |
# even required, for portability to Solaris 10's /usr/bin/tr), |
61 |
since |
62 |
# the square bracket bytes happen to fall in the designated range. |
63 |
test "$(git diff --cached --name-only --diff-filter=A -z $against | |
64 |
LC_ALL=C tr -d '[ -~]\0')" |
65 |
then |
66 |
echo "Error: Attempt to add a non-ascii file name." |
67 |
echo |
68 |
echo "This can cause problems if you want to work" |
69 |
echo "with people on other platforms." |
70 |
echo |
71 |
echo "To be portable it is advisable to rename the file ..." |
72 |
echo |
73 |
echo "If you know what you are doing you can disable this" |
74 |
echo "check using:" |
75 |
echo |
76 |
echo " git config hooks.allownonascii true" |
77 |
echo |
78 |
exit 1 |
79 |
fi |
80 |
|
81 |
XML_VALIDATION=0; |
82 |
TMP_FILE="/tmp/tmp_check.xml" |
83 |
TMP_LOG="/tmp/tmp_check.log" |
84 |
rm -f "$TMP_FILE" "$TMP_LOG" 2> /dev/null |
85 |
|
86 |
for file in ${git diff-index --cached $against}; |
87 |
do |
88 |
filename=${basename "$file"} |
89 |
extension=${filename##*.} |
90 |
if [[ extension = 'xml' ]] ; then |
91 |
echo "checking XML: $file ..." >> "$TMP_LOG" |
92 |
# copy the indexed version in a temporary file |
93 |
git show :"$file" > "$TMP_FILE" |
94 |
xmllint --valid --noout "$TMP_FILE" 2>> "$TMP_LOG" |
95 |
RESULT=$? |
96 |
if [[ ! $RESULT ]] ; then |
97 |
echo "XML non valido: $file" |
98 |
XML_VALIDATION=2; |
99 |
fi; |
100 |
rm -f "$TMP_FILE" |
101 |
fi |
102 |
done; |
103 |
|
104 |
if [[ XML_VALIDATION -ne 0 ]] ; then |
105 |
LOG_FILE="/tmp/git-commit-hook-log_`date '+%y%m%d%H%M%S'`.log" |
106 |
mv "$TMP_FILE" "$LOG_FILE" |
107 |
echo "See $LOG_FILE for details" |
108 |
echo "If you want to ignore this errors re commit with --no-verify option" |
109 |
exit $XML_VALIDATION |
110 |
else |
111 |
rm -f "$TMP_LOG" 2> /dev/null |
112 |
exit 0; |
113 |
fi |
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
> C'è anche da dire che questo controllo potrebbe essere troppo serrato. |
120 |
> Un commit si può fare anche a metà lavoro (ma allora c'è git commit |
121 |
> --no-verify). |
122 |
|
123 |
si... vero |
124 |
|
125 |
si potrebbe usare un pre-push hook |
126 |
|
127 |
la differenza nello script sopra è il calcolo dell'${against} |
128 |
mi dicevano di usare "@{u}" al posto di "HEAD" |
129 |
|
130 |
e subito prima fare un git fetch per essere sicuri di avere l'ultimo |
131 |
aggiornamento |
132 |
|
133 |
lo dicono qui: |
134 |
http://fclose.com/p/linux/man/7-gitrevisions/ |
135 |
git help gitrevisions |
136 |
|
137 |
|
138 |
non correlato ma per completezza: |
139 |
http://book.git-scm.com/4_git_treeishes.html |
140 |
|
141 |
> È proprio per queste cose che troverei utile un piccolo host di |
142 |
> lavoro, invece di gitorious. |
143 |
|
144 |
si può anche creare un altro mirror, magari su github per non restare |
145 |
appiedati se uno dei due va giù... |
146 |
|
147 |
il problema è che poi sta a noi tenere i mirror in sincrono :) |