FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
nodegit/test/tests/stage.js at master · codercatdev/nodegit · GitHub
codercatdev
nodegit
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
nodegit
/
test
/
tests
/
stage.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
523 lines (489 loc) · 17.3 KB
Breadcrumbs
nodegit
/
test
/
tests
/
stage.js
Copy path
File metadata and controls
523 lines (489 loc) · 17.3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
var
assert
=
require
(
"assert"
)
;
var
path
=
require
(
"path"
)
;
var
fse
=
require
(
"fs-extra"
)
;
var
exec
=
require
(
"../../utils/execPromise"
)
;
describe
(
"Stage"
,
function
(
)
{
var
RepoUtils
=
require
(
"../utils/repository_setup"
)
;
var
NodeGit
=
require
(
"../../"
)
;
var
test
;
beforeEach
(
function
(
)
{
test
=
this
;
var
repoDir
=
"../repos/stagingRepo/"
;
var
repoPath
=
path
.
resolve
(
__dirname
,
repoDir
)
;
return
RepoUtils
.
createRepository
(
repoPath
)
.
then
(
function
(
repo
)
{
test
.
repository
=
repo
;
}
)
;
}
)
;
after
(
function
(
)
{
return
fse
.
remove
(
test
.
repository
.
workdir
(
)
)
;
}
)
;
function
stagingTest
(
isUnstaged
,
newFileContent
,
discarding
)
{
var
fileContent
=
newFileContent
||
"One line of text\n"
+
"Two lines of text\n"
+
"Three lines of text\n"
+
"Four lines of text\n"
+
"Five lines of text\n"
+
"Six lines of text\n"
+
"Seven lines of text\n"
+
"Eight lines of text\n"
+
"Nine lines of text\n"
+
"Ten lines of text\n"
+
"Eleven lines of text\n"
+
"Twelve lines of text\n"
+
"Thirteen lines of text\n"
+
"Fourteen lines of text\n"
+
"Fifteen lines of text\n"
+
"Sixteen lines of text\n"
+
"Seventeen lines of text\n"
+
"Eighteen lines of text\n"
+
"Nineteen lines of text\n"
+
"Twenty lines of text\n"
;
var
fileName
=
"stagedLinesTest.txt"
;
var
expectedContent
;
var
workingDirFile
;
var
getDiffFunction
;
if
(
!
isUnstaged
||
discarding
)
{
expectedContent
=
fileContent
.
replace
(
"Three"
,
"Changed three"
)
.
replace
(
"Seventeen"
,
"Changed seventeen"
)
;
workingDirFile
=
expectedContent
.
replace
(
"Fifteen"
,
"Changed fifteen"
)
;
}
else
{
expectedContent
=
fileContent
.
replace
(
"Fifteen"
,
"Changed fifteen"
)
;
workingDirFile
=
expectedContent
.
replace
(
"Three"
,
"Changed three"
)
.
replace
(
"Seventeen"
,
"Changed seventeen"
)
;
}
if
(
isUnstaged
)
{
getDiffFunction
=
function
(
)
{
return
test
.
repository
.
refreshIndex
(
)
.
then
(
function
(
index
)
{
return
NodeGit
.
Diff
.
indexToWorkdir
(
test
.
repository
,
index
,
{
flags
:
NodeGit
.
Diff
.
OPTION
.
SHOW_UNTRACKED_CONTENT
|
NodeGit
.
Diff
.
OPTION
.
RECURSE_UNTRACKED_DIRS
}
)
;
}
)
;
}
;
}
else
{
getDiffFunction
=
function
(
)
{
return
RepoUtils
.
addFileToIndex
(
test
.
repository
,
fileName
)
.
then
(
function
(
)
{
return
test
.
repository
.
getBranchCommit
(
"master"
)
;
}
)
.
then
(
function
(
masterCommit
)
{
var
treePromise
=
masterCommit
.
getTree
(
)
;
var
indexPromise
=
test
.
repository
.
refreshIndex
(
)
;
return
Promise
.
all
(
[
treePromise
,
indexPromise
]
)
;
}
)
.
then
(
function
(
treeAndIndex
)
{
var
masterTree
=
treeAndIndex
[
0
]
;
var
index
=
treeAndIndex
[
1
]
;
return
NodeGit
.
Diff
.
treeToIndex
(
test
.
repository
,
masterTree
,
index
,
{
flags
:
NodeGit
.
Diff
.
OPTION
.
SHOW_UNTRACKED_CONTENT
|
NodeGit
.
Diff
.
OPTION
.
RECURSE_UNTRACKED_DIRS
}
)
;
}
)
;
}
;
}
return
RepoUtils
.
commitFileToRepo
(
test
.
repository
,
fileName
,
fileContent
)
.
then
(
function
(
)
{
return
fse
.
writeFile
(
path
.
join
(
test
.
repository
.
workdir
(
)
,
fileName
)
,
workingDirFile
)
;
}
)
.
then
(
function
(
)
{
return
getDiffFunction
(
)
;
}
)
.
then
(
function
(
fileDiff
)
{
return
fileDiff
.
patches
(
)
;
}
)
.
then
(
function
(
patches
)
{
var
pathPatch
=
patches
.
filter
(
function
(
patch
)
{
return
patch
.
newFile
(
)
.
path
(
)
===
fileName
;
}
)
;
return
pathPatch
[
0
]
.
hunks
(
)
;
}
)
.
then
(
function
(
pathHunks
)
{
var
linePromises
=
[
]
;
pathHunks
.
forEach
(
function
(
pathHunk
)
{
linePromises
.
push
(
pathHunk
.
lines
(
)
)
;
}
)
;
return
Promise
.
all
(
linePromises
)
;
}
)
.
then
(
function
(
lines
)
{
var
linesToStage
=
[
]
;
lines
.
forEach
(
function
(
hunkLines
)
{
hunkLines
.
forEach
(
function
(
line
)
{
if
(
line
.
content
(
)
.
toLowerCase
(
)
.
indexOf
(
"fifteen"
)
>=
0
)
{
linesToStage
.
push
(
line
)
;
}
}
)
;
}
)
;
if
(
discarding
)
{
return
test
.
repository
.
discardLines
(
fileName
,
linesToStage
)
;
}
return
test
.
repository
.
stageLines
(
fileName
,
linesToStage
,
!
isUnstaged
)
;
}
)
.
then
(
function
(
)
{
if
(
discarding
)
{
return
fse
.
readFile
(
path
.
join
(
test
.
repository
.
workdir
(
)
,
fileName
)
,
"utf8"
)
;
}
return
test
.
repository
.
refreshIndex
(
)
.
then
(
function
(
reloadedIndex
)
{
var
pathOid
=
reloadedIndex
.
getByPath
(
fileName
)
.
id
;
return
test
.
repository
.
getBlob
(
pathOid
)
;
}
)
;
}
)
.
then
(
function
(
resultFileContents
)
{
assert
.
equal
(
resultFileContents
.
toString
(
)
,
expectedContent
)
;
}
)
;
}
it
(
"can stage selected lines"
,
function
(
)
{
return
stagingTest
(
true
)
;
}
)
;
it
(
"can unstage selected lines"
,
function
(
)
{
return
stagingTest
(
false
)
;
}
)
;
//This is used to test cases where there are no newline at EOF
var
newlineEofTestFileContent
=
"One line of text\n"
+
"Two lines of text\n"
+
"Three lines of text\n"
+
"Four lines of text\n"
+
"Five lines of text\n"
+
"Six lines of text\n"
+
"Seven lines of text\n"
+
"Eight lines of text\n"
+
"Nine lines of text\n"
+
"Ten lines of text\n"
+
"Eleven lines of text\n"
+
"Twelve lines of text\n"
+
"Thirteen lines of text\n"
+
"Fourteen lines of text\n"
+
"Fifteen lines of text"
;
it
(
"can stage last line with no newline at EOF"
,
function
(
)
{
return
stagingTest
(
true
,
newlineEofTestFileContent
)
;
}
)
;
it
(
"can unstage last line with no newline at EOF"
,
function
(
)
{
return
stagingTest
(
false
,
newlineEofTestFileContent
)
;
}
)
;
it
(
"can stage second to last line with no newline at EOF"
,
function
(
)
{
var
newlineEofTestFileContent2
=
newlineEofTestFileContent
+
"\nSixteen lines of text\nSeventeen lines of text\nEighteen lines of text"
;
return
stagingTest
(
true
,
newlineEofTestFileContent2
)
;
}
)
;
it
(
"can unstage second to last line with no newline at EOF"
,
function
(
)
{
var
newlineEofTestFileContent2
=
newlineEofTestFileContent
+
"\nSixteen lines of text\nSeventeen lines of text\nEighteen lines of text"
;
return
stagingTest
(
false
,
newlineEofTestFileContent2
)
;
}
)
;
//This is used to test case where the last hunk is staged.
var
lastHunkStagedFileContent
=
"Thirteen lines of text\n"
+
"Fourteen lines of text\n"
+
"Fifteen lines of text\n"
+
"Sixteen lines of text\n"
+
"Shforteenteen lines of text\n"
;
it
(
"staging last hunk stages whole file if no filemode changes"
,
function
(
)
{
return
stagingTest
(
true
,
lastHunkStagedFileContent
)
.
then
(
function
(
)
{
return
test
.
repository
.
refreshIndex
(
)
;
}
)
.
then
(
function
(
index
)
{
return
NodeGit
.
Diff
.
indexToWorkdir
(
test
.
repository
,
index
,
{
flags
:
NodeGit
.
Diff
.
OPTION
.
SHOW_UNTRACKED_CONTENT
|
NodeGit
.
Diff
.
OPTION
.
RECURSE_UNTRACKED_DIRS
}
)
;
}
)
.
then
(
function
(
diff
)
{
assert
.
equal
(
Object
.
keys
(
diff
)
.
length
,
0
)
;
//Empty diff
return
diff
.
patches
(
)
;
}
)
.
then
(
function
(
patches
)
{
//patches will have at least one item if there is something unstaged
assert
.
equal
(
patches
.
length
,
0
)
;
}
)
;
}
)
;
function
compareFilemodes
(
vsWorkdir
,
index
,
fileModeDifference
)
{
//Takes diff of head commit vs Workdir (if vsWorkdir is set) or vs Index
//(if vsWorkdir is unset). Note: there's only one file in the filemode
//staging tests for which this helper fn was written.
//index - index to use (vsWorkdir is unset)
//fileModeDifference - expected (newfilemode) - (oldfilemode)
return
test
.
repository
.
getHeadCommit
(
)
.
then
(
function
(
commit
)
{
return
commit
.
getTree
(
)
;
}
)
.
then
(
function
(
tree
)
{
if
(
vsWorkdir
)
{
return
NodeGit
.
Diff
.
treeToWorkdir
(
test
.
repository
,
tree
)
;
}
else
{
return
NodeGit
.
Diff
.
treeToIndex
(
test
.
repository
,
tree
,
index
)
;
}
}
)
.
then
(
function
(
diff
)
{
return
diff
.
getDelta
(
0
)
;
}
)
.
then
(
function
(
delta
)
{
if
(
fileModeDifference
===
0
)
{
if
(
!
delta
)
{
return
true
;
}
else
{
throw
(
"File change when no file change expected."
)
;
}
}
else
{
assert
(
delta
.
newFile
(
)
.
mode
(
)
-
delta
.
oldFile
(
)
.
mode
(
)
===
fileModeDifference
)
;
}
return
true
;
}
)
;
}
function
createAndCommitFiles
(
repo
,
filePaths
,
fileContent
,
afterWriteFn
)
{
filePaths
=
filePaths
instanceof
Array
?
filePaths
:
[
filePaths
]
;
var
filePromises
=
filePaths
.
map
(
function
(
fileName
)
{
return
RepoUtils
.
commitFileToRepo
(
repo
,
fileName
,
fileContent
)
.
then
(
function
(
)
{
//First, create a file, have the same file in both the repo and workdir.
return
fse
.
writeFile
(
path
.
join
(
repo
.
workdir
(
)
,
fileName
)
,
fileContent
)
;
}
)
.
then
(
function
(
)
{
return
afterWriteFn
(
repo
,
fileName
)
;
}
)
;
}
)
;
return
Promise
.
all
(
filePromises
)
;
}
if
(
process
.
platform
==
"linux"
||
process
.
platform
==
"darwin"
)
{
it
(
"can stage filemode changes for one file"
,
function
(
)
{
var
fileContent
=
"Blek"
;
var
fileName
=
"stageFilemodeTest.txt"
;
var
index
;
function
afterWriteFn
(
repo
,
fileName
)
{
return
fse
.
chmod
(
path
.
join
(
repo
.
workdir
(
)
,
fileName
)
,
0755
/* new filemode */
)
;
}
return
createAndCommitFiles
(
test
.
repository
,
fileName
,
fileContent
,
afterWriteFn
)
//Then, diff between head commit and workdir should have filemode change
.
then
(
function
(
)
{
return
compareFilemodes
(
true
,
null
,
0111
/* expect +x */
)
.
then
(
function
(
)
{
return
test
.
repository
.
stageFilemode
(
fileName
,
true
)
;
}
)
;
}
)
//Now lets do a commit...
.
then
(
function
(
)
{
return
test
.
repository
.
refreshIndex
(
)
;
}
)
.
then
(
function
(
_index
)
{
index
=
_index
;
return
index
.
writeTree
(
)
;
}
)
.
then
(
function
(
oid
)
{
return
test
.
repository
.
getHeadCommit
(
)
.
then
(
function
(
parent
)
{
var
signature
=
NodeGit
.
Signature
.
create
(
"Foo bar"
,
"foo@bar.com"
,
123456789
,
60
)
;
return
test
.
repository
.
createCommit
(
"HEAD"
,
signature
,
signature
,
"initial commit"
,
oid
,
[
parent
]
)
;
}
)
;
//... alright, we did a commit.
}
)
// Now if we compare head commit to the workdir,
// there shouldn't be a filemode change
.
then
(
function
(
)
{
return
compareFilemodes
(
true
,
null
,
0
)
;
}
)
;
}
)
;
it
(
"can unstage filemode changes"
,
function
(
)
{
var
fileContent
=
"Blek"
;
var
fileName
=
"stageFilemodeTest2.txt"
;
var
index
;
function
afterWriteFn
(
repo
,
fileName
)
{
return
fse
.
chmod
(
path
.
join
(
repo
.
workdir
(
)
,
fileName
)
,
0755
/* new filemode */
)
;
}
return
createAndCommitFiles
(
test
.
repository
,
fileName
,
fileContent
,
afterWriteFn
)
//Then, diff between head commit and workdir should have filemode change
.
then
(
function
(
)
{
return
compareFilemodes
(
true
,
null
,
0111
/* expect +x */
)
;
}
)
.
then
(
function
(
)
{
return
test
.
repository
.
refreshIndex
(
)
;
}
)
.
then
(
function
(
repoIndex
)
{
//Now we stage the whole file...
index
=
repoIndex
;
return
index
.
addByPath
(
fileName
)
;
}
)
.
then
(
function
(
)
{
return
index
.
write
(
)
;
}
)
.
then
(
function
(
)
{
//We expect the Index to have the filemode changes now.
return
compareFilemodes
(
false
,
index
,
0111
/* expect +x */
)
.
then
(
function
(
)
{
//...then we attempt to unstage filemode
return
test
.
repository
.
stageFilemode
(
fileName
,
false
/* unstage */
)
;
}
)
;
}
)
.
then
(
function
(
)
{
return
test
.
repository
.
refreshIndex
(
)
;
}
)
//We expect the Index to have no filemode changes, since we unstaged.
.
then
(
function
(
freshIndex
)
{
return
compareFilemodes
(
false
,
freshIndex
,
0
/* expect +x */
)
;
}
)
//We also expect the workdir to now have the filemode change.
.
then
(
function
(
)
{
return
compareFilemodes
(
true
,
null
,
0111
/* expect +x */
)
;
}
)
;
}
)
;
}
else
if
(
process
.
platform
==
"win32"
)
{
it
(
"can stage/unstage filemode changes for one file"
,
function
(
)
{
var
fileContent
=
"Blek"
;
var
fileName
=
"stageFilemodeTest.txt"
;
var
index
;
function
afterWriteFn
(
repo
,
fileName
)
{
//change the permission on index
return
exec
(
"git update-index --chmod=+x "
+
fileName
,
{
cwd
:
repo
.
workdir
(
)
}
)
.
then
(
function
(
)
{
//Commit the change with execute bit set
return
exec
(
"git commit -m 'test'"
,
{
cwd
:
repo
.
workdir
(
)
}
)
;
}
)
.
then
(
function
(
)
{
//Then, change the permission on index
return
exec
(
"git update-index --chmod=-x "
+
fileName
,
{
cwd
:
repo
.
workdir
(
)
}
)
;
}
)
;
}
return
createAndCommitFiles
(
test
.
repository
,
fileName
,
fileContent
,
afterWriteFn
)
.
then
(
function
(
)
{
return
test
.
repository
.
refreshIndex
(
)
;
}
)
.
then
(
function
(
repoIndex
)
{
index
=
repoIndex
;
//Head commit vs index
//We expect the Index to have +x
return
compareFilemodes
(
false
,
index
,
-
0111
/* expect +x */
)
;
}
)
.
then
(
function
(
)
{
//...then we attempt to unstage filemode
return
test
.
repository
.
stageFilemode
(
fileName
,
false
/* unstage */
)
;
}
)
.
then
(
function
(
)
{
return
test
.
repository
.
refreshIndex
(
)
;
}
)
.
then
(
function
(
freshIndex
)
{
return
compareFilemodes
(
false
,
freshIndex
,
0
/* expect nochange */
)
;
}
)
;
}
)
;
}
it
(
"can stage/unstage filemode changes for multiple files"
,
function
(
)
{
var
fileContent
=
"Blek"
;
var
fileName
=
[
"stageFilemodeTest.txt"
,
"stageFilemodeTest2.txt"
]
;
var
index
;
var
repoWorkDir
=
test
.
repository
.
workdir
(
)
;
var
signature
=
NodeGit
.
Signature
.
create
(
"Foo bar"
,
"foo@bar.com"
,
123456789
,
60
)
;
return
Promise
.
all
(
fileName
.
map
(
function
(
file
)
{
return
fse
.
writeFile
(
path
.
join
(
repoWorkDir
,
file
)
,
fileContent
)
;
}
)
)
.
then
(
function
(
)
{
// Initial commit
return
test
.
repository
.
refreshIndex
(
)
;
}
)
.
then
(
function
(
index
)
{
return
fileName
.
reduce
(
function
(
lastPromise
,
file
)
{
return
lastPromise
.
then
(
function
(
)
{
return
index
.
addByPath
(
file
)
;
}
)
;
}
,
Promise
.
resolve
(
)
)
.
then
(
function
(
)
{
return
index
.
write
(
)
;
}
)
.
then
(
function
(
)
{
return
index
.
writeTree
(
)
;
}
)
;
}
)
.
then
(
function
(
oid
)
{
return
test
.
repository
.
createCommit
(
"HEAD"
,
signature
,
signature
,
"initial commit"
,
oid
,
[
]
)
;
}
)
.
then
(
function
(
commitOid
)
{
return
test
.
repository
.
getCommit
(
commitOid
)
;
}
)
.
then
(
function
(
)
{
//change the permission on index
return
exec
(
"git update-index --chmod=+x "
+
fileName
[
0
]
,
{
cwd
:
test
.
repository
.
workdir
(
)
}
)
;
}
)
.
then
(
function
(
)
{
//change the permission on index
return
exec
(
"git update-index --chmod=+x "
+
fileName
[
1
]
,
{
cwd
:
test
.
repository
.
workdir
(
)
}
)
;
}
)
.
then
(
function
(
)
{
//Commit the change with execute bit set
return
exec
(
"git commit -m 'test'"
,
{
cwd
:
test
.
repository
.
workdir
(
)
}
)
;
}
)
.
then
(
function
(
)
{
//Then, change the permission on index back to -x
return
exec
(
"git update-index --chmod=-x "
+
fileName
[
0
]
,
{
cwd
:
test
.
repository
.
workdir
(
)
}
)
;
}
)
.
then
(
function
(
)
{
//Then, change the permission on index back to -x
return
exec
(
"git update-index --chmod=-x "
+
fileName
[
1
]
,
{
cwd
:
test
.
repository
.
workdir
(
)
}
)
;
}
)
.
then
(
function
(
)
{
return
test
.
repository
.
refreshIndex
(
)
;
}
)
.
then
(
function
(
repoIndex
)
{
index
=
repoIndex
;
//Head commit vs index
//We expect the Index to have +x
return
compareFilemodes
(
false
,
index
,
-
0111
/* expect +x */
)
;
}
)
.
then
(
function
(
)
{
//...then we attempt to unstage filemode
return
test
.
repository
.
stageFilemode
(
fileName
,
false
/* unstage */
)
;
}
)
.
then
(
function
(
)
{
return
test
.
repository
.
refreshIndex
(
)
;
}
)
.
then
(
function
(
freshIndex
)
{
return
compareFilemodes
(
false
,
freshIndex
,
0
/* expect nochange */
)
;
}
)
;
}
)
;
it
(
"can discard selected lines"
,
function
(
)
{
return
stagingTest
(
true
,
null
,
true
)
;
}
)
;
}
)
;
Back
|
FazBrowse Home
|
New Git URL