org-git-link.el 8.9 KB

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