0

I want to execute the following function on the provided name before creating a new branch in magit:

(defun pb/magit-new-branch-name (branch-name)
  "create a name usable as a branch name from branch-name"

  ;;remove special characters inside
  (replace-regexp-in-string
   "[ \t\n:]+"
   "-"
   ;;remove leading spaces
   (replace-regexp-in-string
    "\\`[ \t\n]*"
    ""
    ;;remove trailing spaces
    (replace-regexp-in-string
     "[ \t\n]*\\'"
     ""
     branch-name
     )
    )
   )
  )

The goal is to be able to copy-paste something like

"   TICKET-3423

Issue description 
"

at the "Branch name:" prompt and have it automatically rewritten to "TICKET-3423-Issue-description".

2

Instead of modifying many Magit commands so that they automatically convert certain invalid input into valid branch names according to some rules as you intend to do, you should consider implementing a specialized yank command.

That command should do the transformation and you have to bind it in some minibuffer keymap (minibuffer-local-map should work).

You can then use it like this:

  1. Put the string into the kill ring the same way you are already doing that.
  2. Invoke the branching command.
  3. When prompted for the branch name do not type C-y, instead type the key you have chosen for your my-yank-branch-name command.

Such a command has the benefit that it can also be used in other contexts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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