如何在 OpenGrok 中处理 git 分支?

发布于 2024-08-13 23:44:46 字数 388 浏览 10 评论 0原文

我们想要使用 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 在创建索引时查看所有分支?

We want to index our (fairly large collection of) git repositories using OpenGrok, and the one thing I haven't been able to figure out is how to index all the branches. From what I can see, it looks like I need to have checked-out copies of each branch that I want to index, so, if a repository has, say, a dozen branches, I need to have a dozen copies of it, one for each branch, e.g.,

git-repo-with-many-branches-master/
git-repo-with-many-branches-branch1/
git-repo-with-many-branches-branch2/
       :
git-repo-with-many-branches-branch12/

Is that really true? Or is there a way to tell OpenGrok to look at all the branches when creating its index?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

尐籹人 2024-08-20 23:44:46

OpenGrok 中的其他层设计用于在多个 SCM 系统上工作,而这些系统不像 git 那样工作,因此不幸的是,您必须检查要索引为单独的 git 存储库的每个分支:-(

您始终可以提交 RFE支持浏览 git 存储库中的多个分支。

The other layers in OpenGrok are designed to work on multiple SCM systems that doesn't work like git, so unfortunately for you you have to check out each branch you want to index as a separate git repository :-(

You could always file an RFE for support for browsing multiple branches in a git repository.

掀纱窥君容 2024-08-20 23:44:46

我专门为此目的编写了一个脚本:daun。您需要在 cron 作业中使用 daun cli 而不是 git cli。请注意,在撰写本文时,它不支持 OpenGrok git 历史记录,因为我们只对 OpenGrok 快速搜索功能感兴趣。我们会向 OpenGrok 用户推荐 GitHub/Bitbucket 等工具来获取基于 Web 的 git 历史记录。

I have written a script exactly for this purpose: daun. You will need to use daun cli instead of git cli in your cron job. Do note that it does not support OpenGrok git history at the time of writing as we are only interested in OpenGrok fast searching capability. We would point our OpenGrok users to tools like GitHub/Bitbucket for web-based git history.

雨后彩虹 2024-08-20 23:44:46

这是我为执行此操作而编写的 bash 脚本。它将克隆任何新分支,更新任何现有分支,并删除不再存在的分支。以下是“有效”的说明;您可以选择让事情变得更安全,但是如果您的服务器只能在 LAN 上访问,那么这已经足够了。我有一个 cron 作业设置,每 30 分钟在服务器上运行一次。要将 cron 作业设置为以 root 身份运行,请运行:

sudo crontab -e

然后粘贴以下内容:

*/30 * * * * /usr/local/bin/opengrok_index.sh

然后写入并关闭:

:wq

您将需要安装“expect”,脚本将使用它来输入 ssh 密钥的密码。这两个命令之一将起作用,具体取决于您使用的 Linux 操作系统:

sudo yum install expect
sudo apt-get install expect

然后在 /usr/local/bin/opengrok_index.sh 创建一个文件:

sudo vi /usr/local/bin/opengrok_index.sh

接下来,粘贴脚本的内容(从本文的底部),然后根据您的系统更改顶部的变量。接下来,更改权限,以便只有 root 可以读取它(其中有密码):

sudo chmod 700 /usr/local/bin/opengrok_index.sh

在期望 cron 作业正常工作之前,您可能希望手动测试运行脚本并使其正常工作。这是我为我的特定设置编写的特定脚本,因此您可能需要放入一些 echo 语句并进行一些调试才能使其正常工作:

sudo /usr/local/bin/opengrok_index.sh

附加说明:

  • 此脚本通过 SSH(而不是 HTTPS)登录到 GIT。因此,您的
    GIT_USER 必须存在于系统上,并且在下面有一个 SSH 密钥
    /home/user/.ssh/id_rsa 有权访问 GIT 存储库。这是
    标准的 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

Here is a bash script I wrote for doing exactly this. It will clone any new branches, update any existing branches, and delete branches that no longer exist. Here are instructions that "work"; you may choose to make things more secure but this is good enough if your server is only accessible on your LAN. I have a cron job setup that just runs it every 30 minutes on the server. To set up the cron job to run as root, run:

sudo crontab -e

Then paste in these contents:

*/30 * * * * /usr/local/bin/opengrok_index.sh

Then write and close:

:wq

You will need to install "expect" which the script uses to enter your ssh key's password. One of these two commands will work depending what linux OS you are using:

sudo yum install expect
sudo apt-get install expect

Then create a file at /usr/local/bin/opengrok_index.sh:

sudo vi /usr/local/bin/opengrok_index.sh

Next, paste in the contents of the script (from the bottom of this post), and change the variables at the top according to your system. Next, change the permissions so only root can read it (it has passwords in it):

sudo chmod 700 /usr/local/bin/opengrok_index.sh

You probably want to test running the script manually and get it working, before expecting the cron job to work. This is a particular script that I wrote for my particular setup, so you may need to put in some echo statements and do some debugging to get it working correctly:

sudo /usr/local/bin/opengrok_index.sh

Additional notes:

  • This script logs into GIT over SSH (not HTTPS). As such, your
    GIT_USER must exist on the system, and have an SSH key under
    /home/user/.ssh/id_rsa that has access to the GIT repo. This is
    standard GIT login stuff so I won't go over it here. The script will
    enter the GIT_USER_SSH_PASSWORD when prompted
  • The script checks out all files as GIT_USER, so you may need to "chown" your CHECKOUT_LOCATION to that user

Script:

#!/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
甜味拾荒者 2024-08-20 23:44:46

我对 OpenGrok 一无所知,但当然你可以使用 Git 更改分支:

git checkout master
# do the indexing here
git checkout branch1
# indexing
git checkout branch2
# and so on...

I don't know anything about OpenGrok but of course you can change branches using Git:

git checkout master
# do the indexing here
git checkout branch1
# indexing
git checkout branch2
# and so on...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文