Git导出差分(diff)包--before/after/patch

*完全由本人原创,转载请注明出处哦~
http://blog.csdn.net/adrianjian/article/details/44085181*

差分包是什么?

即修正前后的差异。包含修正前、后文件目录,和由git diff [-b]生成的差分文件。

  • before: 包含本次修正中,被修改和删除的文件。
  • after: 包含被修改和新增的文件。
  • patch: 差分文件。

为何要导出差分包?

主要目的是与其他的版本控制系统的互操作。

  • 将修正应用到其他CVS,不管该CVS是否可以apply patch
  • 利用其他文件对比工具分析代码修改情况
  • 作为代码评审时的材料

如何导出?

利用以下shell脚本生成。需要系统中安装Git

diffex.sh

#!/bin/bash 

FORCE=n
QUIET=n
BINARY=n
BRANCH=
TARGET=".diffdir"
CURRENT_BRANCH=

# Tell the usage of this script.
# no parameters.
usage()
{
    echo "Usage: `basename $0` [-nqf] [-t TARGET] -b BRANCH"
    echo "  -n  set to output binary diff."
    echo "  -q  quiet mode, minimal messges."
    echo "  -f  force mode, none confirmation."
    echo "  -t  output target directory."
    echo "  -b  the base branch to diff from."
    exit 1
}

# Check whether the current directory is a git repository.
# no parameters.
isrepo()
{
    status=`git status -s`;
    if [ ! -z "$status" ]; then
        echo "Not a repository or unstaged.";
        exit 1;
    fi
}

# Get current branch/tag/commit name.
# no parameters.
getcurrBranch()
{
    CURRENT_BRANCH=`git branch | \
    awk -F " " '$1=="*" && substr($2,0,1)!="(" {print $2} $1=="*" \
    && substr($2,0,1)=="(" {print substr($4,0,length($4)-1)}'`;
    if [ -z "$CURRENT_BRANCH" ]; then
        echo "Get current branch failed.";
        exit 1;
    fi
}
# Ask user to confirm.
# no parameters.
confirm()
{
    if [ "$FORCE" != "y" ]; then
        read -p "Confirm $1? y/n default(y) : ";
        ch="$REPLY"
        if [ "$ch" == "y" -o "$ch" == "" ]; then
            return 0;
        fi
    else
        return 0;
    fi
    return 1;
}

# Adaptable echo
# $1 : message.
nqecho()
{
    if [ "$QUIET" != "y" ]; then
        echo "$1";
    fi
    return 0;
}


isrepo; # Current workdir is a repo.
getcurrBranch; # ##
[ $# -eq 0 ] && usage # At least one patameter required.

# Resolve parameters.
while getopts :fnqt:b: OPTION
do
    case $OPTION in
        f)
            FORCE=y
            ;;
        q)
            QUIET=y
            ;;
        n)
            BINARY=y
            ;;
        b)
            BRANCH=$OPTARG
            ;;
        t)
            TARGET=$OPTARG
            ;;
        \?)
            usage
            ;;
    esac
done

shift $(($OPTIND - 1)) # abandon parameters processed.

if [ -z "$BRANCH" ]; then # parameter -b required.
    echo "You must specify base BRANCH with -b option";
    usage;
fi

# Target dir generating.
mkdir -p "$TARGET";
if [ ! -d "$TARGET" ]; then
    echo "$TARGET is not a diretory";
    exit 1;
fi
if [ -d "$TARGET/__OUT" ]; then
    rm -fr "$TARGET/__OUT";
fi
if mkdir -p "$TARGET/__OUT"; then
    TARGET="$TARGET/__OUT";
else
    echo "Create directory [$TARGET/__OUT] failed."
    exit 1;
fi
odir=`pwd`;
cd "$TARGET";
TARGET=`pwd`;
cd "$odir";

# Generating change list.
FILE_DIFF=`git diff $BRANCH --name-status --no-renames`;
ADD_LIST=`echo "$FILE_DIFF" | awk -F "\t" '$1=="A" {print $2}'`;
MOD_LIST=`echo "$FILE_DIFF" | awk -F "\t" '$1=="M" {print $2}'`;
DEL_LIST=`echo "$FILE_DIFF" | awk -F "\t" '$1=="D" {print $2}'`;

# Ask user confirm all the changes.
index=0;
for line in `echo "$ADD_LIST"`; do
    if confirm "addition of [$line]"; then
        ADD[index]="$line";
        index=$(($index+1));
    fi
done
index=0;
for line in `echo "$MOD_LIST"`; do
    if confirm "modification of [$line]"; then
        MOD[index]="$line";
        index=$(($index+1));
    fi
done
index=0;
for line in `echo "$DEL_LIST"`; do
    if confirm "deletion of [$line]"; then
        DEL[index]="$line";
        index=$(($index+1));
    fi
done

# Print the confirmed changes.
echo "All changes lists below: "
echo "Additions:"
for file in ${ADD[@]}; do
    echo $file;
done
echo "Modifications:"
for file in ${MOD[@]}; do
    echo $file
done
echo "Deletions:"
for file in ${DEL[@]}; do
    echo $file
done

# Generating diff package.
if confirm "changes upon"; then
    echo "Copying files..."
    # before
    if [ ${#MOD[@]} -gt 0 -o ${#DEL[@]} -gt 0 ]; then
        mkdir -p "$TARGET/before/";
        git archive "$BRANCH" --format=tar ${MOD[@]} ${DEL[@]} | tar -x -C "$TARGET/before/";
    fi
    # after
    if [ ${#ADD[@]} -gt 0 -o ${#MOD[@]} -gt 0 ]; then
        mkdir -p "$TARGET/after/";
        git archive "$CURRENT_BRANCH" --format=tar ${ADD[@]} ${MOD[@]} | tar -x -C "$TARGET/after/";
    fi
    # Generating before script, which to copy before files from SCM checkout directory.
    if [ ${#MOD[@]} -gt 0 -o ${#DEL[@]} -gt 0 ]; then
        echo "Generating before copying script...";
        echo '#! /bin/bash' > "$TARGET/bef.sh";
        echo 'odir=`pwd`' >> "$TARGET/bef.sh";
        echo 'cd "$1"' >> "$TARGET/bef.sh";
        echo 'SOURCE=`pwd`' >> "$TARGET/bef.sh";
        echo 'cd "$odir"' >> "$TARGET/bef.sh";
        echo 'odir=`pwd`' >> "$TARGET/bef.sh";
        echo 'cd "$2"' >> "$TARGET/bef.sh";
        echo 'TARGET=`pwd`' >> "$TARGET/bef.sh";
        echo 'cd "$odir"' >> "$TARGET/bef.sh";
        for file in ${MOD[@]}; do
            echo 'mkdir -p $(dirname "$TARGET/'"$file\")" >> "$TARGET/bef.sh";
            echo 'cp -f "$SOURCE/'"$file\""' "$TARGET/'"$file\"" >> "$TARGET/bef.sh";
        done
        for file in ${DEL[@]}; do
            echo 'mkdir -p $(dirname "$TARGET/'"$file\")" >> "$TARGET/bef.sh";
            echo 'cp -f "$SOURCE/'"$file\""' "$TARGET/'"$file\"" >> "$TARGET/bef.sh";
        done
        echo "$TARGET/bef.sh"
    fi
    # Generating delete script, which to delete the corresponding files from SCM checkout directory.
    if [ ${#DEL[@]} -gt 0 ]; then
        echo "Generating deleting script...";
        echo '#! /bin/bash' > "$TARGET/del.sh";
        echo 'odir=`pwd`' >> "$TARGET/del.sh";
        echo 'cd "$1"' >> "$TARGET/del.sh";
        echo 'TARGET=`pwd`' >> "$TARGET/del.sh";
        echo 'cd "$odir"' >> "$TARGET/del.sh";
        for file in ${DEL[@]}; do
            echo 'rm -f "$TARGET/'"$file\"" >> "$TARGET/del.sh";
        done
        echo "$TARGET/del.sh";
    fi
    git diff "$BRANCH" > "$TARGET/diff.patch"
    if [ "$BINARY" == "y" ]; then
        git diff --binary "$BRANCH" > "$TARGET/diff_b.patch"
    fi
fi
echo "";
echo "Done.";

参数解释:

  • -n 指定时,生成binary版PATCH文件
  • -q 静默模式,不输出详细信息
  • -f 强制模式,不要求用户确认
  • -t 指定目标目录,将在$TARGET目录下复写式地生成__OUT子目录;默认值为‘~’
  • -b 指定源分支,即修改前的文件。必须指定

示例

将上述脚本保存为diffex.sh文件。在git库所在目录执行以下命令将会在父目录下生成__OUT目录。

./diffex.sh -nqft ../ -b master

../__OUT目录内容如下:

  • before:修改前的代码。仅包含被修改和被删除的文件。
  • after:修改后的代码。仅包含被修改和新追加的文件。
  • diff.patch:差异文件。从–b指定的commit,到当前分支的差异。也是从before目录到after目录的差异。
  • diff_b.patch:二进制差异文件,可作为git apply命令的参数。
  • bef.sh:用于从源码中以拷贝出被修改的文件,并保持相对目录结构。
  • del.sh:用于删除源码中本次被删除的文件。
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值