unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Ashwin Kafle <ashwin@ashwink.com.np>
To: Eli Zaretskii <eliz@gnu.org>
Cc: Ashwin Kafle <ashwin@ashwink.com.np>,
	52508@debbugs.gnu.org, dgutov@yandex.ru
Subject: bug#52508: [PATCH] Option for vc-delete-file to keep file on disk
Date: Wed, 15 Dec 2021 18:07:27 +0000	[thread overview]
Message-ID: <854k79spwg.fsf@airmail.cc> (raw)
In-Reply-To: <85bl1igo9d.fsf@ashwink.com.np>

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

Eli Zaretskii <eliz@gnu.org> writes:


[...]
>> I've created the attached patch to have vc-delete file to keep files on
>> disk using a prefix argument. I've only tested it for vc-git.
>
> Thanks, sounds useful.
>
> A few minor comments below, mainly to the documentation parts:
[...]

Thanks, now fixed and attached.

>
>> -	(let ((backup-inhibited nil))
>> +	(let ((backup-inhibited nil)
>> + ;; if you don't set this, then for some reason, the file is never
>> brought back
>> +              (backup-by-copying t))
>
> Wouldn't it be better to understand why this mystery happens?

Yeah, a backup function should bring back the original file.  That is
indeed weird.  But I can't read much elisp yet to be of help in here.

In this case, backup-by-copying seems to be the better method for
backups since the intention is to not touch the original files at all.
Reading the docstring of backup-buffer, backup-by-rename doesn't look
fit for this use case.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Option-for-vc-delete-file-to-keep-file-on-disk.patch --]
[-- Type: text/x-diff, Size: 4902 bytes --]

From b8e5b8463f15a53e5fb6ddac5447005d4133606b Mon Sep 17 00:00:00 2001
From: Ashwin Kafle <ashwin@ashwink.com.np>
Date: Wed, 15 Dec 2021 23:49:47 +0545
Subject: [PATCH] Option for vc-delete-file to keep file on disk

Add a prefix argument on vc-delete-file to keep affected
file on disk and keep the current buffer intact.  This option
relies on the backends to not delete files themselves.

* doc/emacs/vc1-xtra.texi: Document the change.
* lisp/vc/vc-git.el (vc-git-delete-file): Make git leave files on disk.
* lisp/vc/vc.el (vc-delete-file): Change vc-delete-file to accept
optional prefix argument.
---
 doc/emacs/vc1-xtra.texi |  3 ++-
 etc/NEWS                |  4 ++++
 lisp/vc/vc-git.el       |  4 ++--
 lisp/vc/vc.el           | 18 ++++++++++++------
 4 files changed, 20 insertions(+), 9 deletions(-)

diff --git a/doc/emacs/vc1-xtra.texi b/doc/emacs/vc1-xtra.texi
index 4cd00cba6c..99e5eef7c1 100644
--- a/doc/emacs/vc1-xtra.texi
+++ b/doc/emacs/vc1-xtra.texi
@@ -122,7 +122,8 @@ VC Delete/Rename
   If you wish to delete a version-controlled file, use the command
 @kbd{M-x vc-delete-file}.  This prompts for the file name, and deletes
 it via the version control system.  The file is removed from the
-working tree, and in the VC Directory buffer
+working tree, and in the VC Directory buffer.  If you give a prefix argument,
+the file is not deleted from disk.
 @iftex
 (@pxref{VC Directory Mode,,, emacs, the Emacs Manual}),
 @end iftex
diff --git a/etc/NEWS b/etc/NEWS
index 8d83b2a7e3..af358aaedb 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -372,6 +372,10 @@ info node.  This command only works for the Emacs and Emacs Lisp manuals.
 
 ** vc
 
+*** 'C-x v x' accepts a prefix argument to keep file on disk.
+Previously 'C-x v x' always deleted the selected file.  Now if you give it
+prefix argument, it will keep the buffer and file on disk intact.
+Currently this is only implemented for vc-git.
 ---
 *** 'C-x v v' on an unregistered file will now use the most specific backend.
 Previously, if you had an SVN-covered "~/" directory, and a Git-covered
diff --git a/lisp/vc/vc-git.el b/lisp/vc/vc-git.el
index 5c6a39aec9..69ef216529 100644
--- a/lisp/vc/vc-git.el
+++ b/lisp/vc/vc-git.el
@@ -1569,8 +1569,8 @@ vc-git-next-revision
     (or (vc-git-symbolic-commit next-rev) next-rev)))
 
 (defun vc-git-delete-file (file)
-  (vc-git-command nil 0 (vc-git--literal-pathspec file) "rm" "-f" "--"))
-
+  (vc-git-command nil 0 (vc-git--literal-pathspec file) "rm" "-f" "--cached" "--"))
+)
 (defun vc-git-rename-file (old new)
   (vc-git-command nil 0 (list old new) "mv" "-f" "--"))
 
