设为首页 收藏本站
查看: 702|回复: 0

[经验分享] git添加比较和合并工具(meld)

[复制链接]

尚未签到

发表于 2018-1-13 11:04:40 | 显示全部楼层 |阅读模式
#!/bin/sh  
# Copyright 2010 - 2012, Tim Henigan <tim.henigan@gmail.com>
  
#
  
# Permission is hereby granted, free of charge, to any person obtaining
  
# a copy of this software and associated documentation files (the
  
# "Software"), to deal in the Software without restriction, including
  
# without limitation the rights to use, copy, modify, merge, publish,
  
# distribute, sublicense, and/or sell copies of the Software, and to
  
# permit persons to whom the Software is furnished to do so, subject to
  
# the following conditions:
  
#
  
# The above copyright notice and this permission notice shall be included
  
# in all copies or substantial portions of the Software.
  
#
  
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  

  

  
# Perform a directory diff between commits in the repository using
  
# the external diff or merge tool specified in the user's config.
  

  
USAGE='[--cached] [--copy-back] [-x|--extcmd=<command>] <commit>{0,2} [-- <path>*]
  
     --cached     Compare to the index rather than the working tree.
  
     --copy-back  Copy files back to the working tree when the diff
  
                  tool exits (in case they were modified by the
  
                  user).  This option is only valid if the diff
  
                  compared with the working tree.
  
     -x=<command>
  
     --extcmd=<command>  Specify a custom command for viewing diffs.
  
                  git-diffall ignores the configured defaults and
  
                  runs $command $LOCAL $REMOTE when this option is
  
                  specified. Additionally, $BASE is set in the
  
                  environment.
  
'
  

  
SUBDIRECTORY_OK=1
  
. "$(git --exec-path)/git-sh-setup"
  

  
TOOL_MODE=diff
  
. "$(git --exec-path)/git-mergetool--lib"
  

  
merge_tool="$(get_merge_tool)"
  
if test -z "$merge_tool"
  
then
  
     echo "Error: Either the 'diff.tool' or 'merge.tool' option must be set."
  
     usage
  
fi
  

  
start_dir=$(pwd)
  


  
# All the file paths returned by the diff command are>  
# of the working copy. So if the script is called from a subdirectory, it
  
# must switch to the root of working copy before trying to use those paths.
  
cdup=$(git rev-parse --show-cdup) &&
  
cd "$cdup" || {
  
     echo >&2 "Cannot chdir to $cdup, the toplevel of the working tree"
  
     exit 1
  
}
  

  
# set up temp dir
  
tmp=$(perl -e 'use File::Temp qw(tempdir);
  
     $t=tempdir("/tmp/git-diffall.XXXXX") or exit(1);
  
     print $t') || exit 1
  
trap 'rm -rf "$tmp"' EXIT
  

  
left=
  
right=
  
paths=
  
dashdash_seen=
  
compare_staged=
  
merge_base=
  
left_dir=
  
right_dir=
  
diff_tool=
  
copy_back=
  

  
while test $# != 0
  
do
  
     case "$1" in
  
     -h|--h|--he|--hel|--help)
  
         usage
  
         ;;
  
     --cached)
  
         compare_staged=1
  
         ;;
  
     --copy-back)
  
         copy_back=1
  
         ;;
  
     -x|--e|--ex|--ext|--extc|--extcm|--extcmd)
  
         if test $# = 1
  
         then
  
             echo You must specify the tool for use with --extcmd
  
             usage
  
         else
  
             diff_tool=$2
  
             shift
  
         fi
  
         ;;
  
     --)
  
         dashdash_seen=1
  
         ;;
  
     -*)
  
         echo Invalid option: "$1"
  
         usage
  
         ;;
  
     *)
  
         # could be commit, commit range or path limiter
  
         case "$1" in
  
         *...*)
  
             left=${1%...*}
  
             right=${1#*...}
  
             merge_base=1
  
             ;;
  
         *..*)
  
             left=${1%..*}
  
             right=${1#*..}
  
             ;;
  
         *)
  
             if test -n "$dashdash_seen"
  
             then
  
                 paths="$paths$1 "
  
             elif test -z "$left"
  
             then
  
                 left=$1
  
             elif test -z "$right"
  
             then
  
                 right=$1
  
             else
  
                 paths="$paths$1 "
  
             fi
  
             ;;
  
         esac
  
         ;;
  
     esac
  
     shift
  
done
  

  
# Determine the set of files which changed
  
if test -n "$left" && test -n "$right"
  
