emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] org-babel-tangle: Do not allow tangling into self
@ 2024-01-08 16:02 Ihor Radchenko
  2024-01-10 21:13 ` Rudolf Adamkovič
  0 siblings, 1 reply; 3+ messages in thread
From: Ihor Radchenko @ 2024-01-08 16:02 UTC (permalink / raw)
  To: emacs-orgmode

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

Hello,

We currently allow tangle to shoot its own foot:

file.org:

#+begin_src org :tangle file.org
Overwrite me.
#+end_src

The attached patch makes Org throw an error in such scenario.

I am almost sure that it is not going to break anyone's workflow.
Or will it?..


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-org-babel-tangle-Do-not-allow-tangling-into-self.patch --]
[-- Type: text/x-patch, Size: 2811 bytes --]

From e2b0913930119737bac69d17738d0ebb44f999f7 Mon Sep 17 00:00:00 2001
Message-ID: <e2b0913930119737bac69d17738d0ebb44f999f7.1704729564.git.yantar92@posteo.net>
From: Ihor Radchenko <yantar92@posteo.net>
Date: Mon, 8 Jan 2024 16:58:24 +0100
Subject: [PATCH] org-babel-tangle: Do not allow tangling into self

* lisp/ob-tangle.el (org-babel-tangle): Throw an error when trying to
tangle into the org file we tangle from.
* etc/ORG-NEWS (It is no longer allowed to tangle into the same file
as Org source): Document the breaking change.
---
 etc/ORG-NEWS      | 12 ++++++++++++
 lisp/ob-tangle.el | 10 +++++++++-
 2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index b808357d8..a8d14673c 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -13,6 +13,18 @@ Please send Org bug reports to mailto:emacs-orgmode@gnu.org.
 
 * Version 9.7 (not released yet)
 ** Important announcements and breaking changes
+*** It is no longer allowed to tangle into the same file as Org source
+
+Previously, =file.org= with the following contents
+
+: #+begin_src org :tangle file.org
+: Text
+: #+end_src
+
+would overwrite itself.
+
+Now, an error is thrown.
+
 *** ~org-agenda-search-headline-for-time~ now ignores all the timestamp in headings
 
 Previously, ~org-agenda-search-headline-for-time~ made Org agenda
diff --git a/lisp/ob-tangle.el b/lisp/ob-tangle.el
index 933a3eba1..914f4b7b6 100644
--- a/lisp/ob-tangle.el
+++ b/lisp/ob-tangle.el
@@ -256,7 +256,8 @@ (defun org-babel-tangle (&optional arg target-file lang-re)
 	     (when (equal arg '(16))
 	       (or (cdr (assq :tangle (nth 2 (org-babel-get-src-block-info 'no-eval))))
 		   (user-error "Point is not in a source code block"))))
-	    path-collector)
+	    path-collector
+            (source-file buffer-file-name))
 	(mapc ;; map over file-names
 	 (lambda (by-fn)
 	   (let ((file-name (car by-fn)))
@@ -313,6 +314,13 @@ (defun org-babel-tangle (&optional arg target-file lang-re)
                                       (compare-buffer-substrings
                                        nil nil nil
                                        tangle-buf nil nil)))))))
+                     (when (equal (if (file-name-absolute-p file-name)
+                                      file-name
+                                    (expand-file-name file-name))
+                                  (if (file-name-absolute-p source-file)
+                                      source-file
+                                    (expand-file-name source-file)))
+                       (error "Not allowed to tangle into the same file as self"))
                      ;; We do not erase, but overwrite previous file
                      ;; to preserve any existing symlinks.
 		     (write-region nil nil file-name)
-- 
2.42.0


[-- Attachment #3: Type: text/plain, Size: 224 bytes --]


-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>

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

* Re: [PATCH] org-babel-tangle: Do not allow tangling into self
  2024-01-08 16:02 [PATCH] org-babel-tangle: Do not allow tangling into self Ihor Radchenko
@ 2024-01-10 21:13 ` Rudolf Adamkovič
  2024-01-26 15:55   ` Ihor Radchenko
  0 siblings, 1 reply; 3+ messages in thread
From: Rudolf Adamkovič @ 2024-01-10 21:13 UTC (permalink / raw)
  To: Ihor Radchenko, emacs-orgmode

Ihor Radchenko <yantar92@posteo.net> writes:

> The attached patch makes Org throw an error in such scenario.

This is a great feature (and it deserves a test, given it protects from
data loss).  For what is worth, I remember making this error at least
twice in the past year.  (Git saved me in both cases, fortunately.)

Rudy
-- 
"I do not fear death.  I had been dead for billions and billions of
years before I was born, and had not suffered the slightest
inconvenience from it."  --- Mark Twain, paraphrased

Rudolf Adamkovič <salutis@me.com> [he/him]
Studenohorská 25
84103 Bratislava
Slovakia


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

* Re: [PATCH] org-babel-tangle: Do not allow tangling into self
  2024-01-10 21:13 ` Rudolf Adamkovič
@ 2024-01-26 15:55   ` Ihor Radchenko
  0 siblings, 0 replies; 3+ messages in thread
From: Ihor Radchenko @ 2024-01-26 15:55 UTC (permalink / raw)
  To: Rudolf Adamkovič; +Cc: emacs-orgmode

Rudolf Adamkovič <salutis@me.com> writes:

> Ihor Radchenko <yantar92@posteo.net> writes:
>
>> The attached patch makes Org throw an error in such scenario.
>
> This is a great feature (and it deserves a test, given it protects from
> data loss).  For what is worth, I remember making this error at least
> twice in the past year.  (Git saved me in both cases, fortunately.)

Applied, onto main; after adding a test.
https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=ef23b4706

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


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

end of thread, other threads:[~2024-01-26 15:52 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-08 16:02 [PATCH] org-babel-tangle: Do not allow tangling into self Ihor Radchenko
2024-01-10 21:13 ` Rudolf Adamkovič
2024-01-26 15:55   ` Ihor Radchenko

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