我的自动git更新过程应该是什么样子?

问题描述:

我来自一个颠覆的背景,最近我的公司做了切换到git。我曾经在我的笔记本电脑上使用cron条目,每晚更新大量结账。这样,我就可以针对我们系统不同组件的当前版本运行;尤其是那些我没有积极发展,但依赖的部分。我想用git实现同样的功能。我的自动git更新过程应该是什么样子?

这里是我的旧的更新过程中使用svn:

#!/bin/bash -e 
checkout="$1" 
svn update --accept postpone ${checkout} 
# Run a script to report conflicts that I would resolve in the morning. 

我已经读了很多博客帖子的主题,并打听,我还没有发现有很多一致的答案。而且,迄今为止我所看到的解决方案都没有达到我所期望的程度。我已经采纳了所有这些意见并创建了下面的脚本。

我应该如何处理子模块?

是否有情况,或陷阱,我没有说明?

#!/bin/bash -e 
checkout="$1" 
now=$(date +%Y%m%dT%H%M%S) 
cd ${checkout} 

# If you are in the middle of a rebase, merge, bisect, or cherry pick, then don't update. 
if [ -e .git/rebase-merge ]; then continue; fi 
if [ -e .git/MERGE_HEAD ]; then continue; fi 
if [ -e .git/BISECT_LOG ]; then continue; fi 
if [ -e .git/CHERRY_PICK_HEAD ]; then continue; fi 

# Determine what branch the project is on, if any. 
ref=$(git branch | grep '^*' | sed 's/^* //') 

if [[ $ref = "(no branch)" ]]; then 
    # The directory is in a headless state. 
    ref=$(git rev-parse HEAD) 
fi 

# If there are any uncommitted changes, stash them. 
stashed=false 
if [[ $(git status --ignore-submodules --porcelain | grep -v '^??') != "" ]]; then 
    stashed=true 
    git stash save "auto-${now}" 
fi 

# If there are any untracked files, add and stash them. 
untracked=false 
if [[ $(git status --ignore-submodules --porcelain) != "" ]]; then 
    untracked=true 
    git add . 
    git stash save "auto-untracked-${now}" 
fi 

# If status is non-empty, at this point, something is very wrong, fail. 
if [[ $(git status --ignore-submodules --porcelain) != "" ]]; then continue; fi 

# If not on master, checkout master. 
if [[ $ref != "master" ]]; then 
    git checkout master 
fi 

# Rebase upstream changes. 
git pull --rebase 

# Restore branch, if necessary. 
if [[ $ref != "master" ]]; then 
    git checkout ${ref} 
fi 

# Restore untracked files, unless there is a conflict. 
if $untracked; then 
    stash_name=$(git stash list | grep ": auto-untracked-${now}\$" | sed "s/^\([^:]*\):.*$/\\1/") 
    git stash pop ${stash_name} 
    git reset HEAD . 
fi 

# Restore uncommitted changes, unless there is a conflict. 
if $stashed; then 
    stash_name=$(git stash list | grep ": auto-${now}\$" | sed "s/^\([^:]*\):.*$/\\1/") 
    git stash pop ${stash_name} 
fi 

# Update submodules. 
git submodule init 
git submodule update --recursive 

谢谢。

+0

您可能想使用''git symbolic-ref''而不是''git branch''获取当前分支名称。 'git branch''是'porcelain' - 它的输出是供人类使用的,而git symbolic-ref是'plumbing' - 它的输出是用于其他脚本的消耗。 – holygeek

+1

最初我使用'git symbolic-ref',但切换到'git branch',因为当代码处于分离头部状态时,它返回退出码1。我想我可以切换到'set -o pipefail; ref = $((git symbolic-ref -q HEAD | sed -e's/refs \/heads \ ///')|| git rev-parse HEAD)' – jmkacz

+0

有趣的方法 - 每天早晨花费解决合并冲突。好主意让您的回购与遥控器保持同步,但如果您处于某种事情的中间,这可能会造成破坏。 – austinmarton

您需要检查子模块是否有变化。见git submodule foreach

+0

请您详细说明一下吗?你是否说在拉动之前,我应该对所有子模块执行相同的预拉动作?同样,与后拉的步骤。 – jmkacz

+0

你应该确保子模块没有变化,取入它们。然后在顶层做更新 - 递归。 –