Search code examples
gitcookiecutter

Create a git versioned project with cookiecutter


I have a nested git repository structure, as below:

outer_repository/
|-- outer_dummy_file
|-- .git 
`-- inner_repository
    |-- .git
    `-- inner_dummy_file

Is it possible to make inner_repository/.git versioned with the outer repository?

Motivation:

I'm making a structure for new projects with cookiecutter. Every newly started project will have a predefined set of files and tools to begin with. One of the requirements for the automated versioning system if for the new project to be versioned with git and having an initial commit. The inner_repository is a structure for the new projects.


Solution

  • It is possible to achieve this without the need to do the git acrobatics. Cookiecutter provides an option to run post generation hooks. This allows creation of the git repository to happen on project creation.

    I've added the following in my cookiecutter repository:

    hooks/
    `-- post_gen_project.py
    

    where post_gen_project.py is:

    import subprocess
    
    subprocess.call(['git', 'init'])
    subprocess.call(['git', 'add', '*'])
    subprocess.call(['git', 'commit', '-m', 'Initial commit'])