Blame


1 6b6c8a78 2021-01-04 op ;;; vc-got-stage.el --- Stage functionalities for vc-got -*- lexical-binding: t; -*-
2 6b6c8a78 2021-01-04 op
3 6b6c8a78 2021-01-04 op ;; Copyright (C) 2021 Omar Polo
4 6b6c8a78 2021-01-04 op
5 6b6c8a78 2021-01-04 op ;; Author: Omar Polo <op@omarpolo.com>
6 6b6c8a78 2021-01-04 op ;; Keywords: vc
7 6b6c8a78 2021-01-04 op
8 6b6c8a78 2021-01-04 op ;; This program is free software; you can redistribute it and/or modify
9 6b6c8a78 2021-01-04 op ;; it under the terms of the GNU General Public License as published by
10 6b6c8a78 2021-01-04 op ;; the Free Software Foundation, either version 3 of the License, or
11 6b6c8a78 2021-01-04 op ;; (at your option) any later version.
12 6b6c8a78 2021-01-04 op
13 6b6c8a78 2021-01-04 op ;; This program is distributed in the hope that it will be useful,
14 6b6c8a78 2021-01-04 op ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 6b6c8a78 2021-01-04 op ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 6b6c8a78 2021-01-04 op ;; GNU General Public License for more details.
17 6b6c8a78 2021-01-04 op
18 6b6c8a78 2021-01-04 op ;; You should have received a copy of the GNU General Public License
19 6b6c8a78 2021-01-04 op ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
20 6b6c8a78 2021-01-04 op
21 6b6c8a78 2021-01-04 op ;;; Commentary:
22 6b6c8a78 2021-01-04 op
23 6b6c8a78 2021-01-04 op ;; Stage-related functions for vc-got. This allows vc-got to stage
24 6b6c8a78 2021-01-04 op ;; and commit individual chunks and not entire filesets.
25 6b6c8a78 2021-01-04 op
26 6b6c8a78 2021-01-04 op ;;; Code:
27 6b6c8a78 2021-01-04 op
28 6b6c8a78 2021-01-04 op (require 'vc)
29 6b6c8a78 2021-01-04 op
30 6b6c8a78 2021-01-04 op (defvar vc-got-program) ;vc-got.el
31 6b6c8a78 2021-01-04 op (declare-function vc-got--diff "vc-got")
32 6b6c8a78 2021-01-04 op (declare-function vc-got--unstage "vc-got" (file))
33 6b6c8a78 2021-01-04 op
34 6b6c8a78 2021-01-04 op (defvar vc-got-stage--process nil
35 6b6c8a78 2021-01-04 op "The got stage process.")
36 6b6c8a78 2021-01-04 op
37 6b6c8a78 2021-01-04 op (defvar vc-got-stage--fileset nil
38 6b6c8a78 2021-01-04 op "Remaining fileset to process.")
39 6b6c8a78 2021-01-04 op
40 6b6c8a78 2021-01-04 op (defun vc-got-stage--assert-proc ()
41 6b6c8a78 2021-01-04 op "Assert no vc-got-stage process is running."
42 6b6c8a78 2021-01-04 op (when (process-live-p vc-got-stage--process)
43 6b6c8a78 2021-01-04 op (error "A vc-got-stage-files is already in progress")))
44 6b6c8a78 2021-01-04 op
45 6b6c8a78 2021-01-04 op (defun vc-got-stage-files (fileset)
46 6b6c8a78 2021-01-04 op "Interactively stage hunks from files in FILESET."
47 6b6c8a78 2021-01-04 op (interactive (list (cadr (vc-deduce-fileset))))
48 6b6c8a78 2021-01-04 op (vc-got-stage--assert-proc)
49 6b6c8a78 2021-01-04 op (if (not fileset)
50 6b6c8a78 2021-01-04 op (message "[vc-got] nothing to stage.")
51 6b6c8a78 2021-01-04 op (setq vc-got-stage--fileset fileset)
52 6b6c8a78 2021-01-04 op (vc-got-stage--next)))
53 6b6c8a78 2021-01-04 op
54 6b6c8a78 2021-01-04 op (defun vc-got-stage--next ()
55 6b6c8a78 2021-01-04 op "Process next file in stage list."
56 6b6c8a78 2021-01-04 op (vc-got-stage--assert-proc)
57 6b6c8a78 2021-01-04 op (let ((file (car vc-got-stage--fileset)))
58 6b6c8a78 2021-01-04 op (if (not file)
59 6b6c8a78 2021-01-04 op (progn (kill-buffer (process-buffer vc-got-stage--process))
60 6b6c8a78 2021-01-04 op (message "[vc-got] stage done."))
61 6b6c8a78 2021-01-04 op (setq vc-got-stage--fileset (cdr vc-got-stage--fileset))
62 6b6c8a78 2021-01-04 op (let ((buf (get-buffer-create "*vc-got-stage*")))
63 6b6c8a78 2021-01-04 op (pop-to-buffer buf)
64 6b6c8a78 2021-01-04 op (with-current-buffer buf
65 6b6c8a78 2021-01-04 op (buffer-disable-undo)
66 6b6c8a78 2021-01-04 op (erase-buffer)
67 6b6c8a78 2021-01-04 op (read-only-mode)
68 6b6c8a78 2021-01-04 op (unless (derived-mode-p 'diff-mode)
69 6b6c8a78 2021-01-04 op (diff-mode)))
70 6b6c8a78 2021-01-04 op (setq vc-got-stage--process
71 6b6c8a78 2021-01-04 op (make-process :name "got"
72 6b6c8a78 2021-01-04 op :buffer buf
73 6b6c8a78 2021-01-04 op :command (list vc-got-program "stage" "-p" file)
74 6b6c8a78 2021-01-04 op :connection 'pty
75 6b6c8a78 2021-01-04 op :filter #'vc-got-stage--filter
76 6b6c8a78 2021-01-04 op :sentinel #'vc-got-stage--sentinel))))))
77 6b6c8a78 2021-01-04 op
78 6b6c8a78 2021-01-04 op (defun vc-got-stage--filter (proc string)
79 6b6c8a78 2021-01-04 op "Filter for got stage process.
80 6b6c8a78 2021-01-04 op PROC is the process, STRING part of its output."
81 6b6c8a78 2021-01-04 op (let ((buf (process-buffer proc)))
82 6b6c8a78 2021-01-04 op (when (buffer-live-p buf)
83 6b6c8a78 2021-01-04 op (let ((inhibit-read-only t))
84 6b6c8a78 2021-01-04 op (with-current-buffer buf
85 6b6c8a78 2021-01-04 op (goto-char (point-max))
86 6b6c8a78 2021-01-04 op (insert string)
87 6b6c8a78 2021-01-04 op (save-excursion
88 6b6c8a78 2021-01-04 op (beginning-of-line)
89 6b6c8a78 2021-01-04 op (let ((msg (cond ((looking-at "^stage this change?")
90 6b6c8a78 2021-01-04 op "Stage this change? ")
91 6b6c8a78 2021-01-04 op ((looking-at "^stage this addition?")
92 6b6c8a78 2021-01-04 op "Stage this addition? "))))
93 6b6c8a78 2021-01-04 op (when msg
94 6b6c8a78 2021-01-04 op (kill-line)
95 6b6c8a78 2021-01-04 op (process-send-string buf (if (y-or-n-p msg) "y\n" "n\n"))
96 6b6c8a78 2021-01-04 op (erase-buffer)))))))))
97 6b6c8a78 2021-01-04 op
98 6b6c8a78 2021-01-04 op (defun vc-got-stage--sentinel (_proc event)
99 6b6c8a78 2021-01-04 op "Sentinel for got stage process.
100 6b6c8a78 2021-01-04 op Should be only called when EVENT is finished."
101 6b6c8a78 2021-01-04 op (when (string= event "finished\n")
102 6b6c8a78 2021-01-04 op (vc-got-stage--next)))
103 6b6c8a78 2021-01-04 op
104 6b6c8a78 2021-01-04 op ;; TODO: make this interactive just as stage is
105 6b6c8a78 2021-01-04 op (defun vc-got-stage-unstage (fileset)
106 6b6c8a78 2021-01-04 op "Unstage staged hunks in FILESET."
107 6b6c8a78 2021-01-04 op (interactive (list (cadr (vc-deduce-fileset))))
108 6b6c8a78 2021-01-04 op (vc-got-stage--assert-proc)
109 6b6c8a78 2021-01-04 op (if fileset
110 6b6c8a78 2021-01-04 op (dolist (file fileset)
111 6b6c8a78 2021-01-04 op (vc-got--unstage file))
112 6b6c8a78 2021-01-04 op (vc-got--unstage nil)))
113 6b6c8a78 2021-01-04 op
114 6b6c8a78 2021-01-04 op (defun vc-got-stage-diff (fileset)
115 6b6c8a78 2021-01-04 op "Pop a buffer with the staged diff for FILESET.
116 6b6c8a78 2021-01-04 op If FILESET is nil, show the diff for every staged hunks."
117 6b6c8a78 2021-01-04 op (interactive (list (cadr (vc-deduce-fileset))))
118 6b6c8a78 2021-01-04 op (with-current-buffer (get-buffer-create "*vc-diff*")
119 6b6c8a78 2021-01-04 op (pop-to-buffer (current-buffer))
120 6b6c8a78 2021-01-04 op (let ((inhibit-read-only t))
121 6b6c8a78 2021-01-04 op (erase-buffer)
122 6b6c8a78 2021-01-04 op (diff-mode)
123 6b6c8a78 2021-01-04 op (if fileset
124 6b6c8a78 2021-01-04 op (dolist (file fileset)
125 6b6c8a78 2021-01-04 op (vc-got--diff "-s" file))
126 6b6c8a78 2021-01-04 op (vc-got--diff "-s")))))
127 6b6c8a78 2021-01-04 op
128 6b6c8a78 2021-01-04 op (provide 'vc-got-stage)
129 6b6c8a78 2021-01-04 op ;;; vc-got-stage.el ends here