emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Chunyang Xu <mail@xuchunyang.me>
To: "Charles C. Berry" <ccberry@ucsd.edu>
Cc: emacs-orgmode@gnu.org, Nicolas Goaziou <mail@nicolasgoaziou.fr>
Subject: Re: Asks user what to do with each tangle-file before overriding?
Date: Mon, 19 Jun 2017 12:57:11 +0800	[thread overview]
Message-ID: <m260fsy3q0.fsf@xuchunyang.me> (raw)
In-Reply-To: <alpine.OSX.2.20.1706180916320.763@charles-berrys-macbook.local>

"Charles C. Berry" <ccberry@ucsd.edu> writes:


[...]

> It sounds like you are re-inventing version control and wanting it to be 
> implemented in org-babel-tangle.
>
> `org-babel-tangle' can do a lot of work to assemble the files that result 
> from tangling.  Modifying `org-babel-tangle' to do what you ask would be 
> far from trivial and make a complicated function even more tortuous.

Yes, I agree it is not an easy task.

> IMHO, it is better to solve your problem as follows: Set up a separate 
> directory into which files are tangled.  Use version control on that 
> directory.  Inspect changes after each tangle using the version control 
> system's diff tools, commit good changes, and revert unwanted ones.  Then 
> run a script that uses the VC's data to identify changed files and copy 
> those files to the location on your system where they will be used.

I found 'C-x s' ('save-some-buffers') can be used as the "version
control", so I made a wrapper command for 'org-babel-tangle' using it.
The wrapper command is not perfect (I guess it doesn't support
remote-file and doesn't always respect tangle-mode, for example), but I
am OK with it and it already does what I want.

   #+BEGIN_SRC emacs-lisp :results silent :lexical yes
     (defun chunyang-org-babel-tangle (&optional arg target-file lang)
       "Like `org-babel-tangle' but don't override without permission."
       (declare (interactive-only org-babel-tangle))
       (interactive "P")
       (require 'ob-tangle)
       (require 'seq)
       (require 'cl-lib)
       (save-some-buffers)
       (let* ((tmpdir (let ((dir "/tmp/org-babel-tangle/"))
                        (or (file-exists-p dir) (make-directory dir))
                        dir))
              (mkbak (lambda (filename)
                       (expand-file-name
                        (replace-regexp-in-string "/" "!" (expand-file-name filename))
                        tmpdir)))
              (diffp (lambda (file-a file-b)
                       (/= 0 (call-process diff-command nil nil nil (expand-file-name file-a) file-b))))
              (swap (lambda (file-a file-b)
                      (let ((tmp (make-temp-name tmpdir)))
                        (copy-file file-a tmp t t t t)
                        (copy-file file-b file-a t t t t)
                        (copy-file tmp file-b t t t t))))
              deleted-files
              tangled-files
              (advice (define-advice delete-file (:around (old-fun filename &rest args) dont-delete)
                        (push filename deleted-files)
                        (copy-file filename (funcall mkbak filename) t t t t)
                        (apply old-fun filename args))))
         (unwind-protect
             (progn
               (setq tangled-files (org-babel-tangle arg target-file lang)
                     deleted-files (nreverse deleted-files))
               (cl-loop for f in (seq-intersection deleted-files tangled-files)
                        for bak = (funcall mkbak f)
                        do
                        (funcall swap f bak)
                        (if (funcall diffp f bak)
                            (with-current-buffer (find-file-noselect f)
                              (message "Diff %s..." f)
                              (delete-region (point-min) (point-max))
                              (insert-file-contents bak))
                          (message "Skip %s..." f))
                        finally (save-some-buffers)))
           (advice-remove 'delete-file advice))))
   #+END_SRC


[...]

  reply	other threads:[~2017-06-19  4:57 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-06-16  5:09 Asks user what to do with each tangle-file before overriding? Chunyang Xu
2017-06-18  7:23 ` Nicolas Goaziou
2017-06-18 11:21   ` Chunyang Xu
2017-06-18 17:12     ` Charles C. Berry
2017-06-19  4:57       ` Chunyang Xu [this message]
2017-06-19  5:06 ` numbchild

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m260fsy3q0.fsf@xuchunyang.me \
    --to=mail@xuchunyang.me \
    --cc=ccberry@ucsd.edu \
    --cc=emacs-orgmode@gnu.org \
    --cc=mail@nicolasgoaziou.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).