diff --git a/lisp/vc/vc.el b/lisp/vc/vc.el
index 64f752f248..a4d390fea8 100644
--- a/lisp/vc/vc.el
+++ b/lisp/vc/vc.el
@@ -2970,14 +2970,17 @@ vc-transfer-file
       (vc-checkin file new-backend comment (stringp comment)))))
 
 ;;;###autoload
-(defun vc-delete-file (file)
+(defun vc-delete-file (file &optional keep-file)
   "Delete file and mark it as such in the version control system.
 If called interactively, read FILE, defaulting to the current
-buffer's file name if it's under version control."
+buffer's file name if it's under version control.
+If a prefix argument is given (optional argument KEEP-FILE) then
+don't delete the file from the disk."
   (interactive (list (read-file-name "VC delete file: " nil
                                      (when (vc-backend buffer-file-name)
                                        buffer-file-name)
-                                     t)))
+                                     t)
+                     current-prefix-arg))
   (setq file (expand-file-name file))
   (let ((buf (get-file-buffer file))
         (backend (vc-backend file)))
@@ -2999,19 +3002,22 @@ vc-delete-file
     (unless (or (file-directory-p file) (null make-backup-files)
                 (not (file-exists-p file)))
       (with-current-buffer (or buf (find-file-noselect file))
-	(let ((backup-inhibited nil))
+	(let ((backup-inhibited nil)
+              ;; If you don't set this, then for some reason, the file is never brought back.
+              (backup-by-copying t))
 	  (backup-buffer))))
     ;; Bind `default-directory' so that the command that the backend
     ;; runs to remove the file is invoked in the correct context.
     (let ((default-directory (file-name-directory file)))
       (vc-call-backend backend 'delete-file file))
     ;; If the backend hasn't deleted the file itself, let's do it for him.
-    (when (file-exists-p file) (delete-file file))
+    (when (and (file-exists-p file) (null keep-file))
+      (delete-file file))
     ;; Forget what VC knew about the file.
     (vc-file-clearprops file)
     ;; Make sure the buffer is deleted and the *vc-dir* buffers are
     ;; updated after this.
-    (vc-resynch-buffer file nil t)))
+    (vc-resynch-buffer file keep-file t)))
 
 ;;;###autoload
 (defun vc-rename-file (old new)
-- 
2.34.1


  reply	other threads:[~2021-12-15 18:07 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-15 10:23 bug#52508: [PATCH] Option for vc-delete-file to keep file on disk Ashwin Kafle
2021-12-15 14:14 ` Eli Zaretskii
2021-12-15 18:07   ` Ashwin Kafle [this message]
2021-12-18  4:41 ` Richard Stallman
2021-12-18  7:07   ` Eli Zaretskii
2021-12-18  7:36   ` Ashwin Kafle
2021-12-20  4:42     ` Richard Stallman
  -- strict thread matches above, loose matches on Subject: below --
2021-12-15  9:53 bug#52507: " Ashwin Kafle
2021-12-15 16:53 ` Juri Linkov
2021-12-15 17:41   ` Ashwin Kafle
2021-12-15 18:06     ` Juri Linkov
2021-12-15 18:26       ` Ashwin Kafle
2021-12-15 18:34         ` Ashwin Kafle
2021-12-16 17:01           ` Juri Linkov
2021-12-26 14:23             ` Ashwin Kafle
2021-12-26 15:38               ` Dmitry Gutov
2021-12-26 15:51                 ` Ashwin Kafle
2021-12-26 15:57                   ` Dmitry Gutov
2021-12-26 16:12                     ` Ashwin Kafle
2021-12-26 16:28                       ` Dmitry Gutov
2021-12-26 17:03                         ` Ashwin Kafle
2021-12-27  0:03                           ` Dmitry Gutov
2021-12-27  4:08                             ` Ashwin Kafle
2021-12-28  0:53                               ` Dmitry Gutov
2022-09-08 14:17                                 ` bug#52508: " Lars Ingebrigtsen

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.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=854k79spwg.fsf@airmail.cc \
    --to=ashwin@ashwink.com.np \
    --cc=52508@debbugs.gnu.org \
    --cc=dgutov@yandex.ru \
    --cc=eliz@gnu.org \
    /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.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).