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

  ViewVC Help
Powered by ViewVC 1.1.0 with CvsGraph 1.7.0