Emacs - 如何将Git存储库推送到多个遥控器

law*_*ist 6 emacs elisp magit

我正在寻找一些帮助,请使用Emacs/Magit将本地存储库更改推送到远程网站并一举推向Github.

我发现了一个非的Emacs /非Magit相关话题(/sf/answers/223681251/),有评论指出,这是推到远程和Github上明确的答案,它有一个几百个竖起大拇指.我假设(可能不正确)这是我计算机上目录中本地 .gitconfig文件的一个很好的起点$HOME.

[remote "GitHub"]
    url = git@github.com:elliottcable/Paws.o.git
    fetch = +refs/heads/*:refs/remotes/GitHub/*
[branch "Master"]
    remote = GitHub
    merge = refs/heads/Master
[remote "Codaset"]
    url = git@codaset.com:elliottcable/paws-o.git
    fetch = +refs/heads/*:refs/remotes/Codaset/*
[remote "Paws"]
    url = git@github.com:Paws/Paws.o.git
    fetch = +refs/heads/*:refs/remotes/Paws/*
Run Code Online (Sandbox Code Playgroud)

Emacs/Magit中的基本Push命令一次只能推送一个:

C-u P P [and then use arrow keys to select from the choices in the minibuffer] RET
Run Code Online (Sandbox Code Playgroud)

请参阅可用命令的Magit备忘单:http://daemianmack.com/magit-cheatsheet.html


初步思考 - 用于/usr/local/git/bin/git remote -v获取已配置的远程列表,然后使用结果推送到每个远程...可行但复杂.

$ MP:my_project.git HOME$ /usr/local/git/bin/git remote -v

  origin    git@github.com:lawlist/my_project.git (fetch)
  origin    git@github.com:lawlist/my_project.git (push)
  remote_website    lawlist@my-website.com:my_project.git (fetch)
  remote_website    lawlist@my-website.com:my_project.git (push)
Run Code Online (Sandbox Code Playgroud)

COMMAND-LINE RECIPE - 分别推送到遥控器和Github:

;; Setup the remote repository and the hook; and the remote destination folder.
ssh lawlist@my-website.com
mkdir /home/lawlist/my_project.git
cd my_project.git
git init --bare
;; git update-server-info # If planning to serve via HTTP
cat > /home/lawlist/my_project.git/hooks/post-receive ;; RET
#!/bin/sh ;; RET
GIT_WORK_TREE=/home/lawlist/my_project git checkout -f ;; RET
;; C-d
chmod 755 /home/lawlist/my_project.git/hooks/post-receive
mkdir /home/lawlist/my_project
exit

;; On local machine.
mkdir /Users/HOME/.0.data/.0.emacs/elpa/my_project.git
touch /Users/HOME/.0.data/.0.emacs/elpa/my_project.git/README.md
cd /Users/HOME/.0.data/.0.emacs/elpa/my_project.git
/usr/local/git/bin/git init
/usr/local/git/bin/git add .
/usr/local/git/bin/git commit -m "First commit."
curl -u lawlist:12345678 https://api.github.com/user/repos -d '{"name":"my_project.git"}'
/usr/local/git/bin/git remote add origin git@github.com:lawlist/my_project.git
/usr/local/git/bin/git remote add remote_website lawlist@my-website.com:my_project.git
/usr/local/git/bin/git push origin master
/usr/local/git/bin/git push remote_website master

;; For modification of local files
/usr/local/git/bin/git add .
/usr/local/git/bin/git commit -m "This is a modification . . . ."
/usr/local/git/bin/git push origin master
/usr/local/git/bin/git push remote_website master
Run Code Online (Sandbox Code Playgroud)

law*_*ist 4

编辑(2014 年 4 月 23 日):\xc2\xa0添加了一个非 Magit 解决方案来暂存所有、提交所有(使用默认提交消息)并推送到所有遥控器。

\n\n

编辑(2014 年 4 月 24 日): \xc2\xa0 所有进程的打印输出现在都发送到git-status-buffer,它显示在函数的末尾 - 带有供用户选择如何处理窗口的选项 - 例如,删除窗口,删除缓冲区和窗口,或者什么都不做。添加了一些漂亮的颜色(propertize "[...]" \'face \'font-lock-warning-face)。依赖于预先存在的 Magit 安装的函数的初稿已移至此答案的底部 - 该函数可以工作,但不如不依赖于 Magit 安装的当前版本那么复杂。

\n\n
(defvar git-status-buffer "*GIT-STATUS*"\n  "The buffer name of the git-status-buffer.")\n\n(defvar git-branch-name nil\n"The current branch of the working Git directory.")\n(make-variable-buffer-local \'git-branch-name)\n\n(defvar git-remote-list nil\n"List of remote locations -- e.g., lawlist_remote or github_remote.")\n(make-variable-buffer-local \'git-remote-list)\n\n(defvar git-commit-message (format "Committed -- %s" (current-time-string))\n"The predetermined Git commit message.")\n(make-variable-buffer-local \'git-commit-message)\n\n(defun git-branch-process-filter (proc string)\n  (with-current-buffer (get-buffer git-status-buffer)\n    (set (make-local-variable \'git-branch-name)\n      (car (split-string string "\\n")))))\n\n(defun git-push-process-filter (proc string)\n  (when (string-match "password" string)\n    (process-send-string\n      proc\n      (concat (read-passwd "Password:  ") "\\n")))\n  (when (and\n      (not (string-equal "Password: " string))\n      (not (string-equal "\\n" string))\n      (not (string-equal "stdin: is not a tty\\n" string)))\n    (with-current-buffer git-status-buffer\n      (goto-char (point-max))\n      (insert "\\n" (replace-regexp-in-string "\\^M" "\\n" string)))))\n\n(defun git-push-process-sentinel (proc string)\n  (when (= 0 (process-exit-status proc))\n    (with-current-buffer (get-buffer git-status-buffer)\n      (insert\n        "\\n"\n        (propertize\n          (format "Process `%s` has finished pushing to `%s`." proc git-remote-name)\n          \'face \'font-lock-warning-face)\n        "\\n"))\n    (throw \'exit nil)))\n\n(defun stage-commit-push-all ()\n"This function does the following:\n  * Save the current working buffer if it has been modified.\n  * Obtain the name of the selected branch in the current working buffer.\n  * Gather a list of all remotes associated with working directory Git project.\n  * Stage all -- `/usr/local/git/bin/git add .`\n  * Commit all -- `/usr/local/git/bin/git commit -m [git-commit-message]`\n  * Push to all remotes:  `/usr/local/git/bin/git push -v [remote] [current-branch]`"\n(interactive)\n  (when (buffer-modified-p)\n    (save-buffer))\n  (when (get-buffer git-status-buffer)\n    (with-current-buffer (get-buffer git-status-buffer)\n      (kill-local-variable \'git-remote-list)\n      (kill-local-variable \'git-branch-name)\n      (erase-buffer)))\n  (start-process\n    "current-branch"\n    git-status-buffer\n    "/usr/local/git/bin/git"\n    "rev-parse"\n    "--abbrev-ref"\n    "HEAD")\n  (set-process-filter (get-process "current-branch") \'git-branch-process-filter)\n  (set-process-sentinel\n    (get-process "current-branch")\n    (lambda (p e) (when (= 0 (process-exit-status p))\n      (set-process-sentinel\n        (start-process\n          "list-remotes"\n          git-status-buffer\n          "/usr/local/git/bin/git"\n          "remote"\n          "-v")\n        (lambda (p e) (when (= 0 (process-exit-status p))\n          (let* (\n              beg\n              end\n              git-remote-name)\n            (with-current-buffer (get-buffer git-status-buffer)\n              (goto-char (point-max))\n              (while (re-search-backward "\\(push\\)" nil t)\n                (beginning-of-line 1)\n                (setq beg (point))\n                (re-search-forward "\\t" nil t)\n                (setq end (- (point) 1))\n                (setq git-remote-name (buffer-substring-no-properties beg end))\n                (setq git-remote-list\n                  (append (cons git-remote-name git-remote-list)))) ))\n          (set-process-sentinel\n            (start-process\n              "stage-all"\n              git-status-buffer\n              "/usr/local/git/bin/git"\n              "add"\n              ".")\n            (lambda (p e) (when (= 0 (process-exit-status p))\n              (with-current-buffer (get-buffer git-status-buffer)\n                (goto-char (point-max))\n                (insert "\\n"))\n              (set-process-sentinel\n                (start-process\n                  "commit-all"\n                  git-status-buffer\n                  "/usr/local/git/bin/git"\n                  "commit"\n                  "-m"\n                  git-commit-message)\n                (lambda (p e) (when (= 0 (process-exit-status p))\n                  (mapcar (lambda (git-remote-name)\n                    (let ((proc\n                        (start-process\n                          "push-process"\n                          git-status-buffer\n                          "/usr/local/git/bin/git"\n                          "push"\n                          "-v"\n                          (format "%s" git-remote-name)\n                          (format "%s"\n                            (with-current-buffer (get-buffer git-status-buffer)\n                              git-branch-name)) )))\n                      (set-process-filter proc \'git-push-process-filter)\n                      (set-process-sentinel proc \'git-push-process-sentinel)\n                      (recursive-edit) ))\n                    (with-current-buffer (get-buffer git-status-buffer)\n                      git-remote-list) )\n                  (display-buffer (get-buffer git-status-buffer))\n                  (message (concat\n                    git-status-buffer\n                    " -- ["\n                    (propertize "d" \'face \'font-lock-warning-face)\n                    "]elete window | ["\n                    (propertize "k" \'face \'font-lock-warning-face)\n                    "]ill buffer + delete window | ["\n                    (propertize "n" \'face \'font-lock-warning-face)\n                    "]othing"))\n                  (let* (\n                      (git-window-options (read-char-exclusive))\n                      (target-window (get-buffer-window git-status-buffer)))\n                    (cond\n                      ((eq git-window-options ?d)\n                        (with-current-buffer (get-buffer git-status-buffer)\n                          (delete-window target-window)))\n                      ((eq git-window-options ?k)\n                        (with-current-buffer (get-buffer git-status-buffer)\n                          (delete-window target-window)\n                          (kill-buffer (get-buffer git-status-buffer))))\n                      ((eq git-window-options ?n)\n                        (message "Done!"))\n                      (t (message "You have exited the sub-function.")) ))\n                )))))))))))))\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n\n

第一稿(2014 年 4 月 19 日):\xc2\xa0 此功能需要预先安装 Magit。上述代码不需要安装Magit。

\n\n
(defun push-to-all-remotes ()\n"This function requires a pre-existing installation of Magit, and the function assumes\nthat the user has already staged and committed -- i.e., it only pushes to all remotes."\n(interactive)\n  (let* (beg end remote)\n    (when (get-buffer "*REMOTES*")\n      (with-current-buffer (get-buffer "*REMOTES*")\n        (erase-buffer)))\n    (set-process-sentinel\n      (start-process\n        "list-remotes"\n        "*REMOTES*"\n        "/usr/local/git/bin/git"\n        "remote"\n        "-v")\n      (lambda (p e) (when (= 0 (process-exit-status p))\n        (with-current-buffer (get-buffer "*REMOTES*")\n          (goto-char (point-max))\n          (while (re-search-backward "\\(push\\)" nil t)\n            (beginning-of-line 1)\n            (setq beg (point))\n            (re-search-forward "\\t" nil t)\n            (setq end (- (point) 1))\n            (setq remote (buffer-substring-no-properties beg end))\n            (magit-run-git-async\n              "push"\n              "-v"\n              remote\n              (magit-get-current-branch))) ))))\n    (display-buffer (get-buffer magit-process-buffer-name)) ))\n
Run Code Online (Sandbox Code Playgroud)\n