This repository was archived by the owner on Jul 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCOBranch.m
More file actions
174 lines (145 loc) · 4.46 KB
/
COBranch.m
File metadata and controls
174 lines (145 loc) · 4.46 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
#import "COBranch.h"
#import <EtoileFoundation/Macros.h>
#import "COPersistentRoot.h"
#import "COPersistentRootPrivate.h"
#import "COItemTree.h"
#import "COSQLiteStore.h"
#import "COEditingContext.h"
@implementation COBranch
NSString *kCOBranchName = @"COBranchName";
- (id)initWithPersistentRoot: (COPersistentRoot*)aRoot
branch: (COUUID*)aBranch
trackCurrentBranch: (BOOL)track
{
SUPERINIT;
persistentRoot_ = aRoot;
ASSIGN(branch_, aBranch);
isTrackingCurrentBranch_ = track;
return self;
}
- (void)dealloc
{
[branch_ release];
[editingContext_ release];
[super dealloc];
}
- (COPersistentRoot *) persistentRoot
{
return persistentRoot_;
}
- (COUUID *) UUID
{
return branch_;
}
- (NSDictionary *) metadata
{
NSDictionary *result = [[[persistentRoot_ savedState] branchPlistForUUID: branch_] metadata];
if (result == nil)
{
return [NSDictionary dictionary];
}
return result;
}
- (void) setMetadata: (NSDictionary *)theMetadata
{
// FIXME: Implement
assert(0);
}
- (NSString *)name
{
return [[self metadata] objectForKey: kCOBranchName];
}
- (void) setName: (NSString *)aName
{
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary: [self metadata]];
[dict setObject: aName forKey: kCOBranchName];
[self setMetadata: dict];
}
- (CORevisionID *)currentRevisionID
{
return [[[persistentRoot_ savedState] branchPlistForUUID: branch_] currentRevisionID];
}
- (COItemTree *)currentStateObjectTree
{
return [[persistentRoot_ store] contentsForRevisionID: [self currentRevisionID]];
}
- (CORevisionID *)headRevisionID
{
return [[[persistentRoot_ savedState] branchPlistForUUID: branch_] headRevisionID];
}
- (CORevisionID *)tailRevisionID
{
return [[[persistentRoot_ savedState] branchPlistForUUID: branch_] tailRevisionID];
}
- (void) unfaultEditingContext
{
if (editingContext_ == nil)
{
editingContext_ = [[COEditingContext alloc] initWithItemTree: [self currentStateObjectTree]];
}
}
// commits immediately. discards any uncommitted edits.
// moves the current state pointer of the branch.
- (void) setCurrentRevisionID: (CORevisionID *)aState
{
[self unfaultEditingContext];
BOOL ok = [[persistentRoot_ store] setCurrentRevision: aState
forBranch: branch_
ofPersistentRoot: [persistentRoot_ UUID]
updateHead: NO]; // FIXME: YES or NO depending on aState
assert(ok);
[[[persistentRoot_ savedState] branchPlistForUUID: branch_] setCurrentRevisionID: aState];
// FIXME: Update head/tail
[editingContext_ setItemTree: [self currentStateObjectTree]];
}
/** @taskunit manipulation */
- (BOOL) commitChangesWithMetadata: (NSDictionary *)metadata
{
[self unfaultEditingContext];
CORevisionID *revId = [[persistentRoot_ store] writeContents: [editingContext_ itemTree]
withMetadata: metadata
parentRevisionID: [self currentRevisionID]
modifiedItems: [[editingContext_ insertedOrModifiedObjectUUIDs] allObjects]];
[editingContext_ clearChangeTracking];
BOOL ok = [[persistentRoot_ store] setCurrentRevision: revId
forBranch: branch_
ofPersistentRoot: [persistentRoot_ UUID]
updateHead: YES];
assert(ok);
[[[persistentRoot_ savedState] branchPlistForUUID: branch_] setCurrentRevisionID:revId];
// FIXME: Update head/tail
return YES;
}
- (void) discardChanges
{
if (editingContext_ != nil)
{
[editingContext_ setItemTree: [self currentStateObjectTree]];
}
}
- (BOOL) hasChanges
{
if (editingContext_ == nil)
{
return NO;
}
return [[editingContext_ insertedObjectUUIDs] count] > 0
|| [[editingContext_ modifiedObjectUUIDs] count] > 0
|| [[editingContext_ deletedObjectUUIDs] count] > 0;
}
- (COEditingContext *)editingContext
{
[self unfaultEditingContext];
return editingContext_;
}
/**
* the branch of the special "current branch" edit queue
* can change.
*/
- (void) setBranch: (COUUID *)aBranch
{
assert(isTrackingCurrentBranch_);
ASSIGN(branch_, aBranch);
[self discardChanges];
}
@end