Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git modify contents of index directly for pre-commit formatting hook

I would like to automatically format my files before they are added to Git's index. Right now, I have a pre-commit hook that looks like this:

#!/bin/bash

set -e
exec 1>&2

workingext="working.$$"
ext="bak.$$"
git diff -z --cached --name-only | egrep -z '\.(pl|pm|t)$' | \
        while read -d'' -r f; do
    # First modify the file in the index
    mv "$f" "$f.$workingext"
    git show :"$f" > "$f"
    perl -c "$f"
    perltidy -pbp -nst -b -bext="$ext" "$f";
    rm -f "$f.$ext"
    git add "$f"
    mv "$f.$workingext" "$f"
    # Then the working copy
    perl -c "$f"
    perltidy -pbp -nst -b -bext="$ext" "$f";
    rm -f "$f.$ext"
done

Basically, I back up the working copies, check out the indexed copies, format them, add them to the index, and then restore the working copies and format them too, so that the differences between the working and indexed copies don't grow too large. I check the syntax of the files using perl -c first so that the formatter perltidy can't get too confused by a syntactically invalid file.

So far, this seems to be working pretty well. However, I'd like to know if there's a way to add the contents of one file to the index under the name of another file. That is, a command like git update-index-from-file --from-file foo.pl.tmp foo.pl would update the file foo.pl such that its contents in the index came entirely from foo.pl.tmp, but whose version in the working directory remained unmodified. This might avoid the dance of renamings I'm doing right now (although that might be useful to ensure I don't lose the contents of the index irretrievably).

like image 954
telotortium Avatar asked Jan 19 '13 03:01

telotortium


People also ask

How do you bypass pre-commit hook?

Use the --no-verify option to skip git commit hooks, e.g. git commit -m "commit message" --no-verify . When the --no-verify option is used, the pre-commit and commit-msg hooks are bypassed.

How do I update pre-commit?

You can update your hooks to the latest version automatically by running pre-commit autoupdate . By default, this will bring the hooks to the latest tag on the default branch.

How do you use a pre-commit hook?

Open a terminal window by using option + T in GitKraken Client. Once the terminal windows is open, change directory to . git/hooks . Then use the command chmod +x pre-commit to make the pre-commit file executable.

What is pre-commit hook in git?

The pre-commit hook is run first, before you even type in a commit message. It's used to inspect the snapshot that's about to be committed, to see if you've forgotten something, to make sure tests run, or to examine whatever you need to inspect in the code.


1 Answers

Yes, that’s possible. First, you need to create a blob from your file by running this command:

git hash-object -w foo.pl.tmp

See here: http://www.kernel.org/pub/software/scm/git/docs/git-hash-object.html

Computes the object ID value for an object with specified type with the contents of the named file (which can be outside of the work tree), and optionally writes the resulting object into the object database. Reports its object ID to its standard output.

Using the blob sha that git hash-object sent to STDOUT, you can now add this blob as “foo.pl” to your index by running

git update-index --add --cacheinfo 100644 *YOURSHA* foo.pl

From the manpage of update-index:

--cacheinfo is used to register a file that is not in the current working directory. This is useful for minimum-checkout merging.

To pretend you have a file with mode and sha1 at path, say:

$ git update-index --cacheinfo mode sha1 path

But I’d like to note that your situation looks a lot like you should actually use a smudge filter to do that stuff. See here to learn about them: https://git-scm.com/book/en/v2/Customizing-Git-Git-Attributes#Keyword-Expansion

like image 122
Chronial Avatar answered Sep 22 '22 16:09

Chronial