shell 如何检查上游的HEAD/latest commit是否出现在forked repo中?

ymzxtsji  于 2023-01-26  发布在  Shell
关注(0)|答案(1)|浏览(115)

我的想法是,为了只在上游推送了一些提交时才自动获取更新,我应该检查它的散列。
2种方法,我可以做的是,检查logs或使用rev-list的东西如下:

# this fetches top 30 commits from forked repo
git rev-list -n 30 main

# this also does the same but not limited to 30
git log --format='%H'

我发现很难比较哈希值,因为我无法从分支回购中获取超过1个提交。
下面解释了我的方法:

git remote add upstream https://github.com/upstream/upstream.git
git fetch upstream --tags

upstream_commit="$(git rev-parse upstream/main)"
echo "Upstream commit: ${upstream_commit}"

# This should store 30 recent commit hashes as an array AFAIK
forked_commits=($(git rev-list -n 30 main))
# another I tried to put forloop is this:
# for forked_commit in "$forked_commits[@]"; do
for forked_commit in $(git log --format='%H'); do
  # but prints only the top most element and exits the loop
  echo "$forked_commit"
  if [ "$upstream_commit" == "$forked_commit" ]; then
    has_new_commits=false
  else
    # since I've added my commits on top, if condition fails and this always returns true
    has_new_commits=true
  fi
done
          
if [ "${has_new_commits}" ]; then
  git checkout main
  git rebase upstream/main
  git push -f origin main
  echo "Rebase successful!"
else
  echo "No commits to be synced!"
fi
rqenqsqc

rqenqsqc1#

好吧,我犯了很多小错。

  • 数组的东西不起作用。我应该直接把它们放在forloop中。
  • 下一个错误是我没有正确地验证它们。也就是说,即使找到了散列,它也没有退出。
  • 使用[ $has_new_commits ]; then是错误的。我应该使用[ $has_new_commits == "true"]; then

更正代码:

git remote add upstream https://github.com/upstream_repo/upstream.git
git fetch upstream --tags

upstream_commit="$(git rev-parse upstream/main)"

for forked_commit in $(git rev-list -n 20 main); do
  if [ $upstream_commit != $forked_commit ]; then
    has_new_commits=true
    continue
  else
    has_new_commits=false
    break
  fi
done

if [ $has_new_commits == "true" ]; then
  echo "New commits found!"
else
  echo "No commits to be synced!"
fi

相关问题