Emacs/Magit -- 如何在Github上创建和删除仓库

3

我在使用Magit的第五步和第七步创建新仓库时遇到了问题。如果存在的话,请问它们对应的交互式函数是什么?

如果没有交互式函数,我想我需要编写自己的shell命令函数,除非当然有人愿意先尝试一下。 :)


创建 -- 命令行示例

1.  $  touch README.md

2.  $  /usr/local/git/bin/git init

3.  $  /usr/local/git/bin/git add .

4.  $  /usr/local/git/bin/git commit -m "First commit."

5.  $  curl -u 'USERNAME' https://api.github.com/user/repos -d '{"name":"REPO-NAME"}'

6.  $  Enter password:  PASSWORD

7.  $  /usr/local/git/bin/git remote add origin git@github.com:USERNAME/REPO-NAME.git

8.  $  /usr/local/git/bin/git push origin master

注意:第5步和第6步可以结合在一起(如果需要),如下所示:curl -u '用户名':'密码' https://api.github.com/user/repos -d '{"name":"仓库名称"}'


DELETE -- 命令行说明

注意:用户令牌 必须 具有 delete_repo 权限。请参见 delete-remote-repo 的文档字符串。

curl -X DELETE -H 'Authorization: token xxx' https://api.github.com/repos/USERNAME/REPO-NAME

编辑(2014年4月13日):初步草稿。

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; https://dev59.com/0H7aa4cB1Zd3GeqPtKqX

(defvar git-username nil
 "The username of the Github account.")
(make-variable-buffer-local 'git-username)

(defvar git-password nil
 "The password of the Github account.")
(make-variable-buffer-local 'git-password)

(defvar git-token nil
 "The applicable token of the Github account.")
(make-variable-buffer-local 'git-token)

(defvar repo-name nil
 "The name of the Github repository.")
(make-variable-buffer-local 'repo-name)

(defun create-remote-repo ()
"Execute this function from the root directory of the repo -- e.g., in dired-mode."
(interactive)
  (setq git-username (read-string "Name of User:  "))
  (setq git-password (read-string "Password of User:  "))
  (setq repo-name (read-string "Name of Repository:  "))
  (set-process-sentinel
    (start-process
      "repo-process"
      "*REPO*"
      "/usr/bin/touch"
      "README.md")
    (lambda (p e) (when (= 0 (process-exit-status p))
      (set-process-sentinel
        (start-process
          "repo-process"
          "*REPO*"
          "/usr/local/git/bin/git"
          "init")
        (lambda (p e) (when (= 0 (process-exit-status p))
          (set-process-sentinel 
            (start-process
              "repo-process"
              "*REPO*"
              "/usr/local/git/bin/git"
              "add"
              ".")
            (lambda (p e) (when (= 0 (process-exit-status p))
              (set-process-sentinel 
                (start-process
                  "repo-process"
                  "*REPO*"
                  "/usr/local/git/bin/git"
                  "commit"
                  "-m"
                  "\"First commit.\"")
                (lambda (p e) (when (= 0 (process-exit-status p))
                  (set-process-sentinel
                    (start-process
                      "repo-process"
                      "*REPO*"
                      "/usr/bin/curl"
                      "-u"
                      (concat
                        git-username
                        ":"
                        git-password)
                      "https://api.github.com/user/repos"
                      "-d"
                      (concat
                        "\{\"name\":\""
                        repo-name
                        "\"\}"))
                    (lambda (p e) (when (= 0 (process-exit-status p))
                      (set-process-sentinel 
                        (start-process
                          "repo-process"
                          "*REPO*"
                          "/usr/local/git/bin/git"
                          "remote"
                          "add"
                          "origin"
                          (concat
                            "git@github.com:"
                            git-username
                            "/"
                            repo-name
                            ".git"))
                        (lambda (p e) (when (= 0 (process-exit-status p))
                          (set-process-sentinel 
                            (start-process
                              "repo-process"
                              "*REPO*"
                              "/usr/local/git/bin/git"
                              "push"
                              "origin"
                              "master")
                            (lambda (p e) (when (= 0 (process-exit-status p))
                              (if (eq major-mode 'dired-mode)
                                (revert-buffer))
                              (display-buffer (get-buffer "*REPO*") nil)
                              (message
                                "Repository `%s` has been successfully created!"
                                repo-name) ))))))))))))))))))))))

(defun delete-remote-repo ()
"To delete a repository, the user must have token `delete_repo` authorization.
Visit your `Account Settings` | `Applications`.  Either edit a current token
or generate a new token with `delete_repo` authorization, and write down the
token in a safe place because it is only displayed one time."
(interactive)
  (setq git-username (read-string "Name of User:  "))
  (setq repo-name (read-string "Name of Repository:  "))
  (setq git-token (read-string "Token (with `delete_repo` authority):  "))
  (set-process-sentinel
    (start-process "delete-repo-process" "*DELETE-REPO*"
      "/usr/bin/curl"
      "-X"
      "DELETE"
      "-H"
      (concat
        "Authorization: token "
        git-token
        )
      (concat
        "https://api.github.com/repos/"
        git-username
        "/"
        repo-name))
    (lambda (p e) (when (= 0 (process-exit-status p))
      (display-buffer (get-buffer "*DELETE-REPO*") nil)
        (if (with-current-buffer (get-buffer "*DELETE-REPO*")
              (equal (buffer-size) 0))
          (progn
            (with-current-buffer (get-buffer "*DELETE-REPO*")
              (insert "It looks like everything worked okay."))
            (message "Repository `%s` has been successfully deleted!" repo-name))
          (message "OOOPS!!!  Something went wrong in the deletion process!") )))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

不错,但我在这里的抱怨是git(程序和协议)比github(网站)更通用。 magit-mode支持前者;将其应用于后者很方便,但有些受限制。 - Dirk Eddelbuettel
1个回答

3

Magit没有提供任何与Github交互的命令。你需要编写自己的命令,使用call-processcurl,或者使用包装了Github API的gh.el。

要添加新的远程仓库,在Magit状态缓冲区中输入M a


谢谢 - 我已经编写了一个名为create-repo的函数,它可以在dired-mode中从所选根存储库目录中工作 - 新函数已发布为我最初问题的编辑。 - lawlist
@lawlist 你真的应该考虑重构一下代码,而且最好使用VC代替调用Git。除了创建Github仓库之外,Emacs可以通过vc.el来完成所有操作。 - user355252

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接