org-attach-git.el 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. ;;; org-attach-git.el --- Automatic git commit extension to org-attach -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2019-2021 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. (defun org-attach-git-use-annex ()
  44. "Return non-nil if git annex can be used."
  45. (let ((git-dir (vc-git-root (expand-file-name org-attach-id-dir))))
  46. (and org-attach-git-annex-cutoff
  47. (or (file-exists-p (expand-file-name "annex" git-dir))
  48. (file-exists-p (expand-file-name ".git/annex" git-dir))))))
  49. (defun org-attach-git-annex-get-maybe (path)
  50. "Call git annex get PATH (via shell) if using git annex.
  51. Signals an error if the file content is not available and it was not retrieved."
  52. (let* ((default-directory (expand-file-name org-attach-id-dir))
  53. (path-relative (file-relative-name path)))
  54. (when (and (org-attach-git-use-annex)
  55. (not
  56. (string-equal
  57. "found"
  58. (shell-command-to-string
  59. (format "git annex find --format=found --in=here %s"
  60. (shell-quote-argument path-relative))))))
  61. (let ((should-get
  62. (if (eq org-attach-git-annex-auto-get 'ask)
  63. (y-or-n-p (format "Run git annex get %s? " path-relative))
  64. org-attach-git-annex-auto-get)))
  65. (unless should-get
  66. (error "File %s stored in git annex but unavailable" path))
  67. (message "Running git annex get \"%s\"." path-relative)
  68. (call-process "git" nil nil nil "annex" "get" path-relative)))))
  69. (defun org-attach-git-commit (&optional _)
  70. "Commit changes to git if `org-attach-id-dir' is properly initialized.
  71. This checks for the existence of a \".git\" directory in that directory.
  72. Takes an unused optional argument for the sake of being compatible
  73. with hook `org-attach-after-change-hook'."
  74. (let* ((dir (expand-file-name org-attach-id-dir))
  75. (git-dir (vc-git-root dir))
  76. (use-annex (org-attach-git-use-annex))
  77. (changes 0))
  78. (when (and git-dir (executable-find "git"))
  79. (with-temp-buffer
  80. (cd dir)
  81. (dolist (new-or-modified
  82. (split-string
  83. (shell-command-to-string
  84. "git ls-files -zmo --exclude-standard") "\0" t))
  85. (if (and use-annex
  86. (>= (file-attribute-size (file-attributes new-or-modified))
  87. org-attach-git-annex-cutoff))
  88. (call-process "git" nil nil nil "annex" "add" new-or-modified)
  89. (call-process "git" nil nil nil "add" new-or-modified))
  90. (cl-incf changes))
  91. (dolist (deleted
  92. (split-string
  93. (shell-command-to-string "git ls-files -z --deleted") "\0" t))
  94. (call-process "git" nil nil nil "rm" deleted)
  95. (cl-incf changes))
  96. (when (> changes 0)
  97. (shell-command "git commit -m 'Synchronized attachments'"))))))
  98. (add-hook 'org-attach-after-change-hook 'org-attach-git-commit)
  99. (add-hook 'org-attach-open-hook 'org-attach-git-annex-get-maybe)
  100. (provide 'org-attach-git)
  101. ;;; org-attach-git.el ends here