-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBitGitRouter.hs
More file actions
189 lines (176 loc) · 7.1 KB
/
BitGitRouter.hs
File metadata and controls
189 lines (176 loc) · 7.1 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
{- |
Git router executable: dispatches @git@ commands to either @bit@ (for .bit repos)
or real git (for .git repos).
Installed by @bit become-git@, this sits on PATH as @git.exe@ and transparently
routes commands based on whether the current directory is a bit or git repo.
Design: standalone binary with no bit module imports — keeps startup fast and
avoids pulling in the entire bit dependency tree.
-}
module Main (main) where
import System.Environment (getArgs, lookupEnv, setEnv, getExecutablePath)
import System.Exit (exitWith)
import System.FilePath ((</>), takeDirectory)
import System.Directory (doesDirectoryExist, doesFileExist, getCurrentDirectory,
findExecutable)
import System.Process (rawSystem)
import Data.List (isPrefixOf)
main :: IO ()
main = do
args <- getArgs
realGit <- findRealGit
junctionMode <- isJunctionMode
if junctionMode
then do
-- Test suite mode: always route to bit (same as bash shim).
-- BIT_GIT_JUNCTION=1 means bit will create .git junctions on init
-- and handle all commands unconditionally.
setEnv "BIT_REAL_GIT" realGit
setEnv "BIT_GIT_JUNCTION" "1"
bitExe <- findBit
exec bitExe args
else do
-- Normal mode: init → real git, import/export → bit,
-- bit repos → bit, else → real git
if isGitInit args
then exec realGit args
else if isBitOnlyCommand args
then do
setEnv "BIT_REAL_GIT" realGit
bitExe <- findBit
exec bitExe args
else do
isBit <- walkUpForBitDir
if isBit
then do
setEnv "BIT_REAL_GIT" realGit
bitExe <- findBit
exec bitExe args
else exec realGit args
-- | Check if junction mode is enabled via BIT_GIT_JUNCTION=1.
isJunctionMode :: IO Bool
isJunctionMode = do
mVal <- lookupEnv "BIT_GIT_JUNCTION"
pure (mVal == Just "1")
-- | Check if the command is a bit-only command (import, export, become-git).
-- These must route to bit even from a non-bit repo.
isBitOnlyCommand :: [String] -> Bool
isBitOnlyCommand = go
where
go [] = False
go ("import":_) = True
go ("export":_) = True
go ("become-git":_) = True
go ("-c":_:rest) = go rest
go ("-C":_:rest) = go rest
go (x:rest)
| "-" `isPrefixOf` x = go rest
| otherwise = False
-- | Check if the command is @git init@.
-- Skips leading flags (-c, -C, --bare) to find the subcommand.
isGitInit :: [String] -> Bool
isGitInit [] = False
isGitInit ("init":_) = True
isGitInit ("-c":_:rest) = isGitInit rest
isGitInit ("-C":_:rest) = isGitInit rest
isGitInit ("--bare":rest) = isGitInit rest
isGitInit (x:rest)
| "-" `isPrefixOf` x = isGitInit rest
| otherwise = False
-- | Walk up from CWD looking for a @.bit/@ directory or @.bit@ file (bitlink).
-- If we hit a @.git/@ directory without a corresponding @.bit/@, this is a
-- pure git repo — stop walking and return False.
walkUpForBitDir :: IO Bool
walkUpForBitDir = getCurrentDirectory >>= go
where
go dir = do
hasBitDir <- doesDirectoryExist (dir </> ".bit")
hasBitFile <- doesFileExist (dir </> ".bit")
if hasBitDir || hasBitFile
then pure True
else do
hasGitDir <- doesDirectoryExist (dir </> ".git")
hasGitFile <- doesFileExist (dir </> ".git")
if hasGitDir || hasGitFile
then pure False -- pure git repo, don't walk further
else let parent = takeDirectory dir
in if parent == dir
then pure False
else go parent
-- | Find the real git binary.
-- Priority: 1) BIT_REAL_GIT env var, 2) config file ~/.bit-router/real-git,
-- 3) search PATH excluding our own directory.
findRealGit :: IO FilePath
findRealGit = do
-- 1. BIT_REAL_GIT env var (set by bit become-git or test shim)
mEnv <- lookupEnv "BIT_REAL_GIT"
case mEnv of
Just p | not (null p) -> pure p
_ -> do
-- 2. Config file written by become-git
home <- getHome
let configFile = home </> ".bit-router" </> "real-git"
hasConfig <- doesFileExist configFile
if hasConfig
then do
content <- readFile configFile
let path = filter (\c -> c /= '\n' && c /= '\r') content
if null path then fallbackSearch else pure path
else fallbackSearch
-- | Search PATH for git, excluding our own directory.
fallbackSearch :: IO FilePath
fallbackSearch = do
myPath <- getExecutablePath
let myDir = takeDirectory myPath
-- findExecutable searches PATH; if it returns our own dir, we need
-- to do a manual search. For now, just use findExecutable and hope
-- the real git is elsewhere on PATH.
mGit <- findExecutable "git"
case mGit of
Just p | takeDirectory p /= myDir -> pure p
-- If we can only find ourselves, try common locations
_ -> do
let candidates = [ "/usr/bin/git"
, "/usr/local/bin/git"
, "C:/Program Files/Git/cmd/git.exe"
, "C:/Program Files (x86)/Git/cmd/git.exe"
]
found <- mapM doesFileExist candidates
case [c | (c, True) <- zip candidates found] of
(c:_) -> pure c
[] -> error "bit-git-router: cannot find real git. Set BIT_REAL_GIT or run 'bit become-git' again."
-- | Find the bit executable. Must be in the same directory as the router,
-- or on PATH.
findBit :: IO FilePath
findBit = do
myPath <- getExecutablePath
let myDir = takeDirectory myPath
-- Check same directory first
let sameDirBit = myDir </> "bit.exe"
hasSameDir <- doesFileExist sameDirBit
if hasSameDir
then pure sameDirBit
else do
let sameDirBitNoExt = myDir </> "bit"
hasSameDirNoExt <- doesFileExist sameDirBitNoExt
if hasSameDirNoExt
then pure sameDirBitNoExt
else do
mBit <- findExecutable "bit"
case mBit of
Just p -> pure p
Nothing -> error "bit-git-router: cannot find 'bit' executable"
-- | Get home directory (cross-platform).
getHome :: IO FilePath
getHome = do
mHome <- lookupEnv "USERPROFILE" -- Windows
case mHome of
Just h -> pure h
Nothing -> do
mUnix <- lookupEnv "HOME"
case mUnix of
Just h -> pure h
Nothing -> pure "."
-- | Execute a program with args, replacing this process.
-- On Windows, rawSystem is effectively exec (waits and exits with same code).
exec :: FilePath -> [String] -> IO ()
exec prog args = rawSystem prog args >>= exitWith