org-git-link.el 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. ;;; org-git-link.el --- Provide org links to specific file version
  2. ;; Copyright (C) 2009-2012 Reimar Finken
  3. ;; Author: Reimar Finken <reimar.finken@gmx.de>
  4. ;; Keywords: files, calendar, hypermedia
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distaributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;; `org-git-link.el' defines two new link types. The `git' link
  17. ;; type is meant to be used in the typical scenario and mimics the
  18. ;; `file' link syntax as closely as possible. The `gitbare' link
  19. ;; type exists mostly for debugging reasons, but also allows e.g.
  20. ;; linking to files in a bare git repository for the experts.
  21. ;; * User friendy form
  22. ;; [[git:/path/to/file::searchstring]]
  23. ;; This form is the familiar from normal org file links
  24. ;; including search options. However, its use is
  25. ;; restricted to files in a working directory and does not
  26. ;; handle bare repositories on purpose (see the bare form for
  27. ;; that).
  28. ;; The search string references a commit (a tree-ish in Git
  29. ;; terminology). The two most useful types of search strings are
  30. ;; - A symbolic ref name, usually a branch or tag name (e.g.
  31. ;; master or nobelprize).
  32. ;; - A ref followed by the suffix @ with a date specification
  33. ;; enclosed in a brace pair (e.g. {yesterday}, {1 month 2
  34. ;; weeks 3 days 1 hour 1 second ago} or {1979-02-26 18:30:00})
  35. ;; to specify the value of the ref at a prior point in time
  36. ;;
  37. ;; * Bare git form
  38. ;; [[gitbare:$GIT_DIR::$OBJECT]]
  39. ;;
  40. ;; This is the more bare metal version, which gives the user most
  41. ;; control. It directly translates to the git command
  42. ;; git --no-pager --git-dir=$GIT_DIR show $OBJECT
  43. ;; Using this version one can also view files from a bare git
  44. ;; repository. For detailed information on how to specify an
  45. ;; object, see the man page of `git-rev-parse' (section
  46. ;; SPECIFYING REVISIONS). A specific blob (file) can be
  47. ;; specified by a suffix clolon (:) followed by a path.
  48. ;;; Code:
  49. (require 'org)
  50. (defcustom org-git-program "git"
  51. "Name of the git executable used to follow git links."
  52. :type '(string)
  53. :group 'org)
  54. ;; org link functions
  55. ;; bare git link
  56. (org-add-link-type "gitbare" 'org-gitbare-open)
  57. (defun org-gitbare-open (str)
  58. (let* ((strlist (org-git-split-string str))
  59. (gitdir (first strlist))
  60. (object (second strlist)))
  61. (org-git-open-file-internal gitdir object)))
  62. (defun org-git-open-file-internal (gitdir object)
  63. (let* ((sha (org-git-blob-sha gitdir object))
  64. (tmpdir (concat temporary-file-directory "org-git-" sha))
  65. (filename (org-git-link-filename object))
  66. (tmpfile (expand-file-name filename tmpdir)))
  67. (unless (file-readable-p tmpfile)
  68. (make-directory tmpdir)
  69. (with-temp-file tmpfile
  70. (org-git-show gitdir object (current-buffer))))
  71. (org-open-file tmpfile)
  72. (set-buffer (get-file-buffer tmpfile))
  73. (setq buffer-read-only t)))
  74. ;; user friendly link
  75. (org-add-link-type "git" 'org-git-open)
  76. (defun org-git-open (str)
  77. (let* ((strlist (org-git-split-string str))
  78. (filepath (first strlist))
  79. (commit (second strlist))
  80. (dirlist (org-git-find-gitdir (file-truename filepath)))
  81. (gitdir (first dirlist))
  82. (relpath (second dirlist)))
  83. (org-git-open-file-internal gitdir (concat commit ":" relpath))))
  84. ;; Utility functions (file names etc)
  85. (defun org-git-split-dirpath (dirpath)
  86. "Given a directory name, return '(dirname basname)"
  87. (let ((dirname (file-name-directory (directory-file-name dirpath)))
  88. (basename (file-name-nondirectory (directory-file-name dirpath))))
  89. (list dirname basename)))
  90. ;; finding the git directory
  91. (defun org-git-find-gitdir (path)
  92. "Given a file (not necessarily existing) file path, return the
  93. a pair (gitdir relpath), where gitdir is the path to the first
  94. .git subdirectory found updstream and relpath is the rest of
  95. the path. Example: (org-git-find-gitdir
  96. \"~/gitrepos/foo/bar.txt\") returns
  97. '(\"/home/user/gitrepos/.git\" \"foo/bar.txt\"). When not in a git repository, return nil."
  98. (let ((dir (file-name-directory path))
  99. (relpath (file-name-nondirectory path)))
  100. (catch 'toplevel
  101. (while (not (file-exists-p (expand-file-name ".git" dir)))
  102. (let ((dirlist (org-git-split-dirpath dir)))
  103. (when (string= (second dirlist) "") ; at top level
  104. (throw 'toplevel nil))
  105. (setq dir (first dirlist)
  106. relpath (concat (file-name-as-directory (second dirlist)) relpath))))
  107. (list (expand-file-name ".git" dir) relpath))))
  108. (if (featurep 'xemacs)
  109. (defalias 'org-git-gitrepos-p 'org-git-find-gitdir)
  110. (defalias 'org-git-gitrepos-p 'org-git-find-gitdir
  111. "Return non-nil if path is in git repository"))
  112. ;; splitting the link string
  113. ;; Both link open functions are called with a string of
  114. ;; consisting of two parts separated by a double colon (::).
  115. (defun org-git-split-string (str)
  116. "Given a string of the form \"str1::str2\", return a list of
  117. two substrings \'(\"str1\" \"str2\"). If the double colon is mising, take str2 to be the empty string."
  118. (let ((strlist (split-string str "::")))
  119. (cond ((= 1 (length strlist))
  120. (list (car strlist) ""))
  121. ((= 2 (length strlist))
  122. strlist)
  123. (t (error "org-git-split-string: only one :: allowed: %s" str)))))
  124. ;; finding the file name part of a commit
  125. (defun org-git-link-filename (str)
  126. "Given an object description (see the man page of
  127. git-rev-parse), return the nondirectory part of the referenced
  128. filename, if it can be extracted. Otherwise, return a valid
  129. filename."
  130. (let* ((match (and (string-match "[^:]+$" str)
  131. (match-string 0 str)))
  132. (filename (and match (file-name-nondirectory match)))) ;extract the final part without slash
  133. filename))
  134. ;; creating a link
  135. (defun org-git-create-searchstring (branch timestring)
  136. (concat branch "@{" timestring "}"))
  137. (defun org-git-create-git-link (file)
  138. "Create git link part to file at specific time"
  139. (interactive "FFile: ")
  140. (let* ((gitdir (first (org-git-find-gitdir (file-truename file))))
  141. (branchname (org-git-get-current-branch gitdir))
  142. (timestring (format-time-string "%Y-%m-%d" (current-time))))
  143. (org-make-link "git:" file "::" (org-git-create-searchstring branchname timestring))))
  144. (defun org-git-store-link ()
  145. "Store git link to current file."
  146. (when (buffer-file-name)
  147. (let ((file (abbreviate-file-name (buffer-file-name))))
  148. (when (org-git-gitrepos-p file)
  149. (org-store-link-props
  150. :type "git"
  151. :link (org-git-create-git-link file))))))
  152. (add-hook 'org-store-link-functions 'org-git-store-link)
  153. (defun org-git-insert-link-interactively (file searchstring &optional description)
  154. (interactive "FFile: \nsSearch string: \nsDescription: ")
  155. (insert (org-make-link-string (org-make-link "git:" file "::" searchstring) description)))
  156. ;; Calling git
  157. (defun org-git-show (gitdir object buffer)
  158. "Show the output of git --git-dir=gitdir show object in buffer."
  159. (unless
  160. (zerop (call-process org-git-program nil buffer nil
  161. "--no-pager" (concat "--git-dir=" gitdir) "show" object))
  162. (error "git error: %s " (save-excursion (set-buffer buffer)
  163. (buffer-string)))))
  164. (defun org-git-blob-sha (gitdir object)
  165. "Return sha of the referenced object"
  166. (with-temp-buffer
  167. (if (zerop (call-process org-git-program nil t nil
  168. "--no-pager" (concat "--git-dir=" gitdir) "rev-parse" object))
  169. (buffer-substring (point-min) (1- (point-max))) ; to strip off final newline
  170. (error "git error: %s " (buffer-string)))))
  171. (defun org-git-get-current-branch (gitdir)
  172. "Return the name of the current branch."
  173. (with-temp-buffer
  174. (if (not (zerop (call-process org-git-program nil t nil
  175. "--no-pager" (concat "--git-dir=" gitdir) "symbolic-ref" "-q" "HEAD")))
  176. (error "git error: %s " (buffer-string))
  177. (goto-char (point-min))
  178. (if (looking-at "^refs/heads/") ; 11 characters
  179. (buffer-substring 12 (1- (point-max))))))) ; to strip off final newline
  180. (provide 'org-git-link)
  181. ;;; org-git-link.el ends here