这是我写的一个bash脚本。它将克隆任何新分支,更新任何现有分支,并删除不再存在的分支。这里是“工作”的说明;您可以选择让事情更安全,但如果您的服务器只能在您的局域网上访问,这已经足够了。我有一个cron作业设置,每隔30分钟在服务器上运行一次。要设置cron作业,以root身份运行,运行:
sudo crontab -e
然后粘贴这些内容:
*/30 * * * * /usr/local/bin/opengrok_index.sh
然后写入和关闭:
:wq
您需要安装脚本用来输入ssh密钥的密码的“expect”。
sudo yum install expect
sudo apt-get install expect
然后创建在/usr/local/bin/opengrok_index.sh文件:
sudo vi /usr/local/bin/opengrok_index.sh
接下来,粘贴在单这两个命令将根据您所使用的Linux操作系统工作脚本的内容(从这篇文章的底部),并根据您的系统更改顶部的变量。接下来,更改权限,以便只有root可以读它(它有它的密码):
sudo chmod 700 /usr/local/bin/opengrok_index.sh
你可能想测试手动运行该脚本,并得到它的工作,期待cron作业工作之前。这是我写的我的特别设置一个特定的脚本,所以你可能需要把一些回声报表,并做一些调试以达到正常工作:
sudo /usr/local/bin/opengrok_index.sh
其他注意事项:
- 这脚本通过SSH(不是HTTPS)登录到GIT。因此,您的系统上必须存在您的GIT_USER,并且在拥有访问GIT仓库的 /home/user/.ssh/id_rsa下有SSH密钥。这是标准GIT登录内容的 ,所以我不会在这里重述。该脚本将 进入GIT_USER_SSH_PASSWORD当提示
- 脚本检查出所有的文件GIT_USER,所以你可能需要“CHOWN”你CHECKOUT_LOCATION到用户
脚本:
#!/bin/bash
SUDO_PASSWORD="password"
CHECKOUT_LOCATION="/var/opengrok/src/"
GIT_PROJECT_NAME="Android"
GIT_USER="username"
GIT_USER_SSH_PASSWORD="password"
GIT_URL="yourgit.domain.com"
OPENGROK_BINARY_FILE="/usr/local/opengrok-0.12.1.6/bin/OpenGrok"
# Run command as GIT_USER which has Git access
function runGitCommand {
git_command="[email protected]"
expect_command="
spawn sudo -u $GIT_USER $git_command
expect {
\"*password for*\" {
send \"$SUDO_PASSWORD\"
send \"\r\"
exp_continue
}
\"*Enter passphrase for key*\" {
send \"$GIT_USER_SSH_PASSWORD\"
send \"\r\"
exp_continue
}
}"
command_result=$(expect -c "$expect_command" || exit 1)
}
# Checkout the specified branch over the network (slow)
function checkoutBranch {
branch=$1
# Check out branch if it does not exist
if [ ! -d "$branch" ]; then
runGitCommand git clone ssh://$GIT_URL/$GIT_PROJECT_NAME
# Rename project to the branch name
mv $GIT_PROJECT_NAME $branch || exit 1
# Otherwise update the existing branch
else
cd $branch || exit 1
runGitCommand git fetch
runGitCommand git pull origin $branch || exit 1
cd ..
fi
}
# If the branch directory does not exist, copy the master
# branch directory then switch to the desired branch.
# This is faster than checkout out over the network.
# Otherwise, update the exisiting branch directory
function updateBranch {
branch=$1
if [ ! -d "$branch" ]; then
mkdir $branch || exit 1
rsync -av master/ $branch || exit 1
cd $branch || exit 1
runGitCommand git checkout -b $branch origin/$branch
cd ..
else
cd $branch || exit 1
runGitCommand git pull origin $branch || exit 1
cd ..
fi
}
# Change to the OpenGrok indexing location to checkout code
cd $CHECKOUT_LOCATION || exit 1
# Check out master branch
checkoutBranch master
# Get a list of all remote branches
cd master || exit 1
old_ifs=$IFS
IFS=$'\n'
origin_branches=($(git branch -r))
IFS=$old_ifs
origin_branches_length=${#origin_branches[@]}
cd .. # Move out of "master" directory
# Loop through and check out all branches
branches=(master)
for origin_branch in "${origin_branches[@]}"
do
# Strip the "origin/" prefix from the branch name
branch=${origin_branch#*/}
# Ignore the "HEAD" branch
# Also skip master since it has already been updated
if [[ $branch == HEAD* ]] || [[ $branch == master* ]]; then
continue
fi
branches+=("$branch")
updateBranch $branch
done
# Get list of branches currently in OpenGrok
old_ifs=$IFS
IFS=$'\n'
local_branches=($(ls -A1))
size=${#local_branches[@]}
IFS=$old_ifs
# Get list of branches that are in OpenGrok, but do not exist
# remotely. These are branches that have been deleted
deleted_branches=()
for local_branch in "${local_branches[@]}"
do
skip=0
for branch in "${branches[@]}"
do
if [[ $local_branch == $branch ]]; then
skip=1;
break;
fi
done
if [[ $skip == "0" ]]; then
deleted_branches+=("$local_branch")
fi
done
# Change to checkout directory again, in case some future code
# change brings us somewhere else. We are deleting recursively
# here and cannot make a mistake!
cd $CHECKOUT_LOCATION
# Delete any branches that no longer exist remotely
for deleted_branch in ${deleted_branches[@]}
do
rm -rf ./$deleted_branch
done
# Reindex OpenGrok
$OPENGROK_BINARY_FILE index
这是4岁。是否有任何修改OpenGrok会在此期间改变答案? – 2014-05-08 20:10:10
据我所知,没有。 – ebneter 2014-05-08 20:52:14