Launch git-fork app from WSL
Tags: wsl, fork, vscode, git
Published: 2019-10-25 01:14:39

As a web developer, I usually work on a Mac at work, and on my Windows 10 PC at home.

This article descrieve how I mange to use the Windows [Fork] app from my [WSL] installation.


## TL:DR

1. From WSL edit your ~/.bashrc file
2. Add the following alias:`alias fork='/mnt/c/Windows/System32/cmd.exe /c "%USERPROFILE%\\AppData\Local\Fork\Fork.exe "$(wslpath -w -a .)'
`
3. Save your changes.
4. Restart the terminal, so the new alias become available.

## How to use

1. From WSL, navigate to a project folder that use git.
2. Run the alias: `fork`.
3. On Windows, the [Fork] app will open an the project git will be selected for view.


## Background 

I use [vscode] and the integrated terminal to manage my projects (for example to run Gulp.js tasks).

And as I do prefer the Linux/Mac terminal better, I configured [vscode] to work with the [WSL] bash as default.

Now, and **this is important**, I made symbolic link between my Window installation `Work` folder at `C:\Work\` and my [WSL] installation at `/home/ubuntu/Work/` folder.
This make super easy to work with any of my project, regardless if I'm working from Windows or from [WSL].


## The problem with [Fork] and [WSL]

One tool I like a lot is the free git client [Fork].

While I usually use git from between the [vscode], or from the [WSL] terminal, I like [Fork] the best to visualize and manage my projects git branches.

[Fork] is available for Mac and Windows, but not for Linux. :(
 
And as I configured [vscode] to use [WSL] as the default bash, I was unable to launch [Fork] from the terminal in the current working folder, as I regulary can on Mac or Windows.

Or that is what I thought, _at first_. :)


## "wslpath" to the rescue

[wslpath] is a [WSL] tool that converts WSL paths to Windows paths.

And since I already created a symbolic link for my `Work` folder under [WSL], all what was left was _just_ to find a way to launch [Fork] on Windows from the [WSL] terminal, and to pass the current working [WSL] folder path converted to a Windows path.

The first part was easy enough. You can launch any Windows app fro [WSL] using this command: `/mnt/c/Windows/System32/cmd.exe /c [PATH TO THE WINDOWS .EXE FILE]`

So to launch [Fork] I can use this command: `/mnt/c/Windows/System32/cmd.exe /c "%USERPROFILE%\AppData\Local\Fork\Fork.exe`.
> The Windows variable `%USERPROFILE%` will be automatically replaced with your Windows User folder path (ex.: `C:\Users\yourusername`).
  
You can pass a path to [Fork] executable file to launch the app and to automatically open the git repository on that path.
On linux, the current folder path is represented by `.`. But passing this to the [Fork] executable on Windows means nothing. I needed a way to convert this to a full and absolute Windows path.

And that is what [wslpath] does!.

So the final command looks like this: `/mnt/c/Windows/System32/cmd.exe /c "%USERPROFILE%\AppData\Local\Fork\Fork.exe "$(wslpath -w -a .)`
> I added two parameters.  
>The `-w` parameter _translate from a WSL path to a Windows path_.
>And the `-a` parameter _force result to absolute path format_.

The final step is to add this working command as a `fork` alias on my [WSL] installation, on your [WLS] user `.bashrc` file.
From your [WSL] installation, edit the `~/.bashrc` file and add the following line: `alias fork='/mnt/c/Windows/System32/cmd.exe /c "%USERPROFILE%\AppData\Local\Fork\Fork.exe "$(wslpath -w -a .)'`


The result is that I can use [Fork] transparently from. between my [WSL] installation. I love it!


Happy coding!!



[WSL]: https://en.wikipedia.org/wiki/Windows_Subsystem_for_Linux
[Fork]: https://git-fork.com/
[vscode]: https://code.visualstudio.com/
[wslpath]: https://blogs.msdn.microsoft.com/commandline/2017/12/22/share-environment-vars-between-wsl-and-windows/