# 如何從 WSL 叫用 TortoiseGit 的 Show log 並在出現錯誤訊息視窗時複製訊息

因為我有些專案是在 WSL 環境下開發，我最近嘗試從 WSL 叫出 [TortoiseGit](https://tortoisegit.org/) 的 [Show log](https://tortoisegit.org/docs/tortoisegit/tgit-dug-showlog.html) 視窗，但是卻頻繁的遇到 `Failed to get UnRev file list` 的問題 (如下圖示)，解決方法其實很簡單，只要執行一下錯誤訊息中的命令就好，但這個視窗無法選取文字，我要怎麼複製呢？原來有個不在文件中的密技可以用！

![image](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/8cef2c1816d8bc6dbfcb-203998063-cfbc69f4-749d-411c-af84-3781f80db897.png)

### 如何複製 TortoiseGit 錯誤訊息視窗上的訊息文字

其實，你只要先用滑鼠在視窗上用左鍵點擊一下，讓視窗呈現在最上方，接著用鍵盤按一下 `Ctrl+C` 就可以複製視窗上的所有訊息，就這麼簡單！😃

接著你到任意文字編輯器，按下 `Ctrl+V` 貼上文字即可：

```
---------------------------
TortoiseGit
---------------------------
fatal: detected dubious ownership in repository at '//wsl.localhost/Ubuntu-20.04/home/will/projects/Learn-Git-in-30-days'
To add an exception for this directory, call:

	git config --global --add safe.directory '%(prefix)///wsl.localhost/Ubuntu-20.04/home/will/projects/Learn-Git-in-30-days'
---------------------------
OK
---------------------------
```

### 關於 `safe.directory` 的背景知識

今年(2022)四月的時候，Git 釋出 `2.35.2` 版本，解決了 2 個安全性問題 ([CVE-2022-24765](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2022-24765), [CVE-2022-24767](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2022-24767))，當你升級到最新版或是在新電腦全新安裝 Git 的時候，新的機制可能會讓你無法正常使用 Git，尤其是你用 Git for Windows 或在 Linux 多人環境下操作，都有可能會踩到地雷！🔥

1.  [CVE-2022-24765](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2022-24765)
    
    在多人使用同一台電腦的情況下，可能會有人偷偷寫入一些惡意檔案在你的 Git 工作目錄，修改你的 Git 設定等等，這種攻擊手法可能會導致你在使用 Git 的時候，意外的執行惡意程式。當 Git 升級到最新版之後，就可以免除這種疑慮，簡單來說，只要你遇到**檔案或目錄**的**擁有者**不是**你自己**(目前登入使用者)，就會自動停止運作，也就會產生本篇文章所描述的那個錯誤畫面。
    
    如果你真的能夠「信任」這個資料夾，可以透過修改 `git config` 的 `safe.directory` 設定來解決！
    
    > 注意: 設定了之後可能會讓你的 Git 目錄變的**不安全**，因為是你自己信任這個資料夾的！🔥
    
2.  [CVE-2022-24767](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2022-24767)
    
    有人在 Git for Windows 的「解除安裝程式」(Uninstaller)植入木馬或惡意軟體，升級到最新版就可以解決被入侵的疑慮。
    

### 關於 WSL 呼叫 TortoiseGit 的相關知識

由於 WSL 是 Linux 環境，但可以直接呼叫 Windows 應用程式，所以我們可以透過以下命令，直接叫用 Windows 的 TortoiseGit 程式：

```bash
/mnt/c/PROGRA~1/TortoiseGit/bin/TortoiseGitProc.exe
```

但是我們需要從 WSL 透過 `git tlog` 這個 Alias 來執行這支程式，所以我們必須在 WSL 輸入以下命令進行設定：

```bash
git config --global alias.tlog '!'"gtlog() { /mnt/c/PROGRA~1/TortoiseGit/bin/TortoiseGitProc.exe /command:log /path:. ; }; gtlog"
```

這裡的 `/path:.` 代表「當前目錄」的意思，但由於我們執行的是 Windows 下的 `TortoiseGitProc.exe` 應用程式，所以 WSL 會自動幫我們把 `.` 轉譯成以下路徑：

```bash
\\wsl.localhost\Ubuntu-20.04\home\will\projects\Learn-Git-in-30-days
```

然而，因為 `TortoiseGitProc.exe` 程式是跑在 Windows 的，因此 TortoiseGit 所讀到的 Git 設定值，其實都來自於 Windows 的 Git 全域設定檔 (`~/.gitconfig`) ，而非 WSL 底下的。所以我們要設定 `safe.directory` 的話，你必須在 Windows 執行以下命令，才能真正將該目錄加入到 `safe.directory` 之中！🔥

```bash
git config --global --add safe.directory '%(prefix)///wsl.localhost/Ubuntu-20.04/home/will/projects/Learn-Git-in-30-days'
```

這裡的 `%(prefix)` 可以說相當重要，他會讓 Git 正確識別這個特殊的 SMB 路徑，而 `wsl.localhost` 是 WSL 專屬的主機名稱，而 `Ubuntu-20.04` 則是 WSL 的 Distro 名稱，剩下的就是你在 WSL 的完整路徑！ 👍

### 總結

最後摘要一下今日重點：

1.  從 WSL 設定 `git tlog` 命令(Git Alias)
    
    ```bash
    git config --global alias.tlog '!'"gtlog() { /mnt/c/PROGRA~1/TortoiseGit/bin/TortoiseGitProc.exe /command:log /path:. ; }; gtlog"
    ```
    
2.  進入 WSL 中的專案資料夾，執行 `git tlog` 觸發 TortoiseGit 的 Show log 視窗，並出現錯誤訊息
    
    點擊視窗後，用鍵盤按下 `Ctrl+C` 複製訊息出來：
    
    ```
    fatal: detected dubious ownership in repository at '//wsl.localhost/Ubuntu-20.04/home/will/projects/Learn-Git-in-30-days'
    To add an exception for this directory, call:
    
      git config --global --add safe.directory '%(prefix)///wsl.localhost/Ubuntu-20.04/home/will/projects/Learn-Git-in-30-days'
    ```
    
3.  從錯誤訊息擷取命令，並回到 Windows 執行以下命令
    
    ```
    git config --global --add safe.directory '%(prefix)///wsl.localhost/Ubuntu-20.04/home/will/projects/Learn-Git-in-30-days'
    ```
    

### 相關連結

-   [safe.directory - Git - git-config Documentation](https://git-scm.com/docs/git-config/2.35.2#Documentation/git-config.txt-safedirectory)
-   [Log Dialog – TortoiseGit – Documentation – TortoiseGit – Windows Shell Interface to Git](https://tortoisegit.org/docs/tortoisegit/tgit-dug-showlog.html)
-   [Git security vulnerability announced | The GitHub Blog](https://github.blog/2022-04-12-git-security-vulnerability-announced/)
-   [Git for Windows' uninstaller vulnerable to DLL hijacking when run under the SYSTEM user account](https://github.com/git-for-windows/git/security/advisories/GHSA-gf48-x3vr-j5c3)
-   [\[ANNOUNCE\] Git v2.35.2 and below for CVE-2022-24765 - Junio C Hamano](https://lore.kernel.org/git/xmqqv8veb5i6.fsf@gitster.g/)
-   [cve 2022 24765 - I cannot add the parent directory to *safe.directory* in Git - Stack Overflow](https://stackoverflow.com/a/71850261)
