I'm thinking about automating my following git workflow in emacs:

  1. checkout to a new branch
  2. remove some pattern in the last commit message
  3. commit (amend) it
  4. push it to upstream

How could I automate 4 steps in emacs?

And I've tried to combine step 2 and step 3 into one key in magit by:

(defun magit-commit-append-new-msg ()
  (interactive)
  (let* ((git-dir (magit-toplevel))
    ;; get last commit msg
    ;; TODO handle remote dir
    (msg (shell-command-to-string (format "git -C \"%s\" log -1 --format=\"%%B\"" git-dir)))
    ;; remove changeid and jira line in msg
    (msg-new (concat msg "[MYTEST]")))
    (magit-git-command-topdir (format "git -C \"%s\" commit --amend -m \"%s\"" git-dir msg-new))))

(magit-define-popup-action 'magit-commit-popup
  ?i
  "Commit with new msg"
  'magit-commit-append-new-msg)

But I've not found a way do all 4 steps yet.

  • Why do you have to create and checkout a new branch? Am I right in assuming that you are on a branch like new-feature and want your modified commit to be pushed to origin/new-feature? – tarsius Jun 3 at 4:43
  • @tarsius The reason I have to checkout a new branch is due to our work process, we have a branch for internal review and before it is pushed to outside we must remove some internal used information from the reviewed commit. – zwy Jun 4 at 12:44

Something like this might work.

(defun my-fixup-head-message-and-publish (remote branch)
  (interactive
   ;; TODO I don't know your needs you should probably adjust this.
   (list "public"
         (concat "publish/" (magit-get-current-branch))))
  (magit-with-toplevel
    (magit-call-git "branch" branch (magit-get-current-branch))
    (magit-call-git "commit" "--amend" "-m"
                    (concat "[MYTEST]"
                            (magit-rev-format "%B")))
    (magit-call-git "push" branch (concat remote "/" branch)
    (magit-refresh))))

You are probably better off asking "please implement this for me" questions on r/emacs.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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