FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
nodegit/lib/repository.js at master · dawnsgithub/nodegit · GitHub
dawnsgithub
nodegit
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
nodegit
/
lib
/
repository.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
1822 lines (1630 loc) · 48.9 KB
Breadcrumbs
nodegit
/
lib
/
repository.js
Copy path
File metadata and controls
1822 lines (1630 loc) · 48.9 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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var
promisify
=
require
(
"promisify-node"
)
;
var
fse
=
promisify
(
require
(
"fs-extra"
)
)
;
var
fp
=
require
(
"lodash/fp"
)
;
var
NodeGit
=
require
(
"../"
)
;
var
Blob
=
NodeGit
.
Blob
;
var
Checkout
=
NodeGit
.
Checkout
;
var
Commit
=
NodeGit
.
Commit
;
var
normalizeOptions
=
NodeGit
.
Utils
.
normalizeOptions
;
var
shallowClone
=
NodeGit
.
Utils
.
shallowClone
;
var
path
=
require
(
"path"
)
;
var
Reference
=
NodeGit
.
Reference
;
var
Remote
=
NodeGit
.
Remote
;
var
Repository
=
NodeGit
.
Repository
;
var
Revwalk
=
NodeGit
.
Revwalk
;
var
Status
=
NodeGit
.
Status
;
var
StatusFile
=
NodeGit
.
StatusFile
;
var
StatusList
=
NodeGit
.
StatusList
;
var
Submodule
=
NodeGit
.
Submodule
;
var
Tag
=
NodeGit
.
Tag
;
var
Tree
=
NodeGit
.
Tree
;
var
TreeBuilder
=
NodeGit
.
Treebuilder
;
var
_discover
=
Repository
.
discover
;
var
_initExt
=
Repository
.
initExt
;
var
_fetchheadForeach
=
Repository
.
prototype
.
fetchheadForeach
;
var
_mergeheadForeach
=
Repository
.
prototype
.
mergeheadForeach
;
function
applySelectedLinesToTarget
(
originalContent
,
newLines
,
pathHunks
,
isStaged
,
reverse
)
{
// 43: ascii code for '+'
// 45: ascii code for '-'
var
lineTypes
=
{
ADDED
:
!
reverse
?
43
:
45
,
DELETED
:
!
reverse
?
45
:
43
}
;
var
newContent
=
""
;
var
oldIndex
=
0
;
var
linesPromises
=
[
]
;
// split the original file into lines
var
oldLines
=
originalContent
.
toString
(
)
.
split
(
"\n"
)
;
// if no selected lines were sent, return the original content
if
(
!
newLines
||
newLines
.
length
===
0
)
{
return
originalContent
;
}
function
lineEqualsFirstNewLine
(
hunkLine
)
{
return
(
(
hunkLine
.
oldLineno
(
)
===
newLines
[
0
]
.
oldLineno
(
)
)
&&
(
hunkLine
.
newLineno
(
)
===
newLines
[
0
]
.
newLineno
(
)
)
)
;
}
function
processSelectedLine
(
hunkLine
)
{
// if this hunk line is a selected line find the selected line
var
newLine
=
newLines
.
filter
(
function
(
nLine
)
{
return
(
(
hunkLine
.
oldLineno
(
)
===
nLine
.
oldLineno
(
)
)
&&
(
hunkLine
.
newLineno
(
)
===
nLine
.
newLineno
(
)
)
)
;
}
)
;
if
(
hunkLine
.
content
(
)
.
indexOf
(
"\\ No newline at end of file"
)
!==
-
1
)
{
return
false
;
}
// determine what to add to the new content
if
(
(
isStaged
&&
newLine
&&
newLine
.
length
>
0
)
||
(
!
isStaged
&&
(
!
newLine
||
newLine
.
length
===
0
)
)
)
{
if
(
hunkLine
.
origin
(
)
!==
lineTypes
.
ADDED
)
{
newContent
+=
hunkLine
.
content
(
)
;
}
if
(
(
isStaged
&&
hunkLine
.
origin
(
)
!==
lineTypes
.
DELETED
)
||
(
!
isStaged
&&
hunkLine
.
origin
(
)
!==
lineTypes
.
ADDED
)
)
{
oldIndex
++
;
}
}
else
{
switch
(
hunkLine
.
origin
(
)
)
{
case
lineTypes
.
ADDED
:
newContent
+=
hunkLine
.
content
(
)
;
if
(
isStaged
)
{
oldIndex
++
;
}
break
;
case
lineTypes
.
DELETED
:
if
(
!
isStaged
)
{
oldIndex
++
;
}
break
;
default
:
newContent
+=
oldLines
[
oldIndex
++
]
;
if
(
oldIndex
<
oldLines
.
length
)
{
newContent
+=
"\n"
;
}
break
;
}
}
}
// find the affected hunk
pathHunks
.
forEach
(
function
(
pathHunk
)
{
linesPromises
.
push
(
pathHunk
.
lines
(
)
)
;
}
)
;
return
Promise
.
all
(
linesPromises
)
.
then
(
function
(
results
)
{
for
(
var
i
=
0
;
i
<
results
.
length
&&
newContent
.
length
<
1
;
i
++
)
{
var
hunkStart
=
isStaged
||
reverse
?
pathHunks
[
i
]
.
newStart
(
)
:
pathHunks
[
i
]
.
oldStart
(
)
;
var
lines
=
results
[
i
]
;
if
(
lines
.
filter
(
lineEqualsFirstNewLine
)
.
length
>
0
)
{
// add content that is before the hunk
while
(
hunkStart
>
(
oldIndex
+
1
)
)
{
newContent
+=
oldLines
[
oldIndex
++
]
+
"\n"
;
}
// modify the lines of the hunk according to the selection
lines
.
forEach
(
processSelectedLine
)
;
// add the rest of the file
while
(
oldLines
.
length
>
oldIndex
)
{
newContent
+=
oldLines
[
oldIndex
++
]
+
(
oldLines
.
length
>
oldIndex
?
"\n"
:
""
)
;
}
}
}
return
newContent
;
}
)
;
}
function
getPathHunks
(
repo
,
index
,
filePath
,
isStaged
,
additionalDiffOptions
)
{
var
diffOptions
=
additionalDiffOptions
?
{
flags
:
additionalDiffOptions
}
:
undefined
;
return
Promise
.
resolve
(
)
.
then
(
function
(
)
{
if
(
isStaged
)
{
return
repo
.
getHeadCommit
(
)
.
then
(
function
getTreeFromCommit
(
commit
)
{
return
commit
.
getTree
(
)
;
}
)
.
then
(
function
getDiffFromTree
(
tree
)
{
return
NodeGit
.
Diff
.
treeToIndex
(
repo
,
tree
,
index
,
diffOptions
)
;
}
)
;
}
return
NodeGit
.
Diff
.
indexToWorkdir
(
repo
,
index
,
{
flags
:
NodeGit
.
Diff
.
OPTION
.
SHOW_UNTRACKED_CONTENT
|
NodeGit
.
Diff
.
OPTION
.
RECURSE_UNTRACKED_DIRS
|
(
additionalDiffOptions
||
0
)
}
)
;
}
)
.
then
(
function
(
diff
)
{
return
NodeGit
.
Status
.
file
(
repo
,
filePath
)
.
then
(
function
(
status
)
{
if
(
!
(
status
&
NodeGit
.
Status
.
STATUS
.
WT_MODIFIED
)
&&
!
(
status
&
NodeGit
.
Status
.
STATUS
.
INDEX_MODIFIED
)
)
{
return
Promise
.
reject
(
"Selected staging is only available on modified files."
)
;
}
return
diff
.
patches
(
)
;
}
)
;
}
)
.
then
(
function
(
patches
)
{
var
pathPatch
=
patches
.
filter
(
function
(
patch
)
{
return
patch
.
newFile
(
)
.
path
(
)
===
filePath
;
}
)
;
if
(
pathPatch
.
length
!==
1
)
{
return
Promise
.
reject
(
"No differences found for this file."
)
;
}
return
pathPatch
[
0
]
.
hunks
(
)
;
}
)
;
}
/**
* Goes through a rebase's rebase operations and commits them if there are
* no merge conflicts
*
*
@param
{
Repository
} repository The repository that the rebase is being
* performed in
*
@param
{
Rebase
} rebase The current rebase being performed
*
@param
{
Signature
} signature Identity of the one performing the rebase
*
@param
{
Function
} beforeNextFn Callback to be called before each
* invocation of next(). If the callback
* returns a promise, the next() will be
* called when the promise resolves.
*
@param
{
Function
} beforeFinishFn Callback called before the invocation
* of finish(). If the callback returns a
* promise, finish() will be called when the
* promise resolves. This callback will be
* provided a detailed overview of the rebase
*
@return
{
Int|Index
} An error code for an unsuccesful rebase or an index for
* a rebase with conflicts
*/
function
performRebase
(
repository
,
rebase
,
signature
,
beforeNextFn
,
beforeFinishFn
)
{
var
beforeNextFnResult
;
function
readRebaseMetadataFile
(
fileName
)
{
return
fse
.
readFile
(
path
.
join
(
repository
.
path
(
)
,
"rebase-merge"
,
fileName
)
,
{
encoding
:
"utf8"
}
)
.
then
(
fp
.
trim
)
;
}
function
calcHeadName
(
input
)
{
return
input
.
replace
(
/
r
e
f
s
\/
h
e
a
d
s
\/
(
.
*
)
/
,
"$1"
)
;
}
function
getPromise
(
)
{
return
rebase
.
next
(
)
.
then
(
function
(
)
{
return
repository
.
refreshIndex
(
)
;
}
)
.
then
(
function
(
index
)
{
if
(
index
.
hasConflicts
(
)
)
{
throw
index
;
}
return
rebase
.
commit
(
null
,
signature
)
;
}
)
.
then
(
function
(
)
{
return
performRebase
(
repository
,
rebase
,
signature
,
beforeNextFn
,
beforeFinishFn
)
;
}
)
.
catch
(
function
(
error
)
{
if
(
error
&&
error
.
errno
===
NodeGit
.
Error
.
CODE
.
ITEROVER
)
{
const
calcRewritten
=
fp
.
flow
(
[
fp
.
split
(
"\n"
)
,
fp
.
map
(
fp
.
split
(
" "
)
)
]
)
;
const
beforeFinishFnPromise
=
!
beforeFinishFn
?
Promise
.
resolve
(
)
:
Promise
.
all
(
[
readRebaseMetadataFile
(
"onto_name"
)
,
readRebaseMetadataFile
(
"onto"
)
,
readRebaseMetadataFile
(
"head-name"
)
.
then
(
calcHeadName
)
,
readRebaseMetadataFile
(
"orig-head"
)
,
readRebaseMetadataFile
(
"rewritten"
)
.
then
(
calcRewritten
)
]
)
.
then
(
function
(
[
ontoName
,
ontoSha
,
originalHeadName
,
originalHeadSha
,
rewritten
]
)
{
return
beforeFinishFn
(
{
ontoName
,
ontoSha
,
originalHeadName
,
originalHeadSha
,
rebase
,
rewritten
}
)
;
}
)
;
return
beforeFinishFnPromise
.
then
(
function
(
)
{
return
rebase
.
finish
(
signature
)
;
}
)
;
}
else
{
throw
error
;
}
}
)
;
}
if
(
beforeNextFn
)
{
beforeNextFnResult
=
beforeNextFn
(
rebase
)
;
// if beforeNextFn returns a promise, chain the promise
return
Promise
.
resolve
(
beforeNextFnResult
)
.
then
(
getPromise
)
;
}
return
getPromise
(
)
;
}
/**
* Creates a branch with the passed in name pointing to the commit
*
*
@async
*
@param
{
String
} startPath The base path where the lookup starts.
*
@param
{
Number
} acrossFs If non-zero, then the lookup will not stop when a
filesystem device change is detected while exploring
parent directories.
*
@param
{
String
} ceilingDirs A list of absolute symbolic link free paths.
the search will stop if any of these paths
are hit. This may be set to null
*
@return
{
String
} Path of the git repository
*/
Repository
.
discover
=
function
(
startPath
,
acrossFs
,
ceilingDirs
,
callback
)
{
return
_discover
(
startPath
,
acrossFs
,
ceilingDirs
)
.
then
(
function
(
foundPath
)
{
foundPath
=
path
.
resolve
(
foundPath
)
;
if
(
typeof
callback
===
"function"
)
{
callback
(
null
,
foundPath
)
;
}
return
foundPath
;
}
,
callback
)
;
}
;
// Override Repository.initExt to normalize initoptions
Repository
.
initExt
=
function
(
repo_path
,
opts
)
{
opts
=
normalizeOptions
(
opts
,
NodeGit
.
RepositoryInitOptions
)
;
return
_initExt
(
repo_path
,
opts
)
;
}
;
Repository
.
getReferences
=
function
(
repo
,
type
,
refNamesOnly
,
callback
)
{
return
Reference
.
list
(
repo
)
.
then
(
function
(
refList
)
{
var
refFilterPromises
=
[
]
;
var
filteredRefs
=
[
]
;
refList
.
forEach
(
function
(
refName
)
{
refFilterPromises
.
push
(
Reference
.
lookup
(
repo
,
refName
)
.
then
(
function
(
ref
)
{
if
(
type
==
Reference
.
TYPE
.
LISTALL
||
ref
.
type
(
)
==
type
)
{
if
(
refNamesOnly
)
{
filteredRefs
.
push
(
refName
)
;
return
;
}
if
(
ref
.
isSymbolic
(
)
)
{
return
ref
.
resolve
(
)
.
then
(
function
(
resolvedRef
)
{
resolvedRef
.
repo
=
repo
;
filteredRefs
.
push
(
resolvedRef
)
;
}
)
.
catch
(
function
(
)
{
// If we can't resolve the ref then just ignore it.
}
)
;
}
else
{
filteredRefs
.
push
(
ref
)
;
}
}
}
)
)
;
}
)
;
return
Promise
.
all
(
refFilterPromises
)
.
then
(
function
(
)
{
if
(
typeof
callback
===
"function"
)
{
callback
(
null
,
filteredRefs
)
;
}
return
filteredRefs
;
}
,
callback
)
;
}
)
;
}
;
/**
* This will set the HEAD to point to the local branch and then attempt
* to update the index and working tree to match the content of the
* latest commit on that branch
*
*
@async
*
@param
{
String|Reference
} branch the branch to checkout
*
@param
{
Object|CheckoutOptions
} opts the options to use for the checkout
*/
Repository
.
prototype
.
checkoutBranch
=
function
(
branch
,
opts
)
{
var
repo
=
this
;
return
repo
.
getReference
(
branch
)
.
then
(
function
(
ref
)
{
if
(
!
ref
.
isBranch
(
)
)
{
return
false
;
}
return
repo
.
checkoutRef
(
ref
,
opts
)
;
}
)
;
}
;
/**
* This will set the HEAD to point to the reference and then attempt
* to update the index and working tree to match the content of the
* latest commit on that reference
*
*
@async
*
@param
{
Reference
} reference the reference to checkout
*
@param
{
Object|CheckoutOptions
} opts the options to use for the checkout
*/
Repository
.
prototype
.
checkoutRef
=
function
(
reference
,
opts
)
{
var
repo
=
this
;
opts
=
opts
||
{
}
;
opts
.
checkoutStrategy
=
opts
.
checkoutStrategy
||
(
NodeGit
.
Checkout
.
STRATEGY
.
SAFE
|
NodeGit
.
Checkout
.
STRATEGY
.
RECREATE_MISSING
)
;
return
repo
.
getReferenceCommit
(
reference
.
name
(
)
)
.
then
(
function
(
commit
)
{
return
commit
.
getTree
(
)
;
}
)
.
then
(
function
(
tree
)
{
return
Checkout
.
tree
(
repo
,
tree
,
opts
)
;
}
)
.
then
(
function
(
)
{
var
name
=
reference
.
name
(
)
;
return
repo
.
setHead
(
name
)
;
}
)
;
}
;
/**
* Continues an existing rebase
*
*
@async
*
@param
{
Signature
} signature Identity of the one performing the rebase
*
@param
{
Function
} beforeNextFn Callback to be called before each step
* of the rebase. If the callback returns a
* promise, the rebase will resume when the
* promise resolves. The rebase object is
* is passed to the callback.
*
@param
{
Function
} beforeFinishFn Callback called before the invocation
* of finish(). If the callback returns a
* promise, finish() will be called when the
* promise resolves. This callback will be
* provided a detailed overview of the rebase
*
@return
{
Oid|Index
} A commit id for a succesful merge or an index for a
* rebase with conflicts
*/
Repository
.
prototype
.
continueRebase
=
function
(
signature
,
beforeNextFn
,
beforeFinishFn
)
{
var
repo
=
this
;
signature
=
signature
||
repo
.
defaultSignature
(
)
;
var
rebase
;
return
repo
.
refreshIndex
(
)
.
then
(
function
(
index
)
{
if
(
index
.
hasConflicts
(
)
)
{
throw
index
;
}
return
NodeGit
.
Rebase
.
open
(
repo
)
;
}
)
.
then
(
function
(
_rebase
)
{
rebase
=
_rebase
;
return
rebase
.
commit
(
null
,
signature
)
.
catch
(
function
(
)
{
// Ignore all errors to prevent
// this routine from choking now
// that we made rebase.commit
// asynchronous
}
)
;
}
)
.
then
(
function
(
)
{
return
performRebase
(
repo
,
rebase
,
signature
,
beforeNextFn
,
beforeFinishFn
)
;
}
)
.
then
(
function
(
error
)
{
if
(
error
)
{
throw
error
;
}
return
repo
.
getBranchCommit
(
"HEAD"
)
;
}
)
;
}
;
/**
* Creates a branch with the passed in name pointing to the commit
*
*
@async
*
@param
{
String
} name Branch name, e.g. "master"
*
@param
{
Commit|String|Oid
} commit The commit the branch will point to
*
@param
{
bool
} force Overwrite branch if it exists
*
@return
{
Reference
}
*/
Repository
.
prototype
.
createBranch
=
function
(
name
,
commit
,
force
)
{
var
repo
=
this
;
if
(
commit
instanceof
Commit
)
{
return
NodeGit
.
Branch
.
create
(
repo
,
name
,
commit
,
force
?
1
:
0
)
;
}
else
{
return
repo
.
getCommit
(
commit
)
.
then
(
function
(
commit
)
{
return
NodeGit
.
Branch
.
create
(
repo
,
name
,
commit
,
force
?
1
:
0
)
;
}
)
;
}
}
;
/**
* Create a blob from a buffer
*
*
@param
{
Buffer
} buffer
*
@return
{
Oid
}
*/
Repository
.
prototype
.
createBlobFromBuffer
=
function
(
buffer
)
{
return
Blob
.
createFromBuffer
(
this
,
buffer
,
buffer
.
length
)
;
}
;
/**
* Create a commit
*
*
@async
*
@param
{
String
} updateRef
*
@param
{
Signature
} author
*
@param
{
Signature
} committer
*
@param
{
String
} message
*
@param
{
Tree|Oid|String
} Tree
*
@param
{
Array
} parents
*
@return
{
Oid
} The oid of the commit
*/
Repository
.
prototype
.
createCommit
=
function
(
updateRef
,
author
,
committer
,
message
,
tree
,
parents
,
callback
)
{
var
repo
=
this
;
var
promises
=
[
]
;
parents
=
parents
||
[
]
;
promises
.
push
(
repo
.
getTree
(
tree
)
)
;
parents
.
forEach
(
function
(
parent
)
{
promises
.
push
(
repo
.
getCommit
(
parent
)
)
;
}
)
;
return
Promise
.
all
(
promises
)
.
then
(
function
(
results
)
{
tree
=
results
[
0
]
;
// Get the normalized values for our input into the function
var
parentsLength
=
parents
.
length
;
parents
=
[
]
;
for
(
var
i
=
0
;
i
<
parentsLength
;
i
++
)
{
parents
.
push
(
results
[
i
+
1
]
)
;
}
return
Commit
.
create
(
repo
,
updateRef
,
author
,
committer
,
null
/* use default message encoding */
,
message
,
tree
,
parents
.
length
,
parents
)
;
}
)
.
then
(
function
(
commit
)
{
if
(
typeof
callback
===
"function"
)
{
callback
(
null
,
commit
)
;
}
return
commit
;
}
,
callback
)
;
}
;
/**
* Creates a new commit on HEAD from the list of passed in files
*
*
@async
*
@param
{
Array
} filesToAdd
*
@param
{
Signature
} author
*
@param
{
Signature
} committer
*
@param
{
String
} message
*
@return
{
Oid
} The oid of the new commit
*/
Repository
.
prototype
.
createCommitOnHead
=
function
(
filesToAdd
,
author
,
committer
,
message
,
callback
)
{
var
repo
=
this
;
return
repo
.
refreshIndex
(
)
.
then
(
function
(
index
)
{
if
(
!
filesToAdd
)
{
filesToAdd
=
[
]
;
}
return
filesToAdd
.
reduce
(
function
(
lastFilePromise
,
filePath
)
{
return
lastFilePromise
.
then
(
function
(
)
{
return
index
.
addByPath
(
filePath
)
;
}
)
;
}
,
Promise
.
resolve
(
)
)
.
then
(
function
(
)
{
return
index
.
write
(
)
;
}
)
.
then
(
function
(
)
{
return
index
.
writeTree
(
)
;
}
)
;
}
)
.
then
(
function
(
treeOid
)
{
return
repo
.
getHeadCommit
(
)
.
then
(
function
(
parent
)
{
if
(
parent
!==
null
)
{
// To handle a fresh repo with no commits
parent
=
[
parent
]
;
}
return
repo
.
createCommit
(
"HEAD"
,
author
,
committer
,
message
,
treeOid
,
parent
)
;
}
)
;
}
,
callback
)
;
}
;
/**
* Creates a new lightweight tag
*
*
@async
*
@param
{
String|Oid
} String sha or Oid
*
@param
{
String
} name the name of the tag
*
@return
{
Reference
}
*/
Repository
.
prototype
.
createLightweightTag
=
function
(
oid
,
name
,
callback
)
{
var
repository
=
this
;
return
Commit
.
lookup
(
repository
,
oid
)
.
then
(
function
(
commit
)
{
// Final argument is `force` which overwrites any previous tag
return
Tag
.
createLightweight
(
repository
,
name
,
commit
,
0
)
;
}
)
.
then
(
function
(
)
{
return
Reference
.
lookup
(
repository
,
"refs/tags/"
+
name
)
;
}
)
;
}
;
/**
* Instantiate a new revision walker for browsing the Repository"s history.
* See also `Commit.prototype.history()`
*
*
@param
{
String|Oid
} String sha or Oid
*
@return
{
RevWalk
}
*/
Repository
.
prototype
.
createRevWalk
=
function
(
)
{
return
Revwalk
.
create
(
this
)
;
}
;
/**
* Creates a new annotated tag
*
*
@async
*
@param
{
String|Oid
} String sha or Oid
*
@param
{
String
} name the name of the tag
*
@param
{
String
} message the description that will be attached to the
* annotated tag
*
@return
{
Tag
}
*/
Repository
.
prototype
.
createTag
=
function
(
oid
,
name
,
message
,
callback
)
{
var
repository
=
this
;
var
signature
=
repository
.
defaultSignature
(
)
;
return
Commit
.
lookup
(
repository
,
oid
)
.
then
(
function
(
commit
)
{
// Final argument is `force` which overwrites any previous tag
return
Tag
.
create
(
repository
,
name
,
commit
,
signature
,
message
,
0
)
;
}
)
.
then
(
function
(
tagOid
)
{
return
repository
.
getTag
(
tagOid
,
callback
)
;
}
)
;
}
;
/**
* Gets the default signature for the default user and now timestamp
*
@return
{
Signature
}
*/
Repository
.
prototype
.
defaultSignature
=
function
(
)
{
var
result
=
NodeGit
.
Signature
.
default
(
this
)
;
if
(
!
result
||
!
result
.
name
(
)
)
{
result
=
NodeGit
.
Signature
.
now
(
"unknown"
,
"unknown@example.com"
)
;
}
return
result
;
}
;
/**
* Deletes a tag from a repository by the tag name.
*
*
@async
*
@param
{
String
} Short or full tag name
*/
Repository
.
prototype
.
deleteTagByName
=
function
(
name
)
{
var
repository
=
this
;
name
=
~
name
.
indexOf
(
"refs/tags/"
)
?
name
.
substr
(
10
)
:
name
;
return
Tag
.
delete
(
repository
,
name
)
;
}
;
/**
* Discard line selection of a specified file.
* Assumes selected lines are unstaged.
*
*
@async
*
@param
{
String
} filePath The relative path of this file in the repo
*
@param
{
Array
} selectedLines The array of DiffLine objects
* selected for discarding
*
@return
{
Number
} 0 or an error code
*/
Repository
.
prototype
.
discardLines
=
function
(
filePath
,
selectedLines
,
additionalDiffOptions
)
{
var
repo
=
this
;
var
fullFilePath
=
path
.
join
(
repo
.
workdir
(
)
,
filePath
)
;
var
index
;
var
originalContent
;
return
repo
.
refreshIndex
(
)
.
then
(
function
(
indexResult
)
{
index
=
indexResult
;
return
fse
.
readFile
(
fullFilePath
,
"utf8"
)
;
}
)
.
then
(
function
(
content
)
{
originalContent
=
content
;
return
getPathHunks
(
repo
,
index
,
filePath
,
false
,
additionalDiffOptions
)
;
}
)
.
then
(
function
(
hunks
)
{
return
applySelectedLinesToTarget
(
originalContent
,
selectedLines
,
hunks
,
false
,
true
)
;
}
)
.
then
(
function
(
newContent
)
{
return
fse
.
writeFile
(
fullFilePath
,
newContent
)
;
}
)
;
}
;
/**
* Fetches from a remote
*
*
@async
*
@param
{
String|Remote
} remote
*
@param
{
Object|FetchOptions
} fetchOptions Options for the fetch, includes
* callbacks for fetching
*/
Repository
.
prototype
.
fetch
=
function
(
remote
,
fetchOptions
,
callback
)
{
var
repo
=
this
;
function
finallyFn
(
error
)
{
if
(
typeof
callback
===
"function"
)
{
callback
(
error
)
;
}
}
return
repo
.
getRemote
(
remote
)
.
then
(
function
(
remote
)
{
return
remote
.
fetch
(
null
,
fetchOptions
,
"Fetch from "
+
remote
)
.
then
(
function
(
)
{
return
remote
.
disconnect
(
)
;
}
)
;
}
)
.
then
(
finallyFn
)
.
catch
(
function
(
error
)
{
finallyFn
(
error
)
;
throw
error
;
}
)
;
}
;
/**
* Fetches from all remotes. This is done in series due to deadlocking issues
* with fetching from many remotes that can happen.
*
*
@async
*
@param
{
Object|FetchOptions
} fetchOptions Options for the fetch, includes
* callbacks for fetching
*
@param
{
Function
} callback
*/
Repository
.
prototype
.
fetchAll
=
function
(
fetchOptions
,
callback
)
{
var
repo
=
this
;
function
createCallbackWrapper
(
fn
,
remote
)
{
return
function
(
)
{
var
args
=
Array
.
prototype
.
slice
.
call
(
arguments
)
;
args
.
push
(
remote
)
;
return
fn
.
apply
(
this
,
args
)
;
}
.
bind
(
this
)
;
}
fetchOptions
=
fetchOptions
||
{
}
;
var
remoteCallbacks
=
fetchOptions
.
callbacks
||
{
}
;
var
credentials
=
remoteCallbacks
.
credentials
;
var
certificateCheck
=
remoteCallbacks
.
certificateCheck
;
var
transferProgress
=
remoteCallbacks
.
transferProgress
;
return
repo
.
getRemotes
(
)
.
then
(
function
(
remotes
)
{
return
remotes
.
reduce
(
function
(
fetchPromise
,
remote
)
{
var
wrappedFetchOptions
=
shallowClone
(
fetchOptions
)
;
var
wrappedRemoteCallbacks
=
shallowClone
(
remoteCallbacks
)
;
if
(
credentials
)
{
wrappedRemoteCallbacks
.
credentials
=
createCallbackWrapper
(
credentials
,
remote
)
;
}
if
(
certificateCheck
)
{
wrappedRemoteCallbacks
.
certificateCheck
=
createCallbackWrapper
(
certificateCheck
,
remote
)
;
}
if
(
transferProgress
)
{
wrappedRemoteCallbacks
.
transferProgress
=
createCallbackWrapper
(
transferProgress
,
remote
)
;
}
wrappedFetchOptions
.
callbacks
=
wrappedRemoteCallbacks
;
return
fetchPromise
.
then
(
function
(
)
{
return
repo
.
fetch
(
remote
,
wrappedFetchOptions
)
;
}
)
;
}
,
Promise
.
resolve
(
)
)
;
}
)
.
then
(
function
(
)
{
if
(
typeof
callback
===
"function"
)
{
callback
(
)
;
}
}
)
;
}
;
/**
*
@async
*
@param
{
FetchheadForeachCb
} callback The callback function to be called on
* each entry
*/
Repository
.
prototype
.
fetchheadForeach
=
function
(
callback
)
{
return
_fetchheadForeach
.
call
(
this
,
callback
,
null
)
;
}
;
/**
* Retrieve the blob represented by the oid.
*
*
@async
*
@param
{
String|Oid
} String sha or Oid
*
@return
{
Blob
}
*/
Repository
.
prototype
.
getBlob
=
function
(
oid
,
callback
)
{
var
repository
=
this
;
return
Blob
.
lookup
(
repository
,
oid
)
.
then
(
function
(
blob
)
{
blob
.
repo
=
repository
;
if
(
typeof
callback
===
"function"
)
{
callback
(
null
,
blob
)
;
}
return
blob
;
}
,
callback
)
;
}
;
/**
* Look up a branch. Alias for `getReference`
*
*
@async
*
@param
{
String|Reference
} name Ref name, e.g. "master", "refs/heads/master"
* or Branch Ref
*
@return
{
Reference
}
*/
Repository
.
prototype
.
getBranch
=
function
(
name
,
callback
)
{
return
this
.
getReference
(
name
,
callback
)
;
}
;
/**
* Look up a branch's most recent commit. Alias to `getReferenceCommit`
*
*
@async
*
@param
{
String|Reference
} name Ref name, e.g. "master", "refs/heads/master"
* or Branch Ref
*
@return
{
Commit
}
*/
Repository
.
prototype
.
getBranchCommit
=
function
(
name
,
callback
)
{
return
this
.
getReferenceCommit
(
name
,
callback
)
;
}
;
/**
* Retrieve the commit identified by oid.
*
*
@async
*
@param
{
String|Oid
} String sha or Oid
*
@return
{
Commit
}
*/
Repository
.
prototype
.
getCommit
=
function
(
oid
,
callback
)
{
var
repository
=
this
;
return
Commit
.
lookup
(
repository
,
oid
)
.
then
(
function
(
commit
)
{
commit
.
repo
=
repository
;
if
(
typeof
callback
===
"function"
)
{
callback
(
null
,
commit
)
;
}
return
commit
;
}
,
callback
)
;
}
;
/**
* Gets the branch that HEAD currently points to
* Is an alias to head()
*
*
@async
*
@return
{
Reference
}
*/
Repository
.
prototype
.
getCurrentBranch
=
function
(
)
{
return
this
.
head
(
)
;
}
;
/**
* Retrieve the commit that HEAD is currently pointing to
*
*
@async
*
@return
{
Commit
}
*/
Repository
.
prototype
.
getHeadCommit
=
function
(
callback
)
{
var
repo
=
this
;
return
Reference
.
nameToId
(
repo
,
"HEAD"
)
.
then
(
function
(
head
)
{
return
repo
.
getCommit
(
head
,
callback
)
;
}
)
.
catch
(
function
(
)
{
return
null
;
}
)
;
}
;
/**
* Retrieve the master branch commit.
*
*
@async
*
@return
{
Commit
}
*/
Repository
.
prototype
.
getMasterCommit
=
function
(
callback
)
{
return
this
.
getBranchCommit
(
"master"
,
callback
)
;
}
;
/**
* Lookup the reference with the given name.
*
*
@async
*
@param
{
String|Reference
} name Ref name, e.g. "master", "refs/heads/master"
* or Branch Ref
*
@return
{
Reference
}
*/
Repository
.
prototype
.
getReference
=
function
(
name
,
callback
)
{
var
repository
=
this
;
return
Reference
.
dwim
(
this
,
name
)
.
then
(
function
(
reference
)
{
if
(
reference
.
isSymbolic
(
)
)
{
return
reference
.
resolve
(
)
.
then
(
function
(
reference
)
{
reference
.
repo
=
repository
;
if
(
typeof
callback
===
"function"
)
{
callback
(
null
,
reference
)
;
}
return
reference
;
}
,
callback
)
;
}
else
{
reference
.
repo
=
repository
;
if
(
typeof
callback
===
"function"
)
{
callback
(
null
,
reference
)
;
}
return
reference
;
}
}
,
callback
)
;
}
;
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL