题
我们想使用OpenGrok索引(相当大的)GIT存储库,而我无法确定的一件事是如何为所有分支索引。从我的看来,我似乎需要拥有要索引的每个分支的签出副本,因此,如果一个存储库有十二个分支,我需要有十二份副本,一个对于每个分支,例如
git-repo-with-many-branches-master/
git-repo-with-many-branches-branch1/
git-repo-with-many-branches-branch2/
:
git-repo-with-many-branches-branch12/
真的是真的吗?还是有办法告诉OpenGrok看 全部 创建其索引时的分支机构?
解决方案
OpenGrok中的其他层旨在在多个不起作用的SCM系统上工作,因此,不幸的是,您必须查看每个要索引的分支作为单独的git存储库:--(
您始终可以向RFE提交支持,以支持GIT存储库中的多个分支。
其他提示
我为此目的编写了一个脚本: daun. 。您将需要使用 daun
CLI而不是 git
CLI在您的Cron工作中。请注意,它在写作时不支持OpenGrok Git历史记录,因为我们只对OpenGrok快速搜索功能感兴趣。我们将我们的OpenGrok用户将基于Web的GIT历史记录的GitHub/Bitbucket等工具指向。
这是我写的一个bash脚本。它将克隆任何新分支,更新任何现有分支,并删除不再存在的分支。这是“工作”的说明;您可以选择使事情变得更安全,但是如果您的服务器仅在LAN上访问,则足够好。我有一个CRON作业设置,只需在服务器上每30分钟运行一次。要设置Cron作业以作为root运行,请运行:
sudo crontab -e
然后粘贴到这些内容中:
*/30 * * * * /usr/local/bin/opengrok_index.sh
然后写下并关闭:
:wq
您将需要安装脚本使用的“期望”来输入SSH键的密码。这两个命令之一将根据您使用的Linux OS来工作:
sudo yum install expect
sudo apt-get install expect
然后在/usr/local/bin/opengrok_index.sh上创建一个文件:
sudo vi /usr/local/bin/opengrok_index.sh
接下来,粘贴在脚本的内容(从本文的底部)粘贴,并根据您的系统更改顶部的变量。接下来,更改权限,以便只能读取它(其中包含密码):
sudo chmod 700 /usr/local/bin/opengrok_index.sh
您可能想手动测试运行脚本并使其正常工作,然后才能期望Cron作业工作。这是我为我的特定设置编写的一个特定脚本,因此您可能需要提出一些回声语句,并进行一些调试以使其正常工作:
sudo /usr/local/bin/opengrok_index.sh
补充说明:
- 该脚本通过SSH(而非HTTPS)登录到GIT中。因此,您的git_user必须存在于系统上,并且在/home/user/.ssh/id_rsa下具有一个可以访问GIT存储库的SSH键。这是标准的git登录内容,所以我不会在这里浏览它。脚本将在提示时输入git_user_ssh_password
- 该脚本将所有文件查看为git_user,因此您可能需要将Checkout_location“ Chown”为该用户
脚本:
#!/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="$@"
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
我对OpenGrok一无所知,但是您当然可以使用git更改分支:
git checkout master
# do the indexing here
git checkout branch1
# indexing
git checkout branch2
# and so on...