This repository has been archived by the owner on Oct 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 197
/
git-new-workdir.cmd
105 lines (77 loc) · 2.5 KB
/
git-new-workdir.cmd
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
@ECHO OFF
REM Create a GIT working directory for an existing repository
REM %1 - Path of the existing repository
REM %2 - Path of the new working directory
REM %3 - Branch (optional)
IF #%1# == ## GOTO usage
IF #%2# == ## GOTO usage
SET GITCMD="%COMSPEC%" /c git
SET BRANCH=%3
REM Get source and destination folder
FOR %%d IN (%1) DO SET SRC=%%~fd
FOR %%d IN (%2) DO SET DST=%%~fd
REM Want to make sure that what is pointed to has a .git directory ...
PUSHD "%SRC%" 2>NUL
IF ERRORLEVEL 1 GOTO error_no_source
%GITCMD% rev-parse --git-dir 2>&1 >NUL
IF ERRORLEVEL 1 GOTO error_no_repository
REM Get full path to .git directory
FOR /F %%d IN ('%GITCMD% rev-parse --git-dir') DO SET GITDIR=%%~fd
POPD
REM Get a short name to prevent spaces
FOR %%f in ("%GITDIR%") DO SET GITSHORTDIR=%%~fsf
REM Don't link to a configured bare repository
FOR /F %%d IN ('%GITCMD% --git-dir=%GITSHORTDIR% config --bool --get core.bare') DO SET IS_BARE=%%d
IF #%IS_BARE%# == #true# GOTO error_bare
REM TODO: Check if the source is a working copy
REM Do not overwrite existing directories
IF EXIST "%DST%" GOTO error_destination_exists
REM Create the workdir and the logs sub dir
MKDIR "%DST%\.git\logs"
IF ERRORLEVEL 1 GOTO error_create_workdir
REM create the links to the original repo. explicitly exclude index, HEAD and
REM logs/HEAD from the list since they are purely related to the current working
REM directory, and should not be shared.
REM Directories
FOR %%x in (refs logs\refs objects info hooks remotes rr-cache svn) DO (
IF EXIST "%GITDIR%\%%x" (
mklink /D "%DST%\.git\%%x" "%GITDIR%\%%x" 2>&1 >NUL
)
)
REM Files
FOR %%x in (config packed-refs) DO (
IF EXIST "%GITDIR%\%%x" (
mklink "%DST%\.git\%%x" "%GITDIR%\%%x" 2>&1 >NUL
)
)
REM Now setup the workdir
PUSHD "%DST%"
REM Copy the HEAD from the original repository as a default branch
COPY "%GITDIR%\HEAD" .git\HEAD >NUL
REM Checkout the branch (either the same as HEAD from the original repository, or
REM the one that was asked for)
%GITCMD% checkout -f %BRANCH%
ECHO Created work dir in "%DST%"
POPD
REM That's it
EXIT /B 0
REM ---- Error messages ----
:usage
ECHO Usage: %0 ^<repository^> ^<new_workdir^> [^<branch^>]
EXIT /B 127
:error_no_source
ECHO Directory not found: "%SRC%"
EXIT /B 128
:error_no_repository
ECHO Not a git repository: "%SRC%"
POPD
EXIT /B 128
:error_bare
ECHO "%SRC%" is a bare repository.
EXIT /B 128
:error_destination_exists
ECHO Destination directory "%DST%" already exists
EXIT /B 128
:error_create_workdir
ECHO Unable to create "%DST%"!
EXIT /B 128