Use git hooks to create an archive of files
Question: I want to create a zip file containing some of the files in the repo, and then
add and commit that as well as the files already in the repo.

I've changed `precommit` to this:

    
    
    #!C:/Python34/python.exe
    
    import tarfile, os
    os.chdir("C:\project\directory")
    
    with tarfile.open("archive.tar.gz", "w:gz") as f:
        for name in ["file", "names"]:
            f.add(name)
    

which creates the files, but I don't no how to add them to the commit.

I tried doing `subprocess.Popen("git add .")`, but it didn't stage the files.


Answer: Note that the hook is called `pre-commit`, not `precommit`.

If I create a hook named `.git/hooks/pre-commit` with the following code:

    
    
    #!/usr/bin/python
    
    import os
    import subprocess
    import tarfile
    
    print 'Adding files to archive.'
    with tarfile.open("archive.tar.gz", "w:gz") as f:
        for name in ["file1", "file2"]:
            f.add(name)
    
    print 'Adding archive to commit.'
    subprocess.call(['git', 'add', 'archive.tar.gz'])
    

And add some files to the repository:

    
    
    $ echo hello world > file1
    $ echo this is a test > file2
    $ git add file1 file2
    

And then commit the changes:

    
    
    $ git commit -m "added some files"
    

I see:

    
    
    Adding files to archive.
    Adding archive to commit.
    [master (root-commit) 38e33dd] added some files
     3 files changed, 2 insertions(+)
     create mode 100644 archive.tar.gz
     create mode 100644 file1
     create mode 100644 file2
    

And looking at the commit, I see:

    
    
    $ git log -1 --name-only
    commit 38e33dd5ba14d1bfe427b50cce37489259fd00c4
    Author: Lars Kellogg-Stedman <lars@example.com>
    Date:   Fri Jun 20 09:02:31 2014 -0400
    
        added some files
    
    archive.tar.gz
    file1
    file2
    

