org-attach-git.el 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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-attach)
  24. (require 'vc-git)
  25. (defcustom org-attach-git-annex-cutoff (* 32 1024)
  26. "If non-nil, files larger than this will be annexed instead of stored."
  27. :group 'org-attach
  28. :version "24.4"
  29. :package-version '(Org . "8.0")
  30. :type '(choice
  31. (const :tag "None" nil)
  32. (integer :tag "Bytes")))
  33. (defcustom org-attach-git-annex-auto-get 'ask
  34. "Confirmation preference for automatically getting annex files.
  35. If this is the symbol `ask', prompt using `y-or-n-p'.
  36. If t, always get. If nil, never get."
  37. :group 'org-attach
  38. :package-version '(Org . "9.0")
  39. :version "26.1"
  40. :type '(choice
  41. (const :tag "confirm with `y-or-n-p'" ask)
  42. (const :tag "always get from annex if necessary" t)
  43. (const :tag "never get from annex" nil)))
  44. (defcustom org-attach-git-dir 'default
  45. "Attachment directory with the Git repository to use.
  46. The default value is to use `org-attach-id-dir'. When set to
  47. `individual-repository', then the directory attached to the
  48. current node, if correctly initialized as a Git repository, will
  49. be used instead."
  50. :group 'org-attach
  51. :package-version '(Org . "9.5")
  52. :type '(choice
  53. (const :tag "Default" default)
  54. (const :tag "Individual repository" individual-repository)))
  55. (defun org-attach-git-use-annex ()
  56. "Return non-nil if git annex can be used."
  57. (let ((git-dir (vc-git-root
  58. (cond ((eq org-attach-git-dir 'default)
  59. (expand-file-name org-attach-id-dir))
  60. ((eq org-attach-git-dir 'individual-repository)
  61. (org-attach-dir))))))
  62. (and org-attach-git-annex-cutoff
  63. (or (file-exists-p (expand-file-name "annex" git-dir))
  64. (file-exists-p (expand-file-name ".git/annex" git-dir))))))
  65. (defun org-attach-git-annex-get-maybe (path)
  66. "Call git annex get PATH (via shell) if using git annex.
  67. Signals an error if the file content is not available and it was not retrieved."
  68. (let* ((default-directory
  69. (cond ((eq org-attach-git-dir 'default)
  70. (expand-file-name org-attach-id-dir))
  71. ((eq org-attach-git-dir 'individual-repository)
  72. (org-attach-dir))))
  73. (path-relative (file-relative-name path)))
  74. (when (and (org-attach-git-use-annex)
  75. (not
  76. (string-equal
  77. "found"
  78. (shell-command-to-string
  79. (format "git annex find --format=found --in=here %s"
  80. (shell-quote-argument path-relative))))))
  81. (let ((should-get
  82. (if (eq org-attach-git-annex-auto-get 'ask)
  83. (y-or-n-p (format "Run git annex get %s? " path-relative))
  84. org-attach-git-annex-auto-get)))
  85. (unless should-get
  86. (error "File %s stored in git annex but unavailable" path))
  87. (message "Running git annex get \"%s\"." path-relative)
  88. (call-process "git" nil nil nil "annex" "get" path-relative)))))
  89. (defun org-attach-git-commit (&optional _)
  90. "Commit changes to git if `org-attach-id-dir' is properly initialized.
  91. This checks for the existence of a \".git\" directory in that directory.
  92. Takes an unused optional argument for the sake of being compatible
  93. with hook `org-attach-after-change-hook'."
  94. (let* ((dir (cond ((eq org-attach-git-dir 'default)
  95. (expand-file-name org-attach-id-dir))
  96. ((eq org-attach-git-dir 'individual-repository)
  97. (org-attach-dir))))
  98. (git-dir (vc-git-root dir))
  99. (use-annex (org-attach-git-use-annex))
  100. (changes 0))
  101. (when (and git-dir (executable-find "git"))
  102. (with-temp-buffer
  103. (cd dir)
  104. (dolist (new-or-modified
  105. (split-string
  106. (shell-command-to-string
  107. "git ls-files -zmo --exclude-standard") "\0" t))
  108. (if (and use-annex
  109. (>= (file-attribute-size (file-attributes new-or-modified))
  110. org-attach-git-annex-cutoff))
  111. (call-process "git" nil nil nil "annex" "add" new-or-modified)
  112. (call-process "git" nil nil nil "add" new-or-modified))
  113. (cl-incf changes))
  114. (dolist (deleted
  115. (split-string
  116. (shell-command-to-string "git ls-files -z --deleted") "\0" t))
  117. (call-process "git" nil nil nil "rm" deleted)
  118. (cl-incf changes))
  119. (when (> changes 0)
  120. (shell-command "git commit -m 'Synchronized attachments'"))))))
  121. (add-hook 'org-attach-after-change-hook 'org-attach-git-commit)
  122. (add-hook 'org-attach-open-hook 'org-attach-git-annex-get-maybe)
  123. (provide 'org-attach-git)
  124. ;;; org-attach-git.el ends here