This repository was archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathgit.coffee
More file actions
238 lines (188 loc) · 7.28 KB
/
git.coffee
File metadata and controls
238 lines (188 loc) · 7.28 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
path = require 'path'
fs = require 'fs-plus'
{Repository} = require '../build/Release/git.node'
statusIndexNew = 1 << 0
statusIndexModified = 1 << 1
statusIndexDeleted = 1 << 2
statusIndexRenamed = 1 << 3
statusIndexTypeChange = 1 << 4
statusWorkingDirNew = 1 << 7
statusWorkingDirModified = 1 << 8
statusWorkingDirDelete = 1 << 9
statusWorkingDirTypeChange = 1 << 10
statusIgnored = 1 << 14
modifiedStatusFlags = statusWorkingDirModified | statusIndexModified |
statusWorkingDirDelete | statusIndexDeleted |
statusWorkingDirTypeChange | statusIndexTypeChange
newStatusFlags = statusWorkingDirNew | statusIndexNew
deletedStatusFlags = statusWorkingDirDelete | statusIndexDeleted
indexStatusFlags = statusIndexNew | statusIndexModified |
statusIndexDeleted | statusIndexRenamed |
statusIndexTypeChange
Repository::release = ->
submoduleRepo?.release() for submodulePath, submoduleRepo of @submodules
@_release()
Repository::getWorkingDirectory = ->
@workingDirectory ?= @_getWorkingDirectory()?.replace(/\/$/, '')
Repository::getShortHead = ->
head = @getHead()
return head unless head?
return head.substring(11) if head.indexOf('refs/heads/') is 0
return head.substring(10) if head.indexOf('refs/tags/') is 0
return head.substring(13) if head.indexOf('refs/remotes/') is 0
return head.substring(0, 7) if head.match(/[a-fA-F0-9]{40}/)
return head
Repository::isStatusModified = (status=0) ->
(status & modifiedStatusFlags) > 0
Repository::isPathModified = (path) ->
@isStatusModified(@getStatus(path))
Repository::isStatusNew = (status=0) ->
(status & newStatusFlags) > 0
Repository::isPathNew = (path) ->
@isStatusNew(@getStatus(path))
Repository::isStatusDeleted = (status=0) ->
(status & deletedStatusFlags) > 0
Repository::isPathDeleted = (path) ->
@isStatusDeleted(@getStatus(path))
Repository::isPathStaged = (path) ->
@isStatusStaged(@getStatus(path))
Repository::isStatusIgnored = (status=0) ->
(status & statusIgnored) > 0
Repository::isStatusStaged = (status=0) ->
(status & indexStatusFlags) > 0
Repository::getUpstreamBranch = (branch) ->
branch ?= @getHead()
return null unless branch?.length > 11
return null unless branch.indexOf('refs/heads/') is 0
shortBranch = branch.substring(11)
branchMerge = @getConfigValue("branch.#{shortBranch}.merge")
return null unless branchMerge?.length > 11
return null unless branchMerge.indexOf('refs/heads/') is 0
branchRemote = @getConfigValue("branch.#{shortBranch}.remote")
return null unless branchRemote?.length > 0
"refs/remotes/#{branchRemote}/#{branchMerge.substring(11)}"
Repository::getAheadBehindCount = (branch='HEAD')->
if branch isnt 'HEAD' and branch.indexOf('refs/heads/') isnt 0
branch = "refs/heads/#{branch}"
counts =
ahead: 0
behind: 0
headCommit = @getReferenceTarget(branch)
return counts unless headCommit?.length > 0
upstream = @getUpstreamBranch()
return counts unless upstream?.length > 0
upstreamCommit = @getReferenceTarget(upstream)
return counts unless upstreamCommit?.length > 0
mergeBase = @getMergeBase(headCommit, upstreamCommit)
return counts unless mergeBase?.length > 0
counts.ahead = @getCommitCount(headCommit, mergeBase)
counts.behind = @getCommitCount(upstreamCommit, mergeBase)
counts
Repository::checkoutReference = (branch, create)->
if branch.indexOf('refs/heads/') isnt 0
branch = "refs/heads/#{branch}"
@checkoutRef(branch, create)
Repository::relativize = (path) ->
return path unless path
if process.platform is 'win32'
path = path.replace(/\\/g, '/')
else
return path unless path[0] is '/'
if @caseInsensitiveFs
lowerCasePath = path.toLowerCase()
workingDirectory = @getWorkingDirectory()
if workingDirectory
workingDirectory = workingDirectory.toLowerCase()
if lowerCasePath.indexOf("#{workingDirectory}/") is 0
return path.substring(workingDirectory.length + 1)
else if lowerCasePath is workingDirectory
return ''
if @openedWorkingDirectory
workingDirectory = @openedWorkingDirectory.toLowerCase()
if lowerCasePath.indexOf("#{workingDirectory}/") is 0
return path.substring(workingDirectory.length + 1)
else if lowerCasePath is workingDirectory
return ''
else
workingDirectory = @getWorkingDirectory()
if workingDirectory
if path.indexOf("#{workingDirectory}/") is 0
return path.substring(workingDirectory.length + 1)
else if path is workingDirectory
return ''
if @openedWorkingDirectory
if path.indexOf("#{@openedWorkingDirectory}/") is 0
return path.substring(@openedWorkingDirectory.length + 1)
else if path is @openedWorkingDirectory
return ''
path
Repository::submoduleForPath = (path) ->
path = @relativize(path)
return null unless path
if @submodules instanceof Function
@submodules()
for submodulePath, submoduleRepo of @submodules
if path is submodulePath
return submoduleRepo
else if path.indexOf("#{submodulePath}/") is 0
# Handle submodules inside of submodules
path = path.substring(submodulePath.length + 1)
return submoduleRepo.submoduleForPath(path) ? submoduleRepo
null
Repository::isWorkingDirectory = (path) ->
return false unless path
if process.platform is 'win32'
path = path.replace(/\\/g, '/')
else
return false unless path[0] is '/'
if @caseInsensitiveFs
lowerCasePath = path.toLowerCase()
return true if lowerCasePath is @getWorkingDirectory()?.toLowerCase()
return true if lowerCasePath is @openedWorkingDirectory?.toLowerCase()
else
return true if path is @getWorkingDirectory()
return true if path is @openedWorkingDirectory
false
realpath = (unrealPath) ->
try
fs.realpathSync(unrealPath)
catch e
unrealPath
isRootPath = (repositoryPath) ->
if process.platform is 'win32'
/^[a-zA-Z]+:[\\\/]$/.test(repositoryPath)
else
repositoryPath is path.sep
openRepository = (repositoryPath) ->
symlink = realpath(repositoryPath) isnt repositoryPath
repositoryPath = repositoryPath.replace(/\\/g, '/') if process.platform is 'win32'
repository = new Repository(repositoryPath)
if repository.exists()
repository.caseInsensitiveFs = fs.isCaseInsensitive()
if symlink
workingDirectory = repository.getWorkingDirectory()
while not isRootPath(repositoryPath)
if realpath(repositoryPath) is workingDirectory
repository.openedWorkingDirectory = repositoryPath
break
repositoryPath = path.resolve(repositoryPath, '..')
repository
else
null
openSubmodules = (repository) ->
repository.submodules = {}
for relativePath in repository.getSubmodulePaths() when relativePath
submodulePath = path.join(repository.getWorkingDirectory(), relativePath)
if submoduleRepo = openRepository(submodulePath)
if submoduleRepo.getPath() is repository.getPath()
submoduleRepo.release()
else
openSubmodules(submoduleRepo)
repository.submodules[relativePath] = submoduleRepo
exports.open = (repositoryPath) ->
repository = openRepository(repositoryPath)
if repository?
# openSubmodules may be long (e.g., on network FS),
# so loading them lazily
repository.submodules = -> openSubmodules(repository)
repository