Skip to content

Commit ae33727

Browse files
committed
add dev/vendor-lib.sh
1 parent 759cc5f commit ae33727

File tree

1 file changed

+166
-0
lines changed

1 file changed

+166
-0
lines changed

dev/vendor-lib.sh

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
6+
ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
7+
SRC_REPO="$ROOT_DIR/../sourcegraph"
8+
SRC_LIB="$SRC_REPO/lib"
9+
DEST_LIB="$ROOT_DIR/lib"
10+
11+
# Validate source repository
12+
echo "Validating source repository..."
13+
cd "$SRC_REPO"
14+
15+
# Check if on main branch
16+
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
17+
if [ "$CURRENT_BRANCH" != "main" ]; then
18+
echo "Error: Source repository is not on main branch (currently on: $CURRENT_BRANCH)"
19+
exit 1
20+
fi
21+
22+
# Check for uncommitted changes
23+
if ! git diff-index --quiet HEAD -- ; then
24+
echo "Error: Source repository has uncommitted changes"
25+
git status --short
26+
exit 1
27+
fi
28+
29+
# Get commit info
30+
COMMIT_HASH=$(git rev-parse HEAD)
31+
COMMIT_DATE=$(git show -s --format=%ci HEAD)
32+
33+
echo "Source repository validated:"
34+
echo " Branch: $CURRENT_BRANCH"
35+
echo " Commit: $COMMIT_HASH"
36+
echo " Date: $COMMIT_DATE"
37+
38+
cd "$ROOT_DIR"
39+
40+
echo ""
41+
echo "Discovering packages to vendor..."
42+
43+
# Get direct imports (including test imports)
44+
DIRECT_IMPORTS=$(go list -f '{{ join .Imports "\n" }}
45+
{{ join .TestImports "\n" }}
46+
{{ join .XTestImports "\n" }}' ./... | grep 'github.com/sourcegraph/sourcegraph/lib' | sort | uniq || true)
47+
48+
if [ -z "$DIRECT_IMPORTS" ]; then
49+
echo "Error: No lib imports found"
50+
exit 1
51+
fi
52+
53+
echo "Direct imports found (including test imports):"
54+
echo "$DIRECT_IMPORTS"
55+
56+
# Get transitive dependencies within lib
57+
echo ""
58+
echo "Finding transitive dependencies..."
59+
cd "$SRC_REPO"
60+
TRANSITIVE=$(echo "$DIRECT_IMPORTS" | xargs go list -f '{{ join .Deps "\n" }}' 2>/dev/null | grep 'github.com/sourcegraph/sourcegraph/lib' | sort | uniq || true)
61+
cd "$SCRIPT_DIR"
62+
63+
# Combine and deduplicate
64+
ALL_PACKAGES=$(echo -e "$DIRECT_IMPORTS\n$TRANSITIVE" | sort | uniq)
65+
66+
echo ""
67+
echo "All packages to vendor:"
68+
echo "$ALL_PACKAGES"
69+
70+
# Remove existing lib directory
71+
if [ -d "$DEST_LIB" ]; then
72+
echo ""
73+
echo "Removing existing $DEST_LIB directory..."
74+
rm -rf "$DEST_LIB"
75+
fi
76+
77+
# Create lib directory
78+
mkdir -p "$DEST_LIB"
79+
80+
# Copy each package
81+
echo ""
82+
echo "Copying packages..."
83+
for pkg in $ALL_PACKAGES; do
84+
# Extract the path after github.com/sourcegraph/sourcegraph/lib/
85+
pkg_path=${pkg#github.com/sourcegraph/sourcegraph/lib/}
86+
87+
src_dir="$SRC_LIB/$pkg_path"
88+
dest_dir="$DEST_LIB/$pkg_path"
89+
90+
if [ ! -d "$src_dir" ]; then
91+
echo "Warning: Source directory not found: $src_dir"
92+
continue
93+
fi
94+
95+
echo " $pkg_path"
96+
mkdir -p "$dest_dir"
97+
98+
# Copy Go files (excluding tests) and other relevant files
99+
for gofile in "$src_dir"/*.go; do
100+
[ -e "$gofile" ] || continue
101+
if [[ ! "$gofile" =~ _test\.go$ ]]; then
102+
cp "$gofile" "$dest_dir/" 2>/dev/null || true
103+
fi
104+
done
105+
cp -r "$src_dir"/*.mod "$dest_dir/" 2>/dev/null || true
106+
cp -r "$src_dir"/*.sum "$dest_dir/" 2>/dev/null || true
107+
108+
# Copy any subdirectories that might contain embedded files or test data
109+
for item in "$src_dir"/*; do
110+
if [ -d "$item" ] && [ "$(basename "$item")" != "." ] && [ "$(basename "$item")" != ".." ]; then
111+
subdir=$(basename "$item")
112+
# Skip if it's a package we're already copying separately
113+
if ! echo "$ALL_PACKAGES" | grep -q "lib/$pkg_path/$subdir"; then
114+
if ls "$item"/*.go 2>/dev/null | grep -v '_test\.go$' >/dev/null 2>&1; then
115+
# Contains non-test Go files, skip it (it's a separate package)
116+
continue
117+
fi
118+
# Copy non-package directories (e.g., testdata, embedded files)
119+
cp -r "$item" "$dest_dir/" 2>/dev/null || true
120+
fi
121+
fi
122+
done
123+
done
124+
125+
# Create go.mod file
126+
echo ""
127+
echo "Creating go.mod file..."
128+
cat > "$DEST_LIB/go.mod" <<EOF
129+
module github.com/sourcegraph/sourcegraph/lib
130+
131+
go 1.23
132+
EOF
133+
134+
# Create README.md
135+
echo "Creating README.md file..."
136+
cat > "$DEST_LIB/README.md" <<EOF
137+
# Vendored lib packages
138+
139+
This directory contains vendored packages from \`github.com/sourcegraph/sourcegraph/lib\`.
140+
141+
## Source
142+
143+
- **Repository**: https://github.com/sourcegraph/sourcegraph
144+
- **Commit**: $COMMIT_HASH
145+
- **Date**: $COMMIT_DATE
146+
147+
## Updating
148+
149+
To update these vendored packages, run:
150+
151+
\`\`\`bash
152+
./dev/vendor-lib.sh
153+
\`\`\`
154+
155+
The script will:
156+
1. Validate that \`../sourcegraph\` is on the \`main\` branch with no uncommitted changes
157+
2. Discover all direct and transitive dependencies on \`lib\` packages
158+
3. Copy only the needed packages
159+
4. Update this README with the new commit information
160+
EOF
161+
162+
echo ""
163+
echo "Vendoring complete! Packages copied to $DEST_LIB"
164+
165+
cd "$DEST_LIB"
166+
go mod tidy

0 commit comments

Comments
 (0)