1

I am using Magit. After staging some changes, when I press to commit something, Magit opens a buffer like this:


# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch feature-no-procrastinate-mode
# Your branch is ahead of 'origin/feature-no-procrastinate-mode' by 4 commits.
#   (use "git push" to publish your local commits)
#
# Changes to be committed:
#   modified:   source/mode/no-procrastinate.lisp
#
# Untracked files:
#   source/no-procrastinate.lisp~
#

Every time this happens I do the following:

1 - I remove all comments until the line with source/mode/no-procrastinate.lisp. There, I usually keep the file name, I add : and,

2 - I write something meaningful about the changes. For instance, the final result of the manual changes would be:


source/mode/no-procrastinate.lisp: insert documentation strings.
#
# Untracked files:
#   source/no-procrastinate.lisp~
#

On one hand, I bet step 1 can be automated with a short elisp function and a hook. On the other hand, step 2 probably can't be automated (yet - let's wait for a new generation of AI :D).

How can I automate step 1? What would be a cool function and a hook for it?

Or, what if it was a command instead of a hook so that it was not completely automatic?

I still do not know much about Elisp to write this from scratch.

2 Answers 2

5

Something like this is already available to you: instead of pressing c to commit, press C (bound to magit-commit-add-log). This starts the commit message with the filename followed by a : as you want.

There is no need to manually remove the comments as they will not appear in the commit message.

1

Although I see no reason not to use Fran Burstall his answer, a way to achieve more directly what you asked is by using the following code:

(defun git-advice (&rest _)
  (when (string= (buffer-name) "COMMIT_EDITMSG")
    (delete-region (point-min) (search-forward "modified:   "))
    (end-of-line)
    (insert ":")))

(advice-add 'switch-to-buffer :after #'git-advice)

I could not find a better 'hook' to use for it, and I could also not find another option to e.g. achieve it using some commit template.

Anyway, so this is just an extra answer to inform about other ways of achieving these things when better options (like Fran's answer) are not available. (Also, you can use this answer if for some reason you are not satisfied with Fran's solution)

(I did not know about the option mentioned by Fran, so I was already looking for an 'alternative' solution)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.