[CookieCutter] Enable git integration

In previous article, after project created it can execute custom python script to customize template. In this example, it will extend the project to integrate with git.It will execute git command directly as it will be more prefer on getting environment settings.

Steps

  1. Add method for calling git commands.
    In file hook/post_gen_project.py, update line as below.

    import os;
    
    def init_git():
        commands = [
            'git init',
            'git add .',
            'git commit -m "Initial commit"'
        ]
        try:
            for command in commands:
                os.system(command)
        except:
            print("Error occurred when init git")
            exit(1)
    
    def main():
        init_git()
    
    if __name__ == "__main__":
        main()

    You can alter command in array commands to customize action such as create feature branch or set remote repository.

  2. Test and verify result
    In command prompt, execute command below to test, expect git commit message will be show.

    cookiecutter java-project-template

About C.H. Ling 260 Articles
a .net / Java developer from Hong Kong and currently located in United Kingdom. Thanks for Google because it solve many technical problems so I build this blog as return. Besides coding and trying advance technology, hiking and traveling is other favorite to me, so I will write down something what I see and what I feel during it. Happy reading!!!

Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.