-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathmajutsu-git.el
More file actions
493 lines (430 loc) · 17.3 KB
/
Copy pathmajutsu-git.el
File metadata and controls
493 lines (430 loc) · 17.3 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
;;; 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 'majutsu-gerrit)
(require 'majutsu-remote)
(require 'crm)
(require 'seq)
(require 'subr-x)
(autoload 'majutsu-gerrit-upload-transient "majutsu-gerrit-upload" nil t)
(declare-function majutsu-bookmark-list "majutsu-bookmark" ())
(declare-function majutsu-repository-transient-prefix "majutsu-core")
(declare-function majutsu-transient-read-remote-patterns "majutsu-remote")
;;; 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))
(defvar majutsu-git-url-history nil
"Minibuffer history for Git remote URLs and paths.")
(defun majutsu-git--read-url-or-path (prompt)
"Read a Git URL or local path with PROMPT."
(majutsu-read-string prompt nil 'majutsu-git-url-history))
(defun majutsu-git--transient-read-url-or-path (prompt initial-input _history)
"Read a Git URL/path transient value with PROMPT and INITIAL-INPUT."
(majutsu-read-string prompt initial-input 'majutsu-git-url-history))
(defun majutsu-git--pathish-url-p (value)
"Return non-nil if VALUE should be converted as a local path for jj."
(and (stringp value)
(or (file-remote-p value)
(file-name-absolute-p value)
(string= value "~")
(string-prefix-p "~/" value))))
(defun majutsu-git--url-or-path-arg (value)
"Return VALUE converted for jj when it is an Emacs local/remote path."
(if (majutsu-git--pathish-url-p value)
(majutsu-convert-filename-for-jj (expand-file-name value))
value))
(defun majutsu-git--expand-option-arg (arg prefix)
"If ARG begins with PREFIX, expand the file name part."
(if-let* ((value (transient-arg-value prefix (list arg))))
(concat prefix
(majutsu-convert-filename-for-jj
(expand-file-name value)))
arg))
(defun majutsu-git--expand-url-option-arg (arg prefix)
"If ARG begins with PREFIX, convert its URL/path value for jj."
(if-let* ((value (transient-arg-value prefix (list arg))))
(concat prefix (majutsu-git--url-or-path-arg value))
arg))
(defun majutsu-git--expand-remote-url-arg (arg)
"Convert ARG as a remote URL option or positional URL/path."
(let* ((expanded (majutsu-git--expand-url-option-arg arg "--fetch="))
(expanded (majutsu-git--expand-url-option-arg expanded "--push=")))
(if (equal expanded arg)
(majutsu-git--url-or-path-arg arg)
expanded)))
(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 ()
"Show Git remotes in the bookmark list."
(interactive)
(require 'majutsu-bookmark)
(pop-to-buffer (majutsu-bookmark-list))
(when-let* ((section (magit-get-section
'((bookmark-group . remote) (bookmark-list)))))
(magit-section-show section)
(goto-char (oref section start))))
(defun majutsu-git-fetch-from (remote)
"Fetch from exactly one named REMOTE."
(interactive (list (majutsu-read-remote-name "Fetch remote" t)))
(majutsu-git-fetch (list "--remote" (concat "exact:" remote))))
(defun majutsu-git-fetch-remote ()
"Fetch only the Git remote at point."
(interactive)
(majutsu-git-fetch-from (or (majutsu-remote-at-point)
(user-error "No Git remote at point"))))
(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-add-transient)))
(let* ((args (mapcar (lambda (arg)
(majutsu-git--expand-url-option-arg arg "--push-url="))
args))
(remote (majutsu-read-new-remote-name "Remote name"))
(url (majutsu-git--url-or-path-arg
(majutsu-git--read-url-or-path (format "URL for %s" remote))))
(exit (majutsu-run-jj
"git" (append '("remote" "add") args (list remote url)))))
(when (zerop exit)
(message "Added remote %s" remote))))
(defun majutsu-git-remote-remove (remote)
"Remove a Git remote and forget its bookmarks."
(interactive
(let ((remote (majutsu-read-remote-name "Remove remote" t)))
(unless (majutsu-confirm
'git-remote-remove
(format "Remove Git remote %s and forget its bookmarks? " remote))
(user-error "Remove canceled"))
(list 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-read-remote-name "Rename remote" t))
(new (majutsu-read-new-remote-name (format "New name for %s" old)))
(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 (args)
"Set URL of a Git remote."
(interactive (list (transient-args 'majutsu-git-remote-set-url-transient)))
(let* ((remote (transient-arg-value "--remote=" args))
(args (seq-remove (lambda (arg)
(transient-arg-value "--remote=" (list arg)))
args)))
(unless remote
(user-error "Remote is required"))
(let* ((args (mapcar #'majutsu-git--expand-remote-url-arg args))
(exit (majutsu-run-jj
"git" (append '("remote" "set-url") (list remote) 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 (majutsu-git--url-or-path-arg
(majutsu-git--read-url-or-path "Source (URL or path)")))
(dest (let ((d (read-directory-name "Destination (optional): " nil nil nil)))
(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))))
(defun majutsu-git-push--read-revset (prompt initial-input history)
"Read revset for `jj git push --revision='."
(when-let* ((value (majutsu-read-revset
prompt
:allow-empty t
:initial-input initial-input
:history history
:completion-args '("git" "push" "-r"))))
(split-string value crm-separator t)))
(defun majutsu-git-push--read-change (prompt initial-input history)
"Read change id for `jj git push --change='."
(majutsu-read-revision
prompt
:allow-empty t
:initial-input initial-input
:history history
:completion-args '("git" "push" "-c")))
(transient-define-argument majutsu-git-push:--revision ()
:description "Revisions"
:class 'transient-option
:shortarg "-r"
:argument "--revision="
:multi-value 'repeat
:prompt "Revisions: "
:reader #'majutsu-git-push--read-revset)
(transient-define-argument majutsu-git-push:--change ()
:description "Change"
:class 'transient-option
:shortarg "-c"
:argument "--change="
:prompt "Change: "
:reader #'majutsu-git-push--read-change)
(transient-define-argument majutsu-git-push:--named ()
:description "Named X=REV"
:class 'transient-option
:key "-N"
:argument "--named="
:prompt "Named (X=REV): ")
(transient-define-argument majutsu-git-push:--option ()
:description "Git option"
:class 'transient-option
:shortarg "-o"
:argument "--option="
:multi-value 'repeat
:prompt "Git push option: ")
(transient-define-argument majutsu-git-fetch:--remote ()
:description "Remote"
:class 'transient-option
:key "-R"
:argument "--remote="
:multi-value 'repeat
:prompt "Remote: "
:reader #'majutsu-transient-read-remote-patterns)
(transient-define-argument majutsu-git-push:--remote ()
:description "Remote"
:class 'transient-option
:key "-R"
:argument "--remote="
:prompt "Remote: "
:reader #'majutsu-transient-read-remote-name)
(transient-define-argument majutsu-git-clone:--remote ()
:description "Remote"
:class 'transient-option
:key "-R"
:argument "--remote="
:prompt "Remote: "
:reader #'majutsu-transient-read-remote-name)
(transient-define-argument majutsu-git:--branch ()
:description "Branch"
:class 'transient-option
:shortarg "-b"
:argument "--branch="
:multi-value 'repeat
:prompt "Branch: ")
(transient-define-argument majutsu-git:--bookmark ()
:description "Bookmark"
:class 'transient-option
:shortarg "-b"
:argument "--bookmark="
:multi-value 'repeat
:reader #'majutsu-read-bookmark-patterns)
(transient-define-argument majutsu-git:--tag ()
:description "Tag"
:class 'transient-option
:key "-T"
:argument "--tag="
:multi-value 'repeat
:reader #'majutsu-read-tag-patterns)
(transient-define-argument majutsu-git-remote-add:--push-url ()
:description "Push URL"
:class 'transient-option
:key "-P"
:argument "--push-url="
:prompt "Push URL: "
:reader #'majutsu-git--transient-read-url-or-path)
(transient-define-argument majutsu-git-remote-set-url:--fetch ()
:description "Fetch URL"
:class 'transient-option
:key "-f"
:argument "--fetch="
:prompt "Fetch URL: "
:reader #'majutsu-git--transient-read-url-or-path)
(transient-define-argument majutsu-git-remote-set-url:--push ()
:description "Push URL"
:class 'transient-option
:key "-p"
:argument "--push="
:prompt "Push URL: "
:reader #'majutsu-git--transient-read-url-or-path)
(transient-define-argument majutsu-git-remote-set-url:--remote ()
:description "Remote"
:class 'transient-option
:key "-R"
:argument "--remote="
:prompt "Remote: "
:reader #'majutsu-transient-read-remote-name)
(defun majutsu-git-push--repo-args (args)
"Keep only stable `jj git push' ARGS for repository defaults."
(seq-filter (lambda (arg)
(or (transient-arg-value "--remote=" (list arg))
(member arg '("--all" "--tracked" "--deleted"
"--allow-empty-description" "--allow-private"))))
args))
(defun majutsu-git-fetch--repo-args (args)
"Keep only stable `jj git fetch' ARGS for repository defaults."
(seq-filter (lambda (arg)
(or (transient-arg-value "--remote=" (list arg))
(member arg '("--tracked" "--all-remotes"))))
args))
;;; 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"
[["Sync"
("p" "Push" majutsu-git-push-transient)
("f" "Fetch" majutsu-git-fetch-transient)
("u" "Gerrit upload" majutsu-gerrit-upload-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)]])
(transient-define-prefix majutsu-git-push-transient ()
"Transient for jj git push."
:man-page "jj-git-push"
:class 'majutsu-repository-transient-prefix
:repo-namespace 'majutsu-git
:repo-key 'majutsu-git-push
:repo-filter #'majutsu-git-push--repo-args
:description "JJ Git Push"
[["Arguments"
(majutsu-git-push:--remote)
(majutsu-git:--bookmark)
(majutsu-git:--tag)
("-a" "All bookmarks and tags" "--all")
("-t" "Tracked only" "--tracked")
("-D" "Deleted" "--deleted")
("-E" "Allow empty desc" "--allow-empty-description")
("-P" "Allow private" "--allow-private")
(majutsu-git-push:--revision)
(majutsu-git-push:--change)
(majutsu-git-push:--named)
(majutsu-git-push:--option)
("-y" "Dry run" "--dry-run")]
[("p" "Push" majutsu-git-push)
("W" "Save repo defaults" majutsu-transient-save-repository-defaults)]])
(transient-define-prefix majutsu-git-fetch-transient ()
"Transient for jj git fetch."
:man-page "jj-git-fetch"
:class 'majutsu-repository-transient-prefix
:repo-namespace 'majutsu-git
:repo-key 'majutsu-git-fetch
:repo-filter #'majutsu-git-fetch--repo-args
:description "JJ Git Fetch"
[["Arguments"
(majutsu-git-fetch:--remote)
(majutsu-git:--branch)
(majutsu-git:--tag)
("-t" "Tracked only" "--tracked")
("-A" "All remotes" "--all-remotes")]
[("f" "Fetch" majutsu-git-fetch)
("W" "Save repo defaults" majutsu-transient-save-repository-defaults)]])
(transient-define-prefix majutsu-git-remote-add-transient ()
"Transient for adding a Git remote."
:man-page "jj-git-remote-add"
:description "JJ Git Remote Add"
[["Arguments"
("-T" "Fetch tags" "--fetch-tags="
:choices ("all" "included" "none"))
(majutsu-git-remote-add:--push-url)]
[("a" "Add" majutsu-git-remote-add)]])
(transient-define-prefix majutsu-git-remote-set-url-transient ()
"Transient for setting Git remote URLs."
:man-page "jj-git-remote-set-url"
:description "JJ Git Remote Set URL"
[["Arguments"
(majutsu-git-remote-set-url:--remote)
(majutsu-git-remote-set-url:--fetch)
(majutsu-git-remote-set-url:--push)]
[("u" "Set URL" majutsu-git-remote-set-url)]])
(transient-define-prefix majutsu-git-remote-transient ()
"Transient for managing Git remotes."
:man-page "jj-git-remote"
:description "JJ Git Remote"
[["Actions"
("l" "List" majutsu-git-remote-list)
("a" "Add" majutsu-git-remote-add-transient)
("d" "Remove" majutsu-git-remote-remove)
("r" "Rename" majutsu-git-remote-rename)
("u" "Set URL" majutsu-git-remote-set-url-transient)]])
(transient-define-prefix majutsu-git-clone-transient ()
"Transient for jj git clone."
:man-page "jj-git-clone"
:description "JJ Git Clone"
[["Arguments"
(majutsu-git-clone:--remote)
("-C" "Colocate" "--colocate")
("-x" "No colocate" "--no-colocate")
("-d" "Depth" "--depth=" :reader #'transient-read-number-N+)
("-T" "Fetch tags" "--fetch-tags=" :choices ("all" "included" "none"))
(majutsu-git:--branch)]
[("c" "Clone" majutsu-git-clone)]])
(transient-define-prefix majutsu-git-init-transient ()
"Transient for jj git init."
:man-page "jj-git-init"
:description "JJ Git Init"
[["Arguments"
("-C" "Colocate" "--colocate")
("-x" "No colocate" "--no-colocate")
("-g" "Use existing git repo" "--git-repo=")]
[("i" "Init" majutsu-git-init)]])
;;; _
(provide 'majutsu-git)
;;; majutsu-git.el ends here