first pass of merging in warp (doesn't build)

This commit is contained in:
Ryan Ward
2026-07-01 16:08:58 -05:00
parent 2f64909469
commit 4770ac06b5
3662 changed files with 414574 additions and 89772 deletions
+108 -30
View File
@@ -12,8 +12,8 @@ if [ -z "$WARP_BOOTSTRAPPED" ]; then
# Appended to $DCS_START to signal that the following message is JSON-encoded.
DCS_JSON_MARKER="d"
# Byte used to signal the end of a DCS.
DCS_END="$(printf '\x9c')"
# Byte sequence used to signal the end of a DCS (7-bit ST: ESC \).
DCS_END="$(printf '\x1b\x5c')"
OSC_START="$(printf '\e]9278;')"
@@ -280,10 +280,11 @@ if [ -z "$WARP_BOOTSTRAPPED" ]; then
if [ "$WARP_IN_MSYS2" = true ]; then
warp_send_hook_via_kv_pairs_start "Preexec"
warp_send_hook_kv_pair "command" "$BASH_COMMAND"
warp_send_hook_kv_pair "session_id" "$WARP_SESSION_ID"
warp_send_hook_via_kv_pairs_end
else
local truncated_command=$(warp_escape_json "$BASH_COMMAND")
warp_send_json_message "{\"hook\": \"Preexec\", \"value\": {\"command\": \"$truncated_command\"}}"
warp_send_json_message "{\"hook\": \"Preexec\", \"value\": {\"command\": \"$truncated_command\", \"session_id\": $WARP_SESSION_ID}}"
fi
warp_maybe_send_reset_grid_osc
@@ -325,7 +326,7 @@ if [ -z "$WARP_BOOTSTRAPPED" ]; then
# If the array is not empty, kill the ongoing pids.
if [[ ! -z $pids ]]; then
# Surpress stderr output; kill writes to stderr if any of the given
# Suppress stderr output; kill writes to stderr if any of the given
# PIDS are not running (which might rarely be the case due to race
# conditions in checking which PIDS to cancel and this kill command.
kill -9 $pids >/dev/null 2>/dev/null
@@ -362,7 +363,7 @@ if [ -z "$WARP_BOOTSTRAPPED" ]; then
fi
# Note that in older versions of bash (the one builtin with MacOS) the `~` character is just that,
# howerver, when used within `""` (double quotes) on a newer (homebrew) bash version,
# however, when used within `""` (double quotes) on a newer (homebrew) bash version,
# it automatically EXPANDS to the actual value of $HOME and needs to be escaped to give a proper
# tilde character. So instead, we have it as a separate variable that uses `''` (single quote)
# to avoid expanding, and use it later within the new bash term title. This way both old and
@@ -434,13 +435,15 @@ if [ -z "$WARP_BOOTSTRAPPED" ]; then
# executed within this block instead of the actual last
# command that was run.
local exit_code=$?
local next_block_id="precmd-$WARP_SESSION_ID-$((block_id++))"
if [ "$WARP_IN_MSYS2" = true ]; then
warp_send_hook_via_kv_pairs_start "CommandFinished"
warp_send_hook_kv_pair "exit_code" "$exit_code"
warp_send_hook_kv_pair "next_block_id" "precmd-$WARP_SESSION_ID-$((block_id++))"
warp_send_hook_kv_pair "next_block_id" "$next_block_id"
warp_send_hook_kv_pair "session_id" "$WARP_SESSION_ID"
warp_send_hook_via_kv_pairs_end
else
warp_send_json_message "{\"hook\": \"CommandFinished\", \"value\": {\"exit_code\": $exit_code, \"next_block_id\": \"precmd-$WARP_SESSION_ID-$((block_id++))\"}}"
warp_send_json_message "{\"hook\": \"CommandFinished\", \"value\": {\"exit_code\": $exit_code, \"next_block_id\": \"$next_block_id\", \"session_id\": $WARP_SESSION_ID}}"
fi
warp_maybe_send_reset_grid_osc
@@ -463,6 +466,8 @@ if [ -z "$WARP_BOOTSTRAPPED" ]; then
unset _WARP_GENERATOR_COMMAND
warp_send_json_message "{\"hook\": \"Precmd\", \"value\": {
\"exit_code\": $exit_code,
\"next_block_id\": \"$next_block_id\",
\"pwd\": \"\",
\"ps1\": \"\",
\"git_head\": \"\",
@@ -574,37 +579,56 @@ if [ -z "$WARP_BOOTSTRAPPED" ]; then
escaped_conda_env=$(warp_escape_json "$CONDA_DEFAULT_ENV")
fi
# Get Node.js version if node is available and we're in a Node.js project
if command -v node > /dev/null 2>&1 && [ "$WARP_IN_MSYS2" = false ]; then
# Get the Node.js version, but only when the Node.js Version chip is enabled.
# Warp sets WARP_PROMPT_NODE_VERSION_ENABLED to "0" when the chip is not in the
# prompt (defaulting to enabled when unset), so we avoid spawning `node` on
# every prompt when the chip is not shown.
if [[ "$WARP_PROMPT_NODE_VERSION_ENABLED" != "0" ]] && command -v node > /dev/null 2>&1 && [ "$WARP_IN_MSYS2" = false ]; then
# Check for package.json in current directory and parent directories
local current_dir="$PWD"
local found_package_json=false
local package_json_dir=""
while [[ "$current_dir" != "/" ]]; do
while [[ -n "$current_dir" ]]; do
if [[ -f "$current_dir/package.json" ]]; then
found_package_json=true
package_json_dir="$current_dir"
break
fi
current_dir=$(dirname "$current_dir")
[[ "$current_dir" == "/" ]] && break
# Strip the last path segment without spawning `dirname`.
current_dir="${current_dir%/*}"
[[ -z "$current_dir" ]] && current_dir="/"
done
# Only show node version if package.json is within a git repository
if [[ "$found_package_json" = true ]]; then
local git_dir="$package_json_dir"
local in_git_repo=false
while [[ "$git_dir" != "/" ]]; do
while [[ -n "$git_dir" ]]; do
if [[ -d "$git_dir/.git" ]]; then
in_git_repo=true
break
fi
git_dir=$(dirname "$git_dir")
[[ "$git_dir" == "/" ]] && break
git_dir="${git_dir%/*}"
[[ -z "$git_dir" ]] && git_dir="/"
done
if [[ "$in_git_repo" = true ]]; then
local node_version=$(node --version 2>/dev/null)
if [[ -n "$node_version" ]]; then
escaped_node_version=$(warp_escape_json "$node_version")
# Cache the resolved version keyed on PWD + PATH so we only spawn
# `node --version` when the directory or PATH changes (PATH changes
# on `nvm use`). The cache vars are global (no `local`) so they
# persist across precmd invocations.
local node_cache_key="$PWD:$PATH"
if [[ "$node_cache_key" == "$_WARP_NODE_VERSION_CACHE_KEY" ]]; then
escaped_node_version="$_WARP_NODE_VERSION_CACHE_VALUE"
else
local node_version=$(node --version 2>/dev/null)
if [[ -n "$node_version" ]]; then
escaped_node_version=$(warp_escape_json "$node_version")
fi
_WARP_NODE_VERSION_CACHE_KEY="$node_cache_key"
_WARP_NODE_VERSION_CACHE_VALUE="$escaped_node_version"
fi
fi
fi
@@ -649,6 +673,8 @@ if [ -z "$WARP_BOOTSTRAPPED" ]; then
# We send the escaped PS1, if we are in active Warp prompt mode, for prompt preview rendering (note the shell's PS1 is unset in this case).
if [ "$WARP_IN_MSYS2" = true ]; then
warp_send_hook_via_kv_pairs_start "Precmd"
warp_send_hook_kv_pair "exit_code" "$exit_code"
warp_send_hook_kv_pair "next_block_id" "$next_block_id"
warp_send_hook_kv_pair "pwd" "$PWD"
warp_send_hook_kv_pair_escaped "ps1" "$deref_ps1"
warp_send_hook_kv_pair "ps1_is_encoded" "false"
@@ -662,6 +688,8 @@ if [ -z "$WARP_BOOTSTRAPPED" ]; then
warp_send_hook_via_kv_pairs_end
else
local escaped_json="{\"hook\": \"Precmd\", \"value\": {
\"exit_code\": $exit_code,
\"next_block_id\": \"$next_block_id\",
\"pwd\": \"$escaped_pwd\",
\"ps1\": \"$escaped_ps1\",
\"honor_ps1\": $honor_ps1,
@@ -761,10 +789,11 @@ if [ -z "$WARP_BOOTSTRAPPED" ]; then
if [ "$WARP_IN_MSYS2" = true ]; then
warp_send_hook_via_kv_pairs_start "InputBuffer"
warp_send_hook_kv_pair "buffer" "$READLINE_LINE"
warp_send_hook_kv_pair "session_id" "$WARP_SESSION_ID"
warp_send_hook_via_kv_pairs_end
else
local escaped_input="$(warp_escape_json "$READLINE_LINE")"
warp_send_json_message "{ \"hook\": \"InputBuffer\", \"value\": { \"buffer\": \"$escaped_input\" } }"
warp_send_json_message "{ \"hook\": \"InputBuffer\", \"value\": { \"buffer\": \"$escaped_input\", \"session_id\": $WARP_SESSION_ID } }"
fi
# This prevents bash from re-printing typeahead after we've removed it.
READLINE_LINE=""
@@ -888,9 +917,10 @@ if [ -z "$WARP_BOOTSTRAPPED" ]; then
function clear() {
if [ "$WARP_IN_MSYS2" = true ]; then
warp_send_hook_via_kv_pairs_start "Clear"
warp_send_hook_kv_pair "session_id" "$WARP_SESSION_ID"
warp_send_hook_via_kv_pairs_end
else
warp_send_json_message "{\"hook\": \"Clear\", \"value\": {}}"
warp_send_json_message "{\"hook\": \"Clear\", \"value\": {\"session_id\": $WARP_SESSION_ID}}"
fi
}
@@ -899,9 +929,10 @@ if [ -z "$WARP_BOOTSTRAPPED" ]; then
if [ "$WARP_IN_MSYS2" = true ]; then
warp_send_hook_via_kv_pairs_start "FinishUpdate"
warp_send_hook_kv_pair "update_id" "$update_id"
warp_send_hook_kv_pair "session_id" "$WARP_SESSION_ID"
warp_send_hook_via_kv_pairs_end
else
warp_send_json_message "{ \"hook\": \"FinishUpdate\", \"value\": { \"update_id\": \"$update_id\"} }"
warp_send_json_message "{ \"hook\": \"FinishUpdate\", \"value\": { \"update_id\": \"$update_id\", \"session_id\": $WARP_SESSION_ID} }"
fi
}
@@ -972,11 +1003,51 @@ if [ -z "$WARP_BOOTSTRAPPED" ]; then
function warp_ssh_helper() {
init_shell_bash=$(init_shell_hook "bash")
init_shell_zsh=$(init_shell_hook "zsh")
local remote_session_id=$(command -p od -An -N8 -tu8 /dev/urandom 2>/dev/null | command -p tr -d ' \n')
if [[ -z "$remote_session_id" || "$remote_session_id" == "0" ]]; then
# If we cannot generate a non-zero random token, run plain SSH instead.
command ssh "${@:1}"
return
fi
# Hex-encode the ZSH environment script we use to bootstrap remote zsh b/c it contains control characters
# We decode on the SSH server using xxd if its available, otherwise fall back to a for-loop over each byte
# and use printf to convert back to plaintext
local zsh_env_script=$(printf '%s' 'unsetopt ZLE; unset RCS; unset GLOBAL_RCS; WARP_SESSION_ID="$(command -p date +%s)$RANDOM"; WARP_USING_WINDOWS_CON_PTY=@@USING_CON_PTY_BOOLEAN@@; WARP_HONOR_PS1='$WARP_HONOR_PS1'; _hostname=$(command -pv hostname >/dev/null 2>&1 && command -p hostname 2>/dev/null || command -p uname -n); _user=$(command -pv whoami >/dev/null 2>&1 && command -p whoami 2>/dev/null || echo $USER); _msg=$(printf "{\"hook\": \"InitShell\", \"value\": {\"session_id\": $WARP_SESSION_ID, \"shell\": \"zsh\", \"user\": \"%s\", \"hostname\": \"%s\"}}" "$_user" "$_hostname" | command -p od -An -v -tx1 | command -p tr -d '"'"' \n'"'"'); printf '"'"'\e]9278;d;%s\x07'"'"' $_msg; unset _hostname _user _msg' | command -p od -An -v -tx1 | command -p tr -d ' \n')
local zsh_env_script=$(printf '%s' 'unsetopt ZLE; unset RCS; unset GLOBAL_RCS; WARP_SESSION_ID='$remote_session_id'; WARP_USING_WINDOWS_CON_PTY=@@USING_CON_PTY_BOOLEAN@@; WARP_HONOR_PS1='$WARP_HONOR_PS1'; _hostname=$(command -pv hostname >/dev/null 2>&1 && command -p hostname 2>/dev/null || command -p uname -n); _user=$(command -pv whoami >/dev/null 2>&1 && command -p whoami 2>/dev/null || echo $USER); _msg=$(printf "{\"hook\": \"InitShell\", \"value\": {\"session_id\": $WARP_SESSION_ID, \"shell\": \"zsh\", \"user\": \"%s\", \"hostname\": \"%s\"}}" "$_user" "$_hostname" | command -p od -An -v -tx1 | command -p tr -d '"'"' \n'"'"'); printf '"'"'\e]9278;d;%s\x07'"'"' $_msg; unset _hostname _user _msg' | command -p od -An -v -tx1 | command -p tr -d ' \n')
# Optionally attach to an existing ControlMaster the user already
# runs for this destination instead of creating our own. Resolve
# the user's configured ControlPath with `ssh -G` (which expands
# tokens like %h/%p/%r/%C into a literal path), then verify the
# master is alive with `ssh -O check`. Both probes are local-only
# commands. On any failure we fall back to creating a Warp-owned
# master, preserving the existing behavior.
local control_path="$SSH_SOCKET_DIR/$WARP_SESSION_ID"
local control_master_mode="yes"
local external_control_master="false"
if [[ "$WARP_SSH_REUSE_CONTROL_MASTER" == "1" ]]; then
local user_control_path=$(command ssh -G "${@:1}" 2>/dev/null | command -p sed -n 's/^controlpath //p')
case "$user_control_path" in
"" | none)
# No ControlPath configured for this destination.
;;
*[![:alnum:]._/~@:+,-]*)
# The resolved path contains characters we cannot safely
# embed in the SSH hook JSON below (e.g. an unexpanded %
# token, quotes, or whitespace); fall back to a
# Warp-owned master.
;;
*)
if command ssh -O check -o ControlPath="$user_control_path" "${@:1}" >/dev/null 2>&1; then
# A live master exists: multiplex through it and let
# the client know Warp does not own it.
control_path="$user_control_path"
control_master_mode="no"
external_control_master="true"
fi
;;
esac
fi
# Keep remote commands up-to-date with shell.rs & bash.sh.
# Note that in this command, we're passing a string to the remote shell. Any variable expansions need to be
@@ -985,7 +1056,7 @@ if [ -z "$WARP_BOOTSTRAPPED" ]; then
# determine what shell is the login shell on the remote machine. We perform a preliminary check to see if
# the remote shell is the Bourne shell to avoid asking it to parse later lines that use syntax it doesn't
# support.
command ssh -o ControlMaster=yes -o ControlPath=$SSH_SOCKET_DIR/$WARP_SESSION_ID \
command ssh -o ControlMaster=$control_master_mode -o ControlPath="$control_path" \
-t "${@:1}" \
"
export TERM_PROGRAM='WarpTerminal'
@@ -997,7 +1068,7 @@ test -n '$WARP_CLIENT_VERSION' && export WARP_CLIENT_VERSION='$WARP_CLIENT_VERSI
# Only forward the protocol version if it was set locally (i.e. the HOANotifications feature flag is on).
test -n '$WARP_CLI_AGENT_PROTOCOL_VERSION' && export WARP_CLI_AGENT_PROTOCOL_VERSION='$WARP_CLI_AGENT_PROTOCOL_VERSION'
hook="'$(printf "{\"hook\": \"SSH\", \"value\": {\"socket_path\": \"'$SSH_SOCKET_DIR/$WARP_SESSION_ID'\", \"remote_shell\": \"%s\"}}" "${SHELL##*/}" | command -p od -An -v -tx1 | command -p tr -d " \n")'"
hook="'$(printf "{\"hook\": \"SSH\", \"value\": {\"socket_path\": \"'$control_path'\", \"remote_shell\": \"%s\", \"session_id\": '"$WARP_SESSION_ID"', \"remote_session_id\": '"$remote_session_id"', \"external_control_master\": '"$external_control_master"'}}" "${SHELL##*/}" | command -p od -An -v -tx1 | command -p tr -d " \n")'"
printf '$OSC_START$DCS_JSON_MARKER$OSC_PARAM_SEPARATOR%s$OSC_END' "'$hook'"
if test "'"${SHELL##*/}" != "bash" -a "${SHELL##*/}" != "zsh"'"; then
@@ -1032,7 +1103,7 @@ case "'${SHELL##*/}'" in
command -p stty raw
HISTCONTROL=ignorespace
HISTIGNORE=" *"
WARP_SESSION_ID="$(command -p date +%s)$RANDOM"
WARP_SESSION_ID='$remote_session_id'
WARP_HONOR_PS1="'$WARP_HONOR_PS1'"
_hostname=$(command -pv hostname >/dev/null 2>&1 && command -p hostname 2>/dev/null || command -p uname -n)
_user=$(command -v whoami >/dev/null 2>&1 && command whoami 2>/dev/null || echo $USER)
@@ -1063,7 +1134,7 @@ esac
function ssh() {
if is_interactive_ssh_session "$@"; then
warp_send_json_message "{\"hook\": \"PreInteractiveSSHSession\", \"value\": {}}"
warp_send_json_message "{\"hook\": \"PreInteractiveSSHSession\", \"value\": {\"session_id\": $WARP_SESSION_ID}}"
# If the SSH wrapper is not enabled for this session, don't use it.
if [ "$WARP_USE_SSH_WRAPPER" = "1" ]; then
@@ -1133,17 +1204,22 @@ esac
rcfiles_end_time="$(LC_ALL="C"; echo $EPOCHREALTIME)"
fi
# Unset HISTFILESIZE if the user rcfiles didn't change it away from our
# very large sentinel value. We need to set the initial value of HISTSIZE
# to ensure that the user's history file doesn't get truncated when we spawn
# the shell, but once bootstrap has completes, we want the value to be what
# it would have been if we hadn't set an initial value.
# Unset HISTFILESIZE and HISTSIZE if the user rcfiles didn't change them
# away from our very large sentinel values. We need to set the initial
# values to ensure that the user's history file and in-memory history list
# don't get truncated when we spawn the shell, but once bootstrap has
# completed, we want the values to be what they would have been if we hadn't
# set initial values.
#
# For more context, see: https://github.com/warpdotdev/Warp/issues/1262
if [[ $HISTFILESIZE == $WARP_INITIAL_HISTFILESIZE ]]; then
unset HISTFILESIZE
fi
unset WARP_INITIAL_HISTFILESIZE
if [[ $HISTSIZE == $WARP_INITIAL_HISTSIZE ]]; then
unset HISTSIZE
fi
unset WARP_INITIAL_HISTSIZE
# Save the value of HISTCONTROL as it existed just after reading the user's
# rcfiles.
@@ -1318,6 +1394,7 @@ esac
warp_send_hook_kv_pair "user" "$_user"
warp_send_hook_kv_pair "hostname" "$_hostname"
warp_send_hook_kv_pair "path" "$PATH"
warp_send_hook_kv_pair "cdpath" "$CDPATH"
warp_send_hook_kv_pair_escaped "env_var_names" "$env_var_names"
warp_send_hook_kv_pair "abbreviations" ""
warp_send_hook_kv_pair_escaped "aliases" "$aliases"
@@ -1338,7 +1415,8 @@ esac
else
local escaped_editor="$(warp_escape_json "$EDITOR")"
local escaped_shell_path="$(warp_escape_json "$BASH")"
local escaped_json="{\"hook\": \"Bootstrapped\", \"value\": {\"histfile\": \"$escaped_histfile\", \"session_id\": $WARP_SESSION_ID, \"shell\": \"bash\", \"home_dir\": \"$HOME\", \"user\":\"$_user\", \"host\":\"$_hostname\", \"path\": \"$escaped_path\", \"editor\": \"$escaped_editor\", \"env_var_names\": \"$escaped_env_var_names\", \"abbreviations\": \"$escaped_abbrs\", \"aliases\": \"$escaped_aliases\", \"function_names\": \"$escaped_function_names\", \"builtins\": \"$escaped_builtins\", \"keywords\": \"$escaped_keywords\", \"shell_version\": \"$BASH_VERSION\", \"shell_options\": \"$escaped_shell_options\", \"rcfiles_start_time\": \"$rcfiles_start_time\", \"rcfiles_end_time\": \"$rcfiles_end_time\", \"vi_mode_enabled\": \"$vi_mode_enabled\", \"os_category\": \"$os_category\", \"linux_distribution\": \"$linux_distribution\", \"wsl_name\": \"$WSL_DISTRO_NAME\", \"shell_path\": \"$escaped_shell_path\"}}"
local escaped_cdpath="$(warp_escape_json "$CDPATH")"
local escaped_json="{\"hook\": \"Bootstrapped\", \"value\": {\"histfile\": \"$escaped_histfile\", \"session_id\": $WARP_SESSION_ID, \"shell\": \"bash\", \"home_dir\": \"$HOME\", \"user\":\"$_user\", \"host\":\"$_hostname\", \"path\": \"$escaped_path\", \"cdpath\": \"$escaped_cdpath\", \"editor\": \"$escaped_editor\", \"env_var_names\": \"$escaped_env_var_names\", \"abbreviations\": \"$escaped_abbrs\", \"aliases\": \"$escaped_aliases\", \"function_names\": \"$escaped_function_names\", \"builtins\": \"$escaped_builtins\", \"keywords\": \"$escaped_keywords\", \"shell_version\": \"$BASH_VERSION\", \"shell_options\": \"$escaped_shell_options\", \"rcfiles_start_time\": \"$rcfiles_start_time\", \"rcfiles_end_time\": \"$rcfiles_end_time\", \"vi_mode_enabled\": \"$vi_mode_enabled\", \"os_category\": \"$os_category\", \"linux_distribution\": \"$linux_distribution\", \"wsl_name\": \"$WSL_DISTRO_NAME\", \"shell_path\": \"$escaped_shell_path\"}}"
warp_send_json_message "$escaped_json"
fi
}