Homebrew更新导致git冲突问题解决

本文介绍了在升级Mac的BigSur后,Homebrew升级导致Formula文件夹中的.rb文件出现Git冲突的问题。作者通过观察错误信息确定是Git冲突格式,并决定保留线上版本的内容。为了解决大量.rb文件的冲突,作者编写了一个Unity Editor脚本,批量处理文件,删除冲突标记并保留服务器版本。脚本可帮助快速修复问题,使Homebrew恢复正常工作。

Mac升级到Big Sur之后Homebrew也升级了一下,结果它的升级Formula文件夹中的.rb文件是用的git直接拉取导致文件内部冲突。
首先是报错,Homebrew随便执行啥都会报类似下面的错误<<<<<<< Updated upstream…

Error: opencv@3: /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula/opencv@3.rb:10: syntax error, unexpected <<
<<<<<<< Updated upstream
^~
/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula/opencv@3.rb:15: syntax error, unexpected ===, expecting end
=======
^~~
/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula/opencv@3.rb:20: syntax error, unexpected >>, expecting end
>>>>>>> Stashed changes
^~
/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula/opencv@3.rb:132: syntax error, unexpected end, expecting end-of-input

打开它提示的路径的rb文件,就发现熟悉的

<<<<<<< Updated upstream
	xxx
=======
	xxx
>>>>>>> Stashed changes

这不正是Git拉取的时候冲突的格式嘛!
想要解决也不难,直接取线上版本也就是Updated upstream和====之间的内容即可,====之后的要么删除要么注释都可以。

于是手动注释了这一个.rb文件,随后发现还有千千万万个.rb文件等着改-。-

总不至于一个个全部手动改,根本看不到头的。。。

于是在Unity里写了个Editor执行的脚本:

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;

public class HomeBrewHelp
{
    /// <summary>
    /// 修复HomeBrew的.rb脚本中的git冲突,使用服务器版本,删除本地版本为空行
    /// </summary>
    [MenuItem("Tools/HomeBrew修复git冲突", false, 1)]
    static void HomeBrewFixGitConflict()
    {
        //路径  
        string fullPath = EditorUtility.OpenFolderPanel("", "", "");
        int TempNum = 0;

        //获取指定路径下面的所有资源文件  
        if (Directory.Exists(fullPath))
        {
            DirectoryInfo direction = new DirectoryInfo(fullPath);
            FileInfo[] files = direction.GetFiles("*", SearchOption.AllDirectories);

            //Debug.Log(files.Length);
            for (int i = 0; i < files.Length; i++)
            {
                //.meta不管,本身不管
                if (files[i].Name.EndsWith(".meta") || files[i].Name.Equals("TranslateEditor.cs") ||
                    files[i].Name.Equals("MiniJSON.cs"))
                {
                    continue;
                }

                // Debug.Log("Name:" + files[i].Name); //打印出来这个文件夹下的所有文件
                if (files[i].Name.EndsWith(".rb"))
                {
                    StreamReader sr = new StreamReader(files[i].FullName);
                    string str_ReadToEnd = sr.ReadToEnd();
                    // Debug.Log(str_ReadToEnd);
                    string[] SplitStr = {"\n"};
                    //按行拆分
                    string[] str_line = str_ReadToEnd.Split(SplitStr, System.StringSplitOptions.None);

                    bool isChanged = false;
                    bool isUpdated = false;

                    for (int j = 0; j < str_line.Length; j++)
                    {
                        //去头尾空白字符
                        string str_Trim = str_line[j].Trim();

                        if (str_Trim.Contains("<<<<<<<"))
                        {
                            str_line[j] = "";
                        }
                        else if (str_Trim.Contains("======="))
                        {
                            str_line[j] = "";
                            isUpdated = true;
                        }
                        else if (str_Trim.Contains(">>>>>>>"))
                        {
                            str_line[j] = "";
                            isUpdated = false;
                            isChanged = true;
                        }
                        else if (isUpdated)
                        {
                            str_line[j] = "";
                        }
                    }

                    sr.Close();
                    sr.Dispose();
                    if (isChanged)
                    {
                        File.WriteAllLines(files[i].FullName, str_line, Encoding.UTF8);
                        TempNum++;
                    }
                }
            }
        }

        AssetDatabase.Refresh();
        Debug.Log("优化成功,共优化" + TempNum + "个脚本");
    }
}

总之就是代码批量处理了一个文件夹下.rb文件的冲突,只取服务器版本,如果熟悉python的朋友也可以自己写python脚本,使用以上方式修复.rb文件后Homebrew可以正常使用。

以下方法未实验,仅提供思路:
如果上述两者都不熟悉的可以研究下删除homebrew重装(确保Formula被删除),或者删除该文件夹再使用homebrew修复。

Last login: Thu Mar 20 15:46:30 on ttys000 qingguo@YideMacBook-Pro ~ % brew doctor Please note that these warnings are just used to help the Homebrew maintainers with debugging if you file an issue. If everything you use Homebrew for is working fine: please don't worry or file an issue; just ignore this. Thanks! Warning: Suspicious https://github.com/Homebrew/brew git origin remote found. The current git origin is: https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git With a non-standard origin, Homebrew won't update properly. You can solve this by setting the origin remote: git -C "/opt/homebrew" remote set-url origin https://github.com/Homebrew/brew Warning: Suspicious https://github.com/Homebrew/homebrew-core git origin remote found. The current git origin is: https://mirrors.ustc.edu.cn/homebrew-core.git With a non-standard origin, Homebrew won't update properly. You can solve this by setting the origin remote: git -C "/opt/homebrew/Library/Taps/homebrew/homebrew-core" remote set-url origin https://github.com/Homebrew/homebrew-core Warning: Unbrewed dylibs were found in /usr/local/lib. If you didn't put them there on purpose they could cause problems when building Homebrew formulae and may need to be deleted. Unexpected dylibs: /usr/local/lib/libvir_design.0.1.0.dylib /usr/local/lib/libvir_design.dylib Warning: You have an unnecessary local Cask tap. This can cause problems installing up-to-date casks. Please remove it by running: brew untap homebrew/cask Warning: You have an unnecessary local Core tap! This can cause problems installing up-to-date formulae. Please remove it by running: brew untap homebrew/core qingguo@YideMacBook-Pro ~ %
03-21
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值