org-attach-git.el 5.5 KB

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