emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Asks user what to do with each tangle-file before overriding?
@ 2017-06-16  5:09 Chunyang Xu
  2017-06-18  7:23 ` Nicolas Goaziou
  2017-06-19  5:06 ` numbchild
  0 siblings, 2 replies; 6+ messages in thread
From: Chunyang Xu @ 2017-06-16  5:09 UTC (permalink / raw)
  To: emacs-orgmode

Hi,

Currently 'C-c C-v C-t' ('org-babel-tangle') simply overrides existing
tangle-file, I would like org to ask me what to do? such as

a) yes (override)
b) no (don't override)
c) show the diff then ask again

In addition, if there is no diff (i.e., having the same contents), user
can choose (e.g., via a user option) to simply pass this tangle-file
without overriding.

I am asking for this feature because I am storing my dotfiles in a
single Org file [1], and when I change my Bash configuration then
tangle, I don't need to worry if Org is tangling other configuration
correctly, especially ~/.ssh/id_rsa and ~/.authinfo.gpg. I would rather
leave these unchanged files than override (even only file modification
timestamps are updated).

[1] https://raw.githubusercontent.com/xuchunyang/dotfiles/master/README.org

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Asks user what to do with each tangle-file before overriding?
  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-19  5:06 ` numbchild
  1 sibling, 1 reply; 6+ messages in thread
From: Nicolas Goaziou @ 2017-06-18  7:23 UTC (permalink / raw)
  To: Chunyang Xu; +Cc: emacs-orgmode

Hello,

Chunyang Xu <mail@xuchunyang.me> writes:

> Currently 'C-c C-v C-t' ('org-babel-tangle') simply overrides existing
> tangle-file, I would like org to ask me what to do? such as
>
> a) yes (override)
> b) no (don't override)
> c) show the diff then ask again
>
> In addition, if there is no diff (i.e., having the same contents), user
> can choose (e.g., via a user option) to simply pass this tangle-file
> without overriding.

I don't think it is a good idea to have file that can be both edited
manually, and auto-generated (i.e., tangled). Or, to put it differently,
the idea behind tangling is that you only handle the code block, not the
file itself.

> I am asking for this feature because I am storing my dotfiles in a
> single Org file [1], and when I change my Bash configuration then
> tangle, I don't need to worry if Org is tangling other configuration
> correctly, especially ~/.ssh/id_rsa and ~/.authinfo.gpg. I would rather
> leave these unchanged files than override (even only file modification
> timestamps are updated).

You could add :tangle no on top of the sensitive blocks and remove the
flag only when you want to generate them.

Regards,

-- 
Nicolas Goaziou

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Asks user what to do with each tangle-file before overriding?
  2017-06-18  7:23 ` Nicolas Goaziou
@ 2017-06-18 11:21   ` Chunyang Xu
  2017-06-18 17:12     ` Charles C. Berry
  0 siblings, 1 reply; 6+ messages in thread
From: Chunyang Xu @ 2017-06-18 11:21 UTC (permalink / raw)
  To: Nicolas Goaziou; +Cc: emacs-orgmode

Nicolas Goaziou <mail@nicolasgoaziou.fr> writes:

[...]

> I don't think it is a good idea to have file that can be both edited
> manually, and auto-generated (i.e., tangled). Or, to put it differently,
> the idea behind tangling is that you only handle the code block, not the
> file itself.

I still think overriding exist files is dangerous, user can lost data
because of it, so it should be safer if org-babel-tangle can let user
decide how to deal with it.

Besides, if a code block is not changed and I have already tangled it
before, I don't want org-babel-tangle to update the last modified
timestamp of the tangle-file every time I tangle, as along as there is
no actual changes. It is simply unnecessary to me and makes the last
modified timestamp more or less useless. So I would like
org-babel-tangle let me choose what I want.

