| 3e3af55 | | | 1 | #!/usr/bin/env bash |
| 3e3af55 | | | 2 | # Grove - Import a Git repository into Mononoke |
| 0d60b20 | | | 3 | # Uses the gitimport tool (inside the mononoke container) to import a Git repo |
| 3e3af55 | | | 4 | # into Mononoke's blobstore. |
| 3e3af55 | | | 5 | # |
| 0d60b20 | | | 6 | # Usage: ./import-repo.sh <git-repo-path-or-url> <repo-name> [commit-sha] |
| 0d60b20 | | | 7 | # |
| 0d60b20 | | | 8 | # If commit-sha is provided, does incremental import (missing-for-commit). |
| 0d60b20 | | | 9 | # Otherwise, does a full-repo import (first time only). |
| 3e3af55 | | | 10 | # |
| 3e3af55 | | | 11 | # Examples: |
| 36387cc | | | 12 | # ./import-repo.sh /path/to/local/repo grove |
| 3e3af55 | | | 13 | # ./import-repo.sh https://github.com/user/repo.git myrepo |
| 36387cc | | | 14 | # ./import-repo.sh /path/to/local/repo grove abc123 # incremental |
| 3e3af55 | | | 15 | |
| 3e3af55 | | | 16 | set -euo pipefail |
| 3e3af55 | | | 17 | |
| 3e3af55 | | | 18 | SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 3e3af55 | | | 19 | GROVE_DIR="$SCRIPT_DIR/.." |
| 0d60b20 | | | 20 | HUB_DIR="$GROVE_DIR/hub" |
| 0d60b20 | | | 21 | DATA_DIR="${GROVE_DATA_DIR:-/data/grove}" |
| 0d60b20 | | | 22 | CONFIG_DIR="$DATA_DIR/mononoke-config" |
| 3e3af55 | | | 23 | |
| 0d60b20 | | | 24 | GIT_SOURCE="${1:?Usage: ./import-repo.sh <git-repo-path-or-url> <repo-name> [commit-sha]}" |
| 0d60b20 | | | 25 | REPO_NAME="${2:?Usage: ./import-repo.sh <git-repo-path-or-url> <repo-name> [commit-sha]}" |
| 0d60b20 | | | 26 | COMMIT_SHA="${3:-}" |
| 3e3af55 | | | 27 | |
| 3e3af55 | | | 28 | echo "=== Grove: Importing Git repo into Mononoke ===" |
| 3e3af55 | | | 29 | echo "Source: $GIT_SOURCE" |
| 3e3af55 | | | 30 | echo "Repo name: $REPO_NAME" |
| 0d60b20 | | | 31 | if [[ -n "$COMMIT_SHA" ]]; then |
| 0d60b20 | | | 32 | echo "Mode: incremental (missing-for-commit $COMMIT_SHA)" |
| 0d60b20 | | | 33 | else |
| 0d60b20 | | | 34 | echo "Mode: full-repo" |
| 0d60b20 | | | 35 | fi |
| 3e3af55 | | | 36 | echo "" |
| 3e3af55 | | | 37 | |
| 3e3af55 | | | 38 | # Step 1: Clone/prepare bare repo for import |
| 0d60b20 | | | 39 | BARE_REPO="$DATA_DIR/$REPO_NAME-bare.git" |
| 0d60b20 | | | 40 | |
| 0d60b20 | | | 41 | echo "Step 1: Preparing bare clone at $BARE_REPO..." |
| 0d60b20 | | | 42 | if [[ -d "$BARE_REPO" ]]; then |
| 0d60b20 | | | 43 | echo " Bare repo already exists, updating..." |
| 0d60b20 | | | 44 | if [[ -d "$GIT_SOURCE" ]]; then |
| 0d60b20 | | | 45 | (cd "$GIT_SOURCE" && git push "$BARE_REPO" --all --force) |
| 0d60b20 | | | 46 | elif [[ "$GIT_SOURCE" == http* ]] || [[ "$GIT_SOURCE" == git@* ]]; then |
| 0d60b20 | | | 47 | git clone --bare "$GIT_SOURCE" "${BARE_REPO}.tmp" |
| 0d60b20 | | | 48 | rm -rf "$BARE_REPO" |
| 0d60b20 | | | 49 | mv "${BARE_REPO}.tmp" "$BARE_REPO" |
| 0d60b20 | | | 50 | fi |
| 3e3af55 | | | 51 | else |
| 0d60b20 | | | 52 | if [[ -d "$GIT_SOURCE" ]]; then |
| 0d60b20 | | | 53 | git clone --bare "$GIT_SOURCE" "$BARE_REPO" |
| 0d60b20 | | | 54 | elif [[ "$GIT_SOURCE" == http* ]] || [[ "$GIT_SOURCE" == git@* ]]; then |
| 0d60b20 | | | 55 | git clone --bare "$GIT_SOURCE" "$BARE_REPO" |
| 0d60b20 | | | 56 | else |
| 0d60b20 | | | 57 | echo "Error: $GIT_SOURCE is not a valid git repo path or URL" |
| 0d60b20 | | | 58 | exit 1 |
| 0d60b20 | | | 59 | fi |
| 3e3af55 | | | 60 | fi |
| 3e3af55 | | | 61 | echo " Bare clone ready at $BARE_REPO" |
| 3e3af55 | | | 62 | |
| 3e3af55 | | | 63 | # Step 2: Create repo config if it doesn't exist |
| 3e3af55 | | | 64 | REPO_DEF_DIR="$CONFIG_DIR/repo_definitions/$REPO_NAME" |
| 3e3af55 | | | 65 | REPO_CFG_DIR="$CONFIG_DIR/repos/$REPO_NAME" |
| 3e3af55 | | | 66 | |
| 3e3af55 | | | 67 | if [[ ! -d "$REPO_DEF_DIR" ]]; then |
| 3e3af55 | | | 68 | echo "" |
| 3e3af55 | | | 69 | echo "Step 2: Creating Mononoke config for '$REPO_NAME'..." |
| 3e3af55 | | | 70 | |
| 3e3af55 | | | 71 | # Find next available repo_id |
| 3e3af55 | | | 72 | MAX_ID=0 |
| 3e3af55 | | | 73 | for f in "$CONFIG_DIR"/repo_definitions/*/server.toml; do |
| 3e3af55 | | | 74 | if [[ -f "$f" ]]; then |
| 3e3af55 | | | 75 | ID=$(grep -oP 'repo_id\s*=\s*\K[0-9]+' "$f" 2>/dev/null || echo 0) |
| 3e3af55 | | | 76 | if (( ID > MAX_ID )); then |
| 3e3af55 | | | 77 | MAX_ID=$ID |
| 3e3af55 | | | 78 | fi |
| 3e3af55 | | | 79 | fi |
| 3e3af55 | | | 80 | done |
| 3e3af55 | | | 81 | NEXT_ID=$((MAX_ID + 1)) |
| 3e3af55 | | | 82 | |
| 3e3af55 | | | 83 | mkdir -p "$REPO_DEF_DIR" "$REPO_CFG_DIR" |
| 3e3af55 | | | 84 | |
| 3e3af55 | | | 85 | cat > "$REPO_DEF_DIR/server.toml" <<EOF |
| 3e3af55 | | | 86 | repo_id = $NEXT_ID |
| 3e3af55 | | | 87 | repo_name = "$REPO_NAME" |
| 3e3af55 | | | 88 | repo_config = "$REPO_NAME" |
| 3e3af55 | | | 89 | enabled = true |
| 6c9fcae | | | 90 | hipster_acl = "default" |
| 3e3af55 | | | 91 | EOF |
| 3e3af55 | | | 92 | |
| 3e3af55 | | | 93 | cat > "$REPO_CFG_DIR/server.toml" <<EOF |
| 3e3af55 | | | 94 | storage_config = "default" |
| 3e3af55 | | | 95 | |
| 0d60b20 | | | 96 | [hook_manager_params] |
| 0d60b20 | | | 97 | disable_acl_checker = true |
| 0d60b20 | | | 98 | |
| 3e3af55 | | | 99 | [push] |
| 3e3af55 | | | 100 | pure_push_allowed = true |
| 3e3af55 | | | 101 | |
| 3e3af55 | | | 102 | [pushrebase] |
| 3e3af55 | | | 103 | rewritedates = false |
| 3e3af55 | | | 104 | |
| 3e3af55 | | | 105 | [source_control_service] |
| 3e3af55 | | | 106 | permit_writes = true |
| 3e3af55 | | | 107 | permit_service_writes = true |
| 3e3af55 | | | 108 | |
| 6c9fcae | | | 109 | [git_configs.git_bundle_uri_config.uri_generator_type.local_fs] |
| 6c9fcae | | | 110 | |
| 0d60b20 | | | 111 | [infinitepush] |
| 0d60b20 | | | 112 | allow_writes = true |
| 0d60b20 | | | 113 | |
| 0d60b20 | | | 114 | [commit_cloud_config] |
| 0d60b20 | | | 115 | |
| 3e3af55 | | | 116 | [derived_data_config] |
| 3e3af55 | | | 117 | enabled_config_name = "default" |
| 3e3af55 | | | 118 | |
| 3e3af55 | | | 119 | [derived_data_config.available_configs.default] |
| 3e3af55 | | | 120 | types = [ |
| 3e3af55 | | | 121 | "blame", |
| 3e3af55 | | | 122 | "changeset_info", |
| 3e3af55 | | | 123 | "fastlog", |
| 3e3af55 | | | 124 | "filenodes", |
| 3e3af55 | | | 125 | "fsnodes", |
| 3e3af55 | | | 126 | "git_commits", |
| 3e3af55 | | | 127 | "git_delta_manifests_v2", |
| 6c9fcae | | | 128 | "unodes", |
| 3e3af55 | | | 129 | "hgchangesets", |
| 3e3af55 | | | 130 | "skeleton_manifests", |
| 1688ad1 | | | 131 | "skeleton_manifests_v2", |
| 6c9fcae | | | 132 | "ccsm", |
| 3e3af55 | | | 133 | ] |
| 0d60b20 | | | 134 | |
| 0d60b20 | | | 135 | [derived_data_config.available_configs.default.git_delta_manifest_v2_config] |
| 0d60b20 | | | 136 | max_inlined_object_size = 2000 |
| 0d60b20 | | | 137 | max_inlined_delta_size = 2000 |
| 0d60b20 | | | 138 | delta_chunk_size = 1000000 |
| 3e3af55 | | | 139 | EOF |
| 3e3af55 | | | 140 | |
| 3e3af55 | | | 141 | echo " Config created with repo_id=$NEXT_ID" |
| 0d60b20 | | | 142 | echo "" |
| 0d60b20 | | | 143 | echo " NOTE: Mononoke must be restarted to pick up the new repo config." |
| 0d60b20 | | | 144 | echo " Run: cd $HUB_DIR && docker compose restart mononoke-slapi grove-bridge mononoke-git" |
| 0d60b20 | | | 145 | echo "" |
| 0d60b20 | | | 146 | read -p " Restart Mononoke now? [y/N] " -n 1 -r |
| 0d60b20 | | | 147 | echo |
| 0d60b20 | | | 148 | if [[ $REPLY =~ ^[Yy]$ ]]; then |
| 0d60b20 | | | 149 | echo " Restarting Mononoke services..." |
| 0d60b20 | | | 150 | (cd "$HUB_DIR" && docker compose restart mononoke-slapi grove-bridge mononoke-git) |
| 0d60b20 | | | 151 | echo " Waiting 5s for services to start..." |
| 0d60b20 | | | 152 | sleep 5 |
| 0d60b20 | | | 153 | fi |
| 3e3af55 | | | 154 | else |
| 3e3af55 | | | 155 | echo "" |
| 3e3af55 | | | 156 | echo "Step 2: Config for '$REPO_NAME' already exists, skipping." |
| 3e3af55 | | | 157 | fi |
| 3e3af55 | | | 158 | |
| 0d60b20 | | | 159 | # Step 3: Run gitimport via docker compose |
| 3e3af55 | | | 160 | echo "" |
| 3e3af55 | | | 161 | echo "Step 3: Importing into Mononoke via gitimport..." |
| 3e3af55 | | | 162 | echo " This may take a while for large repos." |
| 3e3af55 | | | 163 | |
| 0d60b20 | | | 164 | IMPORT_MODE="full-repo" |
| 0d60b20 | | | 165 | IMPORT_ARGS=() |
| 0d60b20 | | | 166 | if [[ -n "$COMMIT_SHA" ]]; then |
| 0d60b20 | | | 167 | IMPORT_MODE="missing-for-commit" |
| 0d60b20 | | | 168 | IMPORT_ARGS=("$COMMIT_SHA") |
| 0d60b20 | | | 169 | fi |
| 3e3af55 | | | 170 | |
| 0d60b20 | | | 171 | (cd "$HUB_DIR" && docker compose run --rm --entrypoint gitimport grove-bridge \ |
| 0d60b20 | | | 172 | --repo-name "$REPO_NAME" \ |
| 0d60b20 | | | 173 | --config-path "$CONFIG_DIR" \ |
| 0d60b20 | | | 174 | --local-configerator-path "$DATA_DIR/configerator" \ |
| 0d60b20 | | | 175 | --cache-mode disabled \ |
| 0d60b20 | | | 176 | --just-knobs-config-path "$DATA_DIR/justknobs.json" \ |
| 0d60b20 | | | 177 | --generate-bookmarks \ |
| 0d60b20 | | | 178 | --derive-hg \ |
| 0d60b20 | | | 179 | --git-command-path /usr/bin/git \ |
| 0d60b20 | | | 180 | --concurrency 5 \ |
| 0d60b20 | | | 181 | "$BARE_REPO" \ |
| 0d60b20 | | | 182 | "$IMPORT_MODE" \ |
| 0d60b20 | | | 183 | "${IMPORT_ARGS[@]+"${IMPORT_ARGS[@]}"}") |
| 3e3af55 | | | 184 | |
| 3e3af55 | | | 185 | echo "" |
| 3e3af55 | | | 186 | echo "=== Import complete ===" |
| 3e3af55 | | | 187 | echo "Repo '$REPO_NAME' is now available in Mononoke." |
| 0d60b20 | | | 188 | echo "" |
| 0d60b20 | | | 189 | echo "Clone via git: git clone https://grove.host/repos/$REPO_NAME" |
| 8d8e815 | | | 190 | echo "Clone via grove: grove clone $REPO_NAME" |