I'm trying to mirror the AOSP code into our local Gerrit system using the bash method below.
#!/bin/bash
repo init -u https://android.googlesource.com/platform/manifest -b android-14.0.0_r9 --mirror
repo sync
repo_list=`repo list -p`
repo forall -c '
if [[ ${REPO_PATH} =~ $repo_list ]]; then
ssh -p 29418 gerritadmin@local-gerrit-host gerrit create-project aosp/${REPO_PATH} --parent All-Projects || echo "Failed to create project for ${REPO_PATH}"
fi
git push ssh://gerritadmin@local-gerrit-host:29418/aosp/${REPO_PATH} +refs/heads/* +refs/tags/* || echo "Failed to push ${REPO_PATH}"
'
However, I'm encountering an issue within the repo forall loop. I have an if condition, but it doesn't seem to work as expected. I'm not sure whether inside repo forall loop bash IF condition work or not.
Mainly, I'm aiming to minimize multiple Gerrit SSH connections to our to local gerrit server to reduce the load.
Could you please assist me in finding the correct approach to achieve this?
There are some issues in the script.
It should be
$repo_list =~ ${REPO_PATH}. Otherwise, it's always false unless there is only one project. Even so, theifclause is unnecessary and flawed.repo list -phas the same effect withrepo forall -c 'echo $REPO_PATH'.$REPO_PATHis always among the output ofrepo list -p. Besides, if there is a pathfoo/bar_bazin$repo_listand$REPO_PATHisfoo/bar, the test still passes while it may be not expected.Actually, the
ifclause here should be testing whether the projectaosp/${REPO_PATH}has already existed on your Gerrit before it's to be created. First, list all the projects that contain the substringaosp/${REPO_PATH}. Unfortunately, it does not take a regular expression.And then test if one of the listed projects is exactly
aosp/${REPO_PATH}instead of something likeaosp/${REPO_PATH}_foo. I think it's also okay to omit the test becausegerrit create-projectwould raise an error if the project already exists.In my experience, always append
< /dev/nullto the Gerrit SSH CLI, especially when it's used in a loop. Without it, the loop breaks after the first run.