Files
majjit/majjit.el

170 lines
6.0 KiB
EmacsLisp
Raw Normal View History

2026-02-24 13:07:35 +01:00
;;; majjit.el --- Clone jujutsu repositories with Magit's interface -*- lexical-binding: t -*-
2026-02-24 00:13:18 +01:00
;; Author: Lucien Cartier-Tilet <lucien@phundrak.com>
;; Maintainer: Lucien Cartier-Tilet <lucien@phundrak.com>
;; Version: 0.1.0
;; Package-Requires: ((emacs "28.1") (magit "4.5.0"))
2026-02-24 13:07:35 +01:00
;; Homepage: https://labs.phundrak.com/phundrak/majjit
2026-02-24 00:13:18 +01:00
;; Keywords: git tools vcs
;; This file is not part of GNU Emacs
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; commentary
;;; Code:
(require 'magit)
2026-02-24 13:07:35 +01:00
(defgroup majjit nil
2026-02-24 00:13:18 +01:00
"Clone your jj repositories with Magit."
:group 'tools
2026-02-24 13:07:35 +01:00
:prefix "majjit-"
:link '(url-link :tag "https://labs.phundrak.com/phundrak/majjit"))
2026-02-24 00:13:18 +01:00
2026-02-24 13:07:35 +01:00
(defcustom majjit-default-directory nil
2026-02-24 00:13:18 +01:00
"See `magit-clone-default-directory'."
2026-02-24 13:07:35 +01:00
:package-version '(majjit . "0.1.0")
:group 'majjit
2026-02-24 00:13:18 +01:00
:type '(choice (const :tag "Value of default-directory")
(directory :tag "Constant directory")
(function :tag "Function's value")))
2026-02-24 13:07:35 +01:00
(defcustom majjit-executable "jj"
2026-02-24 00:13:18 +01:00
"Local executable of jj."
2026-02-24 13:07:35 +01:00
:package-version '(majjit . "0.1.0")
:group 'majjit
2026-02-24 00:13:18 +01:00
:type 'path)
2026-02-24 13:07:35 +01:00
(defcustom majjit-remote-executable "jj"
2026-02-24 00:13:18 +01:00
"Remote executable of jj."
2026-02-24 13:07:35 +01:00
:package-version '(majjit . "0.1.0")
:group 'majjit
2026-02-24 00:13:18 +01:00
:type 'path)
2026-02-24 13:07:35 +01:00
(defcustom majjit-global-arguments
2026-02-24 00:13:18 +01:00
'("--color=never")
"Global jj arguments."
2026-02-24 13:07:35 +01:00
:package-version '(majjit . "0.1.0")
:group 'majjit
2026-02-24 00:13:18 +01:00
:type '(repeat string))
2026-02-24 13:07:35 +01:00
(defun majjit-sentinel (process event)
"Default sentinel used by `majjit-run-jj-async'."
2026-02-24 00:13:18 +01:00
(when (memq (process-status process) '(exit signal))
(setq event (substring event 0 -1))
(when (string-match "^finished" event)
(message (concat (process-name process) " finished")))
(when (eq process magit-this-process)
(setq magit-this-process nil))))
2026-02-24 13:07:35 +01:00
(defun majjit-executable ()
2026-02-24 00:13:18 +01:00
"Return executable for jj.
See `magit-git-executable'."
(if (file-remote-p default-directory)
2026-02-24 13:07:35 +01:00
majjit-remote-executable
majjit-executable))
2026-02-24 00:13:18 +01:00
2026-02-24 13:07:35 +01:00
(defun majjit-run-jj-async (directory &rest args)
2026-02-24 00:13:18 +01:00
"Start jujutsu and return the process object.
ARGS is flattened and then used as arguments to jujutsu. Once cloning is
done, open DIRECTORY with `find-file'.
Inspired by `magit-start-git'."
(let ((default-process-coding-system (magit--process-coding-system))
2026-02-24 13:07:35 +01:00
(magit-git-global-arguments majjit-global-arguments))
2026-02-24 00:13:18 +01:00
(apply #'magit-start-process
2026-02-24 13:07:35 +01:00
(majjit-executable)
2026-02-24 00:13:18 +01:00
nil
(magit-process-git-arguments args))
;; Don't refresh the buffer we're calling from.
(process-put magit-this-process 'inhibit-refresh t)
(set-process-sentinel magit-this-process
(lambda (process event)
2026-02-24 13:07:35 +01:00
(majjit-sentinel process event)
2026-02-24 00:13:18 +01:00
(find-file directory)))))
2026-02-24 13:07:35 +01:00
(defun majjit-read-args ()
2026-02-24 00:13:18 +01:00
"Get all the necessary args for cloning the repository."
2026-02-24 13:07:35 +01:00
(let* ((magit-clone-default-directory majjit-default-directory)
2026-02-24 00:13:18 +01:00
(magit-args (magit-clone-read-args)))
(list (car magit-args)
(cadr magit-args)
(yes-or-no-p "Colocate the repository? "))))
2026-02-24 13:07:35 +01:00
(defun majjit--check-valid-clone-directory (repository directory)
2026-02-24 00:13:18 +01:00
"Check whether the target DIRECTORY is a valid target.
Taken from `magit-clone-internal'. Refer to it for use of REPOSITORY and
DIRECTORY."
(let ((directory (file-name-as-directory (expand-file-name directory))))
(when (file-exists-p directory)
(if (file-directory-p directory)
(when (length> (directory-files directory) 2)
(let ((name (magit-clone--url-to-name repository)))
(unless (and name
(setq directory (file-name-as-directory
(expand-file-name name directory)))
(not (file-exists-p directory)))
(user-error "%s already exists" directory))))
(user-error "%s already exists and is not a directory" directory)))))
2026-02-24 13:07:35 +01:00
(defun majjit--internal (repository directory colocate-p)
2026-02-24 00:13:18 +01:00
"Clone REPOSITORY to DIRECTORY using jj.
If COLOCATE-P is t, colocate the repository with git."
(let ((directory (file-name-as-directory (expand-file-name directory))))
2026-02-24 13:07:35 +01:00
(majjit--check-valid-clone-directory repository directory)
(majjit-run-jj-async directory
2026-02-24 00:13:18 +01:00
"git"
"clone"
(if colocate-p "--colocate" nil)
"--"
repository
(magit-convert-filename-for-git directory))))
;;;###autoload
(defun majjit-clone-from-url (url)
"Clone a repository from a known URL."
(let ((directory (read-directory-name
"Clone to: "
(if (functionp majjit-default-directory)
(funcall majjit-default-directory)
majjit-default-directory)
nil nil
(magit-clone--url-to-name url)))
(colocate-p (yes-or-no-p "Colocate? ")))
(majjit--internal url directory colocate-p)))
2026-02-24 00:13:18 +01:00
;;;###autoload
2026-02-24 13:07:35 +01:00
(defun majjit-clone (repository directory colocate-p)
2026-02-24 00:13:18 +01:00
"Clone a git REPOSITORY using jujutsu into a DIRECTORY.
If COLOCATE-P is t, then colocate the repository with git.
Depends on Magit. Inspired by `magit-clone-regular'."
2026-02-24 13:07:35 +01:00
(interactive (majjit-read-args))
(majjit--internal repository directory colocate-p))
2026-02-24 00:13:18 +01:00
2026-02-24 13:07:35 +01:00
(provide 'majjit)
2026-02-24 00:13:18 +01:00
2026-02-24 13:07:35 +01:00
;;; majjit.el ends here