xcodeproj 合併衝突

20200121

20190218 issue

  • 在 Xcode10 的環境下,若有兩個 feature 分支同時對檔案新增資料夾,在合併到本支後,下面的所有工具及設定都無法完美的解決衝突問題,最慘的狀況還會造成專案無法打開

  • 目前的研究,應該是 pbxproj 在合併之後,檔案結構不正確所造成 參考連結

6D422F9B221A52BE003CDA51 /* ha2-1 */ = {
        isa = PBXGroup;
        children = (
                6D422F9C221A52D1003CDA51 /* ha2-1.swift */,
        );
        path = "ha2-1";

        sourceTree = "<group>"; <-- 合併後會少了這行
}; <-- 合併後會少了這行

6D422F9E221A52F1003CDA51 /* ha2-2 */ = {
        isa = PBXGroup;
        children = (
                6D422F9F221A5309003CDA51 /* ha2-2.swift */,
        );
        path = "ha2-2";

        sourceTree = "<group>";
};
  • 即使在不使用任何手段的狀況下,試圖手動去解決衝突(刪除 <<<<< HEAD 等標記符號),仍然會產生如上述檔案結構不正確的結果,推論這應該是 git 不知如何正確處理 pbxproj 所導致,和用什麼工具及設定無關

  • 目前還是決定使用 *.pbxproj merge=union 的作法,必須注意

    • 就目前研究來看,增減檔案沒有問題

    • 專案的資料夾不可任意增減

    • 遇到專案無法打開,密切注意PBXGroup結構是否不正確,並手動修正

      /* Begin PBXGroup section */
  • Automatically resolving git merge conflicts in Xcode’s project.pbxproj file

    • 1. Create a .gitattributes file

      Assuming you don’t already have one, create a file called .gitattributes in your project’s root directory.

    • 2. Set the merge strategy to union

      Add the following line to your .gitattributes file:

      *.pbxproj merge=union

      This will tell git to merge using the union strategy, meaning it’ll keep both sides (theirs and ours) during a merge. This is almost always what you want in the case of a *.pbxproj merge conflict.

    • 3. Add the .gitattributes file to git and push your changes

      Now that your merge strategy is set, make sure to give the rest of your team this wonderful gift. Add the .gitattributes file to your git repo:

      git add .gitattributes
  • mergepbx

    • 手動解決

      • 如果合併後發生衝突,可執行以下指令(手動用)

        git mergetool --tool=mergepbx [你的專案名稱].xcodeproj/project.pbxproj
      • 但在執行上述指令前,須先在 ~/.gitconfig 加入以下設定

        [mergetool "mergepbx"]
        cmd = mergepbx "$BASE" "$LOCAL" "$REMOTE" -o "$MERGED"
    • 自動解決

      • 如果想合併後自動解決衝突,可在~/.gitconfig 加入以下設定

        [merge "mergepbx"]
        name = XCode project files merger
        driver = /usr/local/bin/mergepbx %O %A %B
      • 在專案目錄下,新增 .gitattributes,並加入以下內容

        *.pbxproj merge=mergepbx
  • xUniqe

    • 補充:pre-commit 內容如下

      #!/bin/sh
      PATH=$PATH:/usr/local/bin  # 加這行 SourceTree 才能正常執
      xunique CathayHospital.xcodeproj
    • 記得修改 pre-commit 權限

      chmod 755 .git/hooks/pre-commit

Last updated