-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmc.js
1148 lines (1022 loc) · 33.8 KB
/
mc.js
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
// Copyright (c) 2008-2011 Tony Garnock-Jones <tonyg@lshift.net>
// Copyright (c) 2008-2009 LShift Ltd. <query@lshift.net>
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation files
// (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
// BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
var Mc = {
_debugMode: false
};
Mc.Util = (function ()
{
function random_hex_string(n) {
var digits = "0123456789abcdef";
var result = "";
for (var i = 0; i < n; i++) {
result = result + digits[Math.floor(Math.random() * 16)];
}
return result;
}
function random_uuid() {
if (Mc._debugMode) {
return random_hex_string(8);
} else {
return [random_hex_string(8),
random_hex_string(4),
"4" + random_hex_string(3),
((Math.floor(Math.random() * 256) & ~64) | 128).toString(16) +
random_hex_string(2),
random_hex_string(12)].join("-");
}
}
function dict_union(s1, s2) {
var result = {};
var k;
for (k in s2) { result[k] = s2[k]; }
for (k in s1) { result[k] = s1[k]; }
return result;
}
function dict_difference(s1, s2) {
var result = {};
var k;
for (k in s1) { result[k] = s1[k]; }
for (k in s2) { delete result[k]; }
return result;
}
function dict_to_set(d) {
var result = {};
for (var k in d) { result[k] = 1; }
return result;
}
function dict_to_set_list(d) {
var result = [];
for (var k in d) { result.push(k); }
return result;
}
function dict_isempty(d) {
for (var k in d) { return false; }
return true;
}
function deepCopy(obj) {
// Courtesy of
// http://keithdevens.com/weblog/archive/2007/Jun/07/javascript.clone
//
// Does not handle recursive structures.
if (obj === null || typeof(obj) != 'object') {
return obj;
}
var temp = obj.constructor();
for (var key in obj) {
temp[key] = deepCopy(obj[key]);
}
return temp;
}
function scalarsEqual(v0, v1) {
if (v0 === v1) return true;
if ((typeof v0 != 'string') &&
(typeof v1 != 'string') &&
(typeof v0.length === 'number' && !(v0.propertyIsEnumerable('length'))) &&
(typeof v1.length === 'number' && !(v1.propertyIsEnumerable('length'))) &&
(v0.length == v1.length))
{
for (var i = 0; i < v0.length; i++) {
if (!scalarsEqual(v0[i], v1[i])) {
return false;
}
}
return true;
}
return false;
}
function subclassResponsibility(methodName) {
throw {message: "Subclass responsibility",
methodName: methodName};
}
function blobIdType(blobId) {
if (!blobId) {
return blobId;
}
var colonPos = blobId.indexOf(":");
return (colonPos != -1) && blobId.substring(0, colonPos);
}
function blobIdKey(blobId) {
if (!blobId) {
return blobId;
}
var colonPos = blobId.indexOf(":");
return (colonPos != -1) && blobId.substring(colonPos + 1);
}
var hasSetTimeout;
try {
hasSetTimeout = window.setTimeout;
hasSetTimeout = !!hasSetTimeout;
} catch (e) {
hasSetTimeout = false;
}
var broadcast;
if (hasSetTimeout) {
broadcast = function (receivers, event) {
/* In a browser, or somewhere with an event loop. */
for (var i = 0; i < receivers.length; i++) {
var receiver = receivers[i];
/* Javascript binding dance to work around mutation of outer receiver binding. */
setTimeout((function (receiver) {
return function () {
try {
receiver(event);
} catch (e) {
alert("Exception in broadcast event handler: " +
JSON.stringify(e));
}
};
})(receiver), 0);
}
};
} else {
broadcast = function (receivers, event) {
/* Somewhere else. Hope that our event listeners are
* carefully enough written that we avoid deadlocks. */
for (var i = 0; i < receivers.length; i++) {
receivers[i](event);
}
};
}
return {
random_uuid: random_uuid,
dict_union: dict_union,
dict_difference: dict_difference,
dict_to_set: dict_to_set,
dict_to_set_list: dict_to_set_list,
dict_isempty: dict_isempty,
deepCopy: deepCopy,
scalarsEqual: scalarsEqual,
subclassResponsibility: subclassResponsibility,
blobIdType: blobIdType,
blobIdKey: blobIdKey,
broadcast: broadcast
};
})();
// A repository is a collection of hash-keyed blobs. When a blob is
// retrieved from the repository, it is revivified, and when it is
// inserted, it is pickled.
//
// Objects stored in a repository need to have a class, called an
// "objectType" below. Object types know how to:
//
// - emptyInstance :: () -> instance
// - pickle :: instance -> jsonobject
// - unpickle :: repository * blobid * jsonobject -> instance
// - diff :: v0:instance * v1:instance -> diff
// - patch :: instance * diff -> instance
// - merge :: v1:instance * v0:instance * v2:instance -> mergeresult
//
// In the Mc.ObjectTypes table below, if a (pseudo-)method is absent, a
// default implementation will be used.
//
// Diffs:
//
// - if no changes were made, null is returned (so null means empty diff)
//
// Merge results:
//
// - a totally clean merge results in the data structure itself in "ok":
// { objectType: "scalar"|"text"|"object", ok: Scalar-or-list-or-dict }
//
// - a merge with conflicts results in:
// { objectType: "scalar", conflict: {a: Scalar, o: Scalar, b: Scalar} }
// { objectType: "text", result: [{ok:}, {conflict:}, ...] }
// { objectType: "object", partial: dict, conflicts: {key: MergeResult} }
// etc.
//
// (Note that dicts here are the only things that can have nested merge
// results.)
//
// An index is a record that also maps "filename"s to inodeIds, and
// inodeIds to blobIds. A commit points to a value, some metadata, and
// a (possibly-empty) list of parent commits.
//
// A checkout is a non-version-controlled object that holds a commit
// and an index and which also provides read/write/merge/commit
// services and a cache of unpickled objects.
// TODO: Add a "children" operation for tracing GC of the repository.
Mc.SimpleObjectType = function (emptyInstanceValue, typeTable) {
this.emptyInstanceValue = emptyInstanceValue;
this.typeTable = typeTable;
};
Mc.SimpleObjectType.prototype.emptyInstance = function () {
return this.emptyInstanceValue;
};
Mc.SimpleObjectType.prototype.typeTableFun = function (key) {
return this.typeTable[key];
};
Mc.SimpleObjectType.prototype.diff = function (v0, v1) {
var removed = Mc.Util.dict_difference(v0, v1);
var added = Mc.Util.dict_difference(v1, v0);
var changed = {};
var common = Mc.Util.dict_difference(v1, added);
for (var prop in common) {
var propType = this.typeTableFun(prop) || Mc.ObjectTypes.simpleScalar;
var p = propType.diff(v0[prop], v1[prop]);
if (p !== null) {
changed[prop] = p;
}
}
var result = {};
if (!Mc.Util.dict_isempty(removed)) result.removed = Mc.Util.dict_to_set_list(removed);
if (!Mc.Util.dict_isempty(added)) result.added = added;
if (!Mc.Util.dict_isempty(changed)) result.changed = changed;
if (Mc.Util.dict_isempty(result)) return null;
return result;
};
Mc.SimpleObjectType.prototype.patch = function (v0, p) {
var result = Mc.Util.deepCopy(v0);
if (p === null) return result;
var k;
if (p.removed) {
for (var i = 0; i < p.removed.length; i++) {
delete result[p.removed[i]];
}
}
if (p.added) { for (k in p.added) { result[k] = p.added[k]; } }
if (p.changed) {
for (k in p.changed) {
var propType = this.typeTableFun(k) || Mc.ObjectTypes.simpleScalar;
result[k] = propType.patch(result[k], p.changed[k]);
}
}
return result;
};
Mc.SimpleObjectType.prototype.merge = function (v1, v0, v2) {
var props = Mc.Util.dict_union(v1, v2);
var bResult = {};
var failures = {};
var haveConflicts = false;
for (var prop in props) {
var propType = (this.typeTableFun(prop) || Mc.ObjectTypes.simpleScalar);
var mergedPropValue = propType.merge(Mc.validInstance(propType, v1[prop]),
Mc.validInstance(propType, v0[prop]),
Mc.validInstance(propType, v2[prop]));
if ("ok" in mergedPropValue) {
bResult[prop] = mergedPropValue.ok;
} else {
failures[prop] = mergedPropValue;
haveConflicts = true;
}
}
if (haveConflicts) {
return {objectType: "object", partial: bResult, conflicts: failures};
} else {
return {objectType: "object", ok: bResult};
}
};
Mc.ObjectTypes = {
Default: {
emptyInstance: function () { Mc.Util.subclassResponsibility("emptyInstance"); },
pickle: function (instance) { return instance; },
unpickle: function (repo, blobId, jsonobject) { return jsonobject; },
diff: function (v0, v1) { Mc.Util.subclassResponsibility("diff"); },
patch: function (v0, p) { Mc.Util.subclassResponsibility("patch"); },
merge: function (v1, v0, v2) { Mc.Util.subclassResponsibility("merge"); }
},
simpleScalar: {
emptyInstance: function () { return undefined; },
diff: function (v0, v1) {
if (Mc.Util.scalarsEqual(v0, v1)) return null;
return {replacement: v1};
},
patch: function (v0, p) {
if (p === null) return v0;
return p.replacement;
},
merge: function (v1, v0, v2) {
if (v1 == v2) return {objectType: "scalar", ok: v1};
if (v1 == v0) return {objectType: "scalar", ok: v2};
if (v2 == v0) return {objectType: "scalar", ok: v1};
return {objectType: "scalar", conflict: {a: v1, o: v0, b: v2}};
}
},
simpleText: {
emptyInstance: function () { return []; },
diff: function (v0, v1) {
var p = Diff.strip_patch(Diff.diff_patch(v0, v1));
return (p.length === 0) ? null : p;
},
patch: function (v0, p) {
if (p === null) return v0;
return Diff.patch(v0, p);
},
merge: function (v1, v0, v2) {
var mergeResult = Diff.diff3_merge(v1, v0, v2, true);
if (mergeResult.length == 1 && ("ok" in mergeResult[0])) {
return {objectType: "text", ok: mergeResult[0].ok};
} else {
return {objectType: "text", result: mergeResult};
}
}
},
paragraphString: {
emptyInstance: function () { return ""; },
diff: function (v0, v1) {
return Mc.ObjectTypes.simpleText.diff(v0.split('\n'), v1.split('\n'));
},
patch: function (v0, p) {
if (p === null) return v0;
return Mc.ObjectTypes.simpleText.patch(v0.split('\n'), p).join('\n');
},
merge: function (v1, v0, v2) {
return Mc.ObjectTypes.simpleText.merge(v1.split('\n'), v0.split('\n'), v2.split('\n'));
}
},
rawObject: new Mc.SimpleObjectType({}, {}),
index: (function () {
var t = new Mc.SimpleObjectType({inodes: {}, names: {}},
{
inodes: new Mc.SimpleObjectType({}, {}),
names: new Mc.SimpleObjectType({}, {})
});
t.merge = function (v1, v0, v2) {
// We cannot merge indexes directly for the same reasons
// we can't merge commits.
throw {message: "Cannot merge indexes", v1: v1, v0: v0, v2: v2};
};
return t;
})(),
commit: {
emptyInstance: function () { return {value: null, parents: [], metadata: {}}; },
diff: function (v0, v1) {
throw {message: "Cannot diff commits", v0: v0, v1: v1};
},
merge: function (v1, v0, v2) {
// We cannot merge commits directly, because it involves
// recursive merges which create new blob IDs, and we
// don't know our repository. Instead, we construct a
// merged commit explicitly, outside of the normal merging
// code.
throw {message: "Cannot merge commits", v1: v1, v0: v0, v2: v2};
}
}
};
Mc.TypeDirectory = {
"scalar": Mc.ObjectTypes.simpleScalar,
"text": Mc.ObjectTypes.simpleText,
"textFile": new Mc.SimpleObjectType({bodyText: ""},
{bodyText: Mc.ObjectTypes.paragraphString}),
"object": Mc.ObjectTypes.rawObject,
"index": Mc.ObjectTypes.index,
"commit": Mc.ObjectTypes.commit
};
Mc.lookupType = function (typeName) {
var t = Mc.TypeDirectory[typeName];
if (!t) {
throw {message: "ObjectType not found",
typeName: typeName};
}
return t;
};
Mc.typeMethod = function (t, methodName) {
if (!t) { t = Mc.ObjectTypes.Default; }
var method = t[methodName];
if (method) {
return function () { return method.apply(t, arguments); };
} else {
return Mc.ObjectTypes.Default[methodName];
}
};
Mc.validInstance = function (t, maybeInstance) {
if (typeof(maybeInstance) == "undefined") {
return Mc.typeMethod(t, "emptyInstance")();
}
return maybeInstance;
};
Mc.Repository = function () {
this.repoId = Mc.Util.random_uuid();
this.blobs = {}; // blobId -> pickledInstanceRecord
this.tags = {}; // repoid/bookmarkname -> {blobId: blobId, isBranch: boolean}
this.remotes = {}; // remotename -> {repoId: remote_repoId}
this.accidentalCleanMerge = true; // set to false to disable
this.changeListeners = {
import: []
};
this.emptyCaches();
var checkout = new Mc.Checkout(this, null);
checkout.anyDirty = true; // cheeky
checkout.activeBranch = "master"; // *very* cheeky
checkout.commit();
};
Mc.Repository.prototype.emptyCaches = function () {
this.cache = {}; // blobId -> unpickledInstance
};
Mc.Repository.prototype.expandTag = function (tagOrBranch) {
if (!tagOrBranch) {
tagOrBranch = "master";
}
var slashPos = tagOrBranch.indexOf("/");
var repoName = null;
var repoId;
var bookmarkName;
if (slashPos == -1) {
repoId = this.repoId;
bookmarkName = tagOrBranch;
} else {
repoName = tagOrBranch.substring(0, slashPos);
var repoInfo = this.remotes[repoName];
if (repoInfo) {
repoId = repoInfo.repoId;
} else {
repoId = repoName; // deals with a given literal repoId
}
bookmarkName = tagOrBranch.substring(slashPos + 1);
}
var finalTag = repoId + "/" + bookmarkName;
return {tag: finalTag,
repoName: repoName,
isRemote: (repoName != null),
repoId: repoId,
bookmarkName: bookmarkName};
};
Mc.Repository.prototype.lookupTag = function (tagOrBranch) {
var expanded = this.expandTag(tagOrBranch);
var tagInfo = this.tags[expanded.tag];
if (tagInfo) {
expanded.blobId = tagInfo.blobId;
expanded.isBranch = tagInfo.isBranch;
return expanded;
} else {
return null;
}
};
Mc.Repository.prototype.prettyTag = function (fullTag) {
var pieces = fullTag.split("/");
if (pieces[0] == this.repoId) {
return pieces[1];
} else {
for (var repoName in this.remotes) {
if (pieces[0] == this.remotes[repoName].repoId) {
return repoName + "/" + pieces[1];
}
}
return fullTag;
}
};
Mc.Repository.prototype.resolve = function (blobIdOrTag) {
if (Mc.Util.blobIdKey(blobIdOrTag) in this.blobs) {
return blobIdOrTag;
} else {
var tagInfo = this.lookupTag(blobIdOrTag);
return tagInfo ? tagInfo.blobId : null;
}
};
Mc.Repository.prototype.maybeResolve = function (blobIdOrTag, shouldResolve) {
// shouldResolve is an optional parameter defaulting to true,
// hence the odd test in the line below
var resolved = (shouldResolve !== false) ? this.resolve(blobIdOrTag) : blobIdOrTag;
if (!resolved) return null;
return resolved;
};
Mc.Repository.prototype.store = function (instance, // a picklable object
objectType, // a key into Mc.TypeDirectory
baseId)
{
var t = Mc.lookupType(objectType);
var jsonInstance = Mc.typeMethod(t, "pickle")(instance);
var jsonText = JSON.stringify(jsonInstance);
var blobId = SHA1.hex_sha1(SHA1.encode_utf8(jsonText));
if (Mc._debugMode) { blobId = "blob-" + blobId.substring(0, 8); }
if (!(blobId in this.blobs)) {
var entry;
if (baseId) {
var differ = Mc.typeMethod(t, "diff");
var diffJson = differ(Mc.validInstance(t, this.lookupUnsafe(baseId)), instance);
if (diffJson === null) {
// No changes to the data? Then claim we're identical to
// our base object.
return objectType + ":" + Mc.Util.blobIdKey(baseId);
}
entry = {baseId: baseId, diff: JSON.stringify(diffJson)};
} else {
entry = {full: jsonText};
}
this.blobs[blobId] = entry;
}
return objectType + ":" + blobId;
};
Mc.Repository.prototype.lookup = function (blobId, shouldResolve) {
return Mc.Util.deepCopy(this.lookupUnsafe(blobId, shouldResolve));
};
Mc.Repository.prototype.lookupUnsafe = function (blobId, shouldResolve) {
var resolved = this.maybeResolve(blobId, shouldResolve);
if (!(resolved in this.cache)) {
var k = Mc.Util.blobIdKey(resolved);
if (!(k in this.blobs)) {
return null;
}
var entry = this.blobs[k];
var t = Mc.lookupType(Mc.Util.blobIdType(resolved));
if (entry.diff) {
// We don't use unpickle(patch(base, diff)) here because
// the base object is the result of lookupUnsafe and
// therefore is already unpickled. The contract of patch
// is to adjust an already-unpickled instance to take into
// account the given patch data.
var patcher = Mc.typeMethod(t, "patch");
this.cache[resolved] =
patcher(Mc.validInstance(t, this.lookupUnsafe(entry.baseId)),
JSON.parse(entry.diff));
} else {
var unpickler = Mc.typeMethod(t, "unpickle");
this.cache[resolved] = unpickler(this, resolved, JSON.parse(entry.full));
}
}
return this.cache[resolved];
};
Mc.Repository.prototype.merge = function (b1, b0, b2) {
b1 = this.resolve(b1);
b2 = this.resolve(b2);
var objectType = Mc.Util.blobIdType(b1);
var ancestorObjectType = Mc.Util.blobIdType(b0);
if ((objectType != Mc.Util.blobIdType(b2)) || ((ancestorObjectType !== null) &&
(objectType != Mc.Util.blobIdType(b0))))
{
throw {message: "Object type mismatch",
blobIds: [b1, b0, b2]};
}
var t = Mc.lookupType(objectType);
if (!t) {
throw {message: "Invalid object type",
objectType: objectType};
}
var inst1 = this.lookupUnsafe(b1);
var inst2 = this.lookupUnsafe(b2);
var inst0;
if (b0) {
inst0 = this.lookupUnsafe(b0, false); // note: not resolved further!
} else {
inst0 = undefined;
}
if (this.accidentalCleanMerge && (b1 == b2)) {
return {objectType: objectType, ok: inst1, mergeBlobId: b1};
}
if (b2 == b0) {
return {objectType: objectType, ok: inst1, mergeBlobId: b1};
}
if (b1 == b0) {
return {objectType: objectType, ok: inst2, mergeBlobId: b2};
}
var result = Mc.typeMethod(t, "merge")(inst1, Mc.validInstance(t, inst0), inst2);
return result;
};
Mc.Repository.prototype.tag = function (blobId, tagName, isBranch) {
this.tags[this.repoId + "/" + tagName] = {blobId: blobId, isBranch: isBranch || false};
};
Mc.Repository.prototype.allBranches = function () {
var result = {};
for (var tag in this.tags) {
if (this.tags[tag].isBranch) {
result[tag] = this.tags[tag].blobId;
}
}
return result;
};
Mc.Repository.prototype.exportRevisions = function () {
return {repoId: this.repoId,
blobs: this.blobs,
tags: this.tags,
remotes: this.remotes};
};
Mc.Repository.prototype.addRemote = function (repoName, repoId, removeOldTags) {
if (this.remotes[repoName] && removeOldTags) {
var tagsToDelete = [];
var tagPrefix = this.remotes[repoName].repoId + "/";
for (var tag in this.tags) {
if (tag.substring(0, tagPrefix.length) === tagPrefix) {
tagsToDelete.push(tag);
}
}
for (var i = 0; i < tagsToDelete.length; i++) {
delete this.tags[tagsToDelete[i]];
}
}
this.remotes[repoName] = {repoId: repoId};
};
Mc.Repository.prototype.importRevisions = function (exportedData) {
var stats = {
newBlobs: 0,
oldBlobs: 0,
newTags: 0
};
for (var blobId in exportedData.blobs) {
if (!(blobId in this.blobs)) {
this.blobs[blobId] = exportedData.blobs[blobId];
stats.newBlobs++;
} else {
stats.oldBlobs++;
}
}
for (var tag in exportedData.tags) {
// TODO: what if the exportedData has the same repoId as us?
// Perhaps an earlier version of ourselves. We should probably
// not simply stomp on our carefully-curated tags in that
// case!
if (tag.substring(0, exportedData.repoId.length) == exportedData.repoId) {
this.tags[tag] = exportedData.tags[tag];
stats.newTags++;
}
}
Mc.Util.broadcast(this.changeListeners.import,
{repo: this, importedFrom: exportedData.repoId, stats: stats});
return stats;
};
Mc.Checkout = function (repo, blobIdOrTag) {
this.repo = repo;
this.changeListeners = {
dirty: [],
inode: [],
name: [],
commit: []
};
this.reflog = [];; // list of pairs [blobId, explanationObject], oldest entry first
this.reflogLimit = 100; // once reflog grows longer than this, oldest entries are pruned
var tagInfo = repo.lookupTag(blobIdOrTag);
this.activeBranch = (tagInfo && !tagInfo.isRemote) ? tagInfo.bookmarkName : null;
this.forceCheckout(blobIdOrTag);
};
Mc.Checkout.prototype.reflogInsert = function (blobId, explanationObject) {
if (this.reflog.length >= this.reflogLimit) {
this.reflog.shift();
}
this.reflog.push([blobId, explanationObject]);
};
Mc.Checkout.prototype.forceCheckout = function (blobIdOrTag) {
var resolved = this.repo.resolve(blobIdOrTag);
this.reflogInsert(this.directParent, {type: "forceCheckout",
blobIdOrTag: blobIdOrTag,
newBlobId: resolved});
var commit = this.repo.lookup(resolved, false);
if (commit) {
var index = this.repo.lookup(commit.value);
if (!index) { throw {message: "Checkout's parent's index not found", commitId: resolved}; }
this.inodes = index.inodes;
this.names = index.names;
this.directParentIndexId = commit.value;
this.directParent = resolved;
} else {
this.inodes = {};
this.names = {};
this.directParentIndexId = undefined;
this.directParent = undefined;
}
this.resetTemporaryState();
Mc.Util.broadcast(this.changeListeners.commit,
{checkout: this, newCommit: false, commit: this.directParent});
};
Mc.Checkout.prototype.resetTemporaryState = function () {
this.unmodifiedInodes = Mc.Util.deepCopy(this.inodes);
this.newInstances = []; // list of {instance:, objectType:, baseId:}
this.dirtyInodes = {}; // inodeId -> ({instanceIndex:instanceIndex} | {blobId:blobId})
this.conflicts = null;
this.setDirty(false);
this.additionalParent = undefined;
};
Mc.Checkout.prototype.setDirty = function (newValue) {
this.anyDirty = newValue;
Mc.Util.broadcast(this.changeListeners.dirty, {dirty: newValue});
};
Mc.Checkout.prototype.ensureClean = function (what) {
if (this.anyDirty) {
throw {message: ("Cannot "+what+" dirty checkout")};
}
};
Mc.Checkout.prototype.tag = function (tagName, force, isBranch) {
var existing = this.repo.lookupTag(tagName);
if (existing && !force) {
return false;
} else {
if (!this.directParent) {
throw {message: "Cannot tag checkout with no parent commit"};
}
if (existing) {
this.reflogInsert(existing.blobId, {type: "tagMoved",
tagInfo: existing,
newBlobId: this.directParent});
}
this.repo.tag(this.directParent, tagName, isBranch || false);
if (isBranch) {
this.activeBranch = tagName;
}
return true;
}
};
Mc.Checkout.prototype.lookupFile = function (fileName, createIfAbsent) {
if (fileName in this.names) {
return this.names[fileName];
} else {
createIfAbsent = createIfAbsent || false;
if (createIfAbsent) {
var newInodeId = Mc.Util.random_uuid();
if (Mc._debugMode) { newInodeId = "inode-" + newInodeId; }
this.names[fileName] = newInodeId;
this.setDirty(true);
return newInodeId;
} else {
throw {message: "File not found", fileName: fileName};
}
}
};
Mc.Checkout.prototype.resolveInode = function (inodeId) {
if (inodeId in this.dirtyInodes) {
return this.dirtyInodes[inodeId];
}
if (inodeId in this.inodes) {
return {blobId: this.inodes[inodeId]};
}
throw {message: "Internal error: missing inode", inodeId: inodeId};
};
Mc.Checkout.prototype.writeFile = function (fileName, instance, objectType) {
objectType = objectType || "object";
var inodeId = this.lookupFile(fileName, true);
this.writeInode(inodeId, instance, objectType, this.unmodifiedInodes[inodeId]);
Mc.Util.broadcast(this.changeListeners.name,
{checkout: this, name: fileName, kind: 'write'});
};
Mc.Checkout.prototype.writeInode = function (inodeId,
instance,
objectType,
baseId)
{
this.newInstances.push({instance: Mc.Util.deepCopy(instance),
objectType: objectType,
baseId: baseId});
this.dirtyInodes[inodeId] = {instanceIndex: (this.newInstances.length - 1)};
this.setDirty(true);
Mc.Util.broadcast(this.changeListeners.inode, {checkout: this, inode: inodeId});
};
Mc.Checkout.prototype.copyFile = function (sourceName, targetName) {
var inodeId = this.lookupFile(sourceName);
var instanceLocation = this.resolveInode(inodeId);
var newInodeId = this.lookupFile(targetName, true);
this.dirtyInodes[newInodeId] = instanceLocation;
this.setDirty(true);
Mc.Util.broadcast(this.changeListeners.inode, {checkout: this, inode: newInodeId});
Mc.Util.broadcast(this.changeListeners.name,
{checkout: this, name: targetName, kind: 'write'});
};
Mc.Checkout.prototype.renameFile = function (sourceName, targetName) {
var inodeId = this.lookupFile(sourceName);
this.names[targetName] = inodeId;
delete this.names[sourceName];
this.setDirty(true);
Mc.Util.broadcast(this.changeListeners.name,
{checkout: this, name: sourceName, kind: 'delete'});
Mc.Util.broadcast(this.changeListeners.name,
{checkout: this, name: targetName, kind: 'write'});
};
Mc.Checkout.prototype.getInstance = function (blobId) {
var instance = this.repo.lookup(blobId, false);
if (!instance) {
throw {message: "Missing blob", blobId: blobId};
}
return instance;
};
Mc.Checkout.prototype.readFile = function (fileName) {
var inodeId = this.lookupFile(fileName);
var instanceLocation = this.resolveInode(inodeId);
var result;
if (instanceLocation.blobId) {
result = {instance: this.getInstance(instanceLocation.blobId),
objectType: Mc.Util.blobIdType(instanceLocation.blobId)};
} else {
result = this.newInstances[instanceLocation.instanceIndex];
}
return Mc.Util.deepCopy(result);
};
Mc.Checkout.prototype.deleteFile = function (fileName) {
if (fileName in this.names) {
delete this.names[fileName];
this.setDirty(true);
Mc.Util.broadcast(this.changeListeners.name,
{checkout: this, name: fileName, kind: 'delete'});
return true;
} else {
return false;
}
};
Mc.Checkout.prototype.fileExists = function (fileName) {
return (fileName in this.names);
};
Mc.Checkout.prototype.forEachFile = function (f) {
for (var name in this.names) {
var inodeId = this.names[name];
f(name, inodeId, inodeId in this.dirtyInodes);
}
};
Mc.Checkout.prototype.forEachFileOfType = function (typeNameOrFilter, f) {
var typeFilter;
if (typeof(typeNameOrFilter) === "string") {
typeFilter = function (typeName) { return typeName === typeNameOrFilter; };
} else {
typeFilter = typeNameOrFilter;
}
for (var name in this.names) {
var inodeId = this.names[name];
var instanceLocation = this.resolveInode(inodeId);
var type;
if (instanceLocation.blobId) {
type = Mc.Util.blobIdType(instanceLocation.blobId);
} else {
type = this.newInstances[instanceLocation.instanceIndex].objectType;
}
if (typeFilter(type)) {
f(name, inodeId, inodeId in this.dirtyInodes);
}
}
};
Mc.Checkout.prototype.isDirty = function () {
return this.anyDirty;
};
Mc.Checkout.prototype.leastCommonAncestor = function (otherCommitId) {
var repo = this.repo;
function lookupParents(blobId) { return repo.lookup(blobId).parents; }
return Graph.least_common_ancestor(lookupParents, this.directParent, otherCommitId);
};
Mc.Checkout.prototype.canMerge = function (otherCommitId) {
var ancestorBlobId = this.leastCommonAncestor(otherCommitId);
return !(this.directParent == ancestorBlobId || otherCommitId == ancestorBlobId);
};
Mc.Checkout.prototype.merge = function (otherBlobIdOrTag) {
this.ensureClean("merge into");
var $elf = this;
var repo = this.repo;
var b1 = this.directParent;
var b2 = repo.resolve(otherBlobIdOrTag);
if (!b2) {
throw {message: "Could not resolve revision name", blobIdOrTag: otherBlobIdOrTag};
}
var b0 = this.leastCommonAncestor(b2);
if (b0 == b1) {
// Fast-forward to b2.
this.forceCheckout(b2);
return false;
}
if (b0 == b2) {
// Fast-forward to b1, but we're already *at* b1.
return false;
}
var commit1 = repo.lookupUnsafe(b1, false);
var commit2 = repo.lookupUnsafe(b2, false);
var commit0 = b0 ? repo.lookupUnsafe(b0, false) : Mc.ObjectTypes.commit.emptyInstance();
var index1 = repo.lookupUnsafe(this.directParentIndexId, false);
var index2 = repo.lookupUnsafe(commit2.value, false);
var index0 = repo.lookupUnsafe(commit0.value, false);
if (!index1) { throw {message: "Parent's index not found", commitId: b1}; }
if (!index2) { throw {message: "Other branch's index not found", commitId: b2}; }
if (!index0) { throw {message: "Ancestor's index not found", commitId: b0}; }
function inodeTableEntryMerger(v1, v0, v2) {
var mr;
mr = Mc.ObjectTypes.simpleScalar.merge(v1, v0, v2);
if (mr.conflict) {
if (typeof(v1) == "undefined" || typeof(v2) == "undefined") {
// DieDieDieMerge for deleted entries.
return {objectType: "inodeTable", ok: {deleted: true}};