if test (count $argv) -lt 1; or test (count $argv) -gt 2 echo "usage: deploy-to [branch]" >&2 return 2 end set host $argv[1] set branch $argv[2] if test -z "$branch" set branch (git branch --show-current) end if test -z "$branch" echo "deploy-to: could not determine the current branch" >&2 return 2 end git rev-parse --show-toplevel >/dev/null 2>&1 or begin echo "deploy-to: run this from a git checkout" >&2 return 2 end set dirty_lines (git status --porcelain) if test (count $dirty_lines) -gt 0 echo "deploy-to: refusing to deploy with a dirty local worktree" >&2 return 1 end git fetch origin or return $status set remote_ref refs/remotes/origin/$branch git rev-parse --verify --quiet $remote_ref >/dev/null or begin echo "deploy-to: origin/$branch does not exist" >&2 return 1 end set local_head (git rev-parse HEAD) set remote_head (git rev-parse $remote_ref) if test "$local_head" != "$remote_head" echo "deploy-to: local HEAD differs from origin/$branch" >&2 echo "deploy-to: push or reset the branch before deploying" >&2 return 1 end set escaped_branch (string escape -- $branch) set escaped_host (string escape -- $host) set remote_command "cd ~/wrk/dotfiles && git fetch origin && git switch -- $escaped_branch && git reset --hard origin/$escaped_branch && sudo nixos-rebuild switch --flake .#$escaped_host --print-build-logs" ssh -t $host $remote_command