git 钩子在自动提交后获取变更集



我有一个钩子,可以获取更改的文件并将它们复制到适用于标准提交的目录中,但合并的自动提交会破坏它,因为最后两次提交不包含任何更改的文件

这是我当前的代码

generateChangeSet() {
    if [ ! -d $WORKDIR ];then
        mkdir $WORKDIR
    fi
    CHANGESET=$(git log -1 --name-status $oldrev $newrev | grep -v -e "^Merge" -e "^commit" -e "^Author" -e "^Date" -e "^ " -e "^$")
    if [ -z $CHANGESET ]; then
      echo Could not detect any files in the change set, aborting push
      exit $RETVAL
    fi
    # Get a list of deleted files
    DELFILES=$WORKDIR/deletedFiles.sh
    if [ -f $DELFILES ];then
        rm -rf $DELFILES
    fi
    for dFile in $(echo $CHANGESET | grep "^D" | awk '{print $2}'); do
        echo deleted file $dFile
        echo rm -f $dFile >> $DELFILES
    done
    if [ -f $DELFILES ];then
        sed -i '1s/^/#!/bin/bashn/' $DELFILES
        chmod +x $DELFILES
        echo find . -depth -type d -empty >> $DELFILES
        echo rm deletedFiles.sh >> $DELFILES
    fi
    echo "Generating diff between $newrev and $oldrev"
    git archive HEAD $(echo $CHANGESET | grep -v "^D" | awk '{print $2}') | (cd $WORKDIR && tar xf -)
}

关于如何让脚本始终获取最新更改文件的任何想法?

谢谢

我首先担心目标($WORKDIR?)可能首先与此处计算的增量不同步。 为什么通过 shell 脚本导出删除操作? 为什么不直接git checkout一个新的目标(设置GIT_WORK_TREE来做到这一点),例如:

what_to_replace=/some/where/tree
tmp_replacement=/some/where/tree.new
rm -rf $tmp_replacement
mkdir $tmp_replacement || fatal "can't create $tmp_replacement"
GIT_WORK_TREE=$tmp_replacement git checkout master -- . ||
    fatal "can't create updated master branch tree"
mv $what_to_replace ${what_to_replace}.old &&
    mv $tmp_replacement $what_to_replace ||
    fatal "can't swap in $tmp_replacement"
rm -rf ${what_to_replace}.old ||
    warn "can't clean up ${what_to_replace}.old"

(这可能需要添加某种锁定,并进一步充实)。 但是,如果您决定继续使用现有代码(可能有一些原因),那么这部分就很愚蠢了:

CHANGESET=$(git log -1 --name-status $oldrev $newrev | grep -v -e "^Merge" -e "^commit" -e "^Author" -e "^Date" -e "^ " -e "^$")

相反,请使用git diff-tree --name-status $oldrev $newrev来比较与给定提交关联的树。 其余的应该很容易。 当然,这一切都假设$oldrev准确地反映了正在更新的任何内容的状态。

最新更新