org-attach-git.el 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. ;;; org-attach-git.el --- Automatic git commit extention to org-attach -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2019 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 extention to org-attach. If the attachment-directory to an
  20. ;; outline node (using either DIR or ID) is initialized as a Git
  21. ;; repository, then org-attach-git will automatically commit changes
  22. ;; when it sees them.
  23. ;;; Code:
  24. (require 'org-attach)
  25. (require 'vc-git)
  26. (defcustom org-attach-git-annex-cutoff (* 32 1024)
  27. "If non-nil, files larger than this will be annexed instead of stored."
  28. :group 'org-attach
  29. :version "24.4"
  30. :package-version '(Org . "8.0")
  31. :type '(choice
  32. (const :tag "None" nil)
  33. (integer :tag "Bytes")))
  34. (defcustom org-attach-git-annex-auto-get 'ask
  35. "Confirmation preference for automatically getting annex files.
  36. If \\='ask, prompt using `y-or-n-p'. 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. (defun org-attach-git-use-annex ()
  45. "Return non-nil if git annex can be used."
  46. (let ((git-dir (vc-git-root (expand-file-name org-attach-id-dir))))
  47. (and org-attach-git-annex-cutoff
  48. (or (file-exists-p (expand-file-name "annex" git-dir))
  49. (file-exists-p (expand-file-name ".git/annex" git-dir))))))
  50. (defun org-attach-git-annex-get-maybe (path)
  51. "Call git annex get PATH (via shell) if using git annex.
  52. Signals an error if the file content is not available and it was not retrieved."
  53. (let* ((default-directory (expand-file-name org-attach-id-dir))
  54. (path-relative (file-relative-name path)))
  55. (when (and (org-attach-git-use-annex)
  56. (not
  57. (string-equal
  58. "found"
  59. (shell-command-to-string
  60. (format "git annex find --format=found --in=here %s"
  61. (shell-quote-argument path-relative))))))
  62. (let ((should-get
  63. (if (eq org-attach-git-annex-auto-get 'ask)
  64. (y-or-n-p (format "Run git annex get %s? " path-relative))
  65. org-attach-git-annex-auto-get)))
  66. (unless should-get
  67. (error "File %s stored in git annex but unavailable" path))
  68. (message "Running git annex get \"%s\"." path-relative)
  69. (call-process "git" nil nil nil "annex" "get" path-relative)))))
  70. (defun org-attach-git-commit ()
  71. "Commit changes to git if `org-attach-id-dir' is properly initialized.
  72. This checks for the existence of a \".git\" directory in that directory."
  73. (let* ((dir (expand-file-name org-attach-id-dir))
  74. (git-dir (vc-git-root dir))
  75. (use-annex (org-attach-git-use-annex))
  76. (changes 0))
  77. (when (and git-dir (executable-find "git"))
  78. (with-temp-buffer
  79. (cd dir)
  80. (dolist (new-or-modified
  81. (split-string
  82. (shell-command-to-string
  83. "git ls-files -zmo --exclude-standard") "\0" t))
  84. (if (and use-annex
  85. (>= (file-attribute-size (file-attributes new-or-modified))
  86. org-attach-git-annex-cutoff))
  87. (call-process "git" nil nil nil "annex" "add" new-or-modified)
  88. (call-process "git" nil nil nil "add" new-or-modified))
  89. (cl-incf changes))
  90. (dolist (deleted
  91. (split-string
  92. (shell-command-to-string "git ls-files -z --deleted") "\0" t))
  93. (call-process "git" nil nil nil "rm" deleted)
  94. (cl-incf changes))
  95. (when (> changes 0)
  96. (shell-command "git commit -m 'Synchronized attachments'"))))))
  97. (add-hook 'org-attach-after-change-hook 'org-attach-git-commit)
  98. (add-hook 'org-attach-open-hook 'org-attach-git-annex-get-maybe)
  99. (provide 'org-attach-git)
  100. ;;; org-attach-git.el ends here