Skip to content

Commit

Permalink
feat(git): Add auto git init-add-commit #102 (#110)
Browse files Browse the repository at this point in the history
Improve user experience by adding this option.

closes #102
  • Loading branch information
imAsparky committed Sep 22, 2021
1 parent 19bb021 commit 42a531a
Show file tree
Hide file tree
Showing 7 changed files with 277 additions and 43 deletions.
25 changes: 18 additions & 7 deletions README.rst
Expand Up @@ -75,7 +75,7 @@ Features
#. An optional GitHub action to run your Tox_ package test suite when a
pull request to the main branch starts.
#. Tox_ configuration for your package includes an OS and Python test matrix.
OS includes Linux, macOS and Windows. Python 3.6 - 3.9.
OS includes Linux, macOS and Windows. Python 3.6 - 3.9. (Uses GitHub actions.)
#. An optional suite of custom GitHub issue templates. The four custom issue
templates prompt users to help provide enough information in a templated
format for each issue type.
Expand All @@ -85,6 +85,10 @@ Features
#. Auto-release to PyPI_ when you push a new tag to main (optional). Coming soon.
#. Use commit tags to release to Test-PyPi_. Coming soon.
#. An optional Command line interface using Click or Argparse.
#. An option to initialise your local git repository, add files and create the
first commit automatically. Also if you have opted to use the
Conventional-Commits_ style git commit message template, cookiecutter
will simultaneously add it to your local git config file.

.. _Cookiecutter: https://github.com/cookiecutter/cookiecutter
.. _cookiecutter-pypackage: https://github.com/audreyfeldroy/cookiecutter-pypackage
Expand All @@ -96,20 +100,27 @@ Features

Quickstart
----------
* Create a GitHub repository.
This project will be aimed towards Github with use of Github Actions.


Install the latest Cookiecutter if you haven't installed it yet (this requires
Cookiecutter 1.4.0 or higher)::
Cookiecutter 1.4.0 or higher):

.. code-block:: bash
pip install -U cookiecutter
pip install -U cookiecutter
Generate a Python package project::
Generate a Python package project:

cookiecutter https://github.com/imAsparky/cookiecutter-py3-package.git
.. code-block:: bash
cookiecutter https://github.com/imAsparky/cookiecutter-py3-package.git
Follow the prompts.

Then:

