From 2a00ec6a49b083f01a14e8f5ca9ffbd7f51137c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Geoffrey=20=E2=80=9CFrogeye=E2=80=9D=20Preud=27homme?= Date: Wed, 30 Oct 2024 16:30:37 +0100 Subject: [PATCH] Add jlab script --- hm/common.nix | 1 + hm/git/default.nix | 2 +- hm/scripts/jlab | 67 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 1 deletion(-) create mode 100755 hm/scripts/jlab diff --git a/hm/common.nix b/hm/common.nix index f0274c6..ec28e98 100644 --- a/hm/common.nix +++ b/hm/common.nix @@ -148,6 +148,7 @@ rename which file + cached-nix-shell # For scripts # Pipe utils gnugrep diff --git a/hm/git/default.nix b/hm/git/default.nix index 985dd44..ea6f1ef 100644 --- a/hm/git/default.nix +++ b/hm/git/default.nix @@ -21,7 +21,7 @@ in then ${lib.getExe config.programs.jujutsu.package} git fetch ${lib.getExe config.programs.jujutsu.package} rebase -d main@origin - ${lib.getExe config.programs.jujutsu.package} branch set main -r @- + ${lib.getExe config.programs.jujutsu.package} bookmark set main -r @- ${lib.getExe config.programs.jujutsu.package} git push else ${pkgs.git}/bin/git --no-optional-locks diff --quiet || echo "Repository is dirty!" diff --git a/hm/scripts/jlab b/hm/scripts/jlab new file mode 100755 index 0000000..1c0bf1e --- /dev/null +++ b/hm/scripts/jlab @@ -0,0 +1,67 @@ +#!/usr/bin/env cached-nix-shell +#! nix-shell -i python3 +#! nix-shell -p python3 python3Packages.gitpython + +""" +glab wrapper for jujutsu +""" + +import json +import pathlib +import pprint +import subprocess +import sys + +import git + + +def git_repo() -> git.Repo: + directory = pathlib.Path.cwd() + for parent in [directory] + list(directory.parents): + if parent.joinpath(".git").is_dir(): + break + else: + raise FileNotFoundError("Not a git repository") + return git.Repo(parent.as_posix()) + +def current_branch() -> str: + # FIXME + # Need to extract this from jj, can do with jj log --no-graph -T 'commit_id' + # (see templates) or show, also see revsets. + # How to search is a good question, usually it will be before @, + # but when editing it will be after. + # How to deal with nested MRs? (at least fail in that case) + + # sp = subprocess.run( + # ["glab", "mr", "view", branch, "--output", "json"], stdout=subprocess.PIPE + # ) + return "pouet" + + +def mr_info(branch: str) -> dict: + sp = subprocess.run( + ["glab", "mr", "view", branch, "--output", "json"], stdout=subprocess.PIPE + ) + return json.loads(sp.stdout) + + +def to_glab() -> None: + subprocess.run(["glab"] + sys.argv[1:]) + + +if sys.argv[1] == "mr": + branch = current_branch() + # TODO If no number specified, add it + if sys.argv[2] == "checkout": + data = mr_info(branch) + pprint.pprint(data) + # TODO If no commit on the branch, then create a jj one and make it have the title of the issue, + # also the branch name + else: + to_glab() +else: + to_glab() + +# TODO Push command that signs and pushes to the right repo + +# TODO Autocomplete