-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmajutsu-git.el
More file actions
325 lines (285 loc) · 12.1 KB
/
majutsu-git.el
File metadata and controls
325 lines (285 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
;;; majutsu-git.el --- Git integration commands for Majutsu -*- lexical-binding: t; -*-
;; Copyright (C) 2025-2026 0WD0
;; Author: 0WD0 <wd.1105848296@gmail.com>
;; Maintainer: 0WD0 <wd.1105848296@gmail.com>
;; Keywords: tools, vc
;; URL: https://github.com/0WD0/majutsu
;; SPDX-License-Identifier: GPL-3.0-or-later
;;; Commentary:
;; This library wraps jj's git-compatible commands and exposes them
;; through Majutsu transients.
;;; Code:
(require 'majutsu)
(require 'seq)
(require 'subr-x)
;;; majutsu-git
(defun majutsu-git--start (args &optional success-msg finish-callback)
"Start `jj git ARGS' asynchronously, for side-effects."
(majutsu-start-jj (append '("git") args) success-msg finish-callback))
(defun majutsu-git--remote-names (&optional directory)
"Return a list of Git remote names for DIRECTORY.
This calls `jj git remote list` and parses the first word of each line."
(let ((default-directory (or directory default-directory)))
(condition-case _
(let* ((lines (or (majutsu-jj-lines "git" "remote" "list") '()))
(names (delq nil
(mapcar (lambda (line)
(car (split-string line "[ :\t]+" t)))
lines))))
(delete-dups names))
(error nil))))
(defun majutsu-git--read-remote (prompt)
(let ((remotes (or (majutsu-git--remote-names (ignore-errors (majutsu--toplevel-safe)))
'("origin"))))
(majutsu-completing-read prompt remotes nil t)))
(defun majutsu-git--expand-option-arg (arg prefix)
"If ARG begins with PREFIX, expand the file name part."
(if (and (stringp arg) (string-prefix-p prefix arg))
(concat prefix
(majutsu-convert-filename-for-jj
(expand-file-name (substring arg (length prefix)))))
arg))
(transient-define-suffix majutsu-git-push (args)
"Push to git remote with ARGS."
(interactive (list (transient-args 'majutsu-git-push-transient)))
(majutsu--message-with-log "Pushing to remote...")
(majutsu-git--start (append '("push") args) "Pushed to remote"))
(defun majutsu-git-fetch (args)
"Fetch from git remote with ARGS from transient."
(interactive (list (transient-args 'majutsu-git-fetch-transient)))
(majutsu--message-with-log "Fetching from remote...")
(majutsu-git--start (append '("fetch") args) "Fetched from remote"))
(defun majutsu-git-remote-list ()
"List Git remotes in a dedicated buffer."
(interactive)
(majutsu-setup-buffer #'majutsu-git-remote-list-mode nil
:buffer "*Majutsu Git Remotes*"))
(defun majutsu-git--remote-line-name (line)
"Return the remote name parsed from LINE."
(let* ((raw (string-trim (substring-no-properties line)))
(token (car (split-string raw "[ \t]+" t))))
token))
(defun majutsu-git--wash-remote-list (_args)
"Wash `jj git remote list' output into remote sections."
(let ((count 0))
(magit-wash-sequence
(lambda ()
(let* ((line (buffer-substring (line-beginning-position)
(line-end-position)))
(trimmed (string-trim (substring-no-properties line)))
(name (and (not (string-empty-p trimmed))
(majutsu-git--remote-line-name line))))
(delete-region (line-beginning-position)
(min (point-max) (1+ (line-end-position))))
(when name
(setq count (1+ count))
(magit-insert-section (jj-git-remote name t)
(magit-insert-heading line)
(insert "\n")))
t)))
(if (zerop count)
(magit-cancel-section)
(insert "\n"))))
(defun majutsu-git-remote-list-refresh-buffer ()
"Refresh the git remote list buffer."
(majutsu--assert-mode 'majutsu-git-remote-list-mode)
(magit-insert-section (git-remote-list)
(majutsu-jj-wash #'majutsu-git--wash-remote-list nil
"git" "remote" "list")))
(defvar-keymap majutsu-git-remote-list-mode-map
:doc "Keymap for `majutsu-git-remote-list-mode'."
:parent majutsu-mode-map)
(define-derived-mode majutsu-git-remote-list-mode majutsu-mode "Majutsu Git Remotes"
"Major mode for viewing git remotes."
:group 'majutsu
(setq-local line-number-mode nil)
(setq-local revert-buffer-function #'majutsu-refresh-buffer))
(defun majutsu-git-remote-add (args)
"Add a Git remote. Prompts for name and URL; respects ARGS from transient."
(interactive (list (transient-args 'majutsu-git-remote-transient)))
(let* ((remote (read-string "Remote name: "))
(url (read-string (format "URL for %s: " remote)))
(fetch-tags (seq-find (lambda (a) (string-prefix-p "--fetch-tags=" a)) args))
(cmd-args (append '("remote" "add")
(when fetch-tags (list fetch-tags))
(list remote url)))
(exit (majutsu-run-jj "git" cmd-args)))
(when (zerop exit)
(message "Added remote %s" remote))))
(defun majutsu-git-remote-remove ()
"Remove a Git remote and forget its bookmarks."
(interactive)
(let ((remote (majutsu-git--read-remote "Remove remote: ")))
(when (and remote (not (string-empty-p remote)))
(let* ((cmd-args (list "remote" "remove" remote))
(exit (majutsu-run-jj "git" cmd-args)))
(when (zerop exit)
(message "Removed remote %s" remote))))))
(defun majutsu-git-remote-rename ()
"Rename a Git remote."
(interactive)
(let* ((old (majutsu-git--read-remote "Rename remote: "))
(new (read-string (format "New name for %s: " old))))
(when (and (not (string-empty-p old)) (not (string-empty-p new)))
(let* ((cmd-args (list "remote" "rename" old new))
(exit (majutsu-run-jj "git" cmd-args)))
(when (zerop exit)
(message "Renamed remote %s -> %s" old new))))))
(defun majutsu-git-remote-set-url ()
"Set URL of a Git remote."
(interactive)
(let* ((remote (majutsu-git--read-remote "Set URL for remote: "))
(url (read-string (format "New URL for %s: " remote))))
(when (and (not (string-empty-p remote)) (not (string-empty-p url)))
(let* ((cmd-args (list "remote" "set-url" remote url))
(exit (majutsu-run-jj "git" cmd-args)))
(when (zerop exit)
(message "Set URL for %s" remote))))))
(defun majutsu-git-clone (args)
"Clone a Git repo into a new jj repo.
Prompts for SOURCE and optional DEST; uses ARGS."
(interactive (list (transient-args 'majutsu-git-clone-transient)))
(let* ((source (read-string "Source (URL or path): "))
(dest (let ((d (read-directory-name "Destination (optional): " nil nil t)))
(when (and d (not (string-empty-p (expand-file-name d))))
;; If user picks current dir, treat as empty and let jj default
(let ((dd (directory-file-name d)))
(if (string= dd (directory-file-name default-directory)) nil dd)))))
(dest-arg (and dest (majutsu-convert-filename-for-jj dest)))
(cmd-args (append '("clone") args (list source) (and dest-arg (list dest-arg)))))
(majutsu--message-with-log "Cloning repository...")
(majutsu-git--start cmd-args "Clone completed")))
(defun majutsu-git-init (args)
"Initialize a new Git-backed jj repo. Prompts for DEST; uses ARGS."
(interactive (list (transient-args 'majutsu-git-init-transient)))
(let* ((dest (file-name-as-directory
(expand-file-name
(read-directory-name "Create repository in: " nil nil nil))))
(args (mapcar (lambda (arg)
(majutsu-git--expand-option-arg arg "--git-repo="))
args))
(cmd-args (append '("init") args (list (majutsu-convert-filename-for-jj dest)))))
(majutsu--message-with-log "Initializing repository...")
(majutsu-git--start cmd-args "Init completed")))
(defun majutsu-git-export ()
"Update the underlying Git repo with changes made in the repo."
(interactive)
(let ((exit (majutsu-run-jj "git" "export")))
(when (zerop exit)
(message "Exported to Git"))))
(defun majutsu-git-import ()
"Update repo with changes made in the underlying Git repo."
(interactive)
(let ((exit (majutsu-run-jj "git" "import")))
(when (zerop exit)
(message "Imported from Git"))))
(defun majutsu-git-root ()
"Show the underlying Git directory of the current repository."
(interactive)
(let* ((raw (string-trim (or (car (majutsu-jj-lines "git" "root")) "")))
(dir (unless (string-empty-p raw)
(majutsu-jj-expand-filename-from-jj raw default-directory))))
(if (or (null dir) (string-empty-p dir))
(message "No underlying Git directory found")
(kill-new dir)
(message "Git root: %s (copied)" dir))))
(transient-define-argument majutsu-git-push:-b ()
:description "Bookmark"
:class 'transient-option
:shortarg "-b"
:argument "--bookmark="
:multi-value 'repeat
:reader #'majutsu-read-bookmarks)
;;; Git Transients
;;;###autoload(autoload 'majutsu-git-transient "majutsu-git" nil t)
(transient-define-prefix majutsu-git-transient ()
"Top-level transient for jj git operations."
:man-page "jj-git"
:transient-non-suffix t
[:description "JJ Git"
:class transient-columns
["Sync"
("p" "Push" majutsu-git-push-transient)
("f" "Fetch" majutsu-git-fetch-transient)
("e" "Export" majutsu-git-export)
("m" "Import" majutsu-git-import)]
["Remotes"
("r" "Manage remotes" majutsu-git-remote-transient)
("o" "Git root" majutsu-git-root)]
["Repository"
("c" "Clone" majutsu-git-clone-transient)
("i" "Init" majutsu-git-init-transient)]
[("q" "Quit" transient-quit-one)]])
(transient-define-prefix majutsu-git-push-transient ()
"Transient for jj git push."
:man-page "jj-git-push"
[:description "JJ Git Push"
:class transient-columns
["Arguments"
("-R" "Remote" "--remote=" :choices majutsu-git--remote-names)
(majutsu-git-push:-b)
("-a" "All bookmarks" "--all")
("-t" "Tracked only" "--tracked")
("-D" "Deleted" "--deleted")
("-E" "Allow empty desc" "--allow-empty-description")
("-P" "Allow private" "--allow-private")
("-r" "Revisions" "--revisions=")
("-c" "Change" "--change=")
("-N" "Named X=REV" "--named=")
("-y" "Dry run" "--dry-run")]
[("p" "Push" majutsu-git-push)
("q" "Quit" transient-quit-one)]])
(transient-define-prefix majutsu-git-fetch-transient ()
"Transient for jj git fetch."
:man-page "jj-git-fetch"
[:description "JJ Git Fetch"
:class transient-columns
["Arguments"
("-R" "Remote" "--remote=" :choices majutsu-git--remote-names)
("-B" "Branch" "--branch=")
("-t" "Tracked only" "--tracked")
("-A" "All remotes" "--all-remotes")]
[("f" "Fetch" majutsu-git-fetch)
("q" "Quit" transient-quit-one)]])
(transient-define-prefix majutsu-git-remote-transient ()
"Transient for managing Git remotes."
:man-page "jj-git-remote"
[:description "JJ Git Remote"
:class transient-columns
["Arguments (add)"
("-T" "Fetch tags" "--fetch-tags="
:choices ("all" "included" "none"))]
["Actions"
("l" "List" majutsu-git-remote-list)
("a" "Add" majutsu-git-remote-add)
("d" "Remove" majutsu-git-remote-remove)
("r" "Rename" majutsu-git-remote-rename)
("u" "Set URL" majutsu-git-remote-set-url)
("q" "Quit" transient-quit-one)]])
(transient-define-prefix majutsu-git-clone-transient ()
"Transient for jj git clone."
:man-page "jj-git-clone"
[:description "JJ Git Clone"
:class transient-columns
["Arguments"
("-R" "Remote name" "--remote=")
("-C" "Colocate" "--colocate")
("-x" "No colocate" "--no-colocate")
("-d" "Depth" "--depth=")
("-T" "Fetch tags" "--fetch-tags=" :choices ("all" "included" "none"))]
[("c" "Clone" majutsu-git-clone)
("q" "Quit" transient-quit-one)]])
(transient-define-prefix majutsu-git-init-transient ()
"Transient for jj git init."
:man-page "jj-git-init"
[:description "JJ Git Init"
:class transient-columns
["Arguments"
("-C" "Colocate" "--colocate")
("-x" "No colocate" "--no-colocate")
("-g" "Use existing git repo" "--git-repo=")]
[("i" "Init" majutsu-git-init)
("q" "Quit" transient-quit-one)]])
;;; _
(provide 'majutsu-git)
;;; majutsu-git.el ends here