* Create a repo and put it there.
This project will be aimed towards Github with use of Github Actions.
* Install the dev requirements into a virtualenv. (``pip install -r requirements_dev.txt``).
* Register_ your project with PyPI.
* Add the repo to your `Read the Docs`_ account + turn on the Read the Docs service hook.
Expand Down
1 change: 1 addition & 0 deletions cookiecutter.json
Expand Up @@ -20,6 +20,7 @@
"use_GH_action_semantic_version": "y",
"use_pre_commit": "y",
"use_GH_custom_issue_templates": "y",
"automatic_set_up_git_and_initial_commit": "y",
"open_source_license": ["MIT license", "BSD license", "ISC license", "Apache Software License 2.0", "GNU General Public License v3", "Not open source"],
"_copy_without_render": [
".github/workflows/test_contribution.yaml",
Expand Down
4 changes: 2 additions & 2 deletions docs/requirements.txt
@@ -1,5 +1,5 @@
Sphinx==4.1.2
myst-parser==0.15.1
furo==2021.8.17b43
myst-parser==0.15.1
Sphinx==4.1.2
sphinx-copybutton==0.4.0
sphinx_inline_tabs==2021.4.11b9
24 changes: 24 additions & 0 deletions docs/source/prompts.rst
Expand Up @@ -179,6 +179,30 @@ project.
| If you prefer, a simple issue template is available for use with all
issues if you choose `no` for this feature.
**automatic_set_up_git_and_initial_commit**
*default = y*

pre-requisites:

#. git_project_name
#. github_username
#. email

Initiation of your local git repository is automatic; all files are added
and committed with a conventional commits style message.

You can run a git reflog and check all is ok before pushing to the
repository.

.. code-block:: bash
git reflog
.. note::

Selecting this option also includes automatically adding the conventional
commits message template to git config if you have chosen that option.


**open_source_license**
*default = MIT*
Expand Down
105 changes: 105 additions & 0 deletions hooks/post_gen_project.py
@@ -1,9 +1,107 @@
#!/usr/bin/env python
"""cookiecutter-py3-package post package generation jobs."""
import os
import subprocess # nosec

PROJECT_DIRECTORY = os.path.realpath(os.path.curdir)

REMOTE_REPO = "git@github.com:{{cookiecutter.github_username}}/\
{{cookiecutter.git_project_name}}.git"


GIT_USER = "{{cookiecutter.full_name}}"
GIT_EMAIL = "{{cookiecutter.email}}"


def post_gen_setup(*args, supress_exception=False, cwd=None):
"""Helper to set up the package with the chosen options."""
cur_dir = os.getcwd()

try:
if cwd:
os.chdir(cwd)

with subprocess.Popen( # nosec
args, stdout=subprocess.PIPE, stderr=subprocess.PIPE
) as proc:

out, err = proc.communicate()
out = out.decode("utf-8")
err = err.decode("utf-8")
if err and not supress_exception:
raise Exception(err)
if err and supress_exception:
return out
return None # This return fixes pylint R1710 see
# https://pycodequ.al/docs/pylint-messages/r1710-inconsistent-return-statements.html
finally:
os.chdir(cur_dir)


def init_git():
"""Initialise git repository and set the remote."""
if not os.path.exists(os.path.join(PROJECT_DIRECTORY, ".git")):
post_gen_setup(
"git",
"init",
"--initial-branch=main",
cwd=PROJECT_DIRECTORY,
)

post_gen_setup(
"git",
"remote",
"add",
"origin",
REMOTE_REPO,
cwd=PROJECT_DIRECTORY,
)
post_gen_setup(
"git",
"config",
"user.name",
GIT_USER,
cwd=PROJECT_DIRECTORY,
)
post_gen_setup(
"git",
"config",
"user.email",
GIT_EMAIL,
cwd=PROJECT_DIRECTORY,
)


def git_add_and_commit_initial():
"""Add the local files and commit to the git repository."""
post_gen_setup(
"git",
"add",
"-A",
cwd=PROJECT_DIRECTORY,
)

post_gen_setup(
"git",
"commit",
"-m",
'"chore(git): Initial Commit"',
cwd=PROJECT_DIRECTORY,
)


def git_configure_custom_commit_message():
"""Configure git to use the custom commit message template."""
if os.path.exists(os.path.join(PROJECT_DIRECTORY, ".git")):
post_gen_setup(
"git",
"config",
"--local",
"commit.template",
".github/.git-commit-template.txt",
cwd=PROJECT_DIRECTORY,
)


def remove_file(filepath):
"""Remove files not required for this generated python package."""
Expand Down Expand Up @@ -60,3 +158,10 @@ def remove_file(filepath):

if "{{ cookiecutter.use_pre_commit }}" != "y":
remove_file(".pre-commit-config.yaml")

if "{{ cookiecutter.automatic_set_up_git_and_initial_commit }}" == "y":
init_git()
git_add_and_commit_initial()

if "{{ cookiecutter.create_conventional_commits_edit_message}}" == "y":
git_configure_custom_commit_message()
4 changes: 2 additions & 2 deletions requirements_dev.txt
Expand Up @@ -2,11 +2,11 @@ alabaster==0.7.12
cookiecutter==1.4.0
furo==2021.8.11b42
myst-parser==0.15.2
python-semantic-release==7.19.2
Sphinx==4.1.2
pre-commit==2.14.1
pytest==5.3.1
pytest-cookies==0.5.1
pytest-cov==2.12.1
python-semantic-release==7.19.2
Sphinx==4.1.2
tox==3.14.1
watchdog==2.1.5

0 comments on commit 42a531a

Please sign in to comment.