-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-open.bat
67 lines (59 loc) · 2.22 KB
/
git-open.bat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
:: git open <branch/commit> <path-to-file>
:: or
:: git open <path-to-file>
:: parameters..
:: --interactive/-i <- will prompt with which program to open the file..?
@echo off
SETLOCAL enabledelayedexpansion
IF [%1]==[] EXIT
IF NOT [%4]==[] EXIT
SET a=0
SET b=0
SET interactive=0
IF %1==-i SET a=1
IF %1==--interactive SET a=1
IF %a%==1 SET /A "b+=1" & SET "interactive=1" & SET "argument1=%2" & SET argument2=%3
SET a=0
IF "%2"=="-i" SET a=1
IF "%2"=="--interactive" SET a=1
IF %a%==1 SET /A "b+=1" & SET "interactive=1" & SET "argument1=%1" & SET argument2=%3
SET a=0
IF "%3"=="-i" SET a=1
IF "%3"=="--interactive" SET a=1
IF %a%==1 SET /A "b+=1" & SET "interactive=1" & SET "argument1=%1" & SET argument2=%2
IF %b% GTR 1 EXIT
IF %interactive%==0 SET "argument1=%1" & SET "argument2=%2"
IF [%argument2%]==[] (
SET branchOrCommit=
:: ^
:: if staged.. staged, otherwise.. HEAD
SET pathToFile=%argument1%
) ELSE (
SET branchOrCommit=%argument1%
SET pathToFile=%argument2%
)
git show %branchOrCommit%:%pathToFile% > NUL || EXIT
SET a=$a=[System.IO.Path]::GetTempFileName();
:: NOTE: using delayed expansion here to prevent e.g. "| Out-Null" from causing issues
SET a=!a! Remove-Item $a;
SET a=!a! New-Item $a -ItemType \"Directory\" ^| Out-Null;
SET a=!a! $b=$a + (\"\%pathToFile%\" -replace '/','\');
:: NOTE: create empty file first to assure all folder(s) within "tempfilename folder" if any is/are created
SET a=!a! New-Item $b -Force ^| Out-Null;
SET a=!a! cmd.exe /C (\"git show %branchOrCommit%:%pathToFile% ^> `\"\" + $b + \"`\"\");
SET a=!a! $c=New-Object System.Diagnostics.Process;
IF %interactive%==1 (
SET a=!a! $c.StartInfo.Filename = \"openwith.exe\";
SET a=!a! $c.StartInfo.Arguments = $b;
) ELSE (
SET a=!a! $c.StartInfo.Filename = $b;
)
SET a=!a! $c.start() ^| Out-Null;
IF %interactive%==1 SET a=!a! Wait-Process -InputObject $c;
:: ^
:: wait for openwith.exe dialog to close
SET a=!a! Start-Sleep -Milliseconds 500;
:: ^
:: wait for arbitrary amount of time as I don't know how to wait for file to finish opening
SET a=!a! Remove-Item -Recurse $a;
powershell.exe -Command "!a!"