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修复。