5 // Created by Pieter de Bie on 17-06-08.
6 // Copyright 2008 __MyCompanyName__. All rights reserved.
9 #import "PBGitGrapher.h"
10 #import "PBGitCommit.h"
12 #import "PBGitGraphLine.h"
18 @implementation PBGitGrapher
22 - (id) initWithRepository: (PBGitRepository*) repo
24 pl = new std::list<PBGitLane *>;
26 PBGitLane::resetColors();
30 void add_line(struct PBGitGraphLine *lines, int *nLines, int upper, int from, int to, int index)
32 // TODO: put in one thing
33 struct PBGitGraphLine a = { upper, from, to, index };
34 lines[(*nLines)++] = a;
37 - (void) decorateCommit: (PBGitCommit *) commit
39 int i = 0, newPos = -1;
40 std::list<PBGitLane *> *currentLanes = new std::list<PBGitLane *>;
41 std::list<PBGitLane *> *previousLanes = (std::list<PBGitLane *> *)pl;
43 int maxLines = (previousLanes->size() + commit.nParents + 2) * 2;
44 struct PBGitGraphLine *lines = (struct PBGitGraphLine *)malloc(sizeof(struct PBGitGraphLine) * maxLines);
47 PBGitLane *currentLane = NULL;
50 // First, iterate over earlier columns and pass through any that don't want this commit
51 if (previous != nil) {
52 // We can't count until numColumns here, as it's only used for the width of the cell.
53 std::list<PBGitLane *>::iterator it = previousLanes->begin();
54 for (; it != previousLanes->end(); ++it) {
56 // This is our commit! We should do a "merge": move the line from
57 // our upperMapping to their lowerMapping
58 if ((*it)->isCommit([commit sha])) {
61 currentLanes->push_back(*it);
62 currentLane = currentLanes->back();
63 newPos = currentLanes->size();
64 add_line(lines, ¤tLine, 1, i, newPos,(*it)->index());
66 add_line(lines, ¤tLine, 0, newPos, newPos,(*it)->index());
69 add_line(lines, ¤tLine, 1, i, newPos,(*it)->index());
74 // We are not this commit.
75 currentLanes->push_back(*it);
76 add_line(lines, ¤tLine, 1, i, currentLanes->size(),(*it)->index());
77 add_line(lines, ¤tLine, 0, currentLanes->size(), currentLanes->size(), (*it)->index());
79 // For existing columns, we always just continue straight down
80 // ^^ I don't know what that means anymore :(
84 //Add your own parents
86 // If we already did the first parent, don't do so again
87 if (!didFirst && currentLanes->size() < MAX_LANES && commit.nParents) {
88 PBGitLane *newLane = new PBGitLane(commit.parentShas);
89 currentLanes->push_back(newLane);
90 newPos = currentLanes->size();
91 add_line(lines, ¤tLine, 0, newPos, newPos, newLane->index());
94 // Add all other parents
96 // If we add at least one parent, we can go back a single column.
97 // This boolean will tell us if that happened
98 BOOL addedParent = NO;
101 for (parentIndex = 1; parentIndex < commit.nParents; ++parentIndex) {
102 git_oid *parent = commit.parentShas + parentIndex;
104 BOOL was_displayed = NO;
105 std::list<PBGitLane *>::iterator it = currentLanes->begin();
106 for (; it != currentLanes->end(); ++it) {
108 if ((*it)->isCommit(parent)) {
109 add_line(lines, ¤tLine, 0, i, newPos,(*it)->index());
117 if (currentLanes->size() >= MAX_LANES)
120 // Really add this parent
122 PBGitLane *newLane = new PBGitLane(parent);
123 currentLanes->push_back(newLane);
124 add_line(lines, ¤tLine, 0, currentLanes->size(), newPos, newLane->index());
127 previous = [[PBGraphCellInfo alloc] initWithPosition:newPos andLines:lines];
128 if (currentLine > maxLines)
129 NSLog(@"Number of lines: %i vs allocated: %i", currentLine, maxLines);
131 previous.nLines = currentLine;
132 previous.sign = commit.sign;
134 // If a parent was added, we have room to not indent.
136 previous.numColumns = currentLanes->size() - 1;
138 previous.numColumns = currentLanes->size();
140 // Update the current lane to point to the new parent
141 if (currentLane && commit.nParents > 0)
142 currentLane->setSha(commit.parentShas[0]);
144 currentLanes->remove(currentLane);
146 delete previousLanes;
149 commit.lineInfo = previous;
154 std::list<PBGitLane *> *lanes = (std::list<PBGitLane *> *)pl;
155 std::list<PBGitLane *>::iterator it = lanes->begin();
156 for (; it != lanes->end(); ++it)