[...]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Asks user what to do with each tangle-file before overriding?
  2017-06-18 11:21   ` Chunyang Xu
@ 2017-06-18 17:12     ` Charles C. Berry
  2017-06-19  4:57       ` Chunyang Xu
  0 siblings, 1 reply; 6+ messages in thread
From: Charles C. Berry @ 2017-06-18 17:12 UTC (permalink / raw)
  To: Chunyang Xu; +Cc: emacs-orgmode, Nicolas Goaziou

On Sun, 18 Jun 2017, Chunyang Xu wrote:

> Nicolas Goaziou <mail@nicolasgoaziou.fr> writes:
>
> [...]
>
>> I don't think it is a good idea to have file that can be both edited
>> manually, and auto-generated (i.e., tangled). Or, to put it differently,
>> the idea behind tangling is that you only handle the code block, not the
>> file itself.
>
> I still think overriding exist files is dangerous, user can lost data
> because of it, so it should be safer if org-babel-tangle can let user
> decide how to deal with it.
>
> Besides, if a code block is not changed and I have already tangled it
> before, I don't want org-babel-tangle to update the last modified
> timestamp of the tangle-file every time I tangle, as along as there is
> no actual changes. It is simply unnecessary to me and makes the last
> modified timestamp more or less useless. So I would like
> org-babel-tangle let me choose what I want.
>
> [...]

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.

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.

HTH,

Chuck

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Asks user what to do with each tangle-file before overriding?
  2017-06-18 17:12     ` Charles C. Berry
@ 2017-06-19  4:57       ` Chunyang Xu
  0 siblings, 0 replies; 6+ messages in thread
From: Chunyang Xu @ 2017-06-19  4:57 UTC (permalink / raw)
  To: Charles C. Berry; +Cc: emacs-orgmode, Nicolas Goaziou

"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


[...]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Asks user what to do with each tangle-file before overriding?
  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-19  5:06 ` numbchild
  1 sibling, 0 replies; 6+ messages in thread
From: numbchild @ 2017-06-19  5:06 UTC (permalink / raw)
  To: Chunyang Xu; +Cc: Org-mode

[-- Attachment #1: Type: text/plain, Size: 1886 bytes --]

I also think Org-mode table src block content to override file is dangerous.

I have another idea. Append src block content to end of file.

I have a sceniro:

File `dotfile-1.org`:
```
* SSH config 1

#+BEGIN_SRC conf :tangle "~/.ssh/config"
fragment 1
#+END_SRC
```

File `dotfile-2.org`:
```
* SSH config 2

#+BEGIN_SRC conf :tangle "~/.ssh/config"
fragment 2
#+END_SRC

```

When the tangle src blocks are in separate files, this override file way is
not suitable.
Of course I can put those src blcoks together, but when I have to organize
literate programming files in separately, the override way is not suitable
anymore. So hope Org-mode can provide `append` way with an `defcustom`
option.


[stardiviner]           <Hack this world!>      GPG key ID: 47C32433
IRC(freeenode): stardiviner                     Twitter:  @numbchild
Key fingerprint = 9BAA 92BC CDDD B9EF 3B36  CB99 B8C4 B8E5 47C3 2433
Blog: http://stardiviner.github.io/

On Fri, Jun 16, 2017 at 1:09 PM, Chunyang Xu <mail@xuchunyang.me> wrote:

> Hi,
>
> Currently 'C-c C-v C-t' ('org-babel-tangle') simply overrides existing
> tangle-file, I would like org to ask me what to do? such as
>
> a) yes (override)
> b) no (don't override)
> c) show the diff then ask again
>
> In addition, if there is no diff (i.e., having the same contents), user
> can choose (e.g., via a user option) to simply pass this tangle-file
> without overriding.
>
> I am asking for this feature because I am storing my dotfiles in a
> single Org file [1], and when I change my Bash configuration then
> tangle, I don't need to worry if Org is tangling other configuration
> correctly, especially ~/.ssh/id_rsa and ~/.authinfo.gpg. I would rather
> leave these unchanged files than override (even only file modification
> timestamps are updated).
>
> [1] https://raw.githubusercontent.com/xuchunyang/dotfiles/
> master/README.org
>
>
>
>

[-- Attachment #2: Type: text/html, Size: 4925 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2017-06-19  5:07 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
2017-06-19  5:06 ` numbchild

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).