Skip to content

Commit b794e1e

Browse files
committed
Add ability to import unpublished PRs %216
1 parent 906f769 commit b794e1e

File tree

9 files changed

+215
-685
lines changed

9 files changed

+215
-685
lines changed

.devcontainer/devcontainer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@
77
"mounts": [
88
"source=ickb-${localWorkspaceFolderBasename}-node_modules,target=${containerWorkspaceFolder}/node_modules,type=volume",
99
"source=ickb-${localWorkspaceFolderBasename}-dist,target=${containerWorkspaceFolder}/dist,type=volume",
10-
"source=pnpm-cache,target=${containerWorkspaceFolder}/.pnpm-store,type=volume"
10+
"source=pnpm-cache,target=${containerWorkspaceFolder}/.pnpm-store,type=volume",
11+
"source=local-store,target=${containerWorkspaceFolder}/.local-store,type=volume"
1112
],
1213
// Features to add to the dev container. More info: https://containers.dev/features.
1314
// "features": {},
1415
// Use 'forwardPorts' to make a list of ports inside the container available locally.
1516
// "forwardPorts": [],
1617
// Use 'postCreateCommand' to run commands after the container is created.
17-
"postCreateCommand": "sudo chown node -R . && npm install -g pnpm@latest && pnpm install",
18+
"postCreateCommand": "sudo chown node -R . && npm install -g pnpm@latest",
1819
// Configure tool-specific properties.
1920
// "customizations": {},
21+
"postStartCommand": "pnpm install",
2022
"remoteUser": "node",
2123
"customizations": {
2224
// Configure properties specific to VS Code.

.devcontainer/setup-local-store.sh

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
# -e: exit immediately if any command exits with non-zero
4+
# -u: treat unset variables as errors
5+
# -o pipefail: fail if any command in a pipeline fails
6+
7+
# Usage: ./.devcontainer/setup-local-store.sh REPO_URL REF [REPO_URL REF …]
8+
# REPO_URL: Git repository HTTPS or SSH URL
9+
# REF: Either a commit SHA (7–40 hex chars), a PR number (digits), or a branch name
10+
if [ $# -lt 2 ] || (( $# % 2 )); then
11+
echo "Usage: $0 REPO_URL REF [REPO_URL REF …]" >&2
12+
exit 1
13+
fi
14+
15+
# Define workspace directories under current working directory
16+
ROOT=$(pwd)
17+
WORKDIR="$ROOT/.local-store" # all cloned repos go here
18+
CACHE="$WORKDIR/.cache" # store last-built SHAs here
19+
mkdir -p "$WORKDIR" "$CACHE" # ensure directories exist
20+
21+
# Process each pair of arguments: repository URL + reference
22+
while (( $# )); do
23+
REPO=$1; REF=$2; shift 2
24+
25+
# Derive short name from repo URL (strip trailing '.git')
26+
NAME=$(basename "$REPO" .git)
27+
REPO_DIR="$WORKDIR/$NAME"
28+
29+
# Clone the repo if not already cloned
30+
mkdir -p "$REPO_DIR"
31+
[ -d "$REPO_DIR/.git" ] || git clone "$REPO" "$REPO_DIR"
32+
33+
# Enter repository directory quietly
34+
pushd "$REPO_DIR" >/dev/null
35+
36+
# Fetch all remote updates and remove deleted refs
37+
git fetch origin --prune
38+
39+
# Initialize flag for detached-SHA workflow
40+
DETACHED=
41+
42+
# Case A: REF is a full (7–40 char) hex commit SHA
43+
if [[ $REF =~ ^[0-9a-f]{7,40}$ ]]; then
44+
# commit SHA
45+
BRANCH="FETCH_HEAD"
46+
SHA="$REF"
47+
DETACHED=1
48+
# explicitly fetch that commit
49+
git fetch --no-tags --depth=1 origin "$SHA"
50+
51+
# Case B: REF is all digits → treat as GitHub pull request number
52+
elif [[ $REF =~ ^[0-9]+$ ]]; then
53+
REMOTE_REF="pull/$REF/head"
54+
BRANCH="pr-$REF"
55+
56+
# Case C: otherwise, REF is a branch name under refs/heads
57+
else
58+
REMOTE_REF="refs/heads/$REF"
59+
# sanitize branch name (slashes → dashes)
60+
BRANCH=${REF//\//-}
61+
fi
62+
63+
# If not in detached-SHA mode, fetch the specific remote ref into our origin namespace
64+
if [[ -z ${DETACHED:-} ]]; then
65+
git fetch origin "$REMOTE_REF:refs/remotes/origin/$BRANCH" --force
66+
# Resolve the commit SHA on origin/<branch>
67+
SHA=$(git rev-parse "origin/$BRANCH")
68+
fi
69+
70+
# Prepare cache filename for this repository (single file per repo)
71+
CACHE_FILE="$CACHE/$NAME.sha"
72+
73+
# If last-built SHA matches current SHA, skip the build
74+
if [[ -f $CACHE_FILE && $(<"$CACHE_FILE") == "$SHA" ]]; then
75+
echo "$NAME/$BRANCH @ $SHA — up to date, skipping build"
76+
else
77+
# Otherwise, reset, clean, and check out the required commit/branch
78+
if [[ -n ${DETACHED:-} ]]; then
79+
# Detached-SHA: reset to the SHA, remove untracked files, then check out the SHA
80+
git reset --hard FETCH_HEAD
81+
git clean -fd
82+
git -c advice.detachedHead=false checkout FETCH_HEAD
83+
else
84+
# Branch or PR: reset to origin/<branch>, remove untracked files, recreate local branch
85+
git reset --hard "origin/$BRANCH"
86+
git clean -fd
87+
git checkout -B "$BRANCH" "origin/$BRANCH"
88+
fi
89+
90+
# Update cache and invoke build steps
91+
echo "$SHA" >"$CACHE_FILE"
92+
echo "Building $NAME/$BRANCH @ $SHA"
93+
pnpm install
94+
pnpm build
95+
fi
96+
97+
# Return to the root directory before next iteration
98+
popd >/dev/null
99+
done

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ dist
1414
dist-ssr
1515
*.local
1616
.pnpm-store
17+
.local-store
1718

1819
# Editor directories and files
1920
.vscode/*

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,24 @@ Use `./sync.sh` to create or update boilerplate in repositories.
66

77
Note: `./sync.sh` doesn't clean up old boilerplate files.
88

9+
Bonus: import un-published monorepos with:
10+
11+
```json
12+
{
13+
"scripts": {
14+
"preinstall": "./.devcontainer/setup-local-store.sh https://github.com/ckb-devrel/ccc.git 9d016b7c0d349f16162e9387532448c81d879f87",
15+
},
16+
"dependencies": {
17+
"@ckb-ccc/core": "link:.local-store/ccc/packages/core",
18+
"@ckb-ccc/udt": "link:.local-store/ccc/packages/udt"
19+
}
20+
}
21+
```
22+
23+
Usage: ./.devcontainer/setup-local-store.sh REPO_URL REF [REPO_URL REF …]
24+
REPO_URL: Git repository HTTPS or SSH URL
25+
REF: Either a commit SHA (7–40 hex chars), a PR number (digits), or a branch name
26+
927
## Dependencies
1028

1129
```mermaid

package.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@
3434
"scripts": {
3535
"prepare": "tsc",
3636
"lint": "eslint ./src",
37-
"clean": "rm -fr pnpm-lock.yaml dist/*",
38-
"revamp": "pnpm clean; pnpm up && node dist/index.js && pnpm lint"
37+
"clean": "rm -fr pnpm-lock.yaml dist/* .local-store/.cache/*",
38+
"preinstall": "./.devcontainer/setup-local-store.sh https://github.com/ckb-devrel/ccc.git 9d016b7c0d349f16162e9387532448c81d879f87",
39+
"revamp": "pnpm clean; pnpm up && pnpm node dist/index.js && pnpm lint"
3940
},
4041
"files": [
4142
"dist",
@@ -48,11 +49,12 @@
4849
"devDependencies": {
4950
"@eslint/js": "^9.29.0",
5051
"eslint": "^9.29.0",
51-
"prettier": "^3.5.3",
52+
"prettier": "^3.6.1",
5253
"typescript": "^5.8.3",
53-
"typescript-eslint": "^8.34.0"
54+
"typescript-eslint": "^8.35.0"
5455
},
5556
"dependencies": {
56-
"@ckb-ccc/core": "canary"
57+
"@ckb-ccc/core": "link:.local-store/ccc/packages/core",
58+
"@ckb-ccc/udt": "link:.local-store/ccc/packages/udt"
5759
}
5860
}

0 commit comments

Comments
 (0)