问题
I have windows 7 but I think it doesn't count, because the hook uses git shell. I tried to dump my database by commit with the following code, but it did not work.
#!/bin/sh
# Refresh the SQL schema file for inclusion within the git commit
# If something fails, exit with status other than 0
set -e
# select dump directory
cd $(git rev-parse --show-toplevel)
cd WebShop/DataBase
# first, remove our original schema
rm -f backup.sql
# generate a new schema
mysqldump -u root --password=root webshopdb > backup.sql
# Add the schema to the next commit
git add backup.sql
# Exit success
exit 0
I've got 2 error messages:
The path doesn't exist, because the space in a directory name breaks it.
cd $(git rev-parse --show-toplevel)
Cannot find command
mysqldump -u root --password=root webshopdb > backup.sql
Is it possible to fix it?
回答1:
I was trying to do this too, and came up with mixed results, but ultimately this does work. Maybe git has had a few updates since last year that fixed the problem? Maybe it's because I'm on a mac? Here's the pre-commit I use now. (Thanks to everyone's help)
#!/bin/sh
# Dump a fresh copy of the database
mysqldump -u root --password="password" myDB --skip-extended-insert > dump.sql
git add dump.sql
Note: The --skip-extended-insert
is not necessary to make any changes to the bash, I just like the way my SQL dumps can be tracked in git with that extra option added.
what seemed to matter was how I was running the git command
If I ran git
from the commandline, then there were no problems.
If I ran git
via SourceTree, then there are errors unless SourceTree is run from the commandline too.
回答2:
If git is in your path in your .profile/.bash_profile etc you shouldn't need to set it. That said you are only "add"ing the file and not committing it. After you add you need to run
git commit -m "updated schema"
回答3:
To debug sh, use
sh -x script
(comment set -e)
and you may source your rc file at the beginning to avoid using full paths :
source ~/.shrc
回答4:
I fixed the errors, it creates the sql file, but still doesn't add it to the commit. I don't know why...
The fixed code:
#!/bin/sh
# Refresh the SQL schema file for inclusion within the git commit
# If something fails, exit with status other than 0
set -e
# select dump directory
cd "$(git rev-parse --show-toplevel)"
# first, remove our original schema
rm -f "WebShop\DataBase\backup.sql"
# generate a new schema
exec "C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqldump.exe" --skip-comments -u root --password=root webshopdb |sed 's$),($),\n($g' > "WebShop\DataBase\backup.sql"
# Add the schema to the next commit
git add "WebShop\DataBase\backup.sql"
# Exit success
exit 0
- If we have whitespace in the path we have to use it between double quotes.
- I used the exec and the full path of mysqldump.exe instead of the mysqldump command.
- Made some changes by mysqldump options:
- skip comments (to prevent file change caused by creation time footer)
- split insert into multiple lines by sed
回答5:
I know the OP already found his solution, but for anyone looking for a pre-commit hook for mysqldump, check this Gist out.. it should do the job for you: https://gist.github.com/wilcollins/dfa33ef20caf6dab5826
#!/bin/bash -e
# -e means exit if any command fails
DBHOST=address.to.your.server
DBUSER=username
DBPASS=password # do this in a more secure fashion
DBNAME=DBNAME
GITREPO=/where/is/your/repo
DUMP=$GITREPO/where/you/store/dumps
NEW=$DUMP/schema.sql
OLD=$NEW.old
DIR=$(pwd)
cd $GITREPO
mv $NEW $OLD
mysqldump -h $DBHOST -u $DBUSER -p$DBPASS $DBNAME --skip-dump-date --single-transaction > $NEW
# NOTE : the new schema.sql file & location need to be added to your GIT repo & ignore .old
if cmp -s $OLD $NEW; then
echo Same
else
echo Differ
git commit $NEW -m "$DBNAME DB update"
echo "schema+data committed"
git push # assuming you have a remote to push to
echo "schema+data pushed"
fi
cd $DIR
来源:https://stackoverflow.com/questions/9235682/git-pre-commit-mysqldump-cannot-find-path-not-existing-command