2014-03-14 20 views
2

当使用'git difftool'时,它会将相对路径传递给外部差异应用程序,当其中一个文件是最新版本时。如何让git difftool总是导出绝对路径

〜/的.gitconfig

[difftool "echo"] 
    cmd = echo $LOCAL $REMOTE 
    path = 
[diff] 
    tool = echo 

示例命令

git difftool e3d654bc65404b9229abc0251a6793ffbfccdee3 6c6b5fd34196402e4fa0e8cf42f681d9fbd5359f 

Viewing: 'app/views/shared/_graph.html.slim' 
Launch 'echo' [Y/n]: y 
app/views/shared/_graph.html.slim /var/folders/fs/3pgvhwkj2vq4qt3q6n74s5880000gn/T//XGFoyj__graph.html.slim 

在这个例子中app/views/shared/_graph.html.slim是将被传递至外部差异应用并且由于其相对的DIFF应用程序不会相对路径知道如何打开它。

我该如何让'git difftool'始终导出绝对路径?

回答

2

这是基于hlovdal和我已经结束了使用VonC答案的解决方案。

〜/ .git_absolute_path.sh

#!/bin/sh 

FILE_PATH="$1" 

case "$FILE_PATH" 
in 
     /*) 
       NEW_PATH="$FILE_PATH" 
       ;; 
     *) 
       NEW_PATH=`git rev-parse --show-toplevel`/"$FILE_PATH" 
       ;; 
esac 

echo "$NEW_PATH" 

〜/的.gitconfig

[difftool "echo"] 
    cmd = echo `~/.git_absolute_path.sh $LOCAL` `~/.git_absolute_path.sh $REMOTE` 
    path = 
[mergetool "echo"] 
    cmd = echo `~/.git_absolute_path.sh $LOCAL` `~/.git_absolute_path.sh $REMOTE` `~/.git_absolute_path.sh $BASE` `~/.git_absolute_path.sh $MERGED` 
    trustExitCode = true 
[diff] 
    tool = echo 

这里的区别是,我们重用每条路径的单个脚本。

+0

不错的反馈。 +1 – VonC

2

您可以使用git rev-parse --show-toplevel在$ LOCAL变量之前添加以获取完整路径。

有趣的是,有一个进展中的补丁(2014年2月),这将为了补充一点非常指挥使从一个子模块中difftool工作:

[PATCH]difftool: support repositories with .git-files

git-difftool.perl#find_worktree变得

sub find_worktree 
{ 
    # Git->repository->wc_path() does not honor changes to the working 
    # tree location made by $ENV{GIT_WORK_TREE} or the 'core.worktree' 
    # config variable. 
    return Git::command_oneline('rev-parse', '--show-toplevel'); 
} 
+0

与我的回答相同的想法,但附加信息。 +1 – hlovdal

3

把echo命令的包装脚本中

$ tail -5 ~/.gitconfig 
[difftool "echo"] 
     cmd = /tmp/test/echo.sh $LOCAL $REMOTE 
     path = 
[diff] 
     tool = echo 
$ cat /tmp/test/echo.sh 
#!/bin/sh 

LOCAL="$1" 
REMOTE="$2" 

case "$LOCAL" 
in 
     /*) 
       L="$LOCAL" 
       ;; 
     *) 
       L=`git rev-parse --show-toplevel`/"$LOCAL" 
       ;; 
esac 

case "$REMOTE" 
in 
     /*) 
       R="$REMOTE" 
       ;; 
     *) 
       R=`git rev-parse --show-toplevel`/"$REMOTE" 
       ;; 
esac 

echo "$L" "$R" 
$ 

则输出的文件的绝对路径:

$ git status 
# On branch master 
# Changes not staged for commit: 
# (use "git add <file>..." to update what will be committed) 
# (use "git checkout -- <file>..." to discard changes in working directory) 
# 
#  modified: dummy 
# 
no changes added to commit (use "git add" and/or "git commit -a") 
$ git difftool 

Viewing: 'a/b/c/dummy' 
Hit return to launch 'echo': 
/tmp/vsDkDe_dummy /tmp/test/a/b/c/dummy 
$ 
+0

与我的回答相同的想法,但更详细。 +1 – VonC

相关问题