Temporarily remove unbroken files before committing to Git

Whenever I commit, I worry that I might have missed the dependency, and I'm looking for the easiest way to test the git tree individually, to ensure that everything in the git index (β€œdelivered”) will actually compile / execute on its own.

My dependencies between files exist on the file system, where I do "git add", so simple compilation and execution tests do not guarantee that everything I checked will be compiled / executed if the tree (or intermediate area) was checked for a clean file the system.

I can have a continuous build, which will be checked after sending, but I prefer not to have any bad commits in the history, which I then have to fix. So I want to create a sandbox that includes tree validation as well as an index / staging area.

One thing I reviewed is to use git stash twice, i.e.:

  • Invoke 'git stash' to save files in index
  • Somehow get a list of files not tracked, 'git add' all these files, save a new stash
  • Delete all previously unverified files
  • Restore original stamp
  • Now I have to have a clean environment in which there is only code already registered and code in the intermediate area that I can compile and test.
  • As soon as you finish, I will return to the file without traces, and then bounce them to leave me in the same position that I was in originally.

(These unsolicited files may be useful, but it’s not necessary what I want to check in the repository - for example, eclipse projects).

I have the feeling that I am reinstalling a simple problem too much.

+4
source share
3 answers

Install this script (or something like that - mine is also stolen) as a pre-commit hook. It copies the index to a temporary working directory and starts the build there. It will catch files that you missed.

I know that there are at least one or two other SO questions that concern this exact problem - testing / checking the index instead of the working directory in pre-commit - but I cannot find them correctly now.

(For completeness, I got this script in my repo as .git-hooks / pre-commit / test-the-index, there are several more scripts there. Using as .git / hooks / pre-commit.)

#!/bin/sh # # Via: http://github.com/jwiegley/git-scripts/blob/master/pre-commit.sh # if [ ! $(git rev-parse --symbolic-full-name HEAD) = refs/heads/master ]; then exit 0 fi # These are the locations I keep my temporary source and build trees in TMPDIR=$HOME/code/myproject-pre-commit MIRROR=$HOME/code/myproject-pre-commit-mirror # Exit with status 1 if any command below fails set -e # Checkout a copy of the current index into MIRROR git checkout-index --prefix=$MIRROR/ -af # Remove files from MIRROR which are no longer present in the index git diff-index --cached --name-only --diff-filter=D -z HEAD | \ (cd $MIRROR && xargs -0 rm -f --) # Copy only _changed files_ from MIRROR to TMPDIR, without copying timestamps. # This includes copying over new files, and deleting removed ones. This way, # "make check" will only rebuild what is necessary to validate the commit. rsync -rlpgoDOc --delete --exclude-from=.git-hooks/excludes $MIRROR/ $TMPDIR/ # Everything else happens in the temporary build tree cd $TMPDIR nosetests exit 0 

This is my actual .git / hooks / pre-commit:

 #!/bin/bash set -e for hook in $(find .git-hooks/pre-commit -perm /u+x,g+x,o+x -type f -not -name '*~' 2>/dev/null) do echo "@@ Running hook: $(basename $hook)" $hook " $@ " done 
+2
source

git stash -u --keep-index before the test, then git stash pop after the test.

+1
source

The installation ignores important files and simply deletes those that are not important git clean -df

0
source

All Articles