Merge branch 'fix_interspacing'
[GitX.git] / PBGitGrapher.m
blob1d6b2251ff6041076d50146d645b6adde2fceb27
1 //
2 //  PBGitGrapher.m
3 //  GitX
4 //
5 //  Created by Pieter de Bie on 17-06-08.
6 //  Copyright 2008 __MyCompanyName__. All rights reserved.
7 //
9 #import "PBGitGrapher.h"
10 #import "PBGitCommit.h"
12 @implementation PBGitGrapher
15 - (void) parseCommits: (NSArray *) commits
17         cellsInfo = [NSMutableArray arrayWithCapacity: [commits count]];
18         int row = 0;
20         PBGraphCellInfo* previous;
21         NSMutableArray* previousLanes = [NSMutableArray array];
23         for (PBGitCommit* commit in commits) {
24                 int i = 0, newPos = -1; 
25                 NSMutableArray* currentLanes = [NSMutableArray array];
26                 NSMutableArray* lines = [NSMutableArray array];
27                 BOOL didFirst = NO;
29                 // First, iterate over earlier columns and pass through any that don't want this commit
30                 if (previous != nil) {
31                         
32                         // We can't count until numColumns here, as it's only used for the width of the cell.
33                         for (NSString* lane in previousLanes) {
34                                 i++;
35                                 // This is our commit! We should do a "merge": move the line from
36                                 // our upperMapping to their lowerMapping
37                                 if ([lane isEqualToString:commit.sha]) {
38                                         if (!didFirst) {
39                                                 didFirst = YES;
40                                                 [currentLanes addObject: [commit.parents objectAtIndex:0]];
41                                                 newPos = [currentLanes count];
42                                         }
43                                         [lines addObject: [PBLine upperLineFrom: i to: newPos]];
44                                 }
45                                 else { 
46                                         // We are not this commit.
47                                         // Try to find an earlier column for this commit.
48                                         int j = 0;
49                                         BOOL found = NO;
50                                         for (NSString* column in currentLanes) {
51                                                 j++;
52                                                 // ??? what is this?
53                                                 if (j == newPos)
54                                                         continue;
55                                                 if ([column isEqualToString: lane]) {
56                                                         // We already have a column for this commit. use it instead
57                                                         [lines addObject: [PBLine upperLineFrom: i to: j]];
58                                                         found = YES;
59                                                         break;
60                                                 }
61                                         }
63                                         // We need a new column for this.
64                                         if (!found) {
65                                                 
66                                                 // This was used as a hack to stop large lanes from drawing
67                                                 //if (previous->columns[i].color == 10)
68                                                 //      continue;
69                                                 
70                                                 [currentLanes addObject: lane];
71                                                 [lines addObject: [PBLine upperLineFrom: [currentLanes count] to: [currentLanes count]]];
72                                                 [lines addObject: [PBLine lowerLineFrom: [currentLanes count] to: [currentLanes count]]];
73                                         }
74                                 }
75                                 // For existing columns, we always just continue straight down
76                                 // ^^ I don't know what that means anymore :(
77                                 [lines addObject:[PBLine lowerLineFrom:newPos to:newPos]];
78                         }
79                 }
80                 
81                 //Add your own parents
82                 
83                 // If we already did the first parent, don't do so again
84                 if (!didFirst) {
85                         [currentLanes addObject: [commit.parents objectAtIndex:0]];
86                         newPos = [currentLanes count];
87                         [lines addObject:[PBLine lowerLineFrom: newPos to: newPos]];
88                 }
89                 
90                 // Add all other parents
91                 
92                 // If we add at least one parent, we can go back a single column.
93                 // This boolean will tell us if that happened
94                 BOOL addedParent = NO;
96                 for (NSString* parent in [commit.parents subarrayWithRange:NSMakeRange(1, [commit.parents count] -1)]) {
97                         int i = 0;
98                         BOOL was_displayed = NO;
99                         for (NSString* column in currentLanes) {
100                                 i++;
101                                 if ([column isEqualToString: parent]) {
102                                         [lines addObject:[PBLine lowerLineFrom: i to: newPos]];
103                                         was_displayed = YES;
104                                         break;
105                                 }
106                         }
107                         if (was_displayed)
108                                 continue;
109                         
110                         // Really add this parent
111                         addedParent = YES;
112                         [currentLanes addObject:parent];
113                         [lines addObject:[PBLine lowerLineFrom: [currentLanes count] to: newPos]];
114                 }
115                 
116                 ++row;
117                 previous = [[PBGraphCellInfo alloc] initWithPosition:newPos andLines:lines];
118                 
119                 // If a parent was added, we have room to not indent.
120                 if (addedParent)
121                         previous.numColumns = [currentLanes count] - 1;
122                 else
123                         previous.numColumns = [currentLanes count];
124                 previousLanes = currentLanes;
125                 [cellsInfo addObject: previous];
126         }
129 - (PBGraphCellInfo*) cellInfoForRow: (int) row
131         return [cellsInfo objectAtIndex: row];
134 - (void) finalize
136         free(cellsInfo);
137         [super finalize];
139 @end