直接从 Emacs 创建拉取请求

Creating Pull requests right from Emacs

我讨厌任何普通开发人员应该做的手动、乏味和重复的任务。最近我意识到 - 在大量 git 回购上创建拉取请求占用了我太多时间。大多数时候你必须一遍又一遍地遵循几乎精确的步骤:

在某些时候,我开始怀疑我是否可以在不使用网络客户端的情况下完成所有这些工作。这似乎是可能的。 Stash and Bitbucket have an api, Github has one as well(虽然不一样——第一个用ssh,后者用http)

现在,这个东西可能会简化一些事情,但我觉得它可以做得更好。

我使用 Emacs(具体来说是 Spacemacs 发行版)。 现在我想知道是否有人已经构建了与 magit 集成的任何东西,或者也许我可以自己做? 我的意思是这会有多难? 脚本应该让您提交然后推送分支,然后使用给定的默认值根据该分支针对 "develop" 创建拉取请求。有没有人做过这样的事?

你们能告诉我一些 elisp 插件利用 magit 的力量来做类似的事情吗?也许我可以自己写点东西。

魔法锻造

magit 现在自带 forge, which is to work with issues/PR/etc. in any of the git forges. It supports github,gitlab 开箱即用,部分支持 bitbucket、gitea、gitweb、cgit、gogs 等

创建新的拉取请求(或问题)(来自 the docs):

' p     (forge-create-pullreq)

C-c C-n [on "Pull requests" section] (forge-create-pullreq)

This command creates a new pull-request for the current repository.

' i     (forge-create-issue)

C-c C-n [on "Issues" section] (forge-create-pullreq)

This command creates a new issue for the current repository.


旧答案:

有一个用于处理 github 拉取请求的 magit 扩展。您可以通过 project here.

你还应该通过 magit 项目的维护者this post

So I am afraid there currently isn't a package that does what you want. If you want to write it yourself, I recommend you use request.el and then only implement those parts of the Github api that you actually need, to avoid over-engineering it.

我在 github 上找到了关于从 emacs 创建 PR 的原始 post。 http://endlessparentheses.com/easily-create-github-prs-from-magit.html

这不适用于 bitbucket(存储)。但这对我来说已经足够了 能够拼凑出适合我的解决方案。

https://github.com/flamingbear/emacs-config/blob/master/site-lisp/lisp/mhs-magit.el

(defun endless/visit-pull-request-url ()
  "Visit the current branch's PR on Github."
  (interactive)
  (let ((repo (magit-get "remote" (magit-get-remote) "url")))
    (if (string-match "github\.com" repo)
    (visit-gh-pull-request repo)
  (visit-bb-pull-request repo))))


(defun visit-gh-pull-request (repo)
  "Visit the current branch's PR on Github."
  (interactive)
  (browse-url
   (format "https://github.com/%s/pull/new/%s"
     (replace-regexp-in-string
      "\`.+github\.com:\(.+\)\.git\'" "\1"
      repo)
     (cdr (magit-get-remote-branch)))))



;; Bitbucket pull requests are kinda funky, it seems to try to just do the
;; right thing, so there's no branches to include.
;; https://bitbucket.org/<username>/<project>/pull-request/new
(defun visit-bb-pull-request (repo)
  (browse-url
   (format "https://bitbucket.org/%s/pull-request/new"
           (replace-regexp-in-string
            "\`.+bitbucket\.org:\(.+\)\.git\'" "\1"
            repo))))

;; visit PR for github or bitbucket repositories with "v"
(eval-after-load 'magit
  '(define-key magit-mode-map "v"
     #'endless/visit-pull-request-url))

(magit-get "remote" (magit-get-remote) "url") returns nil 的情况下,已得到改进。

(defun endless/visit-pull-request-url ()
  "Visit the current branch's PR on Github."
  (interactive)
  (let ((repo (magit-get "remote" (magit-get-remote) "url")))
    (if (not repo)
    (setq repo (magit-get "remote" (magit-get-push-remote) "url")))
    (if (string-match "github\.com" repo)
    (visit-gh-pull-request repo)
      (visit-bb-pull-request repo))))

(defun visit-gh-pull-request (repo)
  "Visit the current branch's PR on Github."
  (interactive)
  (message repo)
  (browse-url
   (format "https://github.com/%s/pull/new/%s"
       (replace-regexp-in-string
        "\`.+github\.com:\(.+\)\(\.git\)?\'" "\1"
        repo)
       (magit-get-current-branch))))

;; Bitbucket pull requests are kinda funky, it seems to try to just do the
;; right thing, so there's no branches to include.
;; https://bitbucket.org/<username>/<project>/pull-request/new
(defun visit-bb-pull-request (repo)
  (message repo)
  (browse-url
   (format "https://bitbucket.org/%s/pull-request/new?source=%s&t=1"
       (replace-regexp-in-string
        "\`.+bitbucket\.org:\(.+\)\.git\'" "\1"
        repo)
       (magit-get-current-branch))))
;; visit PR for github or bitbucket repositories with "v"
(eval-after-load 'magit
  '(define-key magit-mode-map "v"
     #'endless/visit-pull-request-url))