gpt4 book ai didi

git add --patch with difftool

转载 作者:IT王子 更新时间:2023-10-29 01:13:37 24 4
gpt4 key购买 nike

是否可以通过 git add --patch 配置 Git 以使用我配置的 difftool?

我想通过我自己的 difftool 选择要添加到索引的更改。

最佳答案

不,不幸的是。

我想我可以看到工作 - Git 根据当前索引中的内容生成一个临时文件,将其与当前工作树版本的副本一起交给 difftool(以保护您免于进行进一步更改),让我们您使用 difftool 将一些更改移动到索引版本,然后在保存并退出后,暂存该修改后的索引版本中的任何内容。请注意,这将需要 difftool 也有点像编辑器,但并非所有有效的 difftools 都是;其中一些仅用于查看差异。另请注意,这基本上绕过了所有 git add -p。您不会从它那里获得任何用于在 hunk 之间移动、拆分 hunk 等的正常界面。 difftool 将完全负责所有这些。

如果您的 difftool 功能齐全,足以执行此类操作,那么我想您可以编写一个脚本来执行此操作。一个大纲,实际上没有任何错误保护、特殊情况的处理(二进制文件?),并且完全未经测试:

#!/bin/bash
tmpdir=$(mktemp -d)
git diff --name-only |
while read file; do
cp "$file" $tmpdir
# this has your changes in it
work_tree_version="$tmpdir/$file"
# this has the pristine version
index_version=$(git checkout-index --temp "$file")
# and now you bring changes from the work tree version into the index version,
# within the difftool, and save the index version and quit when done
my_difftool "$work_tree_version" "$index_version"

# swap files around to run git add
mv "$file" "$work_tree_version"
mv "$index_version" "$file"
git add "$file"
mv "$work_tree_version" "$file"
# you could also do this by calculating the diff and applying it directly to the index
# git diff --no-index -- "$file" "$original_index_version" | git apply --cached

rm -r $tmpdir

可能有很多方法可以改进它;抱歉,我现在没有时间仔细和彻底地处理它。

关于git add --patch with difftool,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9023928/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com