A simple caching mechanism to ease the `magit-get-svn-ref-info' pain.

* magit.el: Every call to `magit-get-svn-ref-info', post the first, retrieves a cached list.
This commit is contained in:
Phil Jackson 2009-10-28 20:37:35 +00:00
parent 3bfe230051
commit 8ea39540d9
2 changed files with 52 additions and 44 deletions

View file

@ -1935,17 +1935,17 @@ in log buffer."
"log" "--pretty=format:* %H %s" "log" "--pretty=format:* %H %s"
(format "%s/%s..HEAD" remote branch))) (format "%s/%s..HEAD" remote branch)))
(defun magit-insert-unpulled-svn-commits () (defun magit-insert-unpulled-svn-commits (&optional use-cache)
(magit-git-section 'svn-unpulled (magit-git-section 'svn-unpulled
"Unpulled commits (SVN):" 'magit-wash-log "Unpulled commits (SVN):" 'magit-wash-log
"log" "--pretty=format:* %H %s" "log" "--pretty=format:* %H %s"
(format "HEAD..%s" (magit-get-svn-ref)))) (format "HEAD..%s" (magit-get-svn-ref use-cache))))
(defun magit-insert-unpushed-svn-commits () (defun magit-insert-unpushed-svn-commits (&optional use-cache)
(magit-git-section 'svn-unpushed (magit-git-section 'svn-unpushed
"Unpushed commits (SVN):" 'magit-wash-log "Unpushed commits (SVN):" 'magit-wash-log
"log" "--pretty=format:* %H %s" "log" "--pretty=format:* %H %s"
(format "%s..HEAD" (magit-get-svn-ref)))) (format "%s..HEAD" (magit-get-svn-ref use-cache))))
;;; Status ;;; Status
@ -1954,19 +1954,19 @@ in log buffer."
(magit-with-section 'status nil (magit-with-section 'status nil
(let* ((branch (magit-get-current-branch)) (let* ((branch (magit-get-current-branch))
(remote (and branch (magit-get "branch" branch "remote"))) (remote (and branch (magit-get "branch" branch "remote")))
(svn-enabled (magit-svn-enabled)) (svn-enabled (magit-svn-enabled))
(head (magit-git-string (head (magit-git-string
"log" "--max-count=1" "--abbrev-commit" "--pretty=oneline")) "log" "--max-count=1" "--abbrev-commit" "--pretty=oneline"))
(no-commit (not head))) (no-commit (not head)))
(if remote (if remote
(insert (format "Remote: %s %s\n" (insert (format "Remote: %s %s\n"
remote (magit-get "remote" remote "url")))) remote (magit-get "remote" remote "url"))))
(insert (format "Local: %s %s\n" (insert (format "Local: %s %s\n"
(propertize (or branch "(detached)") (propertize (or branch "(detached)")
'face 'magit-branch) 'face 'magit-branch)
(abbreviate-file-name default-directory))) (abbreviate-file-name default-directory)))
(insert (format "Head: %s\n" (insert (format "Head: %s\n"
(if no-commit "nothing commited (yet)" head))) (if no-commit "nothing commited (yet)" head)))
(let ((merge-heads (magit-file-lines ".git/MERGE_HEAD"))) (let ((merge-heads (magit-file-lines ".git/MERGE_HEAD")))
(if merge-heads (if merge-heads
(insert (format "Merging: %s\n" (insert (format "Merging: %s\n"
@ -1977,7 +1977,7 @@ in log buffer."
(if rebase (if rebase
(insert (apply 'format "Rebasing: %s (%s of %s)\n" rebase)))) (insert (apply 'format "Rebasing: %s (%s of %s)\n" rebase))))
(insert "\n") (insert "\n")
(magit-git-exit-code "update-index" "--refresh") (magit-git-exit-code "update-index" "--refresh")
(magit-insert-untracked-files) (magit-insert-untracked-files)
(magit-insert-stashes) (magit-insert-stashes)
(magit-insert-topics) (magit-insert-topics)
@ -1985,8 +1985,8 @@ in log buffer."
(magit-insert-pending-commits) (magit-insert-pending-commits)
(when remote (when remote
(magit-insert-unpulled-commits remote branch)) (magit-insert-unpulled-commits remote branch))
(when svn-enabled (when svn-enabled
(magit-insert-unpulled-svn-commits)) (magit-insert-unpulled-svn-commits t))
(let ((staged (or no-commit (magit-anything-staged-p)))) (let ((staged (or no-commit (magit-anything-staged-p))))
(magit-insert-unstaged-changes (magit-insert-unstaged-changes
(if staged "Unstaged changes:" "Changes:")) (if staged "Unstaged changes:" "Changes:"))
@ -1994,8 +1994,8 @@ in log buffer."
(magit-insert-staged-changes no-commit))) (magit-insert-staged-changes no-commit)))
(when remote (when remote
(magit-insert-unpushed-commits remote branch)) (magit-insert-unpushed-commits remote branch))
(when svn-enabled (when svn-enabled
(magit-insert-unpushed-svn-commits)))))) (magit-insert-unpushed-svn-commits t))))))
(defun magit-init (dir) (defun magit-init (dir)
"Initialize git repository in specified directory" "Initialize git repository in specified directory"
@ -2190,40 +2190,48 @@ in log buffer."
(magit-run-git-async "svn" "dcommit")) (magit-run-git-async "svn" "dcommit"))
(defun magit-svn-enabled () (defun magit-svn-enabled ()
(not (null (magit-get "svn-remote" "svn" "fetch")))) (not (null (magit-get-svn-ref-info))))
(defun magit-get-svn-ref-info () (defvar magit-get-svn-ref-info-cache nil
"As `magit-get-svn-ref-info' might be considered a quite
expensive operation a cache is taken so that `magit-status'
doesn't repeatedly call it.")
(defun magit-get-svn-ref-info (&optional use-cache)
"Gather details about the current git-svn repository (nil if "Gather details about the current git-svn repository (nil if
there isn't one). Keys of the alist are ref-path, trunk-ref-name there isn't one). Keys of the alist are ref-path, trunk-ref-name
and local-ref-name." and local-ref-name. If USE-CACHE is non-nil then return the value
(let* ((fetch (magit-get "svn-remote" "svn" "fetch")) of `magit-get-svn-ref-info-cache'."
(branches (magit-get "svn-remote" "svn" "fetch"))) (if use-cache
(when fetch magit-get-svn-ref-info-cache
(list (let* ((fetch (magit-get "svn-remote" "svn" "fetch"))
(cons 'ref-path (file-name-directory (branches (magit-get "svn-remote" "svn" "fetch"))
(cadr (split-string fetch ":")))) (url)
(cons 'trunk-ref-name (file-name-nondirectory (revision))
(cadr (split-string fetch ":")))) (when fetch
;; get the local ref from the log. This is actually (setq magit-get-svn-ref-info-cache
;; the way that git-svn does it. (list
(cons 'local-ref-name (cons 'ref-path (file-name-directory
(with-temp-buffer (cadr (split-string fetch ":"))))
(insert (magit-git-string "log" "--first-parent")) (cons 'trunk-ref-name (file-name-nondirectory
(goto-char (point-min)) (cadr (split-string fetch ":"))))
(when (re-search-forward "git-svn-id: .+/\\(.+?\\)@" nil t) ;; get the local ref from the log. This is actually
(match-string 1)))))))) ;; the way that git-svn does it.
(cons 'local-ref-name
(with-temp-buffer
(insert (magit-git-string "log" "--first-parent"))
(goto-char (point-min))
(when (re-search-forward "git-svn-id: \\(.+/\\(.+?\\)\\)@\\([0-9]+\\)" nil t)
(setq url (match-string 1)
revision (match-string 3))
(match-string 2))))
(cons 'revision revision)
(cons 'url url)))))))
(defun magit-get-svn-trunk-ref () (defun magit-get-svn-ref (&optional use-cache)
"Get the best guess remote trunk ref for the current git-svn
based branch."
(let ((info (magit-get-svn-ref-info)))
(concat (cdr (assoc 'ref-path info))
(cdr (assoc 'trunk-ref-name info)))))
(defun magit-get-svn-ref ()
"Get the best guess remote ref for the current git-svn based "Get the best guess remote ref for the current git-svn based
branch." branch."
(let ((info (magit-get-svn-ref-info))) (let ((info (magit-get-svn-ref-info use-cache)))
(concat (cdr (assoc 'ref-path info)) (concat (cdr (assoc 'ref-path info))
(cdr (assoc 'local-ref-name info))))) (cdr (assoc 'local-ref-name info)))))

View file

@ -7,8 +7,8 @@
(stub magit-get => "someproject/trunk:refs/remotes/hello/trunk") (stub magit-get => "someproject/trunk:refs/remotes/hello/trunk")
(magit-get-svn-ref-info))) (magit-get-svn-ref-info)))
'(trunk-ref-name . "trunk") '(trunk-ref-name . "trunk")
"correct refpath from `magit-get-svn-ref-info'") "correct trunk-ref-name from `magit-get-svn-ref-info'")
(equal (assoc 'refpath (equal (assoc 'ref-path
(with-mock (with-mock
(stub magit-get => "someproject/trunk:refs/remotes/hello/trunk") (stub magit-get => "someproject/trunk:refs/remotes/hello/trunk")
(magit-get-svn-ref-info))) (magit-get-svn-ref-info)))