Skip to content

Commit

Permalink
Take commit message as script argument (#471)
Browse files Browse the repository at this point in the history
* update cli template script to take commit msg as string

* init_and_commit requires commit message, do not open editor

* take commit msg as argument, diff defaults for add and update

* update tests for changes in commit message handling

* update changelog

* add default commit messages to template scripts

* minor typo in error message

* updating docs for git commit messages

Co-authored-by: Leah Wasser <leah.wasser@colorado.edu>
  • Loading branch information
kcranston and Leah Wasser authored Feb 10, 2022
1 parent 7e4e4af commit 108a75f
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 65 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The format is based on `Keep a Changelog <https://keepachangelog.com/en/1.0.0/>`

[unreleased]
------------
- Rather than opening text editor for commit message, allow user to provide custom message as command line arg; changes name of command line arg to 'commit_message' from 'custom_message' (@kcranston, #466)
- Remove docs build on python 3.6 - sphinx template no longer supports 3.6, add build for 3.9 (@lwasser, #481)
- Cleanup old requirements and directories (@lwasser, #415)
- Update documentation to discuss using extra_files to modify gitignore and readme (@lwasser, #178)
Expand Down
17 changes: 10 additions & 7 deletions abcclassroom/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,10 @@ def new_template():
course_materials/release directory""",
)
parser.add_argument(
"--custom-message",
action="store_true",
help="""Use a custom commit message for git. Will open the default
git text editor for entry (if not set, uses default message 'Initial
commit').""",
"--commit-message",
default="Initial commit",
help="""Enter a commit message for git. If not set, uses default
message 'Initial commit').""",
)
parser.add_argument(
"--github",
Expand All @@ -159,7 +158,6 @@ def new_template():
(Default = fail).""",
)
args = parser.parse_args()

template.new_update_template(args)


Expand Down Expand Up @@ -220,9 +218,14 @@ def update_template():
directory); merge = overwrite existing files add new files
(Default = merge).""",
)
parser.add_argument(
"--commit-message",
default="Updating assignment",
help="""Enter a commit message for git. If not set, uses default
message 'Updating assignment').""",
)
args = parser.parse_args()
# now set the additional args (so that it matches the keys in add_template
# and we can use the same implementation methods)
setattr(args, "github", True)
setattr(args, "custom_message", True)
template.new_update_template(args)
29 changes: 2 additions & 27 deletions abcclassroom/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
# methods that involve the GitHub API

import subprocess
import sys

from .utils import input_editor


def check_git_ssh():
Expand Down Expand Up @@ -134,22 +131,6 @@ def repo_changed(directory):
return bool(ret.stdout)


def get_commit_message():
default_message = """
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# This message will be used as commit and Pull Request message."""
message = input_editor(default_message)
message = "\n".join(
[
line
for line in message.split("\n")
if not line.strip().startswith("#")
]
)
return message


def commit_all_changes(directory, msg=None):
"""Run git add, git commit on a given directory. Checks git status
first and does nothing if no changes.
Expand All @@ -163,7 +144,7 @@ def commit_all_changes(directory, msg=None):
print("No changes in repository {}; doing nothing".format(directory))


def init_and_commit(directory, custom_message=False):
def init_and_commit(directory, commit_message):
"""Run git init, git add, git commit on given directory. Checks git status
first and does nothing if no changes are detected.
Expand All @@ -187,13 +168,7 @@ def init_and_commit(directory, custom_message=False):
git_init(directory)
_master_branch_to_main(directory)
if repo_changed(directory):
message = "Initial commit"
if custom_message:
message = get_commit_message()
if not message:
print("Empty commit message, exiting.")
sys.exit(1) # sys is undefined - ask karen about this
commit_all_changes(directory, message)
commit_all_changes(directory, commit_message)
else:
print("No changes to local repository.")

Expand Down
17 changes: 11 additions & 6 deletions abcclassroom/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def new_update_template(args):
create_template(
mode=args.mode,
push_to_github=args.github,
custom_message=args.custom_message,
commit_message=args.commit_message,
assignment_name=args.assignment,
)
except FileNotFoundError as fnfe:
Expand All @@ -46,7 +46,10 @@ def new_update_template(args):


def create_template(
assignment_name, mode="fail", push_to_github=False, custom_message=False
assignment_name,
mode="fail",
push_to_github=False,
commit_message="Initial commit",
):
"""
Classroom package function that creates or updates an assignment template
Expand All @@ -70,8 +73,10 @@ def create_template(
push_to_github : boolean
True if you want to push to GH
mode : merge, fail
custom message : boolean (default = False)
True if you want to push to github.
commit_message : string
Sets a custom git commit message. For new templates, default is
"Initial commit" and for updating templates, default is
"Updating assignment"
assignment_name : string
name of the assignment
"""
Expand All @@ -97,7 +102,7 @@ def create_template(
except FileNotFoundError as e:
print(e)
raise FileNotFoundError(
"Oops, it looks like the assignment - {} - does not exist"
"Oops, it looks like the assignment - {} - does not exist "
"in the location that I expected it: \n{}. \nDid "
"you spell the assignment name correctly and is there a "
"directory at this path?".format(assignment_name, release_path)
Expand All @@ -112,7 +117,7 @@ def create_template(
)

# Create the local git repository and commit changes
abcgit.init_and_commit(template_repo_path, custom_message)
abcgit.init_and_commit(template_repo_path, commit_message)

# Create / append assignment entry in config - this should only happen if
# the assignment above exists...
Expand Down
35 changes: 28 additions & 7 deletions abcclassroom/tests/test_assignment_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ def test_create_template(course_structure_assignment):
course_dir = Path(config["course_directory"])
templates_dir = Path(config["template_dir"])

abctemplate.create_template(
assignment_name, push_to_github=False, custom_message=False
)
abctemplate.create_template(assignment_name, push_to_github=False)

expected_template_dir = Path(
course_dir, templates_dir, "{}-template".format(assignment_name)
Expand All @@ -40,9 +38,32 @@ def test_create_template_no_assignment(sample_course_structure):
with pytest.raises(
FileNotFoundError, match="Oops, it looks like the assignment"
):
abctemplate.create_template(
"assignment_test", push_to_github=False, custom_message=False
)
abctemplate.create_template("assignment_test", push_to_github=False)


def test_create_custom_commit_message(course_structure_assignment):
"""
Test that top-level create_template uses a custom commit message
if provided.
"""
config, assignment_name, release_path = course_structure_assignment
course_dir = Path(config["course_directory"])
templates_dir = Path(config["template_dir"])

abctemplate.create_template(
assignment_name,
mode="delete",
push_to_github=False,
commit_message="Custom commit message",
)

expected_template_dir = Path(
course_dir, templates_dir, "{}-template".format(assignment_name)
)
assert expected_template_dir.exists()

git_return = abcgit._call_git("log", directory=expected_template_dir)
assert "Custom commit" in git_return.stdout


def test_create_template_dir(course_structure_assignment):
Expand Down Expand Up @@ -132,7 +153,7 @@ def test_create_template_dir_move_git_dir(course_structure_assignment):
# create a file in the template dir, init a git repo and commit
testfile = Path(template_path, "file1.txt")
testfile.touch()
abcgit.init_and_commit(template_path, False)
abcgit.init_and_commit(template_path, "commit message")
assert Path(template_path, ".git").exists()

template_path = abctemplate.create_template_dir(
Expand Down
3 changes: 2 additions & 1 deletion abcclassroom/tests/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ def test_init_and_commit(default_config, tmp_path):
repo_dir.mkdir()
a_file = Path(repo_dir, "testfile.txt")
a_file.write_text("Some text")
abcgit.init_and_commit(repo_dir)
commit_message = "changing some things"
abcgit.init_and_commit(repo_dir, commit_message)
assert Path(repo_dir, ".git").exists()
git_return = abcgit._call_git("log", directory=repo_dir)
assert git_return.stdout.startswith("commit")
Expand Down
4 changes: 4 additions & 0 deletions docs/api/abcclassroom.auth.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.. automodule:: abcclassroom.auth
:members:
:undoc-members:
:show-inheritance:
4 changes: 4 additions & 0 deletions docs/api/abcclassroom.git.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.. automodule:: abcclassroom.git
:members:
:undoc-members:
:show-inheritance:
2 changes: 2 additions & 0 deletions docs/api/abcclassroom.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ Submodules
.. toctree::
:maxdepth: 4

abcclassroom.auth
abcclassroom.clone
abcclassroom.config
abcclassroom.feedback
abcclassroom.git
abcclassroom.github
abcclassroom.quickstart
abcclassroom.roster
Expand Down
25 changes: 8 additions & 17 deletions docs/manage-assignments/new_assignment.rst
Original file line number Diff line number Diff line change
Expand Up @@ -113,23 +113,13 @@ A few notes about how this works:
Text Editors and Git Commit Messages
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The default commit message used when you run
``abc-new-template <assignment-name>`` is **initial commit**. A text editor
will not open in this case.
``abc-new-template <assignment-name>`` is "Initial commit". For
``abc-update-template`` the default message is "Updating assignment".

When you run ``abc-update-template`` the text editor that is specified in your
system configuration settings will open up. If you do not have a text editor
specified, VIM will open as a default.
If you want to provide a custom commit message, use the ``--commit-message``
flag with either script, and provide a message after the flag, e.g.::

If you wish to use nano instead you can run the following in your terminal::

export EDITOR=nano

.. note::
Right now if you try to use an editor like atom that launches outside of the
terminal, abc-classroom will currently fail and return a message saying
**empty commit message** . This may be fixed in the future but for now we
suggest that you use a terminal based editor for your default when using
abc-classroom.
abc-update-template assignment1 --commit-message "adding new notebook"



Expand All @@ -153,8 +143,7 @@ Run ``abc-new-template -h`` to see the options. The output is reproduced below::

optional arguments:
-h, --help show this help message and exit
--custom-message Use a custom commit message for git. Will open the
default git text editor for entry (if not set, uses
--commit-message Provide a custom commit message for git (if not set, uses
default message 'Initial commit').
--github Also perform the GitHub operations (create remote repo
on GitHub and push to remote (by default, only does
Expand Down Expand Up @@ -201,6 +190,8 @@ is reproduced here::

optional arguments:
-h, --help show this help message and exit
--commit-message Provide a custom commit message for git (if not set, uses
default message 'Updating assignment').
--mode {delete,merge}
What to do with existing contents of template
directory. Choices are: delete = remove contents
Expand Down

0 comments on commit 108a75f

Please sign in to comment.