vermiculus / magithub

**DEPRECATED - please use Forge instead!** -- Magit-based interfaces to GitHub
GNU General Public License v3.0
579 stars 63 forks source link

Making magithub-clone non-blocking #338

Closed cmccloud closed 6 years ago

cmccloud commented 6 years ago

Howdy,

I was taking a look at magithub-clone after cloning a largish repo saw emacs unresponsive for a couple minutes. I've refactored this and it seems to be working well, but I was curious as to your thoughts. If it looks good I'll probably take a look to see if there are any other calls that might benefit from a similar treatment.

Let me know if you have any suggestions and I'll make a PR.

Thanks.

(defun magithub-clone (repo dir)
  "Clone REPO.
Banned inside existing GitHub repositories if
`magithub-clone-default-directory' is nil.

See also `magithub-preferred-remote-method'."
  (interactive (let* ((repo (magithub-clone--get-repo))
                      (repo (or (magithub-request
                                 (ghubp-get-repos-owner-repo repo))
                                (let-alist repo
                                  (user-error "Repository %s/%s does not exist"
                                              .owner.login .name))))
                      (name (alist-get 'name repo))
                      (dirname (read-directory-name
                                "Destination: "
                                magithub-clone-default-directory
                                name nil name)))
                 (list repo dirname)))
  ;; Argument validation
  (unless (called-interactively-p 'any)
    (unless (setq repo (magithub-request
                        (ghubp-get-repos-owner-repo repo)))
      (let-alist repo
        (user-error "Repository %s/%s does not exist"
                    .owner.login .name))))
  (let ((parent (file-name-directory dir)))
    (unless (file-exists-p parent)
      (when (magithub-confirm 'clone-create-directory parent)
        (mkdir parent t))))
  (unless (file-writable-p dir)
    (user-error "%s is not writable" dir))

  (let-alist repo
    (when (magithub-confirm-no-error 'clone .full_name dir)
      (let (set-upstream set-proxy)
        (setq set-upstream
              (and .fork (magithub-confirm-no-error
              'clone-fork-set-upstream-to-parent
                          .parent.full_name))
              set-proxy
              (and set-upstream (magithub-confirm-no-error
                 'clone-fork-set-proxy-to-upstream)))
        (condition-case _
            (let ((default-directory dir)
                  (magit-clone-set-remote.pushDefault t))
              (mkdir dir t)
              (magit-clone (magithub-repo--clone-url repo) dir)
              ;; My changes begin here.
              (add-function :after
                            (process-sentinel magit-this-process)
                            (lambda (process event)
                              (unless (process-live-p process)
                                (when set-upstream
                                  (let ((upstream "upstream"))
                                    (when set-proxy (magit-set upstream "magithub.proxy"))
                                    (magit-remote-add upstream (magithub-repo--clone-url .parent))
                                    (magit-set-branch*merge/remote (magit-get-current-branch)
                                           upstream)))
                                (message "magithub-clone complete."))))))))))
vermiculus commented 6 years ago

Merged #340