org-attach-git.el 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. ;;; org-attach-git.el --- Automatic git commit extension to org-attach -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2019-2022 Free Software Foundation, Inc.
  3. ;; Original Author: John Wiegley <johnw@newartisans.com>
  4. ;; Restructurer: Gustav Wikström <gustav@whil.se>
  5. ;; Keywords: org data git
  6. ;; This file is part of GNU Emacs.
  7. ;;
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; An extension to org-attach. If `org-attach-id-dir' is initialized
  20. ;; as a Git repository, then `org-attach-git' will automatically commit
  21. ;; changes when it sees them. Requires git-annex.
  22. ;;; Code:
  23. (require 'org-macs)
  24. (org-assert-version)
  25. (require 'org-attach)
  26. (require 'vc-git)
  27. (defcustom org-attach-git-annex-cutoff (* 32 1024)
  28. "If non-nil, files larger than this will be annexed instead of stored."
  29. :group 'org-attach
  30. :version "24.4"
  31. :package-version '(Org . "8.0")
  32. :type '(choice
  33. (const :tag "None" nil)
  34. (integer :tag "Bytes")))
  35. (defcustom org-attach-git-annex-auto-get 'ask
  36. "Confirmation preference for automatically getting annex files.
  37. If this is the symbol `ask', prompt using `y-or-n-p'.
  38. If t, always get. If nil, never get."
  39. :group 'org-attach
  40. :package-version '(Org . "9.0")
  41. :version "26.1"
  42. :type '(choice
  43. (const :tag "confirm with `y-or-n-p'" ask)
  44. (const :tag "always get from annex if necessary" t)
  45. (const :tag "never get from annex" nil)))
  46. (defcustom org-attach-git-dir 'default
  47. "Attachment directory with the Git repository to use.
  48. The default value is to use `org-attach-id-dir'. When set to
  49. `individual-repository', then the directory attached to the
  50. current node, if correctly initialized as a Git repository, will
  51. be used instead."
  52. :group 'org-attach
  53. :package-version '(Org . "9.5")
  54. :type '(choice
  55. (const :tag "Default" default)
  56. (const :tag "Individual repository" individual-repository)))
  57. (defun org-attach-git-use-annex ()
  58. "Return non-nil if git annex can be used."
  59. (let ((git-dir (vc-git-root
  60. (cond ((eq org-attach-git-dir 'default)
  61. (expand-file-name org-attach-id-dir))
  62. ((eq org-attach-git-dir 'individual-repository)
  63. (org-attach-dir))))))
  64. (and org-attach-git-annex-cutoff
  65. (or (file-exists-p (expand-file-name "annex" git-dir))
  66. (file-exists-p (expand-file-name ".git/annex" git-dir))))))
  67. (defun org-attach-git-annex-get-maybe (path)
  68. "Call git annex get PATH (via shell) if using git annex.
  69. Signals an error if the file content is not available and it was not retrieved."
  70. (let* ((default-directory
  71. (cond ((eq org-attach-git-dir 'default)
  72. (expand-file-name org-attach-id-dir))
  73. ((eq org-attach-git-dir 'individual-repository)
  74. (org-attach-dir))))
  75. (path-relative (file-relative-name path)))
  76. (when (and (org-attach-git-use-annex)
  77. (not
  78. (string-equal
  79. "found"
  80. (shell-command-to-string
  81. (format "git annex find --format=found --in=here %s"
  82. (shell-quote-argument path-relative))))))
  83. (let ((should-get
  84. (if (eq org-attach-git-annex-auto-get 'ask)
  85. (y-or-n-p (format "Run git annex get %s? " path-relative))
  86. org-attach-git-annex-auto-get)))
  87. (unless should-get
  88. (error "File %s stored in git annex but unavailable" path))
  89. (message "Running git annex get \"%s\"." path-relative)
  90. (call-process "git" nil nil nil "annex" "get" path-relative)))))
  91. (defun org-attach-git-commit (&optional _)
  92. "Commit changes to git if `org-attach-id-dir' is properly initialized.
  93. This checks for the existence of a \".git\" directory in that directory.
  94. Takes an unused optional argument for the sake of being compatible
  95. with hook `org-attach-after-change-hook'."
  96. (let* ((dir (cond ((eq org-attach-git-dir 'default)
  97. (expand-file-name org-attach-id-dir))
  98. ((eq org-attach-git-dir 'individual-repository)
  99. (org-attach-dir))))
  100. (git-dir (vc-git-root dir))
  101. (use-annex (org-attach-git-use-annex))
  102. (changes 0))
  103. (when (and git-dir (executable-find "git"))
  104. (with-temp-buffer
  105. (cd dir)
  106. (dolist (new-or-modified
  107. (split-string
  108. (shell-command-to-string
  109. "git ls-files -zmo --exclude-standard") "\0" t))
  110. (if (and use-annex
  111. (>= (file-attribute-size (file-attributes new-or-modified))
  112. org-attach-git-annex-cutoff))
  113. (call-process "git" nil nil nil "annex" "add" new-or-modified)
  114. (call-process "git" nil nil nil "add" new-or-modified))
  115. (cl-incf changes))
  116. (dolist (deleted
  117. (split-string
  118. (shell-command-to-string "git ls-files -z --deleted") "\0" t))
  119. (call-process "git" nil nil nil "rm" deleted)
  120. (cl-incf changes))
  121. (when (> changes 0)
  122. (shell-command "git commit -m 'Synchronized attachments'"))))))
  123. (add-hook 'org-attach-after-change-hook 'org-attach-git-commit)
  124. (add-hook 'org-attach-open-hook 'org-attach-git-annex-get-maybe)
  125. (provide 'org-attach-git)
  126. ;;; org-attach-git.el ends here