[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

git push/pull [was: support for bzr shelve/unshelve in vc-dir]

From: Fabian Ezequiel Gallina
Subject: git push/pull [was: support for bzr shelve/unshelve in vc-dir]
Date: Sat, 5 Dec 2009 05:09:39 -0300

I love vc, the thing is I always wanted the hability to do git
push/pull from vc directly. So today I decided to implement it.

Below is the code, I sure there is a lot of room for improvement since
I consider an Emacs LISP newbie myself, but I guess is a good starting
point for a better implementation.

As you might deduce from the code, when calling vc-git-push and
vc-git-pull the user will be prompted for a repository and a refspec,
both prompts feature sane default values (I hope you will agree on
that) and nice minibuffer-completion.

(defvar vc-git-push-pull-perform 'pull
  "The git command `vc-git--do-push-pull' should execute")

(defun vc-git--do-push-pull (&optional repository refspec)
  "Calls git push/pull with REPOSITORY and REFSPEC as parameters.

The decision of using push or pull is beign made depending the
value of `vc-git-push-pull-perform'."
   (let ((default-refspec (vc-git-working-revision buffer-file-name))
     (setq available-repos
           (let ((table (list ".")))
               (vc-git-command t nil nil "for-each-ref" "--format=%(refname)")
               (goto-char (point-min))
               (while (re-search-forward "^refs/remotes/\\([^/]+\\).*$" nil t)
                 (push (match-string 1) table)))
     (setq default-repo (nth 0 available-repos))
     (setq current-repo
           (completing-read (format "Repository (default %s): " default-repo)
                            available-repos nil 'confirm nil nil default-repo))
     (setq available-refspecs
           (let ((table (list "HEAD"))
               (vc-git-command t nil nil "for-each-ref" "--format=%(refname)")
               (if (equal current-repo ".")
                   (setq ref-regexp
                 (setq ref-regexp (concat "^refs/\\(remotes\\)/"
                                          (regexp-quote current-repo)
               (goto-char (point-min))
               (while (re-search-forward
                       nil t)
                 (push (match-string 2) table)))
     (setq current-refspec
           (completing-read (format "Branch (default %s): " default-refspec)
                            available-refspecs nil 'confirm nil nil
     (list current-repo current-refspec)))
  (let ((buffer (current-buffer))
        (command-buffer-name "*vc-push-pull*"))
     command-buffer-name 0 nil
     (prin1-to-string vc-git-push-pull-perform) repository refspec)
    (set-buffer command-buffer-name)
    (message (buffer-string))
    (set-buffer buffer)))

(defun vc-git-push (&optional repository refspec)
  "Update remote refs along with associated objects."
  (setq vc-git-push-pull-perform 'push)
  (call-interactively 'vc-git--do-push-pull))

(defun vc-git-pull (&optional repository refspec)
  "Fetch from and merge with another repository or a local branch."
  (setq vc-git-push-pull-perform 'pull)
  (call-interactively 'vc-git--do-push-pull))

Now, if this code is good enough. Can it be applied to vc-git.el? If
not can somebody provide a better alternative?

Also it will be really nice to define a menu/keybinding for these two

Best Regards,
Fabián E. Gallina

reply via email to

[Prev in Thread] Current Thread [Next in Thread]