Git-仅丢弃大小写更改 [英] Git - Discard case only changes

查看:86
本文介绍了Git-仅丢弃大小写更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我将git(在Windows上具有Git Extensions 2)与大型VB6代码库一起使用.对于不熟悉VB6的任何人,它都不区分大小写,并且习惯于在保存文件时更改变量名的大小写.可以采取一些步骤来最小化此行为(请参见停止Visual Basic 6(通过更换外壳)),但以这种方式完全消除问题是不可行的.当然,问题在于,案例更改会显示为Git的更改,从而干扰提交历史,以至于几乎找不到实际更改.

So, I'm using git (with Git Extensions 2 on Windows) with a large VB6 codebase. For anyone who isn't familiar with VB6, it is case-insensitive and has a habit of changing the case of variable names whenever you save a file. There are steps which can be taken to minimise this behaviour (see Stop Visual Basic 6 from changing my casing), but it is unfeasible to completely eliminate the problem in this way. The problem of course is that the case changes show up as changes in Git, thus interfering with the commit history to the point where actually changes are almost impossible to find.

我正在寻找一种从源代码管理端处理此问题的方法,希望能收到任何输入.我目前追求的途径是按优先顺序排列的:

I'm looking for a way to handle this from the source control side and would appreciate any input. The avenues I'm currently pursuing are in order of preference are:

  1. 使Git diff大小写不敏感-似乎找不到解决方法.它也不会获取对字符串的更改,但这是我愿意为轻松解决这个问题付出的代价.
  2. 在提交之前仅通过基于大小写的更改重置大块.
  3. 移动到Visual Source Safe,它具有区分大小写的差异选项-否...

我觉得2号选项可能是我最好的选择,但我不确定如何处理它.我目前的思路是:

I've got a feeling that option number 2 is probably my best bet, but I'm not really sure of the best way to handle it. My current line of thinking is:

  • 创建一些工具来自动执行Git命令行
  • 使用交互式提示来迭代所有更改,并拆分为最小的块
  • 对于每个大块,如果仅更改为仅大小写,请将其重置

我很确定这是一个尽可能好的解决方案.暂存前运行此工具将解决所有问题.有人对这种方法有什么想法吗?

I'm pretty sure this is about as good of a solution as is possible. Running this tool before staging will solve all of the problems. Does anyone have any thoughts on this method?

此外,如果我沿这条路线走,最好有一个Git钩子,以防止仅在任何情况下都可以更改.我完全不知道如何实现这样的东西,因此对创建这样一个脚本的任何帮助都是很棒的.

Also, if i do go down this route, it would be preferable to have a Git hook to prevent any case only changes. I have absolutely no idea how to implement something like this, so any help towards creating such a script would be great.

要弄清问题的严重程度,当变量的大小写更改时,它将更改打开文件中具有相同名称的每个变量的每个实例.每次提交时,都会发生多个变量,并且似乎每个已修改文件的〜30%都已更改.这使得手动处理(这是我当前正在做的)非常不切实际,并且仅对很小的提交有用.

To give some idea of the scale of the problem, when the case of a variable changes, it will change EVERY instance of every variable with the same name in open files. Every commit, this will have happened to several variables and it will appear as if ~30% of each modified file has changed. This makes a manual process (which is what I'm currently doing) quite impractical and only useful for really small commits.

非常感谢您的帮助!

推荐答案

#!/bin/bash

# Script to discard any case only changes that haven't been staged.

# Create a backup of the current changes
BACKUPFILE="$( date +%Y%m%d%H%M%S.backup.patch )"
git diff > $BACKUPFILE

# For each file that has been changed
for f in $( git diff --name-only --ignore-submodules=all); do

    # Create a case insensitive patch
    git show :$f | diff -uiw - $f > temp.patch;

    # Reset the file
    git checkout -- $f;

    # Apply the case insensitive patch, hence discarding case only changes
    git apply --whitespace=nowarn temp.patch;

    # git apply doesn't respect autocrlf so replace all LF with CRLF
    # $ matches the end of line
    # '\r' expands to CRLF in a bash shell
    sed s/$/'\r'/ $f > temp.txt;
    mv temp.txt $f;

    # clean up temporary files
    rm temp.patch;

done

echo "If everything is as expected you can delete $BACKUPFILE.
If things have gone wrong you can revert to your previous state by executing;

    git reset --hard HEAD
    git apply $BACKUPFILE"

这篇关于Git-仅丢弃大小写更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