From 92d4554b622ea85ca08d351f696f48a37c67e8b0 Mon Sep 17 00:00:00 2001 From: Kane York Date: Sun, 16 Aug 2020 18:03:32 -0700 Subject: [PATCH] feat(tools/tvlc): init project tvlc is a tool for managing sparse git worktrees of the TVL depot. It is still in development; near-term tasks include a setup script, Nix dependency resolution, worktree removal, and the dispatch script. See cs.tvl.fyi/depot/docs/designs/SPARSE_CHECKOUTS.md for more info. Change-Id: Iad96656f0206178980fe7dcadd3dffe70d690f8f Reviewed-on: https://cl.tvl.fyi/c/depot/+/1760 Tested-by: BuildkiteCI Reviewed-by: tazjin --- third_party/default.nix | 1 + tools/tvlc/OWNERS | 3 ++ tools/tvlc/common.sh | 30 ++++++++++++ tools/tvlc/default.nix | 35 ++++++++++++++ tools/tvlc/tvlc-new | 102 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 171 insertions(+) create mode 100644 tools/tvlc/OWNERS create mode 100644 tools/tvlc/common.sh create mode 100644 tools/tvlc/default.nix create mode 100755 tools/tvlc/tvlc-new diff --git a/third_party/default.nix b/third_party/default.nix index 897602f31..2c5bfa4ad 100644 --- a/third_party/default.nix +++ b/third_party/default.nix @@ -137,6 +137,7 @@ let rustc s6-portable-utils sbcl + shellcheck sqlite stdenvNoCC stern diff --git a/tools/tvlc/OWNERS b/tools/tvlc/OWNERS new file mode 100644 index 000000000..9e7830ab2 --- /dev/null +++ b/tools/tvlc/OWNERS @@ -0,0 +1,3 @@ +inherited: true +owners: + - riking diff --git a/tools/tvlc/common.sh b/tools/tvlc/common.sh new file mode 100644 index 000000000..2c8cc603c --- /dev/null +++ b/tools/tvlc/common.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +set -eu +set -o pipefail + +XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}" +tvlc_root="$XDG_DATA_HOME/tvlc" + +if [ -f "$tvlc_root"/nice_checkout_root ]; then + nice_checkout_root="$(cat "$tvlc_root"/nice_checkout_root)" +fi +nice_checkout_root="${nice_checkout_root:-$HOME/tvlc}" + +depot_root= +if [ -f "$tvlc_root/depot_root" ]; then + depot_root="$(cat "$tvlc_root/depot_root")" +fi +if [ -d /depot ]; then + # don't require config on tvl nixos servers + depot_root="${depot_root:-/depot}" +fi +if [ -n "$depot_root" ]; then + export DEPOT_ROOT="$depot_root" +fi + +if [ ! -d "$tvlc_root" ]; then + echo "tvlc: setup required" + echo "please run 'tvlc setup' from the depot root" + exit 1 +fi diff --git a/tools/tvlc/default.nix b/tools/tvlc/default.nix new file mode 100644 index 000000000..de18af236 --- /dev/null +++ b/tools/tvlc/default.nix @@ -0,0 +1,35 @@ +{ pkgs, depot, ... }: + +let + commonsh = ./common.sh; + + # TODO(riking): path deduction + #tvix-instantiate="${third_party.nix}/bin/nix-instantiate" + pathScripts = pkgs.writeShellScript "imports" '' + ''; + + # setup: git rev-parse --show-toplevel > $tvlc_root/depot_root + # setup: mkdir $tvlc_root/clients + # setup: echo 1 > $tvlc_root/next_clientid + + tvlcNew = pkgs.stdenv.mkDerivation { + name = "tvlc-new"; + src = ./tvlc-new; + doCheck = true; + + unpackPhase = "true"; + buildPhase = '' + substitute ${./tvlc-new} $out --replace common.sh ${commonsh} + ''; + checkPhase = '' + ${pkgs.shellcheck}/bin/shellcheck $out ${commonsh} && echo "SHELLCHECK OK" + ''; + installPhase = '' + chmod +x $out + ''; + }; + +in pkgs.stdenv.mkDerivation rec { + inherit commonsh; + inherit tvlcNew; +} diff --git a/tools/tvlc/tvlc-new b/tools/tvlc/tvlc-new new file mode 100755 index 000000000..e3e65b4f6 --- /dev/null +++ b/tools/tvlc/tvlc-new @@ -0,0 +1,102 @@ +#!/bin/bash + +source common.sh + +set -eu +set -o pipefail + +function usage() { + echo "tvlc new [-n|--name CLIENTNAME] [derivation...]" + echo "" + cat < "$tvlc_root/next_clientid" + +checkout_dir="$tvlc_root/clients/$checkout_id" +mkdir "$checkout_dir" +cd "$DEPOT_ROOT" +git worktree add --no-checkout -b "$branch_name" "$checkout_dir" +# BUG: git not creating the /info/ subdir +mkdir "$DEPOT_ROOT/.git/worktrees/$checkout_id/info" + +cd "$checkout_dir" +git sparse-checkout init --cone +git sparse-checkout set "$@" + +ln -s "$checkout_dir" "$nice_checkout_root"/"$checkout_name" + +echo "$nice_checkout_root/$checkout_name"