then
  
     left_dir="cmt-$(git rev-parse --short $left)"
  
     right_dir="cmt-$(git rev-parse --short $right)"
  

  
     if test -n "$compare_staged"
  
     then
  
         usage
  
     elif test -n "$merge_base"
  
     then
  
         git diff --name-only "$left"..."$right" -- $paths >"$tmp/filelist"
  
     else
  
         git diff --name-only "$left" "$right" -- $paths >"$tmp/filelist"
  
     fi
  
elif test -n "$left"
  
then
  
     left_dir="cmt-$(git rev-parse --short $left)"
  

  
     if test -n "$compare_staged"
  
     then
  
         right_dir="staged"
  
         git diff --name-only --cached "$left" -- $paths >"$tmp/filelist"
  
     else
  
         right_dir="working_tree"
  
         git diff --name-only "$left" -- $paths >"$tmp/filelist"
  
     fi
  
else
  
     left_dir="HEAD"
  

  
     if test -n "$compare_staged"
  
     then
  
         right_dir="staged"
  
         git diff --name-only --cached -- $paths >"$tmp/filelist"
  
     else
  
         right_dir="working_tree"
  
         git diff --name-only -- $paths >"$tmp/filelist"
  
     fi
  
fi
  

  
# Exit immediately if there are no diffs
  
if test ! -s "$tmp/filelist"
  
then
  
     exit 0
  
fi
  

  
if test -n "$copy_back" && test "$right_dir" != "working_tree"
  
then
  
     echo "--copy-back is only valid when diff includes the working tree."
  
     exit 1
  
fi
  

  
# Create the named tmp directories that will hold the files to be compared
  
mkdir -p "$tmp/$left_dir" "$tmp/$right_dir"
  

  
# Populate the tmp/right_dir directory with the files to be compared
  
while read name
  
do
  
     if test -n "$right"
  
     then
  
         ls_list=$(git ls-tree $right "$name")
  
         if test -n "$ls_list"
  
         then
  
             mkdir -p "$tmp/$right_dir/$(dirname "$name")"
  
             git show "$right":"$name" >"$tmp/$right_dir/$name" || true
  
         fi
  
     elif test -n "$compare_staged"
  
     then
  
         ls_list=$(git ls-files -- "$name")
  
         if test -n "$ls_list"
  
         then
  
             mkdir -p "$tmp/$right_dir/$(dirname "$name")"
  
             git show :"$name" >"$tmp/$right_dir/$name"
  
         fi
  
     else
  
         if test -e "$name"
  
         then
  
             mkdir -p "$tmp/$right_dir/$(dirname "$name")"
  
             cp "$name" "$tmp/$right_dir/$name"
  
         fi
  
     fi
  
done < "$tmp/filelist"
  

  
# Populate the tmp/left_dir directory with the files to be compared
  
while read name
  
do
  
     if test -n "$left"
  
     then
  
         ls_list=$(git ls-tree $left "$name")
  
         if test -n "$ls_list"
  
         then
  
             mkdir -p "$tmp/$left_dir/$(dirname "$name")"
  
             git show "$left":"$name" >"$tmp/$left_dir/$name" || true
  
         fi
  
     else
  
         if test -n "$compare_staged"
  
         then
  
             ls_list=$(git ls-tree HEAD "$name")
  
             if test -n "$ls_list"
  
             then
  
                 mkdir -p "$tmp/$left_dir/$(dirname "$name")"
  
                 git show HEAD:"$name" >"$tmp/$left_dir/$name"
  
             fi
  
         else
  
             mkdir -p "$tmp/$left_dir/$(dirname "$name")"
  
             git show :"$name" >"$tmp/$left_dir/$name"
  
         fi
  
     fi
  
done < "$tmp/filelist"
  

  
LOCAL="$tmp/$left_dir"
  
REMOTE="$tmp/$right_dir"
  

  
if test -n "$diff_tool"
  
then
  
     export BASE
  
     eval $diff_tool '"$LOCAL"' '"$REMOTE"'
  
else
  
     run_merge_tool "$merge_tool" false
  
fi
  

  
# Copy files back to the working dir, if requested
  
if test -n "$copy_back" && test "$right_dir" = "working_tree"
  
then
  
     cd "$start_dir"
  
     git_top_dir=$(git rev-parse --show-toplevel)
  
     find "$tmp/$right_dir" -type f |
  
     while read file
  
     do
  
         cp "$file" "$git_top_dir/${file#$tmp/$right_dir/}"
  
     done
  
fi

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-434602-1-1.html 上篇帖子: git的使用[转] 下篇帖子: 分享几个 git 的使用场景
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表