Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 164 lines (136 sloc) 4.612 kB
cef35ac @pieter GitGrapher: Rewrite looping code to C++
authored
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 //
8
9 #import "PBGitGrapher.h"
10 #import "PBGitCommit.h"
11 #import "PBGitLane.h"
e570c3e @pieter Use structs for graph lines
authored
12 #import "PBGitGraphLine.h"
e0dcdf6 @pieter PBGitGrapher: Use std::list rather than vector
authored
13 #import <list>
4e915cc @pieter Remove "extern C" declaration when importing libgit2
authored
14 #import "git/oid.h"
cef35ac @pieter GitGrapher: Rewrite looping code to C++
authored
15
16 using namespace std;
17
18 @implementation PBGitGrapher
19
20 #define MAX_LANES 32
21
22 - (id) initWithRepository: (PBGitRepository*) repo
23 {
b4c2a4f @pieter GitGrapher: free pointers on dealloc
authored
24 pl = new std::list<PBGitLane *>;
cef35ac @pieter GitGrapher: Rewrite looping code to C++
authored
25
26 PBGitLane::resetColors();
27 return self;
28 }
29
e570c3e @pieter Use structs for graph lines
authored
30 void add_line(struct PBGitGraphLine *lines, int *nLines, int upper, int from, int to, int index)
31 {
32 // TODO: put in one thing
33 struct PBGitGraphLine a = { upper, from, to, index };
34 lines[(*nLines)++] = a;
35 }
36
cef35ac @pieter GitGrapher: Rewrite looping code to C++
authored
37 - (void) decorateCommit: (PBGitCommit *) commit
38 {
39 int i = 0, newPos = -1;
e0dcdf6 @pieter PBGitGrapher: Use std::list rather than vector
authored
40 std::list<PBGitLane *> *currentLanes = new std::list<PBGitLane *>;
41 std::list<PBGitLane *> *previousLanes = (std::list<PBGitLane *> *)pl;
cef35ac @pieter GitGrapher: Rewrite looping code to C++
authored
42
641d2db @pieter GitGrapher: Reduce number of lines we output
authored
43 int maxLines = (previousLanes->size() + commit.nParents + 2) * 2;
e570c3e @pieter Use structs for graph lines
authored
44 struct PBGitGraphLine *lines = (struct PBGitGraphLine *)malloc(sizeof(struct PBGitGraphLine) * maxLines);
45 int currentLine = 0;
46
cef35ac @pieter GitGrapher: Rewrite looping code to C++
authored
47 PBGitLane *currentLane = NULL;
48 BOOL didFirst = NO;
49
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.
e0dcdf6 @pieter PBGitGrapher: Use std::list rather than vector
authored
53 std::list<PBGitLane *>::iterator it = previousLanes->begin();
54 for (; it != previousLanes->end(); ++it) {
cef35ac @pieter GitGrapher: Rewrite looping code to C++
authored
55 i++;
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])) {
59 if (!didFirst) {
60 didFirst = YES;
61 currentLanes->push_back(*it);
62 currentLane = currentLanes->back();
63 newPos = currentLanes->size();
e570c3e @pieter Use structs for graph lines
authored
64 add_line(lines, &currentLine, 1, i, newPos,(*it)->index());
641d2db @pieter GitGrapher: Reduce number of lines we output
authored
65 if (commit.nParents)
66 add_line(lines, &currentLine, 0, newPos, newPos,(*it)->index());
cef35ac @pieter GitGrapher: Rewrite looping code to C++
authored
67 }
68 else {
e570c3e @pieter Use structs for graph lines
authored
69 add_line(lines, &currentLine, 1, i, newPos,(*it)->index());
cef35ac @pieter GitGrapher: Rewrite looping code to C++
authored
70 delete *it;
71 }
72 }
73 else {
74 // We are not this commit.
6aa2894 @pieter GitGrapher: Remove unneedded loop
authored
75 currentLanes->push_back(*it);
e570c3e @pieter Use structs for graph lines
authored
76 add_line(lines, &currentLine, 1, i, currentLanes->size(),(*it)->index());
77 add_line(lines, &currentLine, 0, currentLanes->size(), currentLanes->size(), (*it)->index());
cef35ac @pieter GitGrapher: Rewrite looping code to C++
authored
78 }
79 // For existing columns, we always just continue straight down
80 // ^^ I don't know what that means anymore :(
81
82 }
83 }
84 //Add your own parents
85
86 // If we already did the first parent, don't do so again
56a6adc @pieter GitGrapher: Do not crash on only a single commit
authored
87 if (!didFirst && currentLanes->size() < MAX_LANES && commit.nParents) {
8372aee @pieter PBGitCommit: Use git_oids for parent shas
authored
88 PBGitLane *newLane = new PBGitLane(commit.parentShas);
cef35ac @pieter GitGrapher: Rewrite looping code to C++
authored
89 currentLanes->push_back(newLane);
90 newPos = currentLanes->size();
e570c3e @pieter Use structs for graph lines
authored
91 add_line(lines, &currentLine, 0, newPos, newPos, newLane->index());
cef35ac @pieter GitGrapher: Rewrite looping code to C++
authored
92 }
93
94 // Add all other parents
95
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;
99
8372aee @pieter PBGitCommit: Use git_oids for parent shas
authored
100 int parentIndex;
101 for (parentIndex = 1; parentIndex < commit.nParents; ++parentIndex) {
102 git_oid *parent = commit.parentShas + parentIndex;
cef35ac @pieter GitGrapher: Rewrite looping code to C++
authored
103 int i = 0;
104 BOOL was_displayed = NO;
e0dcdf6 @pieter PBGitGrapher: Use std::list rather than vector
authored
105 std::list<PBGitLane *>::iterator it = currentLanes->begin();
106 for (; it != currentLanes->end(); ++it) {
cef35ac @pieter GitGrapher: Rewrite looping code to C++
authored
107 i++;
108 if ((*it)->isCommit(parent)) {
e570c3e @pieter Use structs for graph lines
authored
109 add_line(lines, &currentLine, 0, i, newPos,(*it)->index());
cef35ac @pieter GitGrapher: Rewrite looping code to C++
authored
110 was_displayed = YES;
111 break;
112 }
113 }
114 if (was_displayed)
115 continue;
116
117 if (currentLanes->size() >= MAX_LANES)
118 break;
119
120 // Really add this parent
121 addedParent = YES;
122 PBGitLane *newLane = new PBGitLane(parent);
123 currentLanes->push_back(newLane);
e570c3e @pieter Use structs for graph lines
authored
124 add_line(lines, &currentLine, 0, currentLanes->size(), newPos, newLane->index());
cef35ac @pieter GitGrapher: Rewrite looping code to C++
authored
125 }
126
127 previous = [[PBGraphCellInfo alloc] initWithPosition:newPos andLines:lines];
e570c3e @pieter Use structs for graph lines
authored
128 if (currentLine > maxLines)
129 NSLog(@"Number of lines: %i vs allocated: %i", currentLine, maxLines);
130
131 previous.nLines = currentLine;
cef35ac @pieter GitGrapher: Rewrite looping code to C++
authored
132 previous.sign = commit.sign;
133
134 // If a parent was added, we have room to not indent.
135 if (addedParent)
136 previous.numColumns = currentLanes->size() - 1;
137 else
138 previous.numColumns = currentLanes->size();
139
140 // Update the current lane to point to the new parent
8372aee @pieter PBGitCommit: Use git_oids for parent shas
authored
141 if (currentLane && commit.nParents > 0)
142 currentLane->setSha(commit.parentShas[0]);
e0dcdf6 @pieter PBGitGrapher: Use std::list rather than vector
authored
143 else
144 currentLanes->remove(currentLane);
cef35ac @pieter GitGrapher: Rewrite looping code to C++
authored
145
146 delete previousLanes;
147
148 pl = currentLanes;
149 commit.lineInfo = previous;
150 }
151
152 - (void) finalize
153 {
b4c2a4f @pieter GitGrapher: free pointers on dealloc
authored
154 std::list<PBGitLane *> *lanes = (std::list<PBGitLane *> *)pl;
155 std::list<PBGitLane *>::iterator it = lanes->begin();
156 for (; it != lanes->end(); ++it)
157 delete *it;
158
159 delete lanes;
160
cef35ac @pieter GitGrapher: Rewrite looping code to C++
authored
161 [super finalize];
162 }
163 @end
Something went wrong with that request. Please try again.