org-mode/lisp/org-attach-git.el

118 lines
4.4 KiB
EmacsLisp
Raw Normal View History

org-attach*, org, org-manual, org-news, ox-html, testing/* * lisp/org-attach.el Changed the way attachments deal with property-inheritance. It now adheres to the =org-use-property-inheritance= setting by default but it can be customized if needed (I recommend to enable it!). The property ATTACH_DIR is deprecated in favour of the shorter and simpler property DIR. Added an explicit option to =org-attach= for unsetting attachment-directories (i.e. remove DIR property and deal with the attachments by interaction). Added attachment link type with the prefix "attachment:". Added customizations: - org-attach-dir-relative - org-attach-preferred-new-method - org-attach-use-inheritance - org-attach-id-to-path-function Hooks added: - org-attach-after-change-hook - org-attach-open-hook A new linktype "attachment" is added in order to reduce link-duplication when wanting to link to files in attached folders of nodes. This works for both ID and DIR properties. The goal is to make the functionality for attachment links mirror the functionality for file links. * lisp/org-attach-git.el New file, existing functionality. Code here has been factored out from org-attach.el and if GIT-functionality is to be used this module needs to be required sepatately. It extends org-attach by use of its hooks. Activating git functionality in org-attach is done by loading org-attach-git from now on, instead of customizing a variable. Naming of both functions and tests has been modified to match the move of functionality into its own module. * lisp/org.el Inline images are shown also using attachment-links, exactly the same as it works for file-links today. Make org-open-at-point respect ARG when opening attachment-dir. * lisp/org-compat.el org-attach-directory has been deprecated in favour for org-attach-id-dir. The new name matches its purpose better. * lisp/ox-html.el Export attachment links to images as inline images, in the same way as file links work today. * etc/ORG-NEWS Mention the changes in this patch. * doc/org-manual.org The chapter "Refile, Copy, Archive" has been split into two separate chapters. - "Refile, Copy and Archiving" for information related to moving existing data around. - "Capture, Attachments, RSS Feeds and Protocols" for information related to working with external data. The attachment-part has been rewritten and extended to match the changes in this patch. The new attachment link type is mentioned both inside the attachments chapter and in the chapter dealing with links. Documentation related to external links has been improved. * testing/lisp/test-org-attach-annex.el Require org-attach-git instead of org-attach, since this file tests the GIT-functionality. * testing/lisp/test-org-attach.el Add tests for org-attach. * testing/org-test.el Define a symbol for a file to test attachments with. * testing/examples/* A bunch of new example files and folders are created and are used in testing of org-attach to verify its functionality.
2018-11-25 15:38:44 -05:00
;;; org-attach-git.el --- Automatic git commit extention to org-attach -*- lexical-binding: t; -*-
;; Copyright (C) 2019 Free Software Foundation, Inc.
;; Original Author: John Wiegley <johnw@newartisans.com>
;; Restructurer: Gustav Wikström <gustav@whil.se>
;; Keywords: org data git
;; This file is part of GNU Emacs.
;;
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; An extention to org-attach. If the attachment-directory to an
;; outline node (using either DIR or ID) is initialized as a Git
;; repository, then org-attach-git will automatically commit changes
;; when it sees them.
;;; Code:
(require 'org-attach)
(require 'vc-git)
(defcustom org-attach-git-annex-cutoff (* 32 1024)
"If non-nil, files larger than this will be annexed instead of stored."
:group 'org-attach
:version "24.4"
:package-version '(Org . "8.0")
:type '(choice
(const :tag "None" nil)
(integer :tag "Bytes")))
(defcustom org-attach-git-annex-auto-get 'ask
"Confirmation preference for automatically getting annex files.
If \\='ask, prompt using `y-or-n-p'. If t, always get. If nil, never get."
:group 'org-attach
:package-version '(Org . "9.0")
:version "26.1"
:type '(choice
(const :tag "confirm with `y-or-n-p'" ask)
(const :tag "always get from annex if necessary" t)
(const :tag "never get from annex" nil)))
(defun org-attach-git-use-annex ()
"Return non-nil if git annex can be used."
(let ((git-dir (vc-git-root (expand-file-name org-attach-id-dir))))
(and org-attach-git-annex-cutoff
(or (file-exists-p (expand-file-name "annex" git-dir))
(file-exists-p (expand-file-name ".git/annex" git-dir))))))
(defun org-attach-git-annex-get-maybe (path)
"Call git annex get PATH (via shell) if using git annex.
Signals an error if the file content is not available and it was not retrieved."
(let* ((default-directory (expand-file-name org-attach-id-dir))
(path-relative (file-relative-name path)))
(when (and (org-attach-git-use-annex)
(not
(string-equal
"found"
(shell-command-to-string
(format "git annex find --format=found --in=here %s"
(shell-quote-argument path-relative))))))
(let ((should-get
(if (eq org-attach-git-annex-auto-get 'ask)
(y-or-n-p (format "Run git annex get %s? " path-relative))
org-attach-git-annex-auto-get)))
(unless should-get
(error "File %s stored in git annex but unavailable" path))
(message "Running git annex get \"%s\"." path-relative)
(call-process "git" nil nil nil "annex" "get" path-relative)))))
(defun org-attach-git-commit ()
"Commit changes to git if `org-attach-id-dir' is properly initialized.
This checks for the existence of a \".git\" directory in that directory."
(let* ((dir (expand-file-name org-attach-id-dir))
(git-dir (vc-git-root dir))
(use-annex (org-attach-git-use-annex))
(changes 0))
(when (and git-dir (executable-find "git"))
(with-temp-buffer
(cd dir)
(dolist (new-or-modified
(split-string
(shell-command-to-string
"git ls-files -zmo --exclude-standard") "\0" t))
(if (and use-annex
(>= (file-attribute-size (file-attributes new-or-modified))
org-attach-git-annex-cutoff))
(call-process "git" nil nil nil "annex" "add" new-or-modified)
(call-process "git" nil nil nil "add" new-or-modified))
(cl-incf changes))
(dolist (deleted
(split-string
(shell-command-to-string "git ls-files -z --deleted") "\0" t))
(call-process "git" nil nil nil "rm" deleted)
(cl-incf changes))
(when (> changes 0)
(shell-command "git commit -m 'Synchronized attachments'"))))))
(add-hook 'org-attach-after-change-hook 'org-attach-git-commit)
(add-hook 'org-attach-open-hook 'org-attach-git-annex-get-maybe)
(provide 'org-attach-git)
;;; org-attach-git.el ends here