-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathCOSQLiteStore+Graphviz.m
More file actions
190 lines (152 loc) · 7.1 KB
/
COSQLiteStore+Graphviz.m
File metadata and controls
190 lines (152 loc) · 7.1 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
/*
Copyright (C) 2013 Eric Wasylishen
Date: November 2013
License: MIT (see COPYING)
*/
#import "COSQLiteStore+Graphviz.h"
#import "COSQLiteStore+Private.h"
#import "COSQLiteStorePersistentRootBackingStore.h"
#import "CORevisionInfo.h"
#import "COPersistentRootInfo.h"
#import "COBranchInfo.h"
#import "COJSONSerialization.h"
#import <AppKit/AppKit.h>
@implementation COSQLiteStore (Debugging)
- (NSString *)dotNameForRevisionUUID: (ETUUID *)aUUID
{
NSString *str = [aUUID stringValue];
str = [str stringByReplacingOccurrencesOfString: @"-" withString: @"_"];
return [@"rev_" stringByAppendingString: str];
}
- (NSString *)dotNameForBranchUUID: (ETUUID *)aUUID
{
NSString *str = [aUUID stringValue];
str = [str stringByReplacingOccurrencesOfString: @"-" withString: @"_"];
return [@"branch_" stringByAppendingString: str];
}
- (NSString *)labelForMetadata: (NSDictionary *)metadata
{
NSString *escapedMetadata = [[NSString alloc] initWithData: CODataWithJSONObject(metadata, NULL)
encoding: NSUTF8StringEncoding];
// FIXME: escape escapedMetadata
return escapedMetadata;
}
- (void)writeDotNodeForRevisionInfo: (CORevisionInfo *)revInfo toString: (NSMutableString *)dest
{
if (revInfo.mergeParentRevisionUUID != nil)
{
[dest appendFormat: @" %@ -> %@ [color=orange];\n",
[self dotNameForRevisionUUID: revInfo.mergeParentRevisionUUID],
[self dotNameForRevisionUUID: revInfo.revisionUUID]];
[dest appendFormat: @" %@ -> %@;\n",
[self dotNameForRevisionUUID: revInfo.parentRevisionUUID],
[self dotNameForRevisionUUID: revInfo.revisionUUID]];
}
else if (revInfo.parentRevisionUUID != nil)
{
[dest appendFormat: @" %@ -> %@;\n",
[self dotNameForRevisionUUID: revInfo.parentRevisionUUID],
[self dotNameForRevisionUUID: revInfo.revisionUUID]];
}
else
{
[dest appendFormat: @" %@;\n", [self dotNameForRevisionUUID: revInfo.revisionUUID]];
}
if (revInfo.metadata != nil)
{
[dest appendFormat: @" %@ -> %@_metadata [style=dotted,label=\"metadata\"];\n",
[self dotNameForRevisionUUID: revInfo.revisionUUID],
[self dotNameForRevisionUUID: revInfo.revisionUUID]];
NSString *escapedMetadata = [self labelForMetadata: revInfo.metadata];
[dest appendFormat: @" %@_metadata [shape=box,color=red,label=<%@>];\n",
[self dotNameForRevisionUUID: revInfo.revisionUUID],
escapedMetadata];
}
}
- (NSString *)dotGraphForPersistentRootUUID: (ETUUID *)aPersistentRoot
{
NSMutableString *result = [NSMutableString string];
COPersistentRootInfo *info = [self persistentRootInfoForUUID: aPersistentRoot];
assert(dispatch_get_current_queue() != queue_);
dispatch_sync(queue_, ^()
{
COSQLiteStorePersistentRootBackingStore *backing = [self backingStoreForPersistentRootUUID: aPersistentRoot
createIfNotPresent: YES];
[result appendString: @"digraph G {\n"];
// Add revisions
NSIndexSet *revidsUsed = backing.revidsUsedRange;
for (NSUInteger i = revidsUsed.firstIndex; i != NSNotFound; i = [revidsUsed indexGreaterThanIndex: i])
{
ETUUID *revUUID = [backing revisionUUIDForRevid: i];
if (revUUID != nil)
{
CORevisionInfo *revInfo = [backing revisionInfoForRevisionUUID: revUUID];
[self writeDotNodeForRevisionInfo: revInfo toString: result];
}
}
// Add branches
for (ETUUID *branchUUID in info.branchUUIDs)
{
COBranchInfo *branchInfo = [info branchInfoForUUID: branchUUID];
if ([branchInfo.headRevisionUUID isEqual: branchInfo.currentRevisionUUID])
{
[result appendFormat: @" %@ -> %@ [style=dotted];\n",
[self dotNameForBranchUUID: branchInfo.UUID],
[self dotNameForRevisionUUID: branchInfo.headRevisionUUID]];
}
else
{
[result appendFormat: @" %@ -> %@ [style=dotted,label=\"head\"];\n",
[self dotNameForBranchUUID: branchInfo.UUID],
[self dotNameForRevisionUUID: branchInfo.headRevisionUUID]];
[result appendFormat: @" %@ -> %@ [style=dotted,label=\"current\"];\n",
[self dotNameForBranchUUID: branchInfo.UUID],
[self dotNameForRevisionUUID: branchInfo.currentRevisionUUID]];
}
if (branchInfo.metadata != nil)
{
[result appendFormat: @" %@ -> %@_metadata [style=dotted,label=\"metadata\"];\n",
[self dotNameForBranchUUID: branchInfo.UUID],
[self dotNameForBranchUUID: branchInfo.UUID]];
NSString *escapedMetadata = [self labelForMetadata: branchInfo.metadata];
// FIXME: escape escapedMetadata
[result appendFormat: @" %@_metadata [shape=box,color=red,label=<%@>];\n",
[self dotNameForBranchUUID: branchInfo.UUID],
escapedMetadata];
}
[result appendFormat: @" %@ [shape=box];\n",
[self dotNameForBranchUUID: branchInfo.UUID]];
}
[result appendString: @"}\n"];
});
return result;
}
- (void)showGraphForPersistentRootUUID: (ETUUID *)aUUID
{
NSString *basePath = [NSString stringWithFormat: @"%@-%d",
[NSTemporaryDirectory() stringByAppendingPathComponent: [aUUID stringValue]],
rand()];
NSString *dotGraphPath = [basePath stringByAppendingPathExtension: @"gv"];
[[self dotGraphForPersistentRootUUID: aUUID] writeToFile: dotGraphPath
atomically: YES
encoding: NSUTF8StringEncoding
error: NULL];
COViewDOTGraphFile(dotGraphPath);
}
void COViewDOTGraphFile(NSString *dotFilePath)
{
NSString *pdfPath = [dotFilePath stringByAppendingPathExtension: @"pdf"];
for (NSString *executablePath in @[@"/usr/bin/dot", @"/usr/local/bin/dot"])
{
if (![[NSFileManager defaultManager] fileExistsAtPath: executablePath])
{
continue;
}
NSTask *task = [NSTask launchedTaskWithLaunchPath: executablePath
arguments: @[@"-Tpdf", dotFilePath, @"-o", pdfPath]];
[task waitUntilExit];
[[NSWorkspace sharedWorkspace] openFile: pdfPath];
break;
}
}
@end