天天看點

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 
}

# 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 ;
    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 ;
    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 ;
        fi
    else
        return ;
    fi
    return ;
}

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


isrepo; # Current workdir is a repo.
getcurrBranch; # ##
[ $# -eq  ] && 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 - )) # 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 ;
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 ;
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=;
for line in `echo "$ADD_LIST"`; do
    if confirm "addition of [$line]"; then
        ADD[index]="$line";
        index=$(($index+));
    fi
done
index=;
for line in `echo "$MOD_LIST"`; do
    if confirm "modification of [$line]"; then
        MOD[index]="$line";
        index=$(($index+));
    fi
done
index=;
for line in `echo "$DEL_LIST"`; do
    if confirm "deletion of [$line]"; then
        DEL[index]="$line";
        index=$(($index+));
    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  -o ${#DEL[@]} -gt  ]; then
        mkdir -p "$TARGET/before/";
        git archive "$BRANCH" --format=tar ${MOD[@]} ${DEL[@]} | tar -x -C "$TARGET/before/";
    fi
    # after
    if [ ${#ADD[@]} -gt  -o ${#MOD[@]} -gt  ]; 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  -o ${#DEL[@]} -gt  ]; 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  ]; 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:用于删除源碼中本次被删除的檔案。