/[CvsGraph]/cvsgraph/cvsgraph.c
ViewVC logotype

Annotate of /cvsgraph/cvsgraph.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.59 - (show annotations)
Thu Dec 15 13:06:09 2005 UTC (11 years, 10 months ago) by bertho
Branch: MAIN
Changes since 1.58: +141 -127 lines
File MIME type: text/plain
Implemented multiple regex match for triplets merge_{from,to,color} so
that merges can be detected with more disjoined tag definitions.
1 /*
2 * CvsGraph graphical representation generator of brances and revisions
3 * of a file in cvs/rcs.
4 *
5 * Copyright (C) 2001,2002,2003,2004 B. Stultiens
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 #include "config.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <assert.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #ifdef HAVE_SYS_WAIT_H
33 # include <sys/wait.h>
34 #endif
35 #include <fcntl.h>
36 #include <regex.h>
37 #include <errno.h>
38 #include <ctype.h>
39 #include <time.h>
40 #include <limits.h>
41 #include <regex.h>
42 #include <math.h>
43
44 #ifdef HAVE_GETOPT_H
45 # include <getopt.h>
46 #endif
47
48 #include <gd.h>
49 #include <gdfontt.h>
50
51 #include "cvsgraph.h"
52 #include "utils.h"
53 #include "readconf.h"
54 #include "rcs.h"
55
56 #if !defined(HAVE_IMAGE_GIF) && !defined(HAVE_IMAGE_PNG) && !defined(HAVE_IMAGE_JPEG)
57 # error No image output format available. Check libgd
58 #endif
59
60
61 /*#define DEBUG 1*/
62 /*#define NOGDFILL 1*/
63 /*#define DEBUG_IMAGEMAP 1*/
64
65 #define LOOPSAFEGUARD 10000 /* Max itterations in possible infinite loops */
66
67 #ifndef MAX
68 # define MAX(a,b) ((a) > (b) ? (a) : (b))
69 #endif
70
71 #ifndef MIN
72 # define MIN(a,b) ((a) < (b) ? (a) : (b))
73 #endif
74
75 #define ALIGN_HL 0x00
76 #define ALIGN_HC 0x01
77 #define ALIGN_HR 0x02
78 #define ALIGN_HX 0x0f
79 #define ALIGN_VT 0x00
80 #define ALIGN_VC 0x10
81 #define ALIGN_VB 0x20
82 #define ALIGN_VX 0xf0
83
84 #ifndef M_PI /* math.h should have defined this */
85 # define M_PI 3.14159265358979323846
86 #endif
87 #define ROUND(f) ((f >= 0.0)?((int)(f + 0.5)):((int)(f - 0.5)))
88
89 #define ARROW_LENGTH 12 /* Default arrow dimensions */
90 #define ARROW_WIDTH 3
91
92 /*
93 **************************************************************************
94 * Globals
95 **************************************************************************
96 */
97
98 config_t conf;
99 int debuglevel;
100
101 static color_t white_color = {255, 255, 255, 0};
102 static color_t black_color = {0, 0, 0, 0};
103
104 static branch_t *subtree_branch = NULL; /* Set to the (first) subtree branch that we want to show */
105 static revision_t *subtree_rev = NULL; /* Set to the subtree revision which branches we want to show */
106
107 static msg_stack_t *msg_stack = NULL; /* Messages that would otherwise be sent to stderr goto the image */
108 static int nmsg_stack = 0;
109
110 /*
111 **************************************************************************
112 * Forwards
113 **************************************************************************
114 */
115 static void zap_string(void);
116 static char *dup_string(void);
117 static void add_string_str(const char *s);
118 static void add_string_ch(int ch);
119 static void add_string_date(const char *d);
120 static void add_string_str_html(const char *s, int maxlen);
121 static void add_string_str_len(const char *s, int maxlen);
122
123 static void calc_subtree_size(branch_t *b, int *x, int *y, int *w, int *h);
124
125 /*
126 **************************************************************************
127 * Debug routines
128 **************************************************************************
129 */
130 static void dump_rev(char *p, rev_t *r)
131 {
132 printf("%s", p);
133 if(r)
134 printf("'%s', '%s', %d\n", r->rev, r->branch, r->isbranch);
135 else
136 printf("<null>\n");
137 }
138
139 static void dump_id(char *p, char *d)
140 {
141 printf("%s", p);
142 if(d)
143 printf("'%s'\n", d);
144 else
145 printf("<null>\n");
146 }
147
148 static void dump_idrev(char *p, idrev_t *t)
149 {
150 printf("%s", p);
151 if(t)
152 {
153 printf("'%s' -> ", t->id);
154 dump_rev("", t->rev);
155 }
156 else
157 printf("<null>\n");
158 }
159
160 static void dump_tag(char *p, tag_t *t)
161 {
162 printf("%s", p);
163 if(t)
164 {
165 printf("'%s' -> ", t->tag);
166 dump_rev("", t->rev);
167 }
168 else
169 printf("<null>\n");
170 }
171
172 static void dump_delta(char *p, delta_t *d)
173 {
174 int i;
175 printf("%sdelta.rev : ", p);
176 dump_rev("", d->rev);
177 printf("%sdelta.date : %s\n", p, d->date);
178 printf("%sdelta.author: %s\n", p, d->author);
179 printf("%sdelta.state : %s\n", p, d->state);
180 for(i = 0; d->branches && i < d->branches->nrevs; i++)
181 {
182 printf("%sdelta.branch: ", p);
183 dump_rev("", d->branches->revs[i]);
184 }
185 printf("%sdelta.next : ", p);
186 dump_rev("", d->next);
187 printf("\n");
188 }
189
190 static void dump_dtext(char *p, dtext_t *d)
191 {
192 printf("%sdtext.rev : ", p);
193 dump_rev("", d->rev);
194 printf("%sdtext.log : %d bytes\n", p, d->log ? strlen(d->log) : -1);
195 printf("%sdtext.text : %d bytes\n", p, d->text ? strlen(d->text) : -1);
196 printf("\n");
197 }
198
199 static void dump_rcsfile(rcsfile_t *rcs)
200 {
201 int i;
202 printf("root : '%s'\n", rcs->root);
203 printf("module : '%s'\n", rcs->module);
204 printf("file : '%s'\n", rcs->file);
205 dump_rev("head : ", rcs->head);
206 dump_rev("branch : ", rcs->branch);
207 printf("access :\n");
208 for(i = 0; rcs->access && i < rcs->access->nids; i++)
209 dump_id("\t", rcs->access->ids[i]);
210 printf("tags :\n");
211 for(i = 0; rcs->tags && i < rcs->tags->ntags; i++)
212 dump_tag("\t", rcs->tags->tags[i]);
213 printf("locks :%s\n", rcs->strict ? " (strict)" : "");
214 for(i = 0; rcs->locks && i < rcs->locks->nidrevs; i++)
215 dump_idrev("\t", rcs->locks->idrevs[i]);
216 printf("comment: '%s'\n", rcs->comment);
217 printf("expand : '%s'\n", rcs->expand ? rcs->expand : "(default -kv)");
218 printf("deltas :\n");
219 for(i = 0; rcs->deltas && i < rcs->deltas->ndeltas; i++)
220 dump_delta("\t", rcs->deltas->deltas[i]);
221 printf("desc : '%s'\n", rcs->desc);
222 printf("dtexts :\n");
223 for(i = 0; rcs->dtexts && i < rcs->dtexts->ndtexts; i++)
224 dump_dtext("\t", rcs->dtexts->dtexts[i]);
225
226 fflush(stdout);
227 }
228
229 /*
230 **************************************************************************
231 * Error/Warning Message helpers
232 **************************************************************************
233 */
234 #define MSGBUFSIZE 256
235 void stack_msg(int severity, const char *fmt, ...)
236 {
237 va_list va;
238 int i;
239 char *buf = xmalloc(MSGBUFSIZE);
240 switch(severity)
241 {
242 case MSG_WARN: sprintf(buf, "Warning: "); break;
243 case MSG_ERR: sprintf(buf, "Error: "); break;
244 default: sprintf(buf, "Unqualified error: "); break;
245 }
246 i = strlen(buf);
247 assert(i < MSGBUFSIZE);
248 va_start(va, fmt);
249 vsnprintf(buf+i, MSGBUFSIZE-i, fmt, va);
250 va_end(va);
251 if(!msg_stack)
252 msg_stack = xmalloc(sizeof(*msg_stack));
253 else
254 {
255 msg_stack = xrealloc(msg_stack, (nmsg_stack+1)*sizeof(*msg_stack));
256 }
257 msg_stack[nmsg_stack].msg = buf;
258 msg_stack[nmsg_stack].severity = severity;
259 nmsg_stack++;
260 }
261
262 /*
263 **************************************************************************
264 * Read the rcs file
265 **************************************************************************
266 */
267 static rcsfile_t *get_rcsfile(const char *cvsroot, const char *module, const char *file)
268 {
269 char *cmd = NULL;
270 int rv;
271
272 if(file)
273 {
274 cmd = xmalloc(strlen(cvsroot) + strlen(module) + strlen(file) + 1);
275 sprintf(cmd, "%s%s%s", cvsroot, module, file);
276 if(!(rcsin = fopen(cmd, "rb")))
277 {
278 perror(cmd);
279 return NULL;
280 }
281 input_file = cmd;
282 }
283 else
284 {
285 rcsin = stdin;
286 input_file = "<stdin>";
287 }
288 line_number = 1;
289 rv = rcsparse();
290 if(file)
291 {
292 fclose(rcsin);
293 xfree(cmd);
294 }
295 if(rv)
296 return NULL;
297 input_file = NULL;
298 if(file)
299 {
300 rcsfile->root = xstrdup(cvsroot);
301 rcsfile->module = xstrdup(module);
302 rcsfile->file = xstrdup(file);
303 }
304 else
305 {
306 rcsfile->root = xstrdup("");
307 rcsfile->module = xstrdup("");
308 rcsfile->file = xstrdup("<stdin>");
309 }
310 return rcsfile;
311 }
312
313 /*
314 **************************************************************************
315 * Sort and find helpers
316 **************************************************************************
317 */
318 static int count_dots(const char *s)
319 {
320 int i;
321 for(i = 0; *s; s++)
322 {
323 if(*s == '.')
324 i++;
325 }
326 return i;
327 }
328
329 static int compare_rev(int bcmp, const rev_t *r1, const rev_t *r2)
330 {
331 int d1, d2;
332 char *c1, *c2;
333 char *v1, *v2;
334 char *s1, *s2;
335 int retval = 0;
336 assert(r1 != NULL);
337 assert(r2 != NULL);
338 if(bcmp)
339 {
340 assert(r1->branch != NULL);
341 assert(r2->branch != NULL);
342 c1 = r1->branch;
343 c2 = r2->branch;
344 }
345 else
346 {
347 assert(r1->rev != NULL);
348 assert(r2->rev != NULL);
349 c1 = r1->rev;
350 c2 = r2->rev;
351 }
352
353 d1 = count_dots(c1);
354 d2 = count_dots(c2);
355 if(d1 != d2)
356 {
357 return d1 - d2;
358 }
359
360 s1 = v1 = xstrdup(c1);
361 s2 = v2 = xstrdup(c2);
362 while(1)
363 {
364 char *vc1 = strchr(s1, '.');
365 char *vc2 = strchr(s2, '.');
366 if(vc1 && vc2)
367 *vc1 = *vc2 = '\0';
368 if(*s1 && *s2)
369 {
370 d1 = atoi(s1);
371 d2 = atoi(s2);
372 if(d1 != d2)
373 {
374 retval = d1 - d2;
375 break;
376 }
377 }
378 if(!vc1 || !vc2)
379 break;
380 s1 = vc1 + 1;
381 s2 = vc2 + 1;
382 }
383 xfree(v1);
384 xfree(v2);
385 return retval;
386 }
387
388 /*
389 **************************************************************************
390 * Reorganise the rcsfile for the branches
391 *
392 * Basically, we have a list of deltas (i.e. administrative info on
393 * revisions) and a list of delta text (the actual logs and diffs).
394 * The deltas are linked through the 'next' and the 'branches' fields
395 * which describe the tree-structure of revisions.
396 * The reorganisation means that we put each delta and corresponding
397 * delta text in a revision structure and assign it to a specific
398 * branch. This is required because we want to be able to calculate
399 * the bounding boxes of each branch. The revisions expand vertically
400 * and the branches expand horizontally.
401 * The reorganisation is performed in these steps:
402 * 1 - sort deltas and delta text on revision number for quick lookup
403 * 2 - start at the denoted head revision:
404 * * create a branch structure and add this revision
405 * * for each 'branches' in the delta do:
406 * - walk all 'branches' of the delta and recursively goto 2
407 * with the denoted branch delta as new head
408 * - backlink the newly create sub-branch to the head revision
409 * so that we can draw them recursively
410 * * set head to the 'next' field and goto 2 until no next is
411 * available
412 * 3 - update the administration
413 **************************************************************************
414 */
415 static int sort_delta(const void *d1, const void *d2)
416 {
417 return compare_rev(0, (*(delta_t **)d1)->rev, (*(delta_t **)d2)->rev);
418 }
419
420 static int search_delta(const void *r, const void *d)
421 {
422 return compare_rev(0, (rev_t *)r, (*(delta_t **)d)->rev);
423 }
424
425 static delta_t *find_delta(delta_t **dl, int n, rev_t *r)
426 {
427 delta_t **d;
428 if(!n)
429 return NULL;
430 d = bsearch(r, dl, n, sizeof(*dl), search_delta);
431 if(!d)
432 return NULL;
433 return *d;
434 }
435
436 static int sort_dtext(const void *d1, const void *d2)
437 {
438 return compare_rev(0, (*(dtext_t **)d1)->rev, (*(dtext_t **)d2)->rev);
439 }
440
441 static int search_dtext(const void *r, const void *d)
442 {
443 return compare_rev(0, (rev_t *)r, (*(dtext_t **)d)->rev);
444 }
445
446 static dtext_t *find_dtext(dtext_t **dl, int n, rev_t *r)
447 {
448 dtext_t **d;
449 if(!n)
450 return NULL;
451 d = bsearch(r, dl, n, sizeof(*dl), search_dtext);
452 if(!d)
453 return NULL;
454 return *d;
455 }
456
457 static rev_t *dup_rev(const rev_t *r)
458 {
459 rev_t *t = xmalloc(sizeof(*t));
460 t->rev = xstrdup(r->rev);
461 t->branch = xstrdup(r->branch);
462 t->isbranch = r->isbranch;
463 return t;
464 }
465
466 static branch_t *new_branch(delta_t *d, dtext_t *t)
467 {
468 branch_t *b = xmalloc(sizeof(*b));
469 revision_t *r = xmalloc(sizeof(*r));
470 r->delta = d;
471 r->dtext = t;
472 r->rev = d->rev;
473 r->branch = b;
474 b->branch = dup_rev(d->rev);
475 b->branch->isbranch = 1;
476 b->nrevs = 1;
477 b->revs = xmalloc(sizeof(b->revs[0]));
478 b->revs[0] = r;
479 return b;
480 }
481
482 static revision_t *add_to_branch(branch_t *b, delta_t *d, dtext_t *t)
483 {
484 revision_t *r = xmalloc(sizeof(*r));
485 r->delta = d;
486 r->dtext = t;
487 r->rev = d->rev;
488 r->branch = b;
489 b->revs = xrealloc(b->revs, (b->nrevs+1) * sizeof(b->revs[0]));
490 b->revs[b->nrevs] = r;
491 b->nrevs++;
492 return r;
493 }
494
495 static int sort_branch_height(const void *b1, const void *b2)
496 {
497 return (*(branch_t **)b1)->nrevs - (*(branch_t **)b2)->nrevs;
498 }
499
500 static void build_branch(branch_t ***bl, int *nbl, delta_t **sdl, int nsdl, dtext_t **sdt, int nsdt, delta_t *head)
501 {
502 branch_t *b;
503 dtext_t *text;
504 revision_t *currev;
505
506 assert(head != NULL);
507
508 if(head->flag)
509 {
510 stack_msg(MSG_ERR, "Circular reference on '%s' in branchpoint\n", head->rev->rev);
511 return;
512 }
513 head->flag++;
514 text = find_dtext(sdt, nsdt, head->rev);
515
516 /* Create a new branch for this head */
517 b = new_branch(head, text);
518 *bl = xrealloc(*bl, (*nbl+1)*sizeof((*bl)[0]));
519 (*bl)[*nbl] = b;
520 (*nbl)++;
521 currev = b->revs[0];
522 while(1)
523 {
524 /* Process all sub-branches */
525 if(head->branches)
526 {
527 int i;
528 for(i = 0; i < head->branches->nrevs; i++)
529 {
530 delta_t *d = find_delta(sdl, nsdl, head->branches->revs[i]);
531 int btag = *nbl;
532 if(!d)
533 continue;
534 build_branch(bl, nbl, sdl, nsdl, sdt, nsdt, d);
535
536 /* Set the new branch's origin */
537 (*bl)[btag]->branchpoint = currev;
538
539 /* Add branch to this revision */
540 currev->branches = xrealloc(currev->branches, (currev->nbranches+1)*sizeof(currev->branches[0]));
541 currev->branches[currev->nbranches] = (*bl)[btag];
542 currev->nbranches++;
543 }
544 if(conf.branch_resort)
545 qsort(currev->branches, currev->nbranches, sizeof(currev->branches[0]), sort_branch_height);
546 }
547
548 /* Walk through the next list */
549 if(!head->next)
550 return;
551
552 head = find_delta(sdl, nsdl, head->next);
553 if(!head)
554 {
555 stack_msg(MSG_ERR, "Next revision (%s) not found in deltalist\n", head->next->rev);
556 return;
557 }
558 if(head->flag)
559 {
560 stack_msg(MSG_ERR, "Circular reference on '%s'\n", head->rev->rev);
561 return;
562 }
563 head->flag++;
564 text = find_dtext(sdt, nsdt, head->rev);
565 currev = add_to_branch(b, head, text);
566 }
567 }
568
569 int reorganise_branches(rcsfile_t *rcs)
570 {
571 delta_t **sdelta;
572 int nsdelta;
573 dtext_t **sdtext;
574 int nsdtext;
575 delta_t *head;
576 branch_t **bl;
577 int nbl;
578 int i;
579
580 assert(rcs->deltas != NULL);
581 assert(rcs->head != NULL);
582
583 /* Make a new list for quick lookup */
584 nsdelta = rcs->deltas->ndeltas;
585 sdelta = xmalloc(nsdelta * sizeof(sdelta[0]));
586 memcpy(sdelta, rcs->deltas->deltas, nsdelta * sizeof(sdelta[0]));
587 qsort(sdelta, nsdelta, sizeof(sdelta[0]), sort_delta);
588
589 /* Do the same for the delta text */
590 if(rcs->dtexts)
591 {
592 nsdtext = rcs->dtexts->ndtexts;
593 sdtext = xmalloc(nsdtext * sizeof(sdtext[0]));
594 memcpy(sdtext, rcs->dtexts->dtexts, nsdtext * sizeof(sdtext[0]));
595 qsort(sdtext, nsdtext, sizeof(sdtext[0]), sort_dtext);
596 }
597 else
598 {
599 nsdtext = 0;
600 sdtext = NULL;
601 }
602
603 /* Start from the head of the trunk */
604 head = find_delta(sdelta, nsdelta, rcs->head);
605 if(!head)
606 {
607 stack_msg(MSG_ERR, "Head revision (%s) not found in deltalist\n", rcs->head->rev);
608 return 0;
609 }
610 bl = NULL;
611 nbl = 0;
612 build_branch(&bl, &nbl, sdelta, nsdelta, sdtext, nsdtext, head);
613
614 /* Reverse the head branch */
615 for(i = 0; i < bl[0]->nrevs/2; i++)
616 {
617 revision_t *r;
618 r = bl[0]->revs[i];
619 bl[0]->revs[i] = bl[0]->revs[bl[0]->nrevs-i-1];
620 bl[0]->revs[bl[0]->nrevs-i-1] = r;
621 }
622
623 /* Update the branch-number of the head because it was reversed */
624 xfree(bl[0]->branch->branch);
625 bl[0]->branch->branch = xstrdup(bl[0]->revs[0]->rev->branch);
626
627 /* Keep the admin */
628 rcs->branches = bl;
629 rcs->nbranches = nbl;
630 rcs->sdelta = sdelta;
631 rcs->nsdelta = nsdelta;
632 rcs->sdtext = sdtext;
633 rcs->nsdtext = nsdtext;
634 rcs->active = bl[0];
635 return 1;
636 }
637
638 /*
639 **************************************************************************
640 * Assign the symbolic tags to the revisions and branches
641 *
642 * The tags point to revision numbers somewhere in the tree structure
643 * of branches and revisions. First we make a sorted list of all
644 * revisions and then we assign each tag to the proper revision.
645 **************************************************************************
646 */
647 static int sort_revision(const void *r1, const void *r2)
648 {
649 return compare_rev(0, (*(revision_t **)r1)->delta->rev, (*(revision_t **)r2)->delta->rev);
650 }
651
652 static int search_revision(const void *t, const void *r)
653 {
654 return compare_rev(0, (rev_t *)t, (*(revision_t **)r)->delta->rev);
655 }
656
657 static int sort_branch(const void *b1, const void *b2)
658 {
659 return compare_rev(1, (*(branch_t **)b1)->branch, (*(branch_t **)b2)->branch);
660 }
661
662 static int search_branch(const void *t, const void *b)
663 {
664 return compare_rev(1, (rev_t *)t, (*(branch_t **)b)->branch);
665 }
666
667 static char *previous_rev(const char *c)
668 {
669 int dots = count_dots(c);
670 char *cptr;
671 char *r;
672 if(!dots)
673 {
674 stack_msg(MSG_ERR, "FIXME: previous_rev(\"%s\"): Cannot determine parent branch revision\n", c);
675 return xstrdup("1.0"); /* FIXME: don't know what the parent is */
676 }
677 if(dots & 1)
678 {
679 /* Is is a revision we want the parent of */
680 r = xstrdup(c);
681 cptr = strrchr(r, '.');
682 assert(cptr != NULL);
683 if(dots == 1)
684 {
685 stack_msg(MSG_ERR, "FIXME: previous_rev(\"%s\"): Going beyond top-level?\n", c);
686 /* FIXME: What is the parent of 1.1? */
687 cptr[1] = '\0';
688 strcat(r, "0");
689 return r;
690 }
691 /* Here we have a "x.x[.x.x]+" case */
692 *cptr = '\0';
693 cptr = strrchr(r, '.');
694 assert(cptr != NULL);
695 *cptr = '\0';
696 return r;
697 }
698 /* It is a branch we want the parent of */
699 r = xstrdup(c);
700 cptr = strrchr(r, '.');
701 assert(cptr != NULL);
702 *cptr = '\0';
703 return r;
704 }
705
706 static char *build_regex(size_t n, regmatch_t *m, const char *ms, int idx)
707 {
708 char *cptr;
709 int i;
710
711 if(!conf.merge_to.strs[idx])
712 return NULL;
713
714 zap_string();
715 for(cptr = conf.merge_to.strs[idx]; *cptr; cptr++)
716 {
717 if(*cptr == '%')
718 {
719 if(cptr[1] >= '1' && cptr[1] <= '9')
720 {
721 int idx = cptr[1] - '0';
722 regmatch_t *p = &m[idx];
723 if(idx < n && !(p->rm_so == -1 || p->rm_so >= p->rm_eo))
724 {
725 for(i = p->rm_so; i < p->rm_eo; i++)
726 {
727 if(strchr("^$.*+\\[{()", ms[i]))
728 add_string_ch('\\');
729 add_string_ch(ms[i]);
730 }
731 }
732 cptr++;
733 }
734 else
735 add_string_ch('%');
736 }
737 else
738 add_string_ch(*cptr);
739 }
740 return dup_string();
741 }
742
743 static void find_merges_cvsnt(rcsfile_t *rcs)
744 {
745 int i;
746
747 if(!conf.merge_cvsnt)
748 return;
749
750 for(i = 0; i < rcs->nsrev; i++)
751 {
752 revision_t **r;
753
754 if(!rcs->srev[i]->delta->mergepoint)
755 continue;
756
757 r = bsearch(rcs->srev[i]->delta->mergepoint->rev, rcs->srev, rcs->nsrev, sizeof(rcs->srev[0]), search_revision);
758 if(!r)
759 continue;
760 rcs->merges = xrealloc(rcs->merges, sizeof(rcs->merges[0]) * (rcs->nmerges+1));
761 rcs->merges[rcs->nmerges].type = TR_REVISION;
762 rcs->merges[rcs->nmerges].from.rev = *r;
763 rcs->merges[rcs->nmerges].to.rev = rcs->srev[i];
764 rcs->merges[rcs->nmerges].clr = -1;
765 rcs->nmerges++;
766 (*r)->stripped = -1;
767 rcs->srev[i]->stripped = -1;
768 }
769 }
770
771 static void find_merges(rcsfile_t *rcs)
772 {
773 int i, j;
774 int err;
775 int rcflags = REG_EXTENDED | (conf.merge_nocase ? REG_ICASE : 0);
776 regex_t *refrom = NULL;
777 regex_t *reto = NULL;
778 regmatch_t *matchfrom = NULL;
779
780 if(!conf.merge_from.n || !conf.merge_to.n)
781 return;
782
783 for(j = 0; j < conf.merge_from.n; j++)
784 {
785 if(!conf.merge_from.strs[0] || !conf.merge_to.strs[0])
786 continue;
787
788 refrom = xmalloc(sizeof(*refrom));
789 reto = xmalloc(sizeof(*reto));
790
791 /* Compile the 'from' regex match for merge identification */
792 err = regcomp(refrom, conf.merge_from.strs[j], rcflags);
793 if(err)
794 {
795 char *msg;
796 i = regerror(err, refrom, NULL, 0);
797 msg = xmalloc(i+1);
798 regerror(err, refrom, msg, i+1);
799 stack_msg(MSG_WARN, "%s", msg);
800 xfree(msg);
801 xfree(refrom);
802 xfree(reto);
803 return;
804 }
805 else
806 matchfrom = xmalloc((refrom->re_nsub+1) * sizeof(*matchfrom));
807
808 for(i = 0; i < rcs->tags->ntags; i++)
809 {
810 tag_t *t = rcs->tags->tags[i];
811
812 /* Must be revision tags and not detached */
813 if(t->rev->isbranch || !t->logrev)
814 continue;
815
816 /* Try to find merge tag matches */
817 if(!regexec(refrom, t->tag, refrom->re_nsub+1, matchfrom, 0))
818 {
819 int n;
820 char *to;
821
822 to = build_regex(refrom->re_nsub+1, matchfrom, t->tag, j);
823 if(to)
824 {
825 err = regcomp(reto, to, rcflags);
826 if(err)
827 {
828 char *msg;
829 i = regerror(err, reto, NULL, 0);
830 msg = xmalloc(i+1);
831 regerror(err, reto, msg, i+1);
832 stack_msg(MSG_WARN, "%s", msg);
833 xfree(msg);
834 }
835 else if(!err)
836 {
837 for(n = 0; n < rcs->tags->ntags; n++)
838 {
839 tag_t *nt = rcs->tags->tags[n];
840 /* From and To never should match the same tag or belong to a branch */
841 if(n == i || nt->rev->isbranch || !nt->logrev)
842 continue;
843
844 if(!regexec(reto, nt->tag, 0, NULL, 0))
845 {
846 /* Tag matches */
847 rcs->merges = xrealloc(rcs->merges,
848 sizeof(rcs->merges[0]) * (rcs->nmerges+1));
849 rcs->merges[rcs->nmerges].type = TR_TAG;
850 rcs->merges[rcs->nmerges].to.tag = nt;
851 rcs->merges[rcs->nmerges].from.tag = t;
852 rcs->merges[rcs->nmerges].clr = j;
853 rcs->nmerges++;
854 if(!conf.tag_ignore_merge)
855 {
856 nt->ignore = 0;
857 t->ignore = 0;
858 }
859 /* We cannot (should not) match multiple times */
860 if(!conf.merge_findall)
861 break;
862 }
863 }
864 regfree(reto);
865 }
866 xfree(to);
867 }
868 }
869 }
870 if(matchfrom) xfree(matchfrom);
871 if(refrom) { regfree(refrom); xfree(refrom); }
872 if(reto) xfree(reto);
873 refrom = NULL;
874 reto = NULL;
875 matchfrom = NULL;
876 }
877 }
878
879 static void assign_tags(rcsfile_t *rcs)
880 {
881 int i;
882 int nr;
883 regex_t *regextag = NULL;
884
885 if(conf.tag_ignore && conf.tag_ignore[0])
886 {
887 int err;
888 regextag = xmalloc(sizeof(*regextag));
889 err = regcomp(regextag, conf.tag_ignore, REG_EXTENDED | REG_NOSUB | (conf.tag_nocase ? REG_ICASE : 0));
890 if(err)
891 {
892 char *msg;
893 i = regerror(err, regextag, NULL, 0);
894 msg = xmalloc(i+1);
895 regerror(err, regextag, msg, i+1);
896 stack_msg(MSG_WARN, "%s", msg);
897 xfree(msg);
898 xfree(regextag);
899 regextag = NULL;
900 }
901 }
902
903 for(i = nr = 0; i < rcs->nbranches; i++)
904 nr += rcs->branches[i]->nrevs;
905
906 rcs->srev = xmalloc(nr * sizeof(rcs->srev[0]));
907 rcs->nsrev = nr;
908 for(i = nr = 0; i < rcs->nbranches; i++)
909 {
910 memcpy(&rcs->srev[nr], rcs->branches[i]->revs, rcs->branches[i]->nrevs * sizeof(rcs->branches[i]->revs[0]));
911 nr += rcs->branches[i]->nrevs;
912 }
913
914 qsort(rcs->srev, rcs->nsrev, sizeof(rcs->srev[0]), sort_revision);
915 qsort(rcs->branches, rcs->nbranches, sizeof(rcs->branches[0]), sort_branch);
916
917 if(!rcs->branch)
918 {
919 /* The main trunk is the active trunk */
920 rcs->tags->tags = xrealloc(rcs->tags->tags, (rcs->tags->ntags+1)*sizeof(rcs->tags->tags[0]));
921 rcs->tags->tags[rcs->tags->ntags] = xmalloc(sizeof(tag_t));
922 rcs->tags->tags[rcs->tags->ntags]->tag = xstrdup("MAIN");
923 rcs->tags->tags[rcs->tags->ntags]->rev = xmalloc(sizeof(rev_t));
924 rcs->tags->tags[rcs->tags->ntags]->rev->rev = xstrdup(rcs->active->branch->rev);
925 rcs->tags->tags[rcs->tags->ntags]->rev->branch = xstrdup(rcs->active->branch->branch);
926 rcs->tags->tags[rcs->tags->ntags]->rev->isbranch = 1;
927 rcs->tags->ntags++;
928 }
929
930 /* We should have at least two tags (HEAD and MAIN) */
931 assert(rcs->tags != NULL);
932
933 for(i = 0; i < rcs->tags->ntags; i++)
934 {
935 tag_t *t = rcs->tags->tags[i];
936 if(t->rev->isbranch)
937 {
938 branch_t **b;
939 add_btag:
940 b = bsearch(t->rev, rcs->branches, rcs->nbranches, sizeof(rcs->branches[0]), search_branch);
941 if(!b)
942 {
943 rev_t rev;
944 revision_t **r;
945 /* This happens for the magic branch numbers if there are
946 * no commits within the new branch yet. So, we add the
947 * branch and try to continue.
948 */
949 rev.rev = previous_rev(t->rev->branch);
950 rev.branch = NULL;
951 rev.isbranch = 0;
952 r = bsearch(&rev, rcs->srev, rcs->nsrev, sizeof(rcs->srev[0]), search_revision);
953 xfree(rev.rev);
954 if(!r)
955 {
956 stack_msg(MSG_WARN, "No branch found for tag '%s:%s'", t->tag, t->rev->branch);
957 }
958 else
959 {
960 rcs->branches = xrealloc(rcs->branches, (rcs->nbranches+1)*sizeof(rcs->branches[0]));
961 rcs->branches[rcs->nbranches] = xmalloc(sizeof(branch_t));
962 rcs->branches[rcs->nbranches]->branch = dup_rev(t->rev);
963 rcs->branches[rcs->nbranches]->branchpoint = *r;
964 (*r)->branches = xrealloc((*r)->branches, ((*r)->nbranches+1)*sizeof((*r)->branches[0]));
965 (*r)->branches[(*r)->nbranches] = rcs->branches[rcs->nbranches];
966 (*r)->nbranches++;
967 rcs->nbranches++;
968 /* Resort the branches */
969 qsort(rcs->branches, rcs->nbranches, sizeof(rcs->branches[0]), sort_branch);
970 goto add_btag;
971 }
972 }
973 else
974 {
975 branch_t *bb = *b;
976 bb->tags = xrealloc(bb->tags, (bb->ntags+1)*sizeof(bb->tags[0]));
977 bb->tags[bb->ntags] = t;
978 bb->ntags++;
979 }
980 }
981 else
982 {
983 revision_t **r = bsearch(t->rev, rcs->srev, rcs->nsrev, sizeof(rcs->srev[0]), search_revision);
984 if(!r)
985 {
986 stack_msg(MSG_WARN, "No revision found for tag '%s:%s'\n", t->tag, t->rev->rev);
987 }
988 else
989 {
990 revision_t *rr = *r;
991 t->logrev = rr;
992 if(!conf.rev_maxtags || rr->ntags <= conf.rev_maxtags)
993 {
994 rr->tags = xrealloc(rr->tags, (rr->ntags+1)*sizeof(rr->tags[0]));
995 if(conf.rev_maxtags && rr->ntags == conf.rev_maxtags)
996 {
997 rr->tags[rr->ntags] = xmalloc(sizeof(tag_t));
998 rr->tags[rr->ntags]->tag = xstrdup("...");
999 rr->tags[rr->ntags]->rev = t->rev;
1000 }
1001 else
1002 rr->tags[rr->ntags] = t;
1003 rr->ntags++;
1004 }
1005 }
1006
1007 if(conf.tag_negate)
1008 t->ignore++;
1009 /* Mark the tag ignored if it matches the configuration */
1010 if(regextag && !regexec(regextag, t->tag, 0, NULL, 0))
1011 {
1012 if(conf.tag_negate)
1013 t->ignore--;
1014 else
1015 t->ignore++;
1016 }
1017 }
1018 }
1019
1020 /* We need to reset the first in the list of branches to the
1021 * active branch to ensure the drawing of all
1022 */
1023 if(rcs->active != rcs->branches[0])
1024 {
1025 branch_t **b = bsearch(rcs->active->branch, rcs->branches, rcs->nbranches, sizeof(rcs->branches[0]), search_branch);
1026 branch_t *t;
1027 assert(b != NULL);
1028 t = *b;
1029 *b = rcs->branches[0];
1030 rcs->branches[0] = t;
1031 }
1032
1033 if(regextag)
1034 {
1035 regfree(regextag);
1036 xfree(regextag);
1037 }
1038 }
1039
1040 /*
1041 **************************************************************************
1042 * String expansion
1043 **************************************************************************
1044 */
1045 static char *_string;
1046 static int _nstring;
1047 static int _nastring;
1048
1049 static void zap_string(void)
1050 {
1051 _nstring = 0;
1052 if(_string)
1053 _string[0] = '\0';
1054 }
1055
1056 static char *dup_string(void)
1057 {
1058 if(_string)
1059 return xstrdup(_string);
1060 else
1061 return "";
1062 }
1063
1064 static void add_string_str(const char *s)
1065 {
1066 int l = strlen(s) + 1;
1067 if(_nstring + l > _nastring)
1068 {
1069 _nastring += MAX(128, l);
1070 _string = xrealloc(_string, _nastring * sizeof(_string[0]));
1071 }
1072 memcpy(_string+_nstring, s, l);
1073 _nstring += l-1;
1074 }
1075
1076 static void add_string_ch(int ch)
1077 {
1078 char buf[2];
1079 buf[0] = ch;
1080 buf[1] = '\0';
1081 add_string_str(buf);
1082 }
1083
1084 static void add_string_date(const char *d)
1085 {
1086 struct tm tm, *tmp;
1087 int n;
1088 time_t t;
1089 char *buf;
1090 int nbuf;
1091 char *env;
1092
1093 memset(&tm, 0, sizeof(tm));
1094 n = sscanf(d, "%d.%d.%d.%d.%d.%d",
1095 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
1096 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
1097 tm.tm_mon--;
1098 if(tm.tm_year > 1900)
1099 tm.tm_year -= 1900;
1100
1101 env = getenv("TZ");
1102 putenv("TZ=UTC0");
1103 t = mktime(&tm);
1104 if(env)
1105 {
1106 char *c = xmalloc(strlen(env) + 3 + 1); /* Extra space for TZ and = */
1107 sprintf(c, "TZ=%s", env);
1108 putenv(c);
1109 xfree(c);
1110 }
1111 else
1112 putenv("TZ");
1113
1114 if(n != 6 || t == (time_t)(-1))
1115 {
1116 add_string_str("<invalid date>");
1117 return;
1118 }
1119
1120 tmp = localtime(&t);
1121 nbuf = (strlen(conf.date_format)+1) * 16; /* Should be enough to hold all types of expansions */
1122 buf = xmalloc(nbuf);
1123 strftime(buf, nbuf, conf.date_format, tmp);
1124 add_string_str(buf);
1125 xfree(buf);
1126 }
1127
1128 static void add_string_str_html(const char *s, int maxlen)
1129 {
1130 int l = 0;
1131 char *str = xmalloc(6 * strlen(s) + 1); /* Should hold all char entity-expand */
1132 char *cptr = str;
1133 for(; *s; s++)
1134 {
1135 if(maxlen && l > abs(maxlen))
1136 {
1137 cptr += sprintf(cptr, "...");
1138 break;
1139 }
1140 if(*s < 0x20)
1141 {
1142 if(*s == '\n')
1143 {
1144 if(maxlen < 0)
1145 *cptr++ = ' ';
1146 else
1147 cptr += sprintf(cptr, "<br%s>", conf.html_level == HTMLLEVEL_X ? " /" : "");
1148 }
1149 }
1150 else if(*s >= 0x7f || *s == '"')
1151 cptr += sprintf(cptr, "&#%d;", (int)(unsigned char)*s);
1152 else if(*s == '<')
1153 cptr += sprintf(cptr, "&lt;");
1154 else if(*s == '>')
1155 cptr += sprintf(cptr, "&gt;");
1156 else if(*s == '&')
1157 cptr += sprintf(cptr, "&amp;");
1158 else if(*s == '"')
1159 cptr += sprintf(cptr, "&quot;");
1160 else
1161 *cptr++ = *s;
1162 l++;
1163 }
1164 *cptr = '\0';
1165 add_string_str(str);
1166 xfree(str);
1167 }
1168
1169 static void add_string_str_len(const char *s, int maxlen)
1170 {
1171 int l = strlen(s);
1172 char *str = xmalloc(l + 1 + 3);
1173 strcpy(str, s);
1174 if(maxlen < l)
1175 sprintf(&str[maxlen], "...");
1176 add_string_str(str);
1177 xfree(str);
1178 }
1179
1180 static char *expand_string(const char *s, rcsfile_t *rcs, revision_t *r, rev_t *rev, rev_t *prev, tag_t *tag)
1181 {
1182 char nb[32];
1183 char nr[32];
1184 char *base;
1185 char *exp;
1186 int l;
1187 char ch;
1188
1189 if(!s)
1190 return xstrdup("");
1191
1192 zap_string();
1193
1194 sprintf(nb, "%d", rcs->nbranches + rcs->nfolds);
1195 sprintf(nr, "%d", rcs->nsrev);
1196 for(; *s; s++)
1197 {
1198 if(*s == '%')
1199 {
1200 switch(*++s)
1201 {
1202 case 'c':
1203 case 'C':
1204 add_string_str(conf.cvsroot);
1205 if(*s == 'C' && conf.cvsroot[0] && conf.cvsroot[strlen(conf.cvsroot)-1] == '/')
1206 {
1207 /* Strip the trailing '/' */
1208 _nstring--;
1209 _string[_nstring] = '\0';
1210 }
1211 break;
1212 case 'f':
1213 case 'F':
1214 base = strrchr(rcs->file, '/');
1215 if(!base)
1216 add_string_str(rcs->file);
1217 else
1218 add_string_str(base+1);
1219 if(*s == 'F' && _string[_nstring-1] == 'v' && _string[_nstring-2] == ',')
1220 {
1221 _nstring -= 2;
1222 _string[_nstring] = '\0';
1223 }
1224 break;
1225 case 'p':
1226 base = strrchr(rcs->file, '/');
1227 if(base)
1228 {
1229 char *c = xstrdup(rcs->file);
1230 base = strrchr(c, '/');
1231 assert(base != NULL);
1232 base[1] = '\0';
1233 add_string_str(c);
1234 xfree(c);
1235 }
1236 /*
1237 * We should not add anything here because we can encounter
1238 * a completely empty path, in which case we do not want
1239 * to add any slash. This prevents an inadvertent root redirect.
1240 *
1241 * else
1242 * add_string_str("/");
1243 */
1244 break;
1245 case 'm':
1246 case 'M':
1247 add_string_str(conf.cvsmodule);
1248 if(*s == 'M' && conf.cvsmodule[0] && conf.cvsmodule[strlen(conf.cvsmodule)-1] == '/')
1249 {
1250 /* Strip the trailing '/' */
1251 _nstring--;
1252 _string[_nstring] = '\0';
1253 }
1254 break;
1255 case 'r': add_string_str(nr); break;
1256 case 'b': add_string_str(nb); break;
1257 case '%': add_string_ch('%'); break;
1258 case '0': if(conf.expand[0]) add_string_str(conf.expand[0]); break;
1259 case '1': if(conf.expand[1]) add_string_str(conf.expand[1]); break;
1260 case '2': if(conf.expand[2]) add_string_str(conf.expand[2]); break;
1261 case '3': if(conf.expand[3]) add_string_str(conf.expand[3]); break;
1262 case '4': if(conf.expand[4]) add_string_str(conf.expand[4]); break;
1263 case '5': if(conf.expand[5]) add_string_str(conf.expand[5]); break;
1264 case '6': if(conf.expand[6]) add_string_str(conf.expand[6]); break;
1265 case '7': if(conf.expand[7]) add_string_str(conf.expand[7]); break;
1266 case '8': if(conf.expand[8]) add_string_str(conf.expand[8]); break;
1267 case '9': if(conf.expand[9]) add_string_str(conf.expand[9]); break;
1268 case 'R': if(rev && rev->rev) add_string_str(rev->rev); break;
1269 case 'P': if(prev && prev->rev) add_string_str(prev->rev); break;
1270 case 'B': if(rev && rev->branch) add_string_str(rev->branch); break;
1271 case 't': if(tag && tag->tag) add_string_str(tag->tag); break;
1272 case 'd': if(r && r->delta && r->delta->date) add_string_date(r->delta->date); break;
1273 case 's': if(r && r->delta && r->delta->state) add_string_str(r->delta->state); break;
1274 case 'a': if(r && r->delta && r->delta->author) add_string_str(r->delta->author); break;
1275 case 'L':
1276 case 'l':
1277 ch = *s;
1278 l = 0;
1279 if(s[1] == '[')
1280 {
1281 char *cptr = strchr(s, ']');
1282 char *eptr;
1283 if(cptr)
1284 {
1285 l = strtol(&s[2], &eptr, 10);
1286 if(eptr != cptr)
1287 l = 0;
1288 else
1289 s = cptr;
1290 }
1291 }
1292 if(!conf.parse_logs)
1293 add_string_str("N/A");
1294 else if(r && r->dtext && r->dtext->log)
1295 {
1296 if(ch == 'l')
1297 add_string_str_html(r->dtext->log, l);
1298 else
1299 add_string_str_len(r->dtext->log, abs(l));
1300 }
1301 break;
1302 case '(':
1303 base = dup_string();
1304 exp = expand_string(s+1, rcs, r, rev, prev, tag);
1305 zap_string();
1306 add_string_str(base);
1307 add_string_str_html(exp, 0);
1308 xfree(base);
1309 xfree(exp);
1310 /* Find the %) in this recursion level */
1311 for(; *s; s++)
1312 {
1313 if(*s == '%' && s[1] == ')')
1314 {
1315 s++;
1316 break;
1317 }
1318 }
1319 if(!*s)
1320 {
1321 s--; /* To end outer loop */
1322 stack_msg(MSG_WARN, "string expand: Missing %%) in expansion");
1323 }
1324 break;
1325 case ')':
1326 return dup_string();
1327 default:
1328 add_string_ch('%');
1329 add_string_ch(*s);
1330 break;
1331 }
1332 }
1333 else
1334 add_string_ch(*s);
1335 }
1336 return dup_string();
1337 }
1338
1339 /*
1340 **************************************************************************
1341 * Drawing routines
1342 **************************************************************************
1343 */
1344 static color_t *clr_id = NULL;
1345 static int nclr_id = 0;
1346
1347 static color_t *clr(gdImagePtr im, const char *s, revision_t *r, branch_t *b, int idx)
1348 {
1349 int i;
1350 color_t *c = get_colorref(s, idx);
1351 if(!c)
1352 c = &black_color;
1353 for(i = 0; i < nclr_id; i++)
1354 {
1355 if(c->r == clr_id[i].r && c->g == clr_id[i].g && c->b == clr_id[i].b)
1356 return &clr_id[i];
1357 }
1358 /* FIXME: Do color evaluation */
1359 clr_id = xrealloc(clr_id, (nclr_id+1) * sizeof(*clr_id));
1360 clr_id[nclr_id] = *c;
1361 clr_id[nclr_id].id = gdImageColorAllocate(im, c->r, c->g, c->b);
1362 return &clr_id[nclr_id++];
1363 }
1364
1365 static void zap_clr(void)
1366 {
1367 if(clr_id)
1368 xfree(clr_id);
1369 clr_id = NULL;
1370 nclr_id = 0;
1371 }
1372
1373 static int get_swidth(const char *s, font_t *f)
1374 {
1375 int n;
1376 int m;
1377 if(!s || !*s)
1378 return 0;
1379
1380 #if defined(HAVE_GDIMAGESTRINGFT) || defined(HAVE_GDIMAGESTRINGTTF)
1381 if(conf.use_ttf && f->ttfont)
1382 {
1383 int bb[8];
1384 char *e;
1385 #ifdef HAVE_GDIMAGESTRINGFT
1386 e = gdImageStringFT(NULL, bb, 0, f->ttfont, f->ttsize, 0.0, 0, 0, (char *)s);
1387 #else
1388 e = gdImageStringTTF(NULL, bb, 0, f->ttfont, f->ttsize, 0.0, 0, 0, (char *)s);
1389 #endif
1390 if(!e)
1391 return bb[2] - bb[6];
1392 }
1393 #endif
1394 for(n = m = 0; *s; n++, s++)
1395 {
1396 if(*s == '\n')
1397 {
1398 if(n > m)
1399 m = n;
1400 n = 0;
1401 }
1402 }
1403 if(n > m)
1404 m = n;
1405 return f->gdfont ? m * f->gdfont->w : m;
1406 }
1407
1408 static int get_sheight(const char *s, font_t *f)
1409 {
1410 int nl;
1411 if(!s || !*s)
1412 return 0;
1413
1414 #if defined(HAVE_GDIMAGESTRINGFT) || defined(HAVE_GDIMAGESTRINGTTF)
1415 if(conf.use_ttf && f->ttfont)
1416 {
1417 int bb[8];
1418 char *e;
1419 #ifdef HAVE_GDIMAGESTRINGFT
1420 e = gdImageStringFT(NULL, bb, 0, f->ttfont, f->ttsize, 0.0, 0, 0, (char *)s);
1421 #else
1422 e = gdImageStringTTF(NULL, bb, 0, f->ttfont, f->ttsize, 0.0, 0, 0, (char *)s);
1423 #endif
1424 if(!e)
1425 return bb[3] - bb[7] + 4;
1426 }
1427 #endif
1428 for(nl = 1; *s; s++)
1429 {
1430 if(*s == '\n' && s[1])
1431 nl++;
1432 }
1433 return nl * f->gdfont->h;
1434 }
1435
1436 static void draw_rbox(gdImagePtr im, int x1, int y1, int x2, int y2, int r, color_t *color, color_t *bgcolor)
1437 {
1438 int r2 = 2*r;
1439 if(!r)
1440 gdImageFilledRectangle(im, x1, y1, x2, y2, bgcolor->id);
1441 #ifdef HAVE_GDIMAGEFILLEDARC
1442 else
1443 {
1444 gdImageFilledArc(im, x1+r, y1+r, r2, r2, 180, 270, bgcolor->id, gdArc);
1445 gdImageFilledArc(im, x2-r, y1+r, r2, r2, 270, 360, bgcolor->id, gdArc);
1446 gdImageFilledArc(im, x1+r, y2-r, r2, r2, 90, 180, bgcolor->id, gdArc);
1447 gdImageFilledArc(im, x2-r, y2-r, r2, r2, 0, 90, bgcolor->id, gdArc);
1448 gdImageFilledRectangle(im, x1+r, y1, x2-r, y1+r, bgcolor->id);
1449 gdImageFilledRectangle(im, x1, y1+r, x2, y2-r, bgcolor->id);
1450 gdImageFilledRectangle(im, x1+r, y2-r, x2-r, y2, bgcolor->id);
1451 }
1452 #endif
1453 gdImageLine(im, x1+r, y1, x2-r, y1, color->id);
1454 gdImageLine(im, x1+r, y2, x2-r, y2, color->id);
1455 gdImageLine(im, x1, y1+r, x1, y2-r, color->id);
1456 gdImageLine(im, x2, y1+r, x2, y2-r, color->id);
1457 if(conf.box_shadow)
1458 {
1459 gdImageLine(im, x1+r+1, y2+1, x2-r, y2+1, black_color.id);
1460 gdImageLine(im, x2+1, y1+r+1, x2+1, y2-r, black_color.id);
1461 }
1462 if(r)
1463 {
1464 /* FIXME: Pixelization is not perfect */
1465 gdImageArc(im, x1+r, y1+r, r2, r2, 180, 270, color->id);
1466 gdImageArc(im, x2-r, y1+r, r2, r2, 270, 360, color->id);
1467 gdImageArc(im, x1+r, y2-r, r2, r2, 90, 180, color->id);
1468 if(conf.box_shadow)
1469 {
1470 gdImageArc(im, x2-r+1, y2-r+1, r2, r2, 0, 90, black_color.id);
1471 gdImageArc(im, x2-r+1, y2-r, r2, r2, 0, 90, black_color.id);
1472 gdImageArc(im, x2-r, y2-r+1, r2, r2, 0, 90, black_color.id);
1473 }
1474 gdImageArc(im, x2-r, y2-r, r2, r2, 0, 90, color->id);
1475 #if !defined(NOGDFILL) && !defined(HAVE_GDIMAGEFILLEDARC)
1476 /* BUG: We clip manually because libgd segfaults on out of bound values */
1477 if((x1+x2)/2 >= 0 && (x1+x2)/2 < gdImageSX(im) && (y1+y2)/2 >= 0 && (y1+y2)/2 < gdImageSY(im))
1478 gdImageFillToBorder(im, (x1+x2)/2, (y1+y2)/2, color->id, bgcolor->id);
1479 #endif
1480 }
1481 }
1482
1483 static void draw_string(gdImagePtr im, char *s, font_t *f, int x, int y, int align, color_t *c)
1484 {
1485 int h = get_sheight(s, f);
1486 int xx, yy;
1487 switch(align & ALIGN_HX)
1488 {
1489 default:
1490 case ALIGN_HL: xx = 0; break;
1491 case ALIGN_HC: xx = -get_swidth(s, f)/2; break;
1492 case ALIGN_HR: xx = -get_swidth(s, f); break;
1493 }
1494 switch(align & ALIGN_VX)
1495 {
1496 default:
1497 case ALIGN_VT: yy = 0; break;
1498 case ALIGN_VC: yy = h/2; break;
1499 case ALIGN_VB: yy = h; break;
1500 }
1501 #if defined(HAVE_GDIMAGESTRINGFT) || defined(HAVE_GDIMAGESTRINGTTF)
1502 if(conf.use_ttf && f->ttfont)
1503 {
1504 int bb[8];
1505 char *e;
1506 int cid = conf.anti_alias ? c->id : -c->id;
1507 #ifdef HAVE_GDIMAGESTRINGFT
1508 e = gdImageStringFT(im, bb, cid, f->ttfont, f->ttsize, 0.0, x+xx, y+yy+h-2, (char *)s);
1509 #else
1510 e = gdImageStringTTF(im, bb, cid, f->ttfont, f->ttsize, 0.0, x+xx, y+yy+h-2, (char *)s);
1511 #endif
1512 if(!e)
1513 return;
1514 }
1515 #endif
1516 yy = -yy;
1517 gdImageString(im, f->gdfont, x+xx+1, y+yy, (unsigned char *)s, c->id);
1518 }
1519
1520 static void draw_stringnl(gdImagePtr im, char *s, font_t *f, int x, int y, int align, color_t *c)
1521 {
1522 char *t;
1523 char *d;
1524 d = s = xstrdup(s);
1525 do
1526 {
1527 t = strchr(s, '\n');
1528 if(t)
1529 *t = '\0';
1530 draw_string(im, s, f, x, y, align, c);
1531 y += get_sheight(s, f);
1532 s = t+1;
1533 } while(t);
1534 xfree(d);
1535 }
1536
1537 static void draw_rev(gdImagePtr im, revision_t *r)
1538 {
1539 int lx;
1540 int rx;
1541 int x2;
1542 int i;
1543 int ty;
1544
1545 if(conf.left_right)
1546 {
1547 lx = r->cx;
1548 rx = r->cx + r->w;
1549 ty = r->y - r->h/2;
1550 x2 = r->cx + r->w/2;
1551 }
1552 else
1553 {
1554 lx = r->cx - r->w/2;
1555 rx = lx + r->w;
1556 ty = r->y;
1557 x2 = r->cx;
1558 }
1559 draw_rbox(im, lx, ty, rx, ty+r->h, 0, clr(im, "rev_color", r, NULL, 0), clr(im, "rev_bgcolor", r, NULL, 0));
1560 ty += conf.rev_tspace;
1561 if(!conf.rev_hidenumber)
1562 {
1563 draw_string(im, r->rev->rev, &conf.rev_font, x2, ty, ALIGN_HC, clr(im, "rev_color", r, NULL, 0));
1564 ty += get_sheight(r->rev->rev, &conf.rev_font);
1565 }
1566 draw_stringnl(im, r->revtext, &conf.rev_text_font, x2, ty, ALIGN_HC, clr(im, "rev_text_color", r, NULL, 0));
1567 ty += get_sheight(r->revtext, &conf.rev_text_font);
1568 for(i = 0; i < r->ntags; i++)
1569 {
1570 draw_string(im, r->tags[i]->tag, &conf.tag_font, x2, ty, ALIGN_HC, clr(im, "tag_color", r, NULL, 0));
1571 ty += get_sheight(r->tags[i]->tag, &conf.tag_font) + conf.rev_separator;
1572 }
1573 }
1574
1575 static void draw_branch_box(gdImagePtr im, branch_t *b, int xp, int yp)
1576 {
1577 int lx;
1578 int rx;
1579 int i;
1580 int yy;
1581 int x2;
1582
1583 if(conf.left_right)
1584 {
1585 lx = b->cx;
1586 rx = lx + b->w;
1587 x2 = b->cx + b->w/2;
1588 }
1589 else
1590 {
1591 lx = b->cx - b->w/2;
1592 rx = lx + b->w;
1593 x2 = b->cx;
1594 }
1595 draw_rbox(im, lx+xp, yp, rx+xp, yp+b->h, 5, clr(im, "branch_color", NULL, b, 0), clr(im, "branch_bgcolor", NULL, b, 0));
1596 yy = conf.branch_tspace;
1597 if(!b->nfolds)
1598 {
1599 if(!conf.rev_hidenumber)
1600 {
1601 draw_string(im, b->branch->branch, &conf.branch_font, x2+xp, yp+yy, ALIGN_HC, clr(im, "branch_color", NULL, b, 0));
1602 yy += get_sheight(b->branch->branch, &conf.branch_font);
1603 }
1604 for(i = 0; i < b->ntags; i++)
1605 {
1606 draw_string(im, b->tags[i]->tag, &conf.branch_tag_font, x2+xp, yp+yy, ALIGN_HC, clr(im, "branch_tag_color", NULL, b, 0));
1607 yy += get_sheight(b->tags[i]->tag, &conf.branch_tag_font);
1608 }
1609 }
1610 else
1611 {
1612 int y1, y2;
1613 int tx = lx + b->fw + conf.branch_lspace;
1614 int nx = tx - get_swidth(" ", &conf.branch_font);
1615 draw_string(im, b->branch->branch, &conf.branch_font, nx+xp, yp+yy, ALIGN_HR, clr(im, "branch_color", NULL, b, 0));
1616 y1 = get_sheight(b->branch->branch, &conf.branch_font);
1617 draw_string(im, b->tags[0]->tag, &conf.branch_tag_font, tx+xp, yp+yy, ALIGN_HL, clr(im, "branch_tag_color", NULL, b, 0));
1618 y2 = get_sheight(b->tags[0]->tag, &conf.branch_font);
1619 yy += MAX(y1, y2);
1620 for(i = 0; i < b->nfolds; i++)
1621 {
1622 draw_string(im, b->folds[i]->branch->branch, &conf.branch_font, nx+xp, yp+yy, ALIGN_HR, clr(im, "branch_color", NULL, b, 0));
1623 y1 = get_sheight(b->folds[i]->branch->branch, &conf.branch_font);
1624 draw_string(im, b->folds[i]->tags[0]->tag, &conf.branch_tag_font, tx+xp, yp+yy, ALIGN_HL, clr(im, "branch_tag_color", NULL, b, 0));
1625 y2 = get_sheight(b->folds[i]->tags[0]->tag, &conf.branch_tag_font);
1626 yy += MAX(y1, y2);
1627 }
1628 }
1629 }
1630
1631 static void draw_branch(gdImagePtr im, branch_t *b)
1632 {
1633 int yy, xx;
1634 int i;
1635 int line[4];
1636 int l;
1637 int sign;
1638
1639 line[1] = line[2] = gdTransparent;
1640
1641 /* Trivial clip the branch */
1642 if(conf.left_right)
1643 {
1644 if(b->cx > gdImageSX(im) || b->cx+b->tw < 0 || b->y-b->th/2 > gdImageSY(im) || b->y+b->th/2 < 0)
1645 return;
1646 }
1647 else
1648 {
1649 if(b->cx-b->tw/2 > gdImageSX(im) || b->cx+b->tw/2 < 0 || b->y > gdImageSY(im) || b->y+b->th < 0)
1650 return;
1651 }
1652
1653 draw_branch_box(im, b, 0, conf.left_right ? b->y - b->h/2 : b->y);
1654
1655 if(conf.left_right)
1656 {
1657 if(conf.upside_down)
1658 {
1659 xx = b->cx;
1660 for(i = 0; i < b->nrevs; i++)
1661 {
1662 revision_t *r = b->revs[i];
1663 line[0] = line[3] = clr(im, "rev_color", r, b, 0)->id;
1664 gdImageSetStyle(im, line, r->stripped > 0 ? 4 : 1);
1665 gdImageLine(im, xx, r->y, r->cx+r->w, r->y, gdStyled);
1666 for(sign = l = 1; l < conf.thick_lines; l++)
1667 {
1668 int pp = (l+1)/2*sign;
1669 gdImageLine(im, xx, r->y+pp, r->cx+r->w, r->y+pp, gdStyled);
1670 sign *= -1;
1671 }
1672 draw_rev(im, r);
1673 xx = r->cx;
1674 }
1675 if(conf.branch_dupbox && b->nrevs)
1676 {
1677 i = b->cx - b->tw + b->w;
1678 gdImageLine(im, xx, b->y, i+b->w, b->y, clr(im, "rev_color", NULL, b, 0)->id);
1679 for(sign = l = 1; l < conf.thick_lines; l++)
1680 {
1681 int pp = (l+1)/2*sign;
1682 gdImageLine(im, xx, b->y+pp, i+b->w, b->y+pp, clr(im, "rev_color", NULL, b, 0)->id);
1683 sign *= -1;
1684 }
1685 draw_branch_box(im, b, i - b->cx, b->y - b->h/2);
1686 }
1687 }
1688 else
1689 {
1690 xx = b->cx + b->w;
1691 for(i = 0; i < b->nrevs; i++)
1692 {
1693 revision_t *r = b->revs[i];
1694 line[0] = line[3] = clr(im, "rev_color", r, b, 0)->id;
1695 gdImageSetStyle(im, line, r->stripped > 0 ? 4 : 1);
1696 gdImageLine(im, xx, r->y, r->cx, r->y, gdStyled);
1697 for(sign = l = 1; l < conf.thick_lines; l++)
1698 {
1699 int pp = (l+1)/2*sign;
1700 gdImageLine(im, xx, r->y+pp, r->cx, r->y+pp, gdStyled);
1701 sign *= -1;
1702 }
1703 draw_rev(im, r);
1704 xx = r->cx + r->w;
1705 }
1706 if(conf.branch_dupbox && b->nrevs)
1707 {
1708 i = b->cx + b->tw - b->w;
1709 gdImageLine(im, xx, b->y, i, b->y, clr(im, "rev_color", NULL, b, 0)->id);
1710 for(sign = l = 1; l < conf.thick_lines; l++)
1711 {
1712 int pp = (l+1)/2*sign;
1713 gdImageLine(im, xx, b->y+pp, i, b->y+pp, clr(im, "rev_color", NULL, b, 0)->id);
1714 sign *= -1;
1715 }
1716 draw_branch_box(im, b, i - b->cx, b->y - b->h/2);
1717 }
1718 }
1719 }
1720 else
1721 {
1722 if(conf.upside_down)
1723 {
1724 yy = b->y;
1725 for(i = 0; i < b->nrevs; i++)
1726 {
1727 revision_t *r = b->revs[i];
1728 line[0] = line[3] = clr(im, "rev_color", r, b, 0)->id;
1729 gdImageSetStyle(im, line, r->stripped > 0 ? 4 : 1);
1730 gdImageLine(im, r->cx, yy, r->cx, r->y+r->h, gdStyled);
1731 for(sign = l = 1; l < conf.thick_lines; l++)
1732 {
1733 int pp = (l+1)/2*sign;
1734 gdImageLine(im, r->cx+pp, yy, r->cx+pp, r->y+r->h, gdStyled);
1735 sign *= -1;
1736 }
1737 draw_rev(im, r);
1738 yy = r->y;
1739 }
1740 if(conf.branch_dupbox && b->nrevs)
1741 {
1742 i = b->y - b->th + b->h;
1743 gdImageLine(im, b->cx, yy, b->cx, i, clr(im, "rev_color", NULL, b, 0)->id);
1744 for(sign = l = 1; l < conf.thick_lines; l++)
1745 {
1746 int pp = (l+1)/2*sign;
1747 gdImageLine(im, b->cx+pp, yy, b->cx+pp, i, clr(im, "rev_color", NULL, b, 0)->id);
1748 sign *= -1;
1749 }
1750 draw_branch_box(im, b, 0, i);
1751 }
1752 }
1753 else
1754 {
1755 yy = b->y + b->h;
1756 for(i = 0; i < b->nrevs; i++)
1757 {
1758 revision_t *r = b->revs[i];
1759 line[0] = line[3] = clr(im, "rev_color", r, b, 0)->id;
1760 gdImageSetStyle(im, line, r->stripped > 0 ? 4 : 1);
1761 gdImageLine(im, r->cx, yy, r->cx, r->y, gdStyled);
1762 for(sign = l = 1; l < conf.thick_lines; l++)
1763 {
1764 int pp = (l+1)/2*sign;
1765 gdImageLine(im, r->cx+pp, yy, r->cx+pp, r->y, gdStyled);
1766 sign *= -1;
1767 }
1768 draw_rev(im, r);
1769 yy = r->y + r->h;
1770 }
1771 if(conf.branch_dupbox && b->nrevs)
1772 {
1773 i = b->y + b->th - b->h;
1774 gdImageLine(im, b->cx, yy, b->cx, i, clr(im, "rev_color", NULL, b, 0)->id);
1775 for(sign = l = 1; l < conf.thick_lines; l++)
1776 {
1777 int pp = (l+1)/2*sign;
1778 gdImageLine(im, b->cx+pp, yy, b->cx+pp, i, clr(im, "rev_color", NULL, b, 0)->id);
1779 sign *= -1;
1780 }
1781 draw_branch_box(im, b, 0, i);
1782 }
1783 }
1784 }
1785 }
1786
1787 static void draw_connector(gdImagePtr im, branch_t *b)
1788 {
1789 int l;
1790 int sign;
1791 revision_t *r = b->branchpoint;
1792 int x1 = r->cx + r->w/2 + 2;
1793 int y1 = r->y + r->h/2;
1794 int x2 = b->cx;
1795 int y2 = b->y;
1796
1797 if(conf.left_right)
1798 {
1799 x2 = r->cx + r->w/2;
1800 y2 = r->y + r->h/2 + 3;
1801 x1 = b->cx;
1802 y1 = b->y;
1803 if(conf.upside_down)
1804 x1 += b->w;
1805 }
1806 else
1807 {
1808 x1 = r->cx + r->w/2 + 2;
1809 y1 = r->y + r->h/2;
1810 x2 = b->cx;
1811 y2 = b->y;
1812 if(conf.upside_down)
1813 y2 += b->h;
1814 }
1815 gdImageLine(im, x1, y1, x2, y1, clr(im, "branch_color", NULL, b, 0)->id);
1816 gdImageLine(im, x2, y1, x2, y2, clr(im, "branch_color", NULL, b, 0)->id);
1817 for(sign = l = 1; l < conf.thick_lines; l++)
1818 {
1819 int pp = (l+1)/2*sign;
1820 gdImageLine(im, x1, y1+pp, x2, y1+pp, clr(im, "branch_color", NULL, b, 0)->id);
1821 gdImageLine(im, x2+pp, y1, x2+pp, y2, clr(im, "branch_color", NULL, b, 0)->id);
1822 sign *= -1;
1823 }
1824 }
1825
1826 static void draw_merges(gdImagePtr im, rcsfile_t *rcs, int dot)
1827 {
1828 int i;
1829 for(i = 0; i < rcs->nmerges; i++)
1830 {
1831 revision_t *fr;
1832 revision_t *tr;
1833 int colorid;
1834 int x1, x2, y1, y2;
1835 switch(rcs->merges[i].type)
1836 {
1837 case TR_TAG:
1838 fr = rcs->merges[i].from.tag->logrev;
1839 tr = rcs->merges[i].to.tag->logrev;
1840 colorid = clr(im, "merge_color", NULL, NULL, rcs->merges[i].clr)->id;
1841 break;
1842 case TR_REVISION:
1843 fr = rcs->merges[i].from.rev;
1844 tr = rcs->merges[i].to.rev;
1845 colorid = clr(im, "merge_cvsnt_color", NULL, NULL, 0)->id;
1846 break;
1847 default:
1848 continue;
1849 }
1850 if(!fr || !tr || fr == tr)
1851 continue; /* This can happen with detached tags and self-references */
1852 if(conf.left_right)
1853 {
1854 if(fr->branch == tr->branch)
1855 {
1856 y1 = fr->y - fr->h/2;
1857 y2 = tr->y - tr->h/2;
1858 }
1859 else
1860 {
1861 if(fr->y < tr->y)
1862 {
1863 y1 = fr->y + fr->h/2;
1864 y2 = tr->y - tr->h/2;
1865 }
1866 else
1867 {
1868 y1 = fr->y - fr->h/2;
1869 y2 = tr->y + tr->h/2;
1870 }
1871 }
1872 x1 = fr->cx + fr->w/2;
1873 x2 = tr->cx + tr->w/2;
1874 }
1875 else
1876 {
1877 if(fr->branch == tr->branch)
1878 {
1879 x1 = fr->cx - fr->w/2;
1880 x2 = tr->cx - tr->w/2;
1881 }
1882 else
1883 {
1884 if(fr->cx < tr->cx)
1885 {
1886 x1 = fr->cx + fr->w/2;
1887 x2 = tr->cx - tr->w/2;
1888 }
1889 else
1890 {
1891 x1 = fr->cx - fr->w/2;
1892 x2 = tr->cx + tr->w/2;
1893 }
1894 }
1895 if(rcs->merges[i].type == TR_TAG)
1896 {
1897 y1 = fr->y + rcs->merges[i].from.tag->yofs;
1898 y2 = tr->y + rcs->merges[i].to.tag->yofs;
1899 }
1900 else
1901 {
1902 y1 = fr->y + fr->h/2;
1903 y2 = tr->y + tr->h/2;
1904 }
1905 }
1906 if(dot && !conf.merge_arrows)
1907 {
1908 int o = conf.left_right ? 1 : 0;
1909 gdImageArc(im, x2, y2+o, 8, 8, 0, 360, colorid);
1910 /* BUG: We clip manually because libgd segfaults on out of bound values */
1911 if(x2+1 >= 0 && x2+1 < gdImageSX(im) && y2+o+1 >= 0 && y2+o+1 < gdImageSY(im))
1912 gdImageFillToBorder(im, x2+1, y2+o+1, colorid, colorid);
1913 }
1914 else if(dot && conf.merge_arrows)
1915 {
1916 /*
1917 * Arrow patch from Haroon Rafique <haroon.rafique@utoronto.ca>
1918 * Slightly adapted to be more configurable.
1919 */
1920 int sx, sy; /* start point coordinates */
1921 int ex, ey; /* end point coordinates */
1922 double theta;
1923 double u1, v1, u2, v2;
1924 gdPoint p[3];
1925
1926 sx = x1; sy = y1;
1927 ex = x2; ey = y2;
1928 if(conf.left_right)
1929 {
1930 if(fr->branch == tr->branch)
1931 {
1932 int yy = (y1 < y2 ? y1 : y2) - 5;
1933 /* line from (x1,yy) to (x2,yy) */
1934 sy = ey = yy;
1935 }
1936 else
1937 {
1938 if(y1 > y2)
1939 {
1940 /* line from (x1,y1-3) to (x2,y2+3+1) */
1941 sy = y1-3;
1942 ey = y2+3+1;
1943 }
1944 else
1945 {
1946 /* line from (x1,y1+3+1) to (x2,y2-3) */
1947 sy = y1+3+1;
1948 ey = y2-3;
1949 }
1950 }
1951 }
1952 else
1953 {
1954 if(fr->branch == tr->branch)
1955 {
1956 int xx = (x1 < x2 ? x1 : x2) - 5;
1957 /* line from (xx,y1) to (xx,y2) */
1958 sx = ex = xx;
1959 }
1960 else
1961 {
1962 if(x1 > x2)
1963 {
1964 /* line from (x1-3,y1) to (x2+3,y2) */
1965 sx = x1-3;
1966 ex = x2+3;
1967 }
1968 else
1969 {
1970 /* line from (x1+3,y1) to (x2-3,y2) */
1971 sx = x1+3;
1972 ex = x2-3;
1973 }
1974 }
1975 }
1976 /*
1977 * inspiration for arrow code comes from arrows.c in the
1978 * graphviz package. Thank you, AT&T
1979 */
1980 /* theta in radians */
1981 theta = atan2((double)(sy-ey), (double)(sx-ex));
1982 u1 = (double)conf.arrow_length * cos(theta);
1983 v1 = (double)conf.arrow_length * sin(theta);
1984 u2 = (double)conf.arrow_width * cos(theta + M_PI/2.0);
1985 v2 = (double)conf.arrow_width * sin(theta + M_PI/2.0);
1986 /* points of polygon (triangle) */
1987 p[0].x = ROUND(ex + u1 - u2);
1988 p[0].y = ROUND(ey + v1 - v2);
1989 p[1].x = ex;
1990 p[1].y = ey;
1991 p[2].x = ROUND(ex + u1 + u2);
1992 p[2].y = ROUND(ey + v1 + v2);
1993 /* draw the polygon (triangle) */
1994 gdImageFilledPolygon(im, p, 3, colorid);
1995 }
1996 else
1997 {
1998 if(conf.left_right)
1999 {
2000 if(fr->branch == tr->branch)
2001 {
2002 int yy = (y1 < y2 ? y1 : y2) - 5;
2003 gdImageLine(im, x1, y1, x1, yy, colorid);
2004 gdImageLine(im, x2, y2, x2, yy, colorid);
2005 gdImageLine(im, x1, yy, x2, yy, colorid);
2006 }
2007 else
2008 {
2009 if(y1 > y2)
2010 {
2011 gdImageLine(im, x1, y1, x1, y1-3, colorid);
2012 gdImageLine(im, x2, y2+1, x2, y2+3+1, colorid);
2013 gdImageLine(im, x1, y1-3, x2, y2+3+1, colorid);
2014 }
2015 else
2016 {
2017 gdImageLine(im, x1, y1+1, x1, y1+3+1, colorid);
2018 gdImageLine(im, x2, y2, x2, y2-3, colorid);
2019 gdImageLine(im, x1, y1+3+1, x2, y2-3, colorid);
2020 }
2021 }
2022 }
2023 else
2024 {
2025 if(fr->branch == tr->branch)
2026 {
2027 int xx = (x1 < x2 ? x1 : x2) - 5;
2028 gdImageLine(im, xx, y1, x1, y1, colorid);
2029 gdImageLine(im, xx, y2, x2, y2, colorid);
2030 gdImageLine(im, xx, y1, xx, y2, colorid);
2031 }
2032 else
2033 {
2034 if(x1 > x2)
2035 {
2036 gdImageLine(im, x1, y1, x1-3, y1, colorid);
2037 gdImageLine(im, x2, y2, x2+3, y2, colorid);
2038 gdImageLine(im, x1-3, y1, x2+3, y2, colorid);
2039 }
2040 else
2041 {
2042 gdImageLine(im, x1, y1, x1+3, y1, colorid);
2043 gdImageLine(im, x2, y2, x2-3, y2, colorid);
2044 gdImageLine(im, x1+3, y1, x2-3, y2, colorid);
2045 }
2046 }
2047 }
2048 }
2049 }
2050 }
2051
2052 static void draw_messages(gdImagePtr im, int offset)
2053 {
2054 int i;
2055
2056 for(i = 0; i < nmsg_stack; i++)
2057 {
2058 draw_stringnl(im, msg_stack[i].msg, &conf.msg_font, conf.margin_left, offset, ALIGN_HL|ALIGN_VT, clr(im, "msg_color", NULL, NULL, 0));
2059 offset += msg_stack[i].h;
2060 }
2061 }
2062
2063 static gdImagePtr make_image(rcsfile_t *rcs)
2064 {
2065 gdImagePtr im;
2066 int i;
2067 int bgid;
2068 char *cptr;
2069 int w, h;
2070 int subx = 0, suby = 0;
2071 int msgh = 0;
2072
2073 if(subtree_branch)
2074 {
2075 w = 0;
2076 h = 0;
2077 if(subtree_rev)
2078 {
2079 for(i = 0; i < subtree_rev->nbranches; i++)
2080 calc_subtree_size(subtree_rev->branches[i], &subx, &suby, &w, &h);
2081 }
2082 else
2083 calc_subtree_size(subtree_branch, &subx, &suby, &w, &h);
2084 }
2085 else
2086 {
2087 w = rcs->tw;
2088 h = rcs->th;
2089 }
2090
2091 cptr = expand_string(conf.title, rcs, NULL, NULL, NULL, NULL);
2092 i = get_swidth(cptr, &conf.title_font);
2093 if(i > w)
2094 w = i;
2095
2096 if(!quiet && nmsg_stack)
2097 {
2098 int msgw = 0;
2099 for(i = 0; i < nmsg_stack; i++)
2100 {
2101 int ww = msg_stack[i].w = get_swidth(msg_stack[i].msg, &conf.msg_font);
2102 int hh = msg_stack[i].h = get_sheight(msg_stack[i].msg, &conf.msg_font);
2103 msgh += hh;
2104 h += hh;
2105 if(ww > msgw)
2106 msgw = ww;
2107 }
2108 if(msgw > w)
2109 w = msgw;
2110 }
2111
2112 w += conf.margin_left + conf.margin_right;
2113 h += conf.margin_top + conf.margin_bottom;
2114
2115 im = gdImageCreate(w, h);
2116 bgid = clr(im, "color_bg", NULL, NULL, 0)->id; /* The background is always a unique color, */
2117 zap_clr(); /* so clear the color ref table */
2118 clr(im, "__black_color__", NULL, NULL, 0);
2119
2120 if(conf.transparent_bg)
2121 gdImageColorTransparent(im, bgid);
2122
2123 if(!conf.merge_front)
2124 draw_merges(im, rcs, 0);
2125
2126 for(i = 0; i < rcs->nbranches; i++)
2127 {
2128 if(!rcs->branches[i]->folded && !(subtree_branch && !rcs->branches[i]->subtree_draw))
2129 draw_branch(im, rcs->branches[i]);
2130 }
2131
2132 draw_merges(im, rcs, 1); /* The dots of the merge dest */
2133
2134 for(i = 0; i < rcs->nbranches; i++)
2135 {
2136 if(rcs->branches[i]->branchpoint)
2137 draw_connector(im, rcs->branches[i]);
2138 }
2139
2140 /* Clear the margins if we have a partial tree */
2141 if(subtree_branch)
2142 {
2143 gdImageFilledRectangle(im, 0, 0, w-1, conf.margin_top-1, bgid);
2144 gdImageFilledRectangle(im, 0, 0, conf.margin_left-1, h-1, bgid);
2145 gdImageFilledRectangle(im, 0, h-conf.margin_bottom, w-1, h-1, bgid);
2146 gdImageFilledRectangle(im, w-conf.margin_right, 0, w-1, h-1, bgid);
2147 }
2148
2149 draw_stringnl(im, cptr, &conf.title_font, conf.title_x, conf.title_y, conf.title_align, clr(im, "title_color", NULL, NULL, 0));
2150 xfree(cptr);
2151
2152 if(conf.merge_front)
2153 draw_merges(im, rcs, 0);
2154
2155 if(!quiet)
2156 draw_messages(im, h - conf.margin_bottom/2 - msgh);
2157
2158 return im;
2159 }
2160
2161 /*
2162 **************************************************************************
2163 * Layout routines
2164 *
2165 * Branch BBox:
2166 * left = center_x - total_width / 2 (cx-tw)/2
2167 * right = center_x + total_width / 2 (cx+tw)/2
2168 * top = y_pos (y)
2169 * bottom = y_pos + total_height (y+th)
2170 *
2171 * Margins of branches:
2172 *
2173 * . .
2174 * . .
2175 * +--------------+
2176 * ^
2177 * | branch_margin .
2178 * v .
2179 * ----------------+ .
2180 * | ^ |
2181 * | | branch_connect |
2182 * | v |
2183 *..-+ +t-----+------+ +------+------+
2184 * | l | | |
2185 * | <--> | branch bbox | <--> | branch bbox |
2186 * | | | r | | |
2187 *..-+ | +------------b+ | +-------------+
2188 * | ^ branch_margin
2189 * | | branch_margin
2190 * | v
2191 * | +-------------+
2192 * | . .
2193 * | . .
2194 * |
2195 * branch_margin
2196 *
2197 * FIXME: There are probable som +/-1 errors in the code...
2198 * (notably shadows are not calculated in the margins)
2199 **************************************************************************
2200 */
2201 static void move_branch(branch_t *b, int x, int y)
2202 {
2203 int i;
2204 b->cx += x;
2205 b->y += y;
2206 for(i = 0; i < b->nrevs; i++)
2207 {
2208 b->revs[i]->cx += x;
2209 b->revs[i]->y += y;
2210 }
2211 }
2212
2213 static void initial_reposition_branch(revision_t *r, int *x, int *w)
2214 {
2215 int i, j;
2216 for(j = 0; j < r->nbranches; j++)
2217 {
2218 branch_t *b = r->branches[j];
2219 *x += *w + conf.rev_minline + b->tw/2 - b->cx;
2220 *w = b->tw/2;
2221 move_branch(b, *x, r->y + r->h/2 + conf.branch_connect);
2222 *x = b->cx;
2223 /* Recurse to move branches of branched revisions */
2224 for(i = b->nrevs-1; i >= 0; i--)
2225 {
2226 initial_reposition_branch(b->revs[i], x, w);
2227 }
2228 }
2229 }
2230
2231 static void initial_reposition_branch_lr(revision_t *r, int *y, int *h)
2232 {
2233 int i, j;
2234 for(j = 0; j < r->nbranches; j++)
2235 {
2236 branch_t *b = r->branches[j];
2237 *y += *h + conf.rev_minline + b->th/2 - b->y;
2238 *h = b->th/2;
2239 move_branch(b, r->cx + r->w/2 + conf.branch_connect, *y);
2240 *y = b->y;
2241 /* Recurse to move branches of branched revisions */
2242 for(i = b->nrevs-1; i >= 0; i--)
2243 {
2244 initial_reposition_branch_lr(b->revs[i], y, h);
2245 }
2246 }
2247 }
2248
2249 static void rect_union(int *x, int *y, int *w, int *h, branch_t *b)
2250 {
2251 int x1 = *x;
2252 int x2 = x1 + *w;
2253 int y1 = *y;
2254 int y2 = y1 + *h;
2255 int xx1;
2256 int xx2;
2257 int yy1;
2258 int yy2;
2259
2260 if(conf.left_right)
2261 {
2262 xx1 = b->cx;
2263 yy1 = b->y - b->th/2;
2264 }
2265 else
2266 {
2267 xx1 = b->cx - b->tw/2;
2268 yy1 = b->y;
2269 }
2270 xx2 = xx1 + b->tw;
2271 yy2 = yy1 + b->th;
2272
2273 x1 = MIN(x1, xx1);
2274 x2 = MAX(x2, xx2);
2275 y1 = MIN(y1, yy1);
2276 y2 = MAX(y2, yy2);
2277 *x = x1;
2278 *y = y1;
2279 *w = x2 - x1;
2280 *h = y2 - y1;
2281 }
2282
2283 static void calc_subtree_size(branch_t *b, int *x, int *y, int *w, int *h)
2284 {
2285 int i, j;
2286
2287 rect_union(x, y, w, h, b);
2288
2289 for(i = 0; i < b->nrevs; i++)
2290 {
2291 for(j = 0; j < b->revs[i]->nbranches; j++)
2292 calc_subtree_size(b->revs[i]->branches[j], x, y, w, h);
2293 }
2294 }
2295
2296 static int branch_intersects(int top, int bottom, int left, branch_t *b)
2297 {
2298 int br = b->cx + b->tw/2;
2299 int bt = b->y - conf.branch_connect - conf.branch_margin/2;
2300 int bb = b->y + b->th + conf.branch_margin/2;
2301 return !(bt > bottom || bb < top || br >= left);
2302 }
2303
2304 static int branch_intersects_lr(int left, int right, int top, branch_t *b)
2305 {
2306 int bt = b->y + b->th/2;
2307 int bl = b->cx - conf.branch_connect - conf.branch_margin/2;
2308 int br = b->cx + b->tw + conf.branch_margin/2;
2309 return !(bl > right || br < left || bt >= top);
2310 }
2311
2312 static int kern_branch(rcsfile_t *rcs, branch_t *b)
2313 {
2314 int left = b->cx - b->tw/2;
2315 int top = b->y - conf.branch_connect - conf.branch_margin/2;
2316 int bottom = b->y + b->th + conf.branch_margin/2;
2317 int i;
2318 int xpos = 0;
2319
2320 for(i = 0; i < rcs->nbranches; i++)
2321 {
2322 branch_t *bp = rcs->branches[i];
2323 if(bp == b)
2324 continue;
2325 if(branch_intersects(top, bottom, left, bp))
2326 {
2327 int m = bp->cx + bp->tw/2 + conf.branch_margin;
2328 if(m > xpos)
2329 xpos = m;
2330 }
2331 }
2332 if(xpos && (b->cx - b->tw/2) - xpos > 0)
2333 {
2334 move_branch(b, xpos - (b->cx - b->tw/2), 0);
2335 return 1;
2336 }
2337 return 0;
2338 }
2339
2340 static int kern_branch_lr(rcsfile_t *rcs, branch_t *b)
2341 {
2342 int top = b->y - b->th/2;
2343 int left = b->cx - conf.branch_connect - conf.branch_margin/2;
2344 int right = b->cx + b->tw + conf.branch_margin/2;
2345 int i;
2346 int ypos = 0;
2347
2348 for(i = 0; i < rcs->nbranches; i++)
2349 {
2350 branch_t *bp = rcs->branches[i];
2351 if(bp == b)
2352 continue;
2353 if(branch_intersects_lr(left, right, top, bp))
2354 {
2355 int m = bp->y + bp->th/2 + conf.branch_margin;
2356 if(m > ypos)
2357 ypos = m;
2358 }
2359 }
2360 if(ypos && (b->y - b->th/2) - ypos > 0)
2361 {
2362 move_branch(b, 0, ypos - (b->y - b->th/2));
2363 return 1;
2364 }
2365 return 0;
2366 }
2367
2368 static int kern_tree(rcsfile_t *rcs)
2369 {
2370 int i;
2371 int moved;
2372 int safeguard;
2373 int totalmoved = 0;
2374 for(moved = 1, safeguard = LOOPSAFEGUARD; moved && safeguard; safeguard--)
2375 {
2376 moved = 0;
2377 for(i = 1; i < rcs->nbranches; i++)
2378 {
2379 if(conf.left_right)
2380 moved += kern_branch_lr(rcs, rcs->branches[i]);
2381 else
2382 moved += kern_branch(rcs, rcs->branches[i]);
2383 }
2384 totalmoved += moved;
2385 #ifdef DEBUG
2386 fprintf(stderr, "kern_tree: moved=%d\n", moved);
2387 #endif
2388 }
2389 if(!safeguard)
2390 stack_msg(MSG_WARN, "kern_tree: safeguard terminated possible infinite loop; please report.");
2391 return totalmoved;
2392 }
2393
2394 static int index_of_revision(revision_t *r)
2395 {
2396 branch_t *b = r->branch;
2397 int i;
2398 for(i = 0; i < b->nrevs; i++)
2399 {
2400 if(r == b->revs[i])
2401 return i;
2402 }
2403 stack_msg(MSG_ERR, "index_of_revision: Cannot find revision in branch\n");
2404 return 0;
2405 }
2406
2407 static void branch_bbox(branch_t *br, int *l, int *r, int *t, int *b)
2408 {
2409 if(l) *l = br->cx - br->tw/2;
2410 if(r) *r = br->cx + br->tw/2;
2411 if(t) *t = br->y;
2412 if(b) *b = br->y + br->th + ((conf.branch_dupbox && br->nrevs) ? conf.rev_minline + br->h : 0);
2413 }
2414
2415 static void branch_ext_bbox(branch_t *br, int *l, int *r, int *t, int *b)
2416 {
2417 int extra = conf.branch_margin & 1; /* Correct +/-1 error on div 2 */
2418 branch_bbox(br, l, r, t, b);
2419 if(l) *l -= conf.branch_margin/2;
2420 if(r) *r += conf.branch_margin/2 + extra;
2421 if(t) *t -= conf.branch_connect + conf.branch_margin/2;
2422 if(b) *b += conf.branch_margin/2 + extra;
2423 }
2424
2425 static int branch_distance(branch_t *br1, branch_t *br2)
2426 {
2427 int l1, r1, t1, b1;
2428 int l2, r2, t2, b2;
2429 assert(br1 != NULL);
2430 assert(br2 != NULL);
2431 branch_bbox(br1, &l1, &r1, NULL, NULL);
2432 branch_bbox(br2, &l2, &r2, NULL, NULL);
2433 branch_ext_bbox(br1, NULL, NULL, &t1, &b1);
2434 branch_ext_bbox(br2, NULL, NULL, &t2, &b2);
2435 /* Return:
2436 * - 0 if branches have no horizontal overlap
2437 * - positive if b1 is left of b2
2438 * - negative if b2 is left of b1
2439 */
2440 if((t1 > t2 && t1 < b2) || (b1 > t2 && b1 < b2))
2441 return l1 < l2 ? l2 - r1 : -(l1 - r2);
2442 else
2443 return 0;
2444 }
2445
2446 static int space_needed(branch_t *br1, branch_t *br2)
2447 {
2448 int t1, b1;
2449 int t2, b2;
2450 assert(br1 != NULL);
2451 assert(br2 != NULL);
2452 assert(br1->cx < br2->cx); /* br1 must be left of br2 */
2453 branch_ext_bbox(br1, NULL, NULL, &t1, &b1);
2454 branch_ext_bbox(br2, NULL, NULL, &t2, &b2);
2455 /* Return:
2456 * - positive if top br1 is located lower than br2
2457 * - negatve is top br2 is located lower than br1
2458 */
2459 if(t1 > t2)
2460 return -(t1 - b2);
2461 else
2462 return t2 - b1;
2463 }
2464
2465 static void move_yr_branch(branch_t *b, int dy)
2466 {
2467 int i, j;
2468 #ifdef DEBUG
2469 /* fprintf(stderr, "move_yr_branch: b=%s, dy=%d\n", b->branch->branch, dy);*/
2470 #endif
2471 b->y += dy;
2472 for(i = 0; i < b->nrevs; i++)
2473 {
2474 b->revs[i]->y += dy;
2475 for(j = 0; j < b->revs[i]->nbranches; j++)
2476 {
2477 #ifdef DEBUG
2478 /* fprintf(stderr, ".");*/
2479 #endif
2480 move_yr_branch(b->revs[i]->branches[j], dy);
2481 }
2482 }
2483 }
2484
2485 static void move_trunk(revision_t *r, int dy)
2486 {
2487 int i, j;
2488 branch_t *b = r->branch;
2489 b->th += dy;
2490 for(i = index_of_revision(r); i < b->nrevs; i++)
2491 {
2492 #ifdef DEBUG
2493 fprintf(stderr, "move_trunk: start %s, moving %s by %d (b's %d)\n", r->rev->rev, b->revs[i]->rev->rev, dy, b->revs[i]->nbranches);
2494 #endif
2495 b->revs[i]->y += dy;
2496 for(j = 0; j < b->revs[i]->nbranches; j++)
2497 {
2498 move_yr_branch(b->revs[i]->branches[j], dy);
2499 }
2500 }
2501 }
2502
2503 static int space_below(rcsfile_t *rcs, revision_t *r)
2504 {
2505 int i, j;
2506 int bl, br, bb;
2507 int space = INT_MAX;
2508 branch_t *b = r->branch;
2509 branch_t *minb = NULL;
2510
2511 branch_ext_bbox(b, &bl, &br, NULL, &bb);
2512 for(i = 0; i < rcs->nbranches; i++)
2513 {
2514 int tbl, tbr, tbt;
2515 branch_t *tb = rcs->branches[i];
2516 branch_ext_bbox(tb, &tbl, &tbr, &tbt, NULL);
2517 if(tb == b)
2518 continue;
2519 if(tbt > bb) /* Must be below our branch */
2520 {
2521 if(tb->branchpoint) /* Take account for the horiz connector */
2522 tbl = tb->branchpoint->cx + tb->branchpoint->branch->tw/2;
2523 if((bl >= tbl && bl <= tbr) || (br <= tbr && br >= tbl))
2524 {
2525 int s = tbt - bb - conf.branch_connect;
2526 if(s < space)
2527 {
2528 space = s;
2529 minb = tb;
2530 }
2531 }
2532 }
2533 }
2534 if(b->branchpoint)
2535 {
2536 for(i = index_of_revision(r); i < b->nrevs; i++)
2537 {
2538 for(j = 0; j < b->revs[i]->nbranches; j++)
2539 {
2540 int s = space_below(rcs, b->revs[i]->branches[j]->revs[0]);
2541 if(s < space)
2542 space = s;
2543 }
2544 }
2545 }
2546 #ifdef DEBUG
2547 fprintf(stderr, "space_below: from %s have %d to %s\n", b->branch->branch, space, minb ? minb->branch->branch : "<recursed>");
2548 #endif
2549 return space;
2550 }
2551
2552 static int space_available(rcsfile_t *rcs, branch_t *colbr, branch_t *tagbr, int *nl, revision_t **bpcommon)
2553 {
2554 int i;
2555 int space = 0;
2556 int nlinks = 0;
2557 revision_t *r;
2558 branch_t *b;
2559 branch_t *ancestor;
2560 revision_t *branchpoint;
2561
2562 if(!tagbr->branchpoint || !colbr->branchpoint)
2563 {
2564 stack_msg(MSG_WARN, "space_available: Trying to stretch the top?");
2565 return 0;
2566 }
2567
2568 r = colbr->branchpoint;
2569 b = r->branch;
2570 branchpoint = tagbr->branchpoint;
2571 ancestor = branchpoint->branch;
2572 assert(b != NULL);
2573 assert(ancestor != NULL);
2574
2575 while(1)
2576 {
2577 int s;
2578 int rtag = b == ancestor ? index_of_revision(branchpoint)+1 : 0;
2579 for(i = index_of_revision(r); i >= rtag; i--)
2580 {
2581 if(i > 0)
2582 s = b->revs[i]->y - (b->revs[i-1]->y + b->revs[i-1]->h);
2583 else
2584 s = b->revs[i]->y - (b->y + b->h);
2585 if(s < conf.rev_maxline)
2586 {
2587 space += conf.rev_maxline - s;
2588 nlinks++;
2589 }
2590 }
2591 s = space_below(rcs, r);
2592 if(s < space)
2593 space = s;
2594 #ifdef DEBUG
2595 if(space < 0)
2596 return -1;
2597 #endif
2598 if(b == ancestor)
2599 break;
2600 r = b->branchpoint;
2601 if(!r)
2602 {
2603 /* Not a common ancestor */
2604 r = colbr->branchpoint;
2605 b = r->branch;
2606 branchpoint = ancestor->branchpoint;
2607 if(!branchpoint)
2608 {
2609 stack_msg(MSG_WARN, "space_available: No common ancestor?");
2610 return 0;
2611 }
2612 ancestor = branchpoint->branch;
2613 assert(ancestor != NULL);
2614 nlinks = 0;
2615 space = 0;
2616 continue; /* Restart with a new ancestor */
2617 }
2618 b = r->branch;
2619 }
2620 if(nl)
2621 *nl = nlinks; /* Return the number of links that can stretch */
2622 if(bpcommon)
2623 *bpcommon = branchpoint; /* Return the ancestral branchpoint on the common branch */
2624 return space;
2625 }
2626
2627 static int stretch_branches(rcsfile_t *rcs, branch_t *br1, branch_t *br2, int totalstretch)
2628 {
2629 revision_t *r;
2630 revision_t *bpcommon = NULL;
2631 branch_t *ancestor = NULL;
2632 branch_t *b;
2633 int i;
2634 int space;
2635 int nlinks = 0;
2636 int dy;
2637 int rest;
2638
2639 space = space_available(rcs, br1, br2, &nlinks, &bpcommon);
2640 if(bpcommon)
2641 ancestor = bpcommon->branch;
2642
2643 #ifdef DEBUG
2644 if(space == -1)
2645 return 0;
2646 fprintf(stderr, "stretch_branches: space available %d over %d links common %s\n", space, nlinks, ancestor->branch->branch);
2647 #endif
2648 if(space < totalstretch)
2649 return 0;
2650
2651 dy = totalstretch / nlinks;
2652 rest = totalstretch - dy * nlinks;
2653
2654 r = br1->branchpoint;
2655 b = r->branch;
2656 while(1)
2657 {
2658 int rtag = b == ancestor ? index_of_revision(bpcommon)+1 : 0;
2659 for(i = index_of_revision(r); i >= rtag; i--)
2660 {
2661 int s, q;
2662 if(i > 0)
2663 s = b->revs[i]->y - (b->revs[i-1]->y + b->revs[i-1]->h);
2664 else
2665 s = b->revs[i]->y - (b->y + b->h);
2666 q = conf.rev_maxline - s;
2667 if(q > 0)
2668 {
2669 int d = rest ? rest/nlinks+1 : 0;
2670 if(q >= dy+d)
2671 {
2672 move_trunk(b->revs[i], dy+d);
2673 }
2674 else
2675 {
2676 move_trunk(b->revs[i], q);
2677 rest += dy+d - q;
2678 }
2679 rest -= d;
2680 nlinks--;
2681 }
2682 }
2683 if(b == ancestor)
2684 break;
2685 r = b->branchpoint;
2686 assert(r != NULL); /* else 'space_available' wouldn't have returned positively */
2687 b = r->branch;
2688 }
2689 return 1;
2690 }
2691
2692 static branch_t *find_collision_branch(rcsfile_t *rcs, branch_t *b)
2693 {
2694 int i;
2695 int dist = INT_MAX;
2696 branch_t *col = NULL;
2697
2698 for(i = 0; i < rcs->nbranches; i++)
2699 {
2700 int t = branch_distance(rcs->branches[i], b);
2701 if(t > 0 && t < dist)
2702 {
2703 dist = t;
2704 col = rcs->branches[i];
2705 }
2706 }
2707 return col;
2708 }
2709
2710 static void auto_stretch(rcsfile_t *rcs)
2711 {
2712 int i;
2713 int safeguard;
2714
2715 for(i = 0, safeguard = LOOPSAFEGUARD; i < rcs->nbranches && safeguard; i++)
2716 {
2717 int bl, pr;
2718 branch_t *b = rcs->branches[i];
2719 if(!b->branchpoint)
2720 continue;
2721 branch_bbox(b, &bl, NULL, NULL, NULL);
2722 branch_bbox(b->branchpoint->branch, NULL, &pr, NULL, NULL);
2723 if(bl - conf.branch_margin - pr > 0)
2724 {
2725 branch_t *col;
2726 int spaceneeded;
2727 /* There is a potential to move branch b further left.
2728 * All branches obstructing this one from moving further
2729 * left must be originating from revisions below
2730 * b->branchpoint until a common ancester.
2731 * So, we search all branches for a branch that lies left
2732 * of b and is closest to b. This is then the collission
2733 * branch that needs to be moved.
2734 */
2735 col = find_collision_branch(rcs, b);
2736 if(!col)
2737 continue;
2738 spaceneeded = space_needed(col, b);
2739 if(spaceneeded < 0)
2740 continue;
2741 #ifdef DEBUG
2742 fprintf(stderr, "auto_stretch: %s collides %s need %d\n", b->branch->branch, col->branch->branch, spaceneeded);
2743 #endif
2744 /* Trace the collision branch back to find the common ancester
2745 * of both col and b. All revisions encountered while traversing
2746 * backwards must be stretched, including all revisions on the
2747 * common ancester from where the branches sprout.
2748 */
2749 if(stretch_branches(rcs, col, b, spaceneeded))
2750 {
2751 if(kern_tree(rcs))
2752 {
2753 /* Restart the process because movement can
2754 * cause more movement.
2755 */
2756 i = 0 - 1; /* -1 for the i++ of the loop */
2757 safeguard--; /* Prevent infinite loop, just in case */
2758 }
2759 /*return;*/
2760 }
2761 }
2762 }
2763 if(!safeguard)
2764 stack_msg(MSG_ERR, "auto_stretch: safeguard terminated possible infinite loop; please report.");
2765 }
2766
2767 static void fold_branch(rcsfile_t *rcs, revision_t *r)
2768 {
2769 int i, j;
2770 branch_t *btag = NULL;
2771
2772 for(i = 0; i < r->nbranches; i++)
2773 {
2774 branch_t *b = r->branches[i];
2775 if(!b->nrevs && b->ntags < 2)
2776 {
2777 /* No commits in this branch and no duplicate tags */
2778 if(!btag)
2779 btag = b;
2780 else
2781 {
2782 /* We have consecutive empty branches, fold */
2783 b->folded = 1;
2784 b->folded_to = btag;
2785 for(j = 0; j < rcs->nbranches; j++)
2786 {
2787 if(b == rcs->branches[j])
2788 {
2789 /* Zap the branch from the admin */
2790 memmove(&rcs->branches[j],
2791 &rcs->branches[j+1],
2792 (rcs->nbranches - j - 1)*sizeof(rcs->branches[0]));
2793 rcs->nbranches--;
2794 rcs->nfolds++;
2795 break;
2796 }
2797
2798 }
2799 memmove(&r->branches[i], &r->branches[i+1], (r->nbranches - i - 1)*sizeof(r->branches[0]));
2800 r->nbranches--;
2801 i--; /* We have one less now */
2802
2803 /* Add to the fold-list */
2804 btag->folds = xrealloc(btag->folds, (btag->nfolds+1) * sizeof(btag->folds[0]));
2805 btag->folds[btag->nfolds] = b;
2806 btag->nfolds++;
2807 }
2808 }
2809 else
2810 {
2811 if(!conf.branch_foldall)
2812 btag = NULL; /* Start a new box */
2813 /* Recursively fold sub-branches */
2814 for(j = 0; j < b->nrevs; j++)
2815 fold_branch(rcs, b->revs[j]);
2816 }
2817 }
2818 }
2819
2820 static void mark_subtree(branch_t *b)
2821 {
2822 int i, j;
2823 b->subtree_draw = 1;
2824 for(i = 0; i < b->nrevs; i++)
2825 {
2826 for(j = 0; j < b->revs[i]->nbranches; j++)
2827 mark_subtree(b->revs[i]->branches[j]);
2828 }
2829 }
2830
2831 static void make_layout(rcsfile_t *rcs)
2832 {
2833 int i, j;
2834 int x, y;
2835 int w, h;
2836 int w2;
2837
2838 /* Remove all unwanted revisions */
2839 if(conf.strip_untagged)
2840 {
2841 int fr = conf.strip_first_rev ? 0 : 1;
2842 for(i = 0; i < rcs->nbranches; i++)
2843 {
2844 branch_t *bp = rcs->branches[i];
2845 for(j = fr; j < bp->nrevs-1; j++)
2846 {
2847 if(!bp->revs[j]->ntags && bp->revs[j]->stripped >= 0 && !bp->revs[j]->nbranches)
2848 {
2849 bp->revs[j]->stripped = 1;
2850 memmove(&bp->revs[j], &bp->revs[j+1], (bp->nrevs-j-1) * sizeof(bp->revs[0]));
2851 bp->nrevs--;
2852 j--;
2853 }
2854 }
2855 }
2856 }
2857
2858 /* Find the sub-tree(s) we want to see */
2859 if(conf.branch_subtree && conf.branch_subtree[0])
2860 {
2861 branch_t **b;
2862 revision_t **r;
2863 rev_t rev;
2864 int k;
2865 char *tag = conf.branch_subtree;
2866
2867 /* First translate any symbolic tag to a real branch/revision number */
2868 if(rcs->tags)
2869 {
2870 for(k = 0; k < rcs->tags->ntags; k++)
2871 {
2872 if(!strcmp(conf.branch_subtree, rcs->tags->tags[k]->tag))
2873 {
2874 if(rcs->tags->tags[k]->rev->isbranch)
2875 tag = rcs->tags->tags[k]->rev->branch;
2876 else
2877 tag = rcs->tags->tags[k]->rev->rev;
2878 break;
2879 }
2880 }
2881 }
2882
2883 /* Find the corresponding branch */
2884 rev.branch = tag;
2885 rev.rev = NULL;
2886 rev.isbranch = 1;
2887 b = bsearch(&rev, rcs->branches, rcs->nbranches, sizeof(rcs->branches[0]), search_branch);
2888 if(b)
2889 {
2890 if((*b)->branchpoint)
2891 {
2892 subtree_branch = *b;
2893 for(k = 0; k < (*b)->branchpoint->nbranches; k++)
2894 mark_subtree((*b)->branchpoint->branches[k]);
2895 }
2896 /*
2897 * else -> we want everything.
2898 * This happens for the top level branch because it has no
2899 * branchpoint. We do not set the subtree_branch, which then
2900 * results in drawing the whole tree as if we did not select a
2901 * particular branch.
2902 */
2903 }
2904 else
2905 {
2906 /* Maybe it is a revision we want all subtrees from */
2907 rev.rev = tag;
2908 rev.branch = NULL;
2909 rev.isbranch = 0;
2910 r = bsearch(&rev, rcs->srev, rcs->nsrev, sizeof(rcs->srev[0]), search_revision);
2911 if(r)
2912 {
2913 if((*r)->nbranches)
2914 {
2915 subtree_branch = (*r)->branches[0];
2916 subtree_rev = *r;
2917 for(k = 0; k < (*r)->nbranches; k++)
2918 mark_subtree((*r)->branches[k]);
2919 }
2920 /*
2921 * else -> we select everything.
2922 * This happens for the any revision that has no branches.
2923 * We do not set the subtree_branch, which then results in
2924 * drawing the whole tree as if we did not select a
2925 * particular revision's branches.
2926 */
2927 }
2928 }
2929 }
2930
2931 /* Fold all empty branches in one box on the same branchpoint */
2932 if(conf.branch_fold)
2933 {
2934 for(i = 0; i < rcs->branches[0]->nrevs; i++)
2935 {
2936 if(rcs->branches[0]->revs[i]->nbranches > 0)
2937 fold_branch(rcs, rcs->branches[0]->revs[i]);
2938 }
2939 }
2940
2941 /* Remove all unwanted tags */
2942 for(i = 0; i < rcs->nbranches; i++)
2943 {
2944 branch_t *bp = rcs->branches[i];
2945 for(j = 0; j < bp->nrevs; j++)
2946 {
2947 revision_t *r = bp->revs[j];
2948 int k;
2949 for(k = 0; k < r->ntags; k++)
2950 {
2951 if(r->tags[k]->ignore > 0)
2952 {
2953 memmove(&r->tags[k], &r->tags[k+1], (r->ntags-k-1) * sizeof(r->tags[0]));
2954 r->ntags--;
2955 k--;
2956 }
2957 }
2958 }
2959 }
2960
2961 /* Calculate the box-sizes of the revisions */
2962 for(i = 0; i < rcs->nsrev; i++)
2963 {
2964 revision_t *rp;
2965 int w;
2966 int h;
2967 rp = rcs->srev[i];
2968 rp->revtext = expand_string(conf.rev_text, rcs, rp, rp->rev, NULL, rp->ntags ? rp->tags[0] : NULL);
2969 w = get_swidth(rp->revtext, &conf.rev_text_font);
2970 j = get_swidth(rp->rev->rev, &conf.rev_font);
2971 if(j > w)
2972 w = j;
2973 h = get_sheight(rp->revtext, &conf.rev_text_font);
2974 if(!conf.rev_hidenumber)
2975 h += get_sheight(rp->rev->rev, &conf.rev_font);
2976 for(j = 0; j < rp->ntags; j++)
2977 {
2978 int ww = get_swidth(rp->tags[j]->tag, &conf.tag_font);
2979 int th;
2980 if(ww > w) w = ww;
2981 th = get_sheight(rp->tags[j]->tag, &conf.tag_font) + conf.rev_separator;
2982 rp->tags[j]->yofs = h + th/2 + conf.rev_tspace;
2983 h += th;
2984 }
2985 rp->w = w + conf.rev_lspace + conf.rev_rspace;
2986 rp->h = h + conf.rev_tspace + conf.rev_bspace;
2987 }
2988
2989 /* Calculate the box-sizes of the branches */
2990 for(i = 0; i < rcs->nbranches; i++)
2991 {
2992 branch_t *bp = rcs->branches[i];
2993 int w;
2994 int h;
2995 if(!bp->nfolds)
2996 {
2997 w = get_swidth(bp->branch->branch, &conf.branch_font);
2998 if(conf.rev_hidenumber)
2999 h = 0;
3000 else
3001 h = get_sheight(bp->branch->branch, &conf.branch_font);
3002 for(j = 0; j < bp->ntags; j++)
3003 {
3004 int ww = get_swidth(bp->tags[j]->tag, &conf.branch_tag_font);
3005 if(ww > w) w = ww;
3006 h += get_sheight(bp->tags[j]->tag, &conf.branch_tag_font);
3007 }
3008 }
3009 else
3010 {
3011 int h1, h2;
3012 int w1, w2;
3013 int fw;
3014 w1 = get_swidth(bp->branch->branch, &conf.branch_font);
3015 w1 += get_swidth(" ", &conf.branch_font);
3016 w2 = get_swidth(bp->tags[0]->tag, &conf.branch_tag_font);
3017 fw = w1;
3018 w = w1 + w2;
3019 h1 = get_sheight(bp->branch->branch, &conf.branch_font);
3020 h2 = get_sheight(bp->tags[0]->tag, &conf.branch_tag_font);
3021 h = MAX(h1, h2);
3022 for(j = 0; j < bp->nfolds; j++)
3023 {
3024 w1 = get_swidth(bp->folds[j]->branch->branch, &conf.branch_font);
3025 w1 += get_swidth(" ", &conf.branch_font);
3026 w2 = get_swidth(bp->folds[j]->tags[0]->tag, &conf.branch_tag_font);
3027 if(w1 > fw)
3028 fw = w1;
3029 if(w1 + w2 > w)
3030 w = w1 + w2;
3031 h1 = get_sheight(bp->folds[j]->branch->branch, &conf.branch_font);
3032 h2 = get_sheight(bp->folds[j]->tags[0]->tag, &conf.branch_tag_font);
3033 h += MAX(h1, h2);
3034 }
3035 bp->fw = fw;
3036 }
3037 w += conf.branch_lspace + conf.branch_rspace;
3038 h += conf.branch_tspace + conf.branch_bspace;
3039 bp->w = w;
3040 bp->h = h;
3041 if(conf.left_right)
3042 {
3043 for(j = 0; j < bp->nrevs; j++)
3044 {
3045 if(bp->revs[j]->h > h)
3046 h = bp->revs[j]->h;
3047 w += bp->revs[j]->w + conf.rev_minline;
3048 }
3049 if(conf.branch_dupbox && bp->nrevs)
3050 w += bp->w + conf.rev_minline;
3051 }
3052 else
3053 {
3054 for(j = 0; j < bp->nrevs; j++)
3055 {
3056 if(bp->revs[j]->w > w)
3057 w = bp->revs[j]->w;
3058 h += bp->revs[j]->h + conf.rev_minline;
3059 }
3060 if(conf.branch_dupbox && bp->nrevs)
3061 h += bp->h + conf.rev_minline;
3062 }
3063 bp->th = h;
3064 bp->tw = w;
3065 }
3066
3067 /* Calculate the relative positions of revs in a branch */
3068 if(conf.left_right)
3069 {
3070 for(i = 0; i < rcs->nbranches; i++)
3071 {
3072 branch_t *b = rcs->branches[i];
3073 y = b->th/2;
3074 x = b->w;
3075 b->y = y;
3076 b->cx = 0;
3077 for(j = 0; j < b->nrevs; j++)
3078 {
3079 x += conf.rev_minline;
3080 b->revs[j]->y = y;
3081 b->revs[j]->cx = x;
3082 x += b->revs[j]->w;
3083 }
3084 }
3085 }
3086 else
3087 {
3088 for(i = 0; i < rcs->nbranches; i++)
3089 {
3090 branch_t *b = rcs->branches[i];
3091 x = b->tw/2;
3092 y = b->h;
3093 b->cx = x;
3094 b->y = 0;
3095 for(j = 0; j < b->nrevs; j++)
3096 {
3097 y += conf.rev_minline;
3098 b->revs[j]->cx = x;
3099 b->revs[j]->y = y;
3100 y += b->revs[j]->h;
3101 }
3102 }
3103 }
3104
3105 /* Initially reposition the branches from bottom to top progressively right */
3106 if(conf.left_right)
3107 {
3108 x = rcs->branches[0]->y;
3109 w2 = rcs->branches[0]->th / 2;
3110 for(i = rcs->branches[0]->nrevs-1; i >= 0; i--)
3111 {
3112 initial_reposition_branch_lr(rcs->branches[0]->revs[i], &x, &w2);
3113 }
3114 }
3115 else
3116 {
3117 x = rcs->branches[0]->cx;
3118 w2 = rcs->branches[0]->tw / 2;
3119 for(i = rcs->branches[0]->nrevs-1; i >= 0; i--)
3120 {
3121 initial_reposition_branch(rcs->branches[0]->revs[i], &x, &w2);
3122 }
3123 }
3124
3125 /* Initially move branches left if there is room */
3126 kern_tree(rcs);
3127
3128 /* Try to kern the branches more by expanding the inter-revision spacing */
3129 if(conf.auto_stretch && !conf.left_right)
3130 auto_stretch(rcs);
3131
3132 /* Calculate overall image size */
3133 if(conf.left_right)
3134 {
3135 x = rcs->branches[0]->cx;
3136 y = rcs->branches[0]->y - rcs->branches[0]->th/2;
3137 }
3138 else
3139 {
3140 x = rcs->branches[0]->cx - rcs->branches[0]->tw/2;
3141 y = rcs->branches[0]->y;
3142 }
3143 w = rcs->branches[0]->tw;
3144 h = rcs->branches[0]->th;
3145 for(i = 1; i < rcs->nbranches; i++)
3146 rect_union(&x, &y, &w, &h, rcs->branches[i]);
3147 rcs->tw = w;
3148 rcs->th = h;
3149
3150 /* Flip the entire tree */
3151 if(conf.upside_down)
3152 {
3153 if(conf.left_right)
3154 {
3155 x += rcs->tw;
3156 for(i = 0; i < rcs->nbranches; i++)
3157 {
3158 branch_t *b = rcs->branches[i];
3159 for(j = 0; j < b->nrevs; j++)
3160 {
3161 revision_t *r = b->revs[j];
3162 r->cx = x - r->cx - r->w;
3163 }
3164 b->cx = x - b->cx - b->w;
3165 }
3166 }
3167 else
3168 {
3169 y += rcs->th;
3170 for(i = 0; i < rcs->nbranches; i++)
3171 {
3172 branch_t *b = rcs->branches[i];
3173 for(j = 0; j < b->nrevs; j++)
3174 {
3175 revision_t *r = b->revs[j];
3176 r->y = y - r->y - r->h;
3177 }
3178 b->y = y - b->y - b->h;
3179 }
3180 }
3181 }
3182
3183 /* Relocate the lot if we only draw a sub-tree */
3184 if(subtree_branch)
3185 {
3186 int xx, yy;
3187
3188 if(subtree_branch->folded) /* Fix the reference if the branch got folded */
3189 subtree_branch = subtree_branch->folded_to;
3190
3191 xx = conf.left_right ? subtree_branch->cx : subtree_branch->cx - subtree_branch->tw/2;
3192 yy = conf.left_right ? subtree_branch->y - subtree_branch->th/2 : subtree_branch->y;
3193 if(subtree_branch != rcs->branches[0])
3194 {
3195 if(conf.left_right)
3196 xx -= conf.branch_connect;
3197 else
3198 yy -= conf.branch_connect;
3199 }
3200 for(i = 0; i < rcs->nbranches; i++)
3201 move_branch(rcs->branches[i], -xx, -yy);
3202 }
3203
3204 /* Move everything w.r.t. the top-left margin */
3205 for(i = 0; i < rcs->nbranches; i++)
3206 move_branch(rcs->branches[i], conf.margin_left, conf.margin_top);
3207 }
3208
3209 /*
3210 **************************************************************************
3211 * Imagemap functions
3212 **************************************************************************
3213 */
3214 static void map_merge_box(rcsfile_t *rcs, FILE *fp, revision_t *fr, revision_t *tr, gdImagePtr im, int x1, int y1, int x2, int y2)
3215 {
3216 char *href = expand_string(conf.map_merge_href, rcs, tr, tr->rev, fr->rev, NULL);
3217 char *alt = expand_string(conf.map_merge_alt, rcs, tr, tr->rev, fr->rev, NULL);
3218 const char *htp = conf.html_level == HTMLLEVEL_X ? " /" : "";
3219
3220 if(x1 > 0 && x2 > 0 && y1 > 0 && y2 > 0)
3221 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s%s>\n",
3222 href, x1, y1, x2, y2, alt, htp);
3223 xfree(alt);
3224 xfree(href);
3225
3226 if(im)
3227 {
3228 gdImageFilledRectangle(im, x1-2, y1-2, x1+2, y1+2, clr(im, "title_color", NULL, NULL, 0)->id);
3229 gdImageFilledRectangle(im, x2-2, y2-2, x2+2, y2+2, clr(im, "tag_color.id", NULL, NULL, 0)->id);
3230 gdImageLine(im, x1, y1, x2, y2, clr(im, "title_color", NULL, NULL, 0)->id);
3231 }
3232 }
3233
3234 static void map_merges(rcsfile_t *rcs, FILE *fp, gdImagePtr im)
3235 {
3236 int i;
3237 int tagh2 = get_sheight("Hg", &conf.tag_font) / 2;
3238 int bm = conf.branch_margin / 2;
3239
3240 for(i = 0; i < rcs->nmerges; i++)
3241 {
3242 revision_t *fr;
3243 revision_t *tr;
3244 int x1, x2, y1, y2;
3245 switch(rcs->merges[i].type)
3246 {
3247 case TR_TAG:
3248 fr = rcs->merges[i].from.tag->logrev;
3249 tr = rcs->merges[i].to.tag->logrev;
3250 break;
3251 case TR_REVISION:
3252 fr = rcs->merges[i].from.rev;
3253 tr = rcs->merges[i].to.rev;
3254 break;
3255 default:
3256 continue;
3257 }
3258 if(!fr || !tr || fr == tr)
3259 continue; /* This can happen with detached tags and self-references */
3260 if(conf.left_right)
3261 {
3262 if(fr->branch == tr->branch)
3263 {
3264 y1 = fr->y - fr->h/2;
3265 y2 = tr->y - tr->h/2;
3266 }
3267 else
3268 {
3269 if(fr->y < tr->y)
3270 {
3271 y1 = fr->y + fr->h/2;
3272 y2 = tr->y - tr->h/2;
3273 }
3274 else
3275 {
3276 y1 = fr->y - fr->h/2;
3277 y2 = tr->y + tr->h/2;
3278 }
3279 }
3280 x1 = fr->cx + fr->w/2;
3281 x2 = tr->cx + tr->w/2;
3282 }
3283 else
3284 {
3285 if(fr->branch == tr->branch)
3286 {
3287 x1 = fr->cx - fr->w/2;
3288 x2 = tr->cx - tr->w/2;
3289 }
3290 else
3291 {
3292 if(fr->cx < tr->cx)
3293 {
3294 x1 = fr->cx + fr->w/2;
3295 x2 = tr->cx - tr->w/2;
3296 }
3297 else
3298 {
3299 x1 = fr->cx - fr->w/2;
3300 x2 = tr->cx + tr->w/2;
3301 }
3302 }
3303 if(rcs->merges[i].type == TR_TAG)
3304 {
3305 y1 = fr->y + rcs->merges[i].from.tag->yofs;
3306 y2 = tr->y + rcs->merges[i].to.tag->yofs;
3307 }
3308 else
3309 {
3310 y1 = fr->y + fr->h/2;
3311 y2 = tr->y + tr->h/2;
3312 }
3313 }
3314
3315 if(conf.left_right)
3316 {
3317 if(fr->branch == tr->branch)
3318 {
3319 map_merge_box(rcs, fp, fr, tr, im, x1-bm, y1-bm, x1+bm, y1);
3320 map_merge_box(rcs, fp, fr, tr, im, x2-bm, y2-bm, x2+bm, y2);
3321 }
3322 else
3323 {
3324 if(y1 > y2)
3325 {
3326 map_merge_box(rcs, fp, fr, tr, im, x1-bm, y1-bm, x1+bm, y1);
3327 map_merge_box(rcs, fp, fr, tr, im, x2-bm, y2, x2+bm, y2+bm);
3328 }
3329 else
3330 {
3331 map_merge_box(rcs, fp, fr, tr, im, x1-bm, y1, x1+bm, y1+bm);
3332 map_merge_box(rcs, fp, fr, tr, im, x2-bm, y2-bm, x2+bm, y2);
3333 }
3334 }
3335 }
3336 else
3337 {
3338 if(fr->branch == tr->branch)
3339 {
3340 map_merge_box(rcs, fp, fr, tr, im, x1-bm, y1-tagh2, x1, y1+tagh2);
3341 map_merge_box(rcs, fp, fr, tr, im, x2-bm, y2-tagh2, x2, y2+tagh2);
3342 }
3343 else
3344 {
3345 if(x1 > x2)
3346 {
3347 map_merge_box(rcs, fp, fr, tr, im, x1-bm, y1-tagh2, x1, y1+tagh2);
3348 map_merge_box(rcs, fp, fr, tr, im, x2, y2-tagh2, x2+bm, y2+tagh2);
3349 }
3350 else
3351 {
3352 map_merge_box(rcs, fp, fr, tr, im, x1, y1-tagh2, x1+bm, y1+tagh2);
3353 map_merge_box(rcs, fp, fr, tr, im, x2-bm, y2-tagh2, x2, y2+tagh2);
3354 }
3355 }
3356 }
3357 }
3358 }
3359
3360 static void make_imagemap(rcsfile_t *rcs, FILE *fp, gdImagePtr im)
3361 {
3362 int i, j;
3363 const char *htp = conf.html_level == HTMLLEVEL_X ? " /" : "";
3364
3365 switch(conf.html_level)
3366 {
3367 case HTMLLEVEL_4:
3368 fprintf(fp, "<map name=\"%s\" id=\"%s\">\n", conf.map_name, conf.map_name);
3369 break;
3370 case HTMLLEVEL_X:
3371 fprintf(fp, "<map id=\"%s\">\n", conf.map_name);
3372 break;
3373 default:
3374 fprintf(fp, "<map name=\"%s\">\n", conf.map_name);
3375 }
3376
3377 for(i = 0; i < rcs->nbranches; i++)
3378 {
3379 branch_t *b = rcs->branches[i];
3380 tag_t *tag = b->ntags ? b->tags[0] : NULL;
3381 char *bhref;
3382 char *balt;
3383 int x1;
3384 int x2;
3385 int y1;
3386 int y2;
3387
3388 if(subtree_branch && !b->subtree_draw)
3389 continue;
3390
3391 bhref = expand_string(conf.map_branch_href, rcs, NULL, b->branch, NULL, tag);
3392 balt = expand_string(conf.map_branch_alt, rcs, NULL, b->branch, NULL, tag);
3393
3394 if(!b->nfolds)
3395 {
3396 if(conf.left_right)
3397 {
3398 x1 = b->cx;
3399 y1 = b->y - b->h/2;
3400 x2 = b->cx + b->w;
3401 y2 = b->y + b->h/2;
3402 }
3403 else
3404 {
3405 x1 = b->cx - b->w/2;
3406 y1 = b->y;
3407 x2 = b->cx + b->w/2;
3408 y2 = b->y + b->h;
3409 }
3410 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s%s>\n",
3411 bhref, x1, y1, x2, y2, balt, htp);
3412 if(im)
3413 {
3414 gdImageFilledRectangle(im, x1-2, y1-2, x1+2, y1+2, clr(im, "title_color", NULL, NULL, 0)->id);
3415 gdImageFilledRectangle(im, x2-2, y2-2, x2+2, y2+2, clr(im, "tag_color", NULL, NULL, 0)->id);
3416 gdImageLine(im, x1, y1, x2, y2, clr(im, "title_color", NULL, NULL, 0)->id);
3417 }
3418 }
3419 else
3420 {
3421 int yy1, yy2, yy;
3422 if(conf.left_right)
3423 {
3424 x1 = b->cx + conf.branch_lspace;
3425 y1 = b->y - b->h/2 + conf.branch_tspace;
3426 }
3427 else
3428 {
3429 x1 = b->cx - b->w/2 + conf.branch_lspace;
3430 y1 = b->y + conf.branch_tspace;
3431 }
3432 x2 = x1 + b->w - conf.branch_rspace;
3433
3434 yy1 = get_sheight(b->branch->branch, &conf.branch_font);
3435 yy2 = get_sheight(b->tags[0]->tag, &conf.branch_tag_font);
3436 yy = MAX(yy1, yy2);
3437 y2 = y1 + yy;
3438 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s%s>\n",
3439 bhref, x1, y1, x2, y2, balt, htp);
3440
3441 y1 += yy;
3442 y2 += yy;
3443 for(j = 0; j < b->nfolds; j++)
3444 {
3445 branch_t *fb = b->folds[j];
3446 tag_t *t = fb->tags[0];
3447 xfree(bhref);
3448 xfree(balt);
3449 bhref = expand_string(conf.map_branch_href, rcs, NULL, fb->branch, NULL, t);
3450 balt = expand_string(conf.map_branch_alt, rcs, NULL, fb->branch, NULL, t);
3451 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s%s>\n",
3452 bhref, x1, y1, x2, y2, balt, htp);
3453 yy1 = get_sheight(fb->branch->branch, &conf.branch_font);
3454 yy2 = get_sheight(fb->tags[0]->tag, &conf.branch_tag_font);
3455 yy = MAX(yy1, yy2);
3456 y1 += yy;
3457 y2 += yy;
3458 }
3459 }
3460
3461 for(j = 0; j < b->nrevs; j++)
3462 {
3463 revision_t *r = b->revs[j];
3464 revision_t* r1;
3465 int xoff = 1;
3466 int yoff = 1;
3467 char *href;
3468 char *alt;
3469
3470 tag = r->ntags ? r->tags[0] : NULL;
3471 href = expand_string(conf.map_rev_href, rcs, r, r->rev, NULL, tag);
3472 alt = expand_string(conf.map_rev_alt, rcs, r, r->rev, NULL, tag);
3473 if(conf.left_right)
3474 {
3475 x1 = r->cx;
3476 y1 = r->y - r->h/2;
3477 x2 = r->cx + r->w;
3478 y2 = r->y + r->h/2;
3479 }
3480 else
3481 {
3482 x1 = r->cx - r->w/2;
3483 y1 = r->y;
3484 x2 = r->cx + r->w/2;
3485 y2 = r->y + r->h;
3486 }
3487 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s%s>\n",
3488 href, x1, y1, x2, y2, alt, htp);
3489 if(im)
3490 {
3491 gdImageFilledRectangle(im, x1-2, y1-2, x1+2, y1+2, clr(im, "title_color", NULL, NULL, 0)->id);
3492 gdImageFilledRectangle(im, x2-2, y2-2, x2+2, y2+2, clr(im, "tag_color", NULL, NULL, 0)->id);
3493 gdImageLine(im, x1, y1, x2, y2, clr(im, "title_color", NULL, NULL, 0)->id);
3494 }
3495 xfree(href);
3496 xfree(alt);
3497 if(j > 0 || b->branchpoint)
3498 {
3499 if(j > 0)
3500 {
3501 r1 = b->revs[j-1];
3502 if(conf.left_right)
3503 {
3504 yoff = MIN(r->h, r1->h)/4;
3505 x1 = conf.upside_down ? r1->cx : r1->cx + r1->w;
3506 }
3507 else
3508 {
3509 xoff = MIN(r->w, r1->w)/4;
3510 y1 = conf.upside_down ? r1->y : r1->y + r1->h;
3511 }
3512 }
3513 else
3514 {
3515 r1 = b->branchpoint;
3516 if(conf.left_right)
3517 {
3518 yoff = MIN(r->h, b->h)/4;
3519 x1 = conf.upside_down ? b->cx : b->cx + b->w;
3520 }
3521 else
3522 {
3523 xoff = MIN(r->w, b->w)/4;
3524 y1 = conf.upside_down ? b->y : b->y + b->h;
3525 }
3526 }
3527 if(conf.left_right)
3528 {
3529 y1 = r->y - yoff;
3530 y2 = r->y + yoff;
3531 x2 = conf.upside_down ? r->cx + r->w : r->cx;
3532 yoff = 0;
3533 }
3534 else
3535 {
3536 x1 = r->cx - xoff;
3537 x2 = r->cx + xoff;
3538 y2 = conf.upside_down ? r->y + r->h : r->y;
3539 xoff = 0;
3540 }
3541 if(x1 > x2)
3542 {
3543 int tt = x1;
3544 x1 = x2;
3545 x2 = tt;
3546 }
3547 if(y1 > y2)
3548 {
3549 int tt = y1;
3550 y1 = y2;
3551 y2 = tt;
3552 }
3553 href = expand_string(conf.map_diff_href, rcs, r, r->rev, r1->rev, tag);
3554 alt = expand_string(conf.map_diff_alt, rcs, r, r->rev, r1->rev, tag);
3555 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s%s>\n",
3556 href,
3557 x1+xoff, y1+yoff, x2-xoff, y2-yoff,
3558 alt, htp);
3559 if(im)
3560 {
3561 gdImageFilledRectangle(im, x1-2, y1-2, x1+2, y1+2, clr(im, "title_color", NULL, NULL, 0)->id);
3562 gdImageFilledRectangle(im, x2-2, y2-2, x2+2, y2+2, clr(im, "tag_color", NULL, NULL, 0)->id);
3563 gdImageLine(im, x1, y1, x2, y2, clr(im, "title_color", NULL, NULL, 0)->id);
3564 }
3565 xfree(href);
3566 xfree(alt);
3567 }
3568 }
3569 if(conf.branch_dupbox && b->nrevs)
3570 {
3571 if(conf.left_right)
3572 {
3573 x1 = conf.upside_down ? b->cx + b->w - b->tw : b->cx - b->w + b->tw;
3574 y1 = b->y - b->h/2;
3575 x2 = x1 + b->w;
3576 y2 = b->y + b->h/2;
3577 }
3578 else
3579 {
3580 x1 = b->cx - b->w/2;
3581 y1 = conf.upside_down ? b->y + b->h - b->th : b->y - b->h + b->th;
3582 x2 = b->cx + b->w/2;
3583 y2 = y1 + b->h;
3584 }
3585 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s%s>\n",
3586 bhref, x1, y1, x2, y2, balt, htp);
3587 if(im)
3588 {
3589 gdImageFilledRectangle(im, x1-2, y1-2, x1+2, y1+2, clr(im, "title_color", NULL, NULL, 0)->id);
3590 gdImageFilledRectangle(im, x2-2, y2-2, x2+2, y2+2, clr(im, "tag_color", NULL, NULL, 0)->id);
3591 gdImageLine(im, x1, y1, x2, y2, clr(im, "title_color", NULL, NULL, 0)->id);
3592 }
3593 }
3594 xfree(bhref);
3595 xfree(balt);
3596 }
3597
3598 map_merges(rcs, fp, im);
3599
3600 fprintf(fp, "</map>\n");
3601 }
3602
3603 /*
3604 **************************************************************************
3605 * Program entry
3606 **************************************************************************
3607 */
3608 static const char usage_str[] =
3609 "Usage: cvsgraph [options] <file>\n"
3610 " -b Add a branch box at both sides of the trunk (config value is negated)\n"
3611 " -c <file> Read alternative config from <file>\n"
3612 " -d <level> Enable debug mode at <level>\n"
3613 " -h This message\n"
3614 " -i Generate an imagemap instead of image\n"
3615 " -I <file> Also write the imagemap to <file>\n"
3616 " -k Auto stretch the tree (config value is negated)\n"
3617 " -M <name> Use <name> as imagemap name\n"
3618 " -m <mod> Use <mod> as cvs module\n"
3619 " -o <file> Output to <file>\n"
3620 " -O <opt=val> Set option opt to value val\n"
3621 " -q Be quiet (i.e. no warnings)\n"
3622 " -r <path> Use <path> as cvsroot path\n"
3623 " -s Strip untagged revisions (config value is negated)\n"
3624 " -S Also strip the first revision (config value is negated)\n"
3625 " -u Upside down image (mirror vertically; config value is negated)\n"
3626 " -V Print version and exit\n"
3627 " -x [34x] Specify level of HTML 3.2 (default), 4.0 or XHTML\n"
3628 " -[0-9] <txt> Use <txt> for expansion\n"
3629 ;
3630
3631 #define VERSION_STR "1.5.2"
3632 #define NOTICE_STR "Copyright (c) 2001,2002,2003,2004 B.Stultiens"
3633
3634 static void append_slash(char **path)
3635 {
3636 int l;
3637 assert(path != NULL);
3638 assert(*path != NULL);
3639 l = strlen(*path);
3640 if(!l || (*path)[l-1] == '/')
3641 return;
3642 *path = xrealloc(*path, l+2);
3643 strcat(*path, "/");
3644 }
3645
3646 int main(int argc, char *argv[])
3647 {
3648 extern int rcs_flex_debug;
3649 extern int rcsdebug;
3650 int optc;
3651 char *confpath = NULL;
3652 char *outfile = NULL;
3653 char *cvsroot = NULL;
3654 char *cvsmodule = NULL;
3655 int imagemap = 0;
3656 int upsidedown = 0;
3657 int bdupbox = 0;
3658 int stripuntag = 0;
3659 int stripfirst = 0;
3660 int autostretch = 0;
3661 int htmllevel = 0;
3662 char *imgmapname = NULL;
3663 char *imgmapfile = NULL;
3664 int lose = 0;
3665 FILE *fp;
3666 char *rcsfilename;
3667 rcsfile_t *rcs;
3668 gdImagePtr im;
3669
3670 while((optc = getopt(argc, argv, "0:1:2:3:4:5:6:7:8:9:bc:d:hI:ikM:m:O:o:qr:SsuVx:")) != EOF)
3671 {
3672 switch(optc)
3673 {
3674 case 'b':
3675 bdupbox = 1;
3676 break;
3677 case 'c':
3678 confpath = xstrdup(optarg);
3679 break;
3680 case 'd':
3681 debuglevel = strtol(optarg, NULL, 0);
3682 break;
3683 case 'I':
3684 imgmapfile = xstrdup(optarg);
3685 break;
3686 case 'i':
3687 imagemap = 1;
3688 break;
3689 case 'k':
3690 autostretch = 1;
3691 break;
3692 case 'M':
3693 imgmapname = xstrdup(optarg);
3694 break;
3695 case 'm':
3696 cvsmodule = xstrdup(optarg);
3697 break;
3698 case 'O':
3699 stack_option(optarg);
3700 break;
3701 case 'o':
3702 outfile = xstrdup(optarg);
3703 break;
3704 case 'q':
3705 quiet = 1;
3706 break;
3707 case 'r':
3708 cvsroot = xstrdup(optarg);
3709 break;
3710 case 'S':
3711 stripfirst = 1;
3712 break;
3713 case 's':
3714 stripuntag = 1;
3715 break;
3716 case 'u':
3717 upsidedown = 1;
3718 break;
3719 case 'V':
3720 fprintf(stdout, "cvsgraph v%s, %s\n", VERSION_STR, NOTICE_STR);
3721 return 0;
3722 case 'x':
3723 switch(optarg[0])
3724 {
3725 case '3':
3726 htmllevel = HTMLLEVEL_3;
3727 break;
3728 case '4':
3729 htmllevel = HTMLLEVEL_4;
3730 break;
3731 case 'x':
3732 htmllevel = HTMLLEVEL_X;
3733 break;
3734 default:
3735 fprintf(stderr, "Invalid HTML level in -x\n");
3736 lose++;
3737 }
3738 break;
3739 case 'h':
3740 fprintf(stdout, "%s", usage_str);
3741 return 0;
3742 default:
3743 if(isdigit(optc))
3744 {
3745 conf.expand[optc-'0'] = xstrdup(optarg);
3746 }
3747 else
3748 lose++;
3749 }
3750 }
3751
3752 if(lose)
3753 {
3754 fprintf(stderr, "%s", usage_str);
3755 return 1;
3756 }
3757
3758 if(debuglevel)
3759 {
3760 setvbuf(stdout, NULL, 0, _IONBF);
3761 setvbuf(stderr, NULL, 0, _IONBF);
3762 }
3763 rcs_flex_debug = (debuglevel & DEBUG_RCS_LEX) != 0;
3764 rcsdebug = (debuglevel & DEBUG_RCS_YACC) != 0;
3765
3766 /* Set defaults */
3767 conf.tag_font.gdfont = gdFontTiny;
3768 conf.rev_font.gdfont = gdFontTiny;
3769 conf.branch_font.gdfont = gdFontTiny;
3770 conf.branch_tag_font.gdfont = gdFontTiny;
3771 conf.title_font.gdfont = gdFontTiny;
3772 conf.rev_text_font.gdfont = gdFontTiny;
3773 conf.msg_font.gdfont = gdFontTiny;
3774
3775 conf.anti_alias = 1;
3776 conf.thick_lines = 1;
3777 conf.branch_fold = 1;
3778
3779 conf.cvsroot = xstrdup("");
3780 conf.cvsmodule = xstrdup("");
3781 conf.date_format = xstrdup("%d-%b-%Y %H:%M:%S");
3782 conf.title = xstrdup("");
3783 conf.map_name = xstrdup("CvsGraphImageMap");
3784 conf.map_branch_href = xstrdup("href=\"unset: conf.map_branch_href\"");
3785 conf.map_branch_alt = xstrdup("alt=\"%B\"");
3786 conf.map_rev_href = xstrdup("href=\"unset: conf.map_rev_href\"");
3787 conf.map_rev_alt = xstrdup("alt=\"%R\"");
3788 conf.map_diff_href = xstrdup("href=\"unset: conf.map_diff_href\"");
3789 conf.map_diff_alt = xstrdup("alt=\"%P &lt;-&gt; %R\"");
3790 conf.map_merge_href = xstrdup("href=\"unset: conf.map_merge_href\"");
3791 conf.map_merge_alt = xstrdup("alt=\"%P &lt;-&gt; %R\"");
3792 conf.rev_text = xstrdup("%d");
3793 conf.branch_subtree = xstrdup("");
3794 conf.tag_ignore = xstrdup("");
3795 conf.merge_from.n = 0;
3796 conf.merge_from.strs = NULL;
3797 conf.merge_to.n = 0;
3798 conf.merge_to.strs = NULL;
3799 conf.merge_arrows = 1;
3800 conf.merge_cvsnt = 1;
3801 conf.arrow_width = ARROW_WIDTH;
3802 conf.arrow_length = ARROW_LENGTH;
3803
3804 conf.color_bg = white_color;
3805 conf.branch_bgcolor = white_color;
3806 conf.branch_color = black_color;
3807 conf.branch_tag_color = black_color;
3808 conf.rev_color = black_color;
3809 conf.rev_bgcolor = white_color;
3810 conf.merge_color.n = 0;
3811 conf.merge_color.clrs = NULL;
3812 conf.merge_cvsnt_color = black_color;
3813 conf.tag_color = black_color;
3814 conf.title_color = black_color;
3815 conf.rev_text_color = black_color;
3816 conf.msg_color = black_color;
3817
3818 conf.image_quality = 100;
3819 conf.image_compress = -1; /* Use default zlib setting */
3820 conf.rev_maxline = -1; /* Checked later to set to default */
3821
3822 read_config(confpath);
3823
3824 if(conf.rev_maxline == -1) conf.rev_maxline = 5 * conf.rev_minline;
3825
3826 /* Set overrides */
3827 if(cvsroot) conf.cvsroot = cvsroot;
3828 if(cvsmodule) conf.cvsmodule = cvsmodule;
3829 if(imgmapname) conf.map_name = imgmapname;
3830 if(upsidedown) conf.upside_down = !conf.upside_down;
3831 if(bdupbox) conf.branch_dupbox = !conf.branch_dupbox;
3832 if(stripuntag) conf.strip_untagged = !conf.strip_untagged;
3833 if(stripfirst) conf.strip_first_rev = !conf.strip_first_rev;
3834 if(autostretch) conf.auto_stretch = !conf.auto_stretch;
3835 if(htmllevel) conf.html_level = htmllevel;
3836
3837 if(conf.rev_minline >= conf.rev_maxline)
3838 {
3839 if(conf.auto_stretch)
3840 stack_msg(MSG_WARN, "Auto stretch is only possible if rev_minline < rev_maxline");
3841 conf.auto_stretch = 0;
3842 }
3843
3844 if(conf.thick_lines < 1)
3845 conf.thick_lines = 1;
3846 if(conf.thick_lines > 11)
3847 conf.thick_lines = 11;
3848
3849 if(conf.image_quality < 0 || conf.image_quality > 100)
3850 {
3851 stack_msg(MSG_WARN, "JPEG quality (image_quality) must be between 0 and 100");
3852 conf.image_quality = 100;
3853 }
3854
3855 if(conf.image_compress < -1 || conf.image_compress > 9)
3856 {
3857 stack_msg(MSG_WARN, "PNG compression (image_compress) must be between -1 and 9");
3858 conf.image_compress = -1;
3859 }
3860
3861 append_slash(&conf.cvsroot);
3862 append_slash(&conf.cvsmodule);
3863
3864 if(optind >= argc)
3865 {
3866 #ifdef __WIN32__
3867 /* Bad hack for DOS/Windows */
3868 if(setmode(fileno(stdin), O_BINARY) == -1)
3869 {
3870 perror("Set binary mode for stdin");
3871 return 1;
3872 }
3873 #endif
3874 rcsfilename = NULL;
3875 }
3876 else
3877 rcsfilename = argv[optind];
3878
3879 rcs = get_rcsfile(conf.cvsroot, conf.cvsmodule, rcsfilename);
3880 if(!rcs)
3881 return 1;
3882
3883 if(debuglevel & DEBUG_RCS_FILE)
3884 dump_rcsfile(rcs);
3885
3886 if(!reorganise_branches(rcs))
3887 return 1;
3888
3889 assign_tags(rcs);
3890 find_merges(rcs);
3891 find_merges_cvsnt(rcs);
3892
3893 if(outfile)
3894 {
3895 if((fp = fopen(outfile, "wb")) == NULL)
3896 {
3897 perror(outfile);
3898 return 1;
3899 }
3900 }
3901 else
3902 {
3903 fp = stdout;
3904 #ifdef __WIN32__
3905 /* Bad hack for DOS/Windows */
3906 if(setmode(fileno(fp), O_BINARY) == -1)
3907 {
3908 perror("Set binary mode for stdout");
3909 return 1;
3910 }
3911 #endif
3912 }
3913
3914 make_layout(rcs);
3915
3916 if(!imagemap)
3917 {
3918 /* Create an image */
3919 im = make_image(rcs);
3920
3921 if(conf.image_interlace)
3922 gdImageInterlace(im, 1);
3923
3924 #ifdef DEBUG_IMAGEMAP
3925 {
3926 FILE *nulfile = fopen("/dev/null", "w");
3927 make_imagemap(rcs, nulfile, im);
3928 fclose(nulfile);
3929 }
3930 #endif
3931 switch(conf.image_type)
3932 {
3933 #ifdef HAVE_IMAGE_GIF
3934 # ifndef HAVE_IMAGE_PNG
3935 default:
3936 # endif
3937 case IMAGE_GIF:
3938 gdImageGif(im, fp);
3939 break;
3940 #endif
3941 #ifdef HAVE_IMAGE_PNG
3942 default:
3943 case IMAGE_PNG:
3944 #ifdef HAVE_GDIMAGEPNGEX
3945 gdImagePngEx(im, fp, conf.image_compress);
3946 #else
3947 gdImagePng(im, fp);
3948 #endif
3949 break;
3950 #endif
3951 #ifdef HAVE_IMAGE_JPEG
3952 # if !defined(HAVE_IMAGE_GIF) && !defined(HAVE_IMAGE_PNG)
3953 default:
3954 # endif
3955 case IMAGE_JPEG:
3956 gdImageJpeg(im, fp, conf.image_quality);
3957 break;
3958 #endif
3959 }
3960
3961 gdImageDestroy(im);
3962 }
3963 else
3964 {
3965 /* Create an imagemap */
3966 make_imagemap(rcs, fp, NULL);
3967 }
3968
3969 /* Also create imagemap to file if requested */
3970 if(imgmapfile)
3971 {
3972 FILE *ifp = fopen(imgmapfile, "wb");
3973 if(!ifp)
3974 {
3975 perror(imgmapfile);
3976 return 1;
3977 }
3978 make_imagemap(rcs, ifp, NULL);
3979 fclose(ifp);
3980 }
3981
3982 if(outfile)
3983 fclose(fp);
3984
3985 return 0;
3986 }

  ViewVC Help
Powered by ViewVC 1.1.0 with CvsGraph 1.7.0