Skip to content

Commit 5166a24

Browse files
Fix metta command to detect and use closest checkout (#1629)
When running metta from a git worktree (or another checkout), it now detects and uses the uv project from the current repository (instead of the uv project from which the metta cli was originally installed) This allows the metta command to work correctly with multiple worktrees. [Asana Task](https://app.asana.com/1/1209016784099267/project/1210348820405981/task/1210873403437186)
1 parent 8df8b9e commit 5166a24

2 files changed

Lines changed: 27 additions & 5 deletions

File tree

‎metta/setup/installer/bin/metta‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,31 @@
22
# Wrapper script for metta command
33
# This allows users to run 'metta' directly without prefixing with 'uv run'
44

5-
# Get the directory where this script is located
6-
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
7-
# Get the project root directory (three levels up from metta/setup/installer/bin)
8-
PROJECT_DIR="$(cd "$SCRIPT_DIR/../../.." && pwd)"
5+
# Function to find the metta repository root
6+
find_metta_root() {
7+
local dir="$PWD"
8+
while [ "$dir" != "/" ]; do
9+
# Check if this is a metta repository
10+
if [ -f "$dir/metta/setup/metta_cli.py" ] && [ -d "$dir/.git" ]; then
11+
echo "$dir"
12+
return 0
13+
fi
14+
dir="$(dirname "$dir")"
15+
done
16+
return 1
17+
}
18+
19+
# First, try to find if we're in a metta repository
20+
if metta_root=$(find_metta_root); then
21+
# We're in a metta repository (could be main repo or worktree)
22+
PROJECT_DIR="$metta_root"
23+
else
24+
# Fall back to the original install location
25+
# Get the real path of this script (following symlinks)
26+
SCRIPT_PATH="$(readlink -f "$0" 2>/dev/null || readlink "$0" 2>/dev/null || echo "$0")"
27+
SCRIPT_DIR="$(cd "$(dirname "$SCRIPT_PATH")" && pwd)"
28+
PROJECT_DIR="$(cd "$SCRIPT_DIR/../../.." && pwd)"
29+
fi
930

1031
# Run metta using uv with the project directory
1132
exec uv run --project "$PROJECT_DIR" python -m metta.setup.metta_cli "$@"

‎metta/setup/metta_cli.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import sys
66
from pathlib import Path
77

8+
from metta.common.util.fs import get_repo_root
89
from metta.setup.config import CURRENT_CONFIG_VERSION, PROFILE_DEFINITIONS, SetupConfig, UserType
910
from metta.setup.local_commands import LocalCommands
1011
from metta.setup.registry import get_all_modules, get_applicable_modules
@@ -17,7 +18,7 @@
1718

1819
class MettaCLI:
1920
def __init__(self):
20-
self.repo_root: Path = Path(__file__).parent.parent.parent
21+
self.repo_root: Path = get_repo_root()
2122
self.config: SetupConfig = SetupConfig()
2223
self.path_setup: PathSetup = PathSetup(self.repo_root)
2324
self.local_commands: LocalCommands = LocalCommands(self.repo_root)

0 commit comments

Comments
 (0)