/[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.38 - (show annotations)
Fri Jul 16 13:24:53 2004 UTC (13 years, 3 months ago) by bertho
Branch: MAIN
CVS Tags: REL_1_4_1
Changes since 1.37: +1 -1 lines
File MIME type: text/plain
Fix a longstanding date-bug in showing local time. Now timezone is accounted for.
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 /* We cannot (should not) match multiple times */
770 break;
771 }
772 }
773 regfree(reto);
774 }
775 xfree(to);
776 }
777 }
778 }
779 if(matchfrom) xfree(matchfrom);
780 if(refrom) { regfree(refrom); xfree(refrom); }
781 if(reto) xfree(reto);
782 }
783
784 static void assign_tags(rcsfile_t *rcs)
785 {
786 int i;
787 int nr;
788
789 for(i = nr = 0; i < rcs->nbranches; i++)
790 nr += rcs->branches[i]->nrevs;
791
792 rcs->srev = xmalloc(nr * sizeof(rcs->srev[0]));
793 rcs->nsrev = nr;
794 for(i = nr = 0; i < rcs->nbranches; i++)
795 {
796 memcpy(&rcs->srev[nr], rcs->branches[i]->revs, rcs->branches[i]->nrevs * sizeof(rcs->branches[i]->revs[0]));
797 nr += rcs->branches[i]->nrevs;
798 }
799
800 qsort(rcs->srev, rcs->nsrev, sizeof(rcs->srev[0]), sort_revision);
801 qsort(rcs->branches, rcs->nbranches, sizeof(rcs->branches[0]), sort_branch);
802
803 if(!rcs->branch)
804 {
805 /* The main trunk is the active trunk */
806 rcs->tags->tags = xrealloc(rcs->tags->tags, (rcs->tags->ntags+1)*sizeof(rcs->tags->tags[0]));
807 rcs->tags->tags[rcs->tags->ntags] = xmalloc(sizeof(tag_t));
808 rcs->tags->tags[rcs->tags->ntags]->tag = xstrdup("MAIN");
809 rcs->tags->tags[rcs->tags->ntags]->rev = xmalloc(sizeof(rev_t));
810 rcs->tags->tags[rcs->tags->ntags]->rev->rev = xstrdup(rcs->active->branch->rev);
811 rcs->tags->tags[rcs->tags->ntags]->rev->branch = xstrdup(rcs->active->branch->branch);
812 rcs->tags->tags[rcs->tags->ntags]->rev->isbranch = 1;
813 rcs->tags->ntags++;
814 }
815
816 /* We should have at least two tags (HEAD and MAIN) */
817 assert(rcs->tags != 0);
818
819 for(i = 0; i < rcs->tags->ntags; i++)
820 {
821 tag_t *t = rcs->tags->tags[i];
822 if(t->rev->isbranch)
823 {
824 branch_t **b;
825 add_btag:
826 b = bsearch(t->rev, rcs->branches, rcs->nbranches, sizeof(rcs->branches[0]), search_branch);
827 if(!b)
828 {
829 rev_t rev;
830 revision_t **r;
831 /* This happens for the magic branch numbers if there are
832 * no commits within the new branch yet. So, we add the
833 * branch and try to continue.
834 */
835 rev.rev = previous_rev(t->rev->branch);
836 rev.branch = NULL;
837 rev.isbranch = 0;
838 r = bsearch(&rev, rcs->srev, rcs->nsrev, sizeof(rcs->srev[0]), search_revision);
839 xfree(rev.rev);
840 if(!r)
841 {
842 if(!quiet)
843 fprintf(stderr, "No branch found for tag '%s:%s'\n", t->tag, t->rev->branch);
844 }
845 else
846 {
847 rcs->branches = xrealloc(rcs->branches, (rcs->nbranches+1)*sizeof(rcs->branches[0]));
848 rcs->branches[rcs->nbranches] = xmalloc(sizeof(branch_t));
849 rcs->branches[rcs->nbranches]->branch = dup_rev(t->rev);
850 rcs->branches[rcs->nbranches]->branchpoint = *r;
851 (*r)->branches = xrealloc((*r)->branches, ((*r)->nbranches+1)*sizeof((*r)->branches[0]));
852 (*r)->branches[(*r)->nbranches] = rcs->branches[rcs->nbranches];
853 (*r)->nbranches++;
854 rcs->nbranches++;
855 /* Resort the branches */
856 qsort(rcs->branches, rcs->nbranches, sizeof(rcs->branches[0]), sort_branch);
857 goto add_btag;
858 }
859 }
860 else
861 {
862 branch_t *bb = *b;
863 bb->tags = xrealloc(bb->tags, (bb->ntags+1)*sizeof(bb->tags[0]));
864 bb->tags[bb->ntags] = t;
865 bb->ntags++;
866 }
867 }
868 else
869 {
870 revision_t **r = bsearch(t->rev, rcs->srev, rcs->nsrev, sizeof(rcs->srev[0]), search_revision);
871 if(!r)
872 {
873 if(!quiet)
874 fprintf(stderr, "No revision found for tag '%s:%s'\n", t->tag, t->rev->rev);
875 }
876 else
877 {
878 revision_t *rr = *r;
879 t->logrev = rr;
880 if(!conf.rev_maxtags || rr->ntags <= conf.rev_maxtags)
881 {
882 rr->tags = xrealloc(rr->tags, (rr->ntags+1)*sizeof(rr->tags[0]));
883 if(conf.rev_maxtags && rr->ntags == conf.rev_maxtags)
884 {
885 rr->tags[rr->ntags] = xmalloc(sizeof(tag_t));
886 rr->tags[rr->ntags]->tag = xstrdup("...");
887 rr->tags[rr->ntags]->rev = t->rev;
888 }
889 else
890 rr->tags[rr->ntags] = t;
891 rr->ntags++;
892 }
893 }
894 }
895 }
896
897 /* We need to reset the first in the list of branches to the
898 * active branch to ensure the drawing of all
899 */
900 if(rcs->active != rcs->branches[0])
901 {
902 branch_t **b = bsearch(rcs->active->branch, rcs->branches, rcs->nbranches, sizeof(rcs->branches[0]), search_branch);
903 branch_t *t;
904 assert(b != NULL);
905 t = *b;
906 *b = rcs->branches[0];
907 rcs->branches[0] = t;
908 }
909 }
910
911 /*
912 **************************************************************************
913 * String expansion
914 **************************************************************************
915 */
916 static char *_string;
917 static int _nstring;
918 static int _nastring;
919
920 static void zap_string(void)
921 {
922 _nstring = 0;
923 if(_string)
924 _string[0] = '\0';
925 }
926
927 static char *dup_string(void)
928 {
929 if(_string)
930 return xstrdup(_string);
931 else
932 return "";
933 }
934
935 static void add_string_str(const char *s)
936 {
937 int l = strlen(s) + 1;
938 if(_nstring + l > _nastring)
939 {
940 _nastring += MAX(128, l);
941 _string = xrealloc(_string, _nastring * sizeof(_string[0]));
942 }
943 memcpy(_string+_nstring, s, l);
944 _nstring += l-1;
945 }
946
947 static void add_string_ch(int ch)
948 {
949 char buf[2];
950 buf[0] = ch;
951 buf[1] = '\0';
952 add_string_str(buf);
953 }
954
955 static void add_string_date(const char *d)
956 {
957 struct tm tm, *tmp;
958 int n;
959 time_t t;
960 char *buf;
961 int nbuf;
962
963 memset(&tm, 0, sizeof(tm));
964 n = sscanf(d, "%d.%d.%d.%d.%d.%d",
965 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
966 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
967 tm.tm_mon--;
968 if(tm.tm_year > 1900)
969 tm.tm_year -= 1900;
970 t = mktime(&tm) - timezone;
971 if(n != 6 || t == (time_t)(-1))
972 {
973 add_string_str("<invalid date>");
974 return;
975 }
976
977 tmp = localtime(&t);
978 nbuf = strlen(conf.date_format) * 16; /* Should be enough to hold all types of expansions */
979 buf = xmalloc(nbuf);
980 strftime(buf, nbuf, conf.date_format, tmp);
981 add_string_str(buf);
982 xfree(buf);
983 }
984
985 static void add_string_str_html(const char *s, int maxlen)
986 {
987 int l = 0;
988 char *str = xmalloc(6 * strlen(s) + 1); /* Should hold all char entity-expand */
989 char *cptr = str;
990 for(; *s; s++)
991 {
992 if(maxlen && l > abs(maxlen))
993 {
994 cptr += sprintf(cptr, "...");
995 break;
996 }
997 if(*s < 0x20)
998 {
999 if(*s == '\n')
1000 {
1001 if(maxlen < 0)
1002 *cptr++ = ' ';
1003 else
1004 cptr += sprintf(cptr, "<br%s>", conf.html_level == HTMLLEVEL_X ? " /" : "");
1005 }
1006 }
1007 else if(*s >= 0x7f || *s == '"')
1008 cptr += sprintf(cptr, "&#%d;", (int)(unsigned char)*s);
1009 else if(*s == '<')
1010 cptr += sprintf(cptr, "&lt;");
1011 else if(*s == '>')
1012 cptr += sprintf(cptr, "&gt;");
1013 else if(*s == '&')
1014 cptr += sprintf(cptr, "&amp;");
1015 else if(*s == '"')
1016 cptr += sprintf(cptr, "&quot;");
1017 else
1018 *cptr++ = *s;
1019 l++;
1020 }
1021 *cptr = '\0';
1022 add_string_str(str);
1023 xfree(str);
1024 }
1025
1026 static void add_string_str_len(const char *s, int maxlen)
1027 {
1028 int l = strlen(s);
1029 char *str = xmalloc(l + 1 + 3);
1030 strcpy(str, s);
1031 if(maxlen < l)
1032 sprintf(&str[maxlen], "...");
1033 add_string_str(str);
1034 xfree(str);
1035 }
1036
1037 char *expand_string(const char *s, rcsfile_t *rcs, revision_t *r, rev_t *rev, rev_t *prev, tag_t *tag)
1038 {
1039 char nb[32];
1040 char nr[32];
1041 char *base;
1042 char *exp;
1043 int l;
1044 char ch;
1045
1046 if(!s)
1047 return xstrdup("");
1048
1049 zap_string();
1050
1051 sprintf(nb, "%d", rcs->nbranches);
1052 sprintf(nr, "%d", rcs->nsrev);
1053 for(; *s; s++)
1054 {
1055 if(*s == '%')
1056 {
1057 switch(*++s)
1058 {
1059 case 'c':
1060 case 'C':
1061 add_string_str(conf.cvsroot);
1062 if(*s == 'C' && conf.cvsroot[0] && conf.cvsroot[strlen(conf.cvsroot)-1] == '/')
1063 {
1064 /* Strip the trailing '/' */
1065 _nstring--;
1066 _string[_nstring] = '\0';
1067 }
1068 break;
1069 case 'f':
1070 case 'F':
1071 base = strrchr(rcs->file, '/');
1072 if(!base)
1073 add_string_str(rcs->file);
1074 else
1075 add_string_str(base+1);
1076 if(*s == 'F' && _string[_nstring-1] == 'v' && _string[_nstring-2] == ',')
1077 {
1078 _nstring -= 2;
1079 _string[_nstring] = '\0';
1080 }
1081 break;
1082 case 'p':
1083 base = strrchr(rcs->file, '/');
1084 if(base)
1085 {
1086 char *c = xstrdup(rcs->file);
1087 base = strrchr(c, '/');
1088 assert(base != NULL);
1089 base[1] = '\0';
1090 add_string_str(c);
1091 xfree(c);
1092 }
1093 /*
1094 * We should not add anything here because we can encounter
1095 * a completely empty path, in which case we do not want
1096 * to add any slash. This prevents an inadvertent root redirect.
1097 *
1098 * else
1099 * add_string_str("/");
1100 */
1101 break;
1102 case 'm':
1103 case 'M':
1104 add_string_str(conf.cvsmodule);
1105 if(*s == 'M' && conf.cvsmodule[0] && conf.cvsmodule[strlen(conf.cvsmodule)-1] == '/')
1106 {
1107 /* Strip the trailing '/' */
1108 _nstring--;
1109 _string[_nstring] = '\0';
1110 }
1111 break;
1112 case 'r': add_string_str(nr); break;
1113 case 'b': add_string_str(nb); break;
1114 case '%': add_string_ch('%'); break;
1115 case '0': if(conf.expand[0]) add_string_str(conf.expand[0]); break;
1116 case '1': if(conf.expand[1]) add_string_str(conf.expand[1]); break;
1117 case '2': if(conf.expand[2]) add_string_str(conf.expand[2]); break;
1118 case '3': if(conf.expand[3]) add_string_str(conf.expand[3]); break;
1119 case '4': if(conf.expand[4]) add_string_str(conf.expand[4]); break;
1120 case '5': if(conf.expand[5]) add_string_str(conf.expand[5]); break;
1121 case '6': if(conf.expand[6]) add_string_str(conf.expand[6]); break;
1122 case '7': if(conf.expand[7]) add_string_str(conf.expand[7]); break;
1123 case '8': if(conf.expand[8]) add_string_str(conf.expand[8]); break;
1124 case '9': if(conf.expand[9]) add_string_str(conf.expand[9]); break;
1125 case 'R': if(rev && rev->rev) add_string_str(rev->rev); break;
1126 case 'P': if(prev && prev->rev) add_string_str(prev->rev); break;
1127 case 'B': if(rev && rev->branch) add_string_str(rev->branch); break;
1128 case 't': if(tag && tag->tag) add_string_str(tag->tag); break;
1129 case 'd': if(r && r->delta && r->delta->date) add_string_date(r->delta->date); break;
1130 case 's': if(r && r->delta && r->delta->state) add_string_str(r->delta->state); break;
1131 case 'a': if(r && r->delta && r->delta->author) add_string_str(r->delta->author); break;
1132 case 'L':
1133 case 'l':
1134 ch = *s;
1135 l = 0;
1136 if(s[1] == '[')
1137 {
1138 char *cptr = strchr(s, ']');
1139 char *eptr;
1140 if(cptr)
1141 {
1142 l = strtol(&s[2], &eptr, 10);
1143 if(eptr != cptr)
1144 l = 0;
1145 else
1146 s = cptr;
1147 }
1148 }
1149 if(!conf.parse_logs)
1150 add_string_str("N/A");
1151 else if(r && r->dtext && r->dtext->log)
1152 {
1153 if(ch == 'l')
1154 add_string_str_html(r->dtext->log, l);
1155 else
1156 add_string_str_len(r->dtext->log, abs(l));
1157 }
1158 break;
1159 case '(':
1160 base = dup_string();
1161 exp = expand_string(s+1, rcs, r, rev, prev, tag);
1162 zap_string();
1163 add_string_str(base);
1164 add_string_str_html(exp, 0);
1165 xfree(base);
1166 xfree(exp);
1167 /* Find the %) in this recursion level */
1168 for(; *s; s++)
1169 {
1170 if(*s == '%' && s[1] == ')')
1171 {
1172 s++;
1173 break;
1174 }
1175 }
1176 if(!*s)
1177 {
1178 s--; /* To end outer loop */
1179 if(!quiet)
1180 fprintf(stderr, "string expand: Missing %%) in expansion\n");
1181 }
1182 break;
1183 case ')':
1184 return dup_string();
1185 default:
1186 add_string_ch('%');
1187 add_string_ch(*s);
1188 break;
1189 }
1190 }
1191 else
1192 add_string_ch(*s);
1193 }
1194 return dup_string();
1195 }
1196
1197 /*
1198 **************************************************************************
1199 * Drawing routines
1200 **************************************************************************
1201 */
1202 static int get_swidth(const char *s, font_t *f)
1203 {
1204 int n;
1205 int m;
1206 if(!s || !*s)
1207 return 0;
1208
1209 #if defined(HAVE_GDIMAGESTRINGFT) || defined(HAVE_GDIMAGESTRINGTTF)
1210 if(conf.use_ttf && f->ttfont)
1211 {
1212 int bb[8];
1213 char *e;
1214 #ifdef HAVE_GDIMAGESTRINGFT
1215 e = gdImageStringFT(NULL, bb, 0, f->ttfont, f->ttsize, 0.0, 0, 0, (char *)s);
1216 #else
1217 e = gdImageStringTTF(NULL, bb, 0, f->ttfont, f->ttsize, 0.0, 0, 0, (char *)s);
1218 #endif
1219 if(!e)
1220 return bb[2] - bb[6];
1221 }
1222 #endif
1223 for(n = m = 0; *s; n++, s++)
1224 {
1225 if(*s == '\n')
1226 {
1227 if(n > m)
1228 m = n;
1229 n = 0;
1230 }
1231 }
1232 if(n > m)
1233 m = n;
1234 return f->gdfont ? m * f->gdfont->w : m;
1235 }
1236
1237 static int get_sheight(const char *s, font_t *f)
1238 {
1239 int nl;
1240 if(!s || !*s)
1241 return 0;
1242
1243 #if defined(HAVE_GDIMAGESTRINGFT) || defined(HAVE_GDIMAGESTRINGTTF)
1244 if(conf.use_ttf && f->ttfont)
1245 {
1246 int bb[8];
1247 char *e;
1248 #ifdef HAVE_GDIMAGESTRINGFT
1249 e = gdImageStringFT(NULL, bb, 0, f->ttfont, f->ttsize, 0.0, 0, 0, (char *)s);
1250 #else
1251 e = gdImageStringTTF(NULL, bb, 0, f->ttfont, f->ttsize, 0.0, 0, 0, (char *)s);
1252 #endif
1253 if(!e)
1254 return bb[3] - bb[7] + 4;
1255 }
1256 #endif
1257 for(nl = 1; *s; s++)
1258 {
1259 if(*s == '\n' && s[1])
1260 nl++;
1261 }
1262 return nl * f->gdfont->h;
1263 }
1264
1265 static void draw_rbox(gdImagePtr im, int x1, int y1, int x2, int y2, int r, color_t *color, color_t *bgcolor)
1266 {
1267 int r2 = 2*r;
1268 gdImageLine(im, x1+r, y1, x2-r, y1, color->id);
1269 gdImageLine(im, x1+r, y2, x2-r, y2, color->id);
1270 gdImageLine(im, x1, y1+r, x1, y2-r, color->id);
1271 gdImageLine(im, x2, y1+r, x2, y2-r, color->id);
1272 if(conf.box_shadow)
1273 {
1274 gdImageLine(im, x1+r+1, y2+1, x2-r, y2+1, black_color.id);
1275 gdImageLine(im, x2+1, y1+r+1, x2+1, y2-r, black_color.id);
1276 }
1277 if(r)
1278 {
1279 /* FIXME: Pixelization is not perfect */
1280 gdImageArc(im, x1+r, y1+r, r2, r2, 180, 270, color->id);
1281 gdImageArc(im, x2-r, y1+r, r2, r2, 270, 360, color->id);
1282 gdImageArc(im, x1+r, y2-r, r2, r2, 90, 180, color->id);
1283 if(conf.box_shadow)
1284 {
1285 gdImageArc(im, x2-r+1, y2-r+1, r2, r2, 0, 90, black_color.id);
1286 gdImageArc(im, x2-r+1, y2-r, r2, r2, 0, 90, black_color.id);
1287 gdImageArc(im, x2-r, y2-r+1, r2, r2, 0, 90, black_color.id);
1288 }
1289 gdImageArc(im, x2-r, y2-r, r2, r2, 0, 90, color->id);
1290 }
1291 #ifndef NOGDFILL
1292 gdImageFillToBorder(im, (x1+x2)/2, (y1+y2)/2, color->id, bgcolor->id);
1293 #endif
1294 }
1295
1296 static void draw_string(gdImagePtr im, char *s, font_t *f, int x, int y, int align, color_t *c)
1297 {
1298 int h = get_sheight(s, f);
1299 int xx, yy;
1300 switch(align & ALIGN_HX)
1301 {
1302 default:
1303 case ALIGN_HL: xx = 0; break;
1304 case ALIGN_HC: xx = -get_swidth(s, f)/2; break;
1305 case ALIGN_HR: xx = -get_swidth(s, f); break;
1306 }
1307 switch(align & ALIGN_VX)
1308 {
1309 default:
1310 case ALIGN_VT: yy = 0; break;
1311 case ALIGN_VC: yy = h/2; break;
1312 case ALIGN_VB: yy = h; break;
1313 }
1314 #if defined(HAVE_GDIMAGESTRINGFT) || defined(HAVE_GDIMAGESTRINGTTF)
1315 if(conf.use_ttf && f->ttfont)
1316 {
1317 int bb[8];
1318 char *e;
1319 int cid = conf.anti_alias ? c->id : -c->id;
1320 #ifdef HAVE_GDIMAGESTRINGFT
1321 e = gdImageStringFT(im, bb, cid, f->ttfont, f->ttsize, 0.0, x+xx, y+yy+h-2, (char *)s);
1322 #else
1323 e = gdImageStringTTF(im, bb, cid, f->ttfont, f->ttsize, 0.0, x+xx, y+yy+h-2, (char *)s);
1324 #endif
1325 if(!e)
1326 return;
1327 }
1328 #endif
1329 yy = -yy;
1330 gdImageString(im, f->gdfont, x+xx+1, y+yy, s, c->id);
1331 }
1332
1333 static void draw_stringnl(gdImagePtr im, char *s, font_t *f, int x, int y, int align, color_t *c)
1334 {
1335 char *t;
1336 char *d;
1337 d = s = xstrdup(s);
1338 do
1339 {
1340 t = strchr(s, '\n');
1341 if(t)
1342 *t = '\0';
1343 draw_string(im, s, f, x, y, align, c);
1344 y += get_sheight(s, f);
1345 s = t+1;
1346 } while(t);
1347 xfree(d);
1348 }
1349
1350 static void draw_rev(gdImagePtr im, revision_t *r)
1351 {
1352 int lx;
1353 int rx;
1354 int x2;
1355 int i;
1356 int ty;
1357
1358 if(conf.left_right)
1359 {
1360 lx = r->cx;
1361 rx = r->cx + r->w;
1362 ty = r->y - r->h/2;
1363 x2 = r->cx + r->w/2;
1364 }
1365 else
1366 {
1367 lx = r->cx - r->w/2;
1368 rx = lx + r->w;
1369 ty = r->y;
1370 x2 = r->cx;
1371 }
1372 draw_rbox(im, lx, ty, rx, ty+r->h, 0, &conf.rev_color, &conf.rev_bgcolor);
1373 ty += conf.rev_tspace;
1374 draw_string(im, r->rev->rev, &conf.rev_font, x2, ty, ALIGN_HC, &conf.rev_color);
1375 ty += get_sheight(r->rev->rev, &conf.rev_font);
1376 draw_stringnl(im, r->revtext, &conf.rev_text_font, x2, ty, ALIGN_HC, &conf.rev_text_color);
1377 ty += get_sheight(r->revtext, &conf.rev_text_font);
1378 for(i = 0; i < r->ntags; i++)
1379 {
1380 draw_string(im, r->tags[i]->tag, &conf.tag_font, x2, ty, ALIGN_HC, &conf.tag_color);
1381 ty += get_sheight(r->tags[i]->tag, &conf.tag_font) + conf.rev_separator;
1382 }
1383 }
1384
1385 static void draw_branch_box(gdImagePtr im, branch_t *b, int xp, int yp)
1386 {
1387 int lx;
1388 int rx;
1389 int i;
1390 int yy;
1391 int x2;
1392
1393 if(conf.left_right)
1394 {
1395 lx = b->cx;
1396 rx = lx + b->w;
1397 x2 = b->cx + b->w/2;
1398 }
1399 else
1400 {
1401 lx = b->cx - b->w/2;
1402 rx = lx + b->w;
1403 x2 = b->cx;
1404 }
1405 draw_rbox(im, lx+xp, yp, rx+xp, yp+b->h, 5, &conf.branch_color, &conf.branch_bgcolor);
1406 yy = conf.branch_tspace;
1407 if(!b->nfolds)
1408 {
1409 draw_string(im, b->branch->branch, &conf.branch_font, x2+xp, yp+yy, ALIGN_HC, &conf.branch_color);
1410 yy += get_sheight(b->branch->branch, &conf.branch_font);
1411 for(i = 0; i < b->ntags; i++)
1412 {
1413 draw_string(im, b->tags[i]->tag, &conf.branch_tag_font, x2+xp, yp+yy, ALIGN_HC, &conf.branch_tag_color);
1414 yy += get_sheight(b->tags[i]->tag, &conf.branch_tag_font);
1415 }
1416 }
1417 else
1418 {
1419 int y1, y2;
1420 int tx = lx + b->fw + conf.branch_lspace;
1421 int nx = tx - get_swidth(" ", &conf.branch_font);
1422 draw_string(im, b->branch->branch, &conf.branch_font, nx+xp, yp+yy, ALIGN_HR, &conf.branch_color);
1423 y1 = get_sheight(b->branch->branch, &conf.branch_font);
1424 draw_string(im, b->tags[0]->tag, &conf.branch_tag_font, tx+xp, yp+yy, ALIGN_HL, &conf.branch_tag_color);
1425 y2 = get_sheight(b->tags[0]->tag, &conf.branch_font);
1426 yy += MAX(y1, y2);
1427 for(i = 0; i < b->nfolds; i++)
1428 {
1429 draw_string(im, b->folds[i]->branch->branch, &conf.branch_font, nx+xp, yp+yy, ALIGN_HR, &conf.branch_color);
1430 y1 = get_sheight(b->folds[i]->branch->branch, &conf.branch_font);
1431 draw_string(im, b->folds[i]->tags[0]->tag, &conf.branch_tag_font, tx+xp, yp+yy, ALIGN_HL, &conf.branch_tag_color);
1432 y2 = get_sheight(b->folds[i]->tags[0]->tag, &conf.branch_tag_font);
1433 yy += MAX(y1, y2);
1434 }
1435 }
1436 }
1437
1438 static void draw_branch(gdImagePtr im, branch_t *b)
1439 {
1440 int yy, xx;
1441 int i;
1442 int line[4];
1443 int l;
1444 int sign;
1445
1446 line[0] = conf.rev_color.id;
1447 line[1] = gdTransparent;
1448 line[1] = gdTransparent;
1449 line[3] = conf.rev_color.id;
1450
1451 draw_branch_box(im, b, 0, conf.left_right ? b->y - b->h/2 : b->y);
1452
1453 if(conf.left_right)
1454 {
1455 if(conf.upside_down)
1456 {
1457 xx = b->cx;
1458 for(i = 0; i < b->nrevs; i++)
1459 {
1460 revision_t *r = b->revs[i];
1461 gdImageSetStyle(im, line, r->stripped ? 4 : 1);
1462 gdImageLine(im, xx, r->y, r->cx+r->w, r->y, gdStyled);
1463 for(sign = l = 1; l < conf.thick_lines; l++)
1464 {
1465 int pp = (l+1)/2*sign;
1466 gdImageLine(im, xx, r->y+pp, r->cx+r->w, r->y+pp, gdStyled);
1467 sign *= -1;
1468 }
1469 draw_rev(im, r);
1470 xx = r->cx;
1471 }
1472 if(conf.branch_dupbox && b->nrevs)
1473 {
1474 i = b->cx - b->tw + b->w;
1475 gdImageLine(im, xx, b->y, i+b->w, b->y, conf.rev_color.id);
1476 for(sign = l = 1; l < conf.thick_lines; l++)
1477 {
1478 int pp = (l+1)/2*sign;
1479 gdImageLine(im, xx, b->y+pp, i+b->w, b->y+pp, conf.rev_color.id);
1480 sign *= -1;
1481 }
1482 draw_branch_box(im, b, i - b->cx, b->y - b->h/2);
1483 }
1484 }
1485 else
1486 {
1487 xx = b->cx + b->w;
1488 for(i = 0; i < b->nrevs; i++)
1489 {
1490 revision_t *r = b->revs[i];
1491 gdImageSetStyle(im, line, r->stripped ? 4 : 1);
1492 gdImageLine(im, xx, r->y, r->cx, r->y, gdStyled);
1493 for(sign = l = 1; l < conf.thick_lines; l++)
1494 {
1495 int pp = (l+1)/2*sign;
1496 gdImageLine(im, xx, r->y+pp, r->cx, r->y+pp, gdStyled);
1497 sign *= -1;
1498 }
1499 draw_rev(im, r);
1500 xx = r->cx + r->w;
1501 }
1502 if(conf.branch_dupbox && b->nrevs)
1503 {
1504 i = b->cx + b->tw - b->w;
1505 gdImageLine(im, xx, b->y, i, b->y, conf.rev_color.id);
1506 for(sign = l = 1; l < conf.thick_lines; l++)
1507 {
1508 int pp = (l+1)/2*sign;
1509 gdImageLine(im, xx, b->y+pp, i, b->y+pp, conf.rev_color.id);
1510 sign *= -1;
1511 }
1512 draw_branch_box(im, b, i - b->cx, b->y - b->h/2);
1513 }
1514 }
1515 }
1516 else
1517 {
1518 if(conf.upside_down)
1519 {
1520 yy = b->y;
1521 for(i = 0; i < b->nrevs; i++)
1522 {
1523 revision_t *r = b->revs[i];
1524 gdImageSetStyle(im, line, r->stripped ? 4 : 1);
1525 gdImageLine(im, r->cx, yy, r->cx, r->y+r->h, gdStyled);
1526 for(sign = l = 1; l < conf.thick_lines; l++)
1527 {
1528 int pp = (l+1)/2*sign;
1529 gdImageLine(im, r->cx+pp, yy, r->cx+pp, r->y+r->h, gdStyled);
1530 sign *= -1;
1531 }
1532 draw_rev(im, r);
1533 yy = r->y;
1534 }
1535 if(conf.branch_dupbox && b->nrevs)
1536 {
1537 i = b->y - b->th + b->h;
1538 gdImageLine(im, b->cx, yy, b->cx, i, conf.rev_color.id);
1539 for(sign = l = 1; l < conf.thick_lines; l++)
1540 {
1541 int pp = (l+1)/2*sign;
1542 gdImageLine(im, b->cx+pp, yy, b->cx+pp, i, conf.rev_color.id);
1543 sign *= -1;
1544 }
1545 draw_branch_box(im, b, 0, i);
1546 }
1547 }
1548 else
1549 {
1550 yy = b->y + b->h;
1551 for(i = 0; i < b->nrevs; i++)
1552 {
1553 revision_t *r = b->revs[i];
1554 gdImageSetStyle(im, line, r->stripped ? 4 : 1);
1555 gdImageLine(im, r->cx, yy, r->cx, r->y, gdStyled);
1556 for(sign = l = 1; l < conf.thick_lines; l++)
1557 {
1558 int pp = (l+1)/2*sign;
1559 gdImageLine(im, r->cx+pp, yy, r->cx+pp, r->y, gdStyled);
1560 sign *= -1;
1561 }
1562 draw_rev(im, r);
1563 yy = r->y + r->h;
1564 }
1565 if(conf.branch_dupbox && b->nrevs)
1566 {
1567 i = b->y + b->th - b->h;
1568 gdImageLine(im, b->cx, yy, b->cx, i, conf.rev_color.id);
1569 for(sign = l = 1; l < conf.thick_lines; l++)
1570 {
1571 int pp = (l+1)/2*sign;
1572 gdImageLine(im, b->cx+pp, yy, b->cx+pp, i, conf.rev_color.id);
1573 sign *= -1;
1574 }
1575 draw_branch_box(im, b, 0, i);
1576 }
1577 }
1578 }
1579 }
1580
1581 static void draw_connector(gdImagePtr im, branch_t *b)
1582 {
1583 int l;
1584 int sign;
1585 revision_t *r = b->branchpoint;
1586 int x1 = r->cx + r->w/2 + 2;
1587 int y1 = r->y + r->h/2;
1588 int x2 = b->cx;
1589 int y2 = b->y;
1590
1591 if(conf.left_right)
1592 {
1593 x2 = r->cx + r->w/2;
1594 y2 = r->y + r->h/2 + 3;
1595 x1 = b->cx;
1596 y1 = b->y;
1597 if(conf.upside_down)
1598 x1 += b->w;
1599 }
1600 else
1601 {
1602 x1 = r->cx + r->w/2 + 2;
1603 y1 = r->y + r->h/2;
1604 x2 = b->cx;
1605 y2 = b->y;
1606 if(conf.upside_down)
1607 y2 += b->h;
1608 }
1609 gdImageLine(im, x1, y1, x2, y1, conf.branch_color.id);
1610 gdImageLine(im, x2, y1, x2, y2, conf.branch_color.id);
1611 for(sign = l = 1; l < conf.thick_lines; l++)
1612 {
1613 int pp = (l+1)/2*sign;
1614 gdImageLine(im, x1, y1+pp, x2, y1+pp, conf.branch_color.id);
1615 gdImageLine(im, x2+pp, y1, x2+pp, y2, conf.branch_color.id);
1616 sign *= -1;
1617 }
1618 }
1619
1620 static void draw_merges(gdImagePtr im, rcsfile_t *rcs, int dot)
1621 {
1622 int i;
1623 for(i = 0; i < rcs->nmerges; i++)
1624 {
1625 revision_t *fr = rcs->merges[i].from->logrev;
1626 revision_t *tr = rcs->merges[i].to->logrev;
1627 int x1, x2, y1, y2;
1628 if(!fr || !tr || fr == tr)
1629 continue; /* This can happen with detached tags and self-references */
1630 if(conf.left_right)
1631 {
1632 if(fr->branch == tr->branch)
1633 {
1634 y1 = fr->y - fr->h/2;
1635 y2 = tr->y - tr->h/2;
1636 }
1637 else
1638 {
1639 if(fr->y < tr->y)
1640 {
1641 y1 = fr->y + fr->h/2;
1642 y2 = tr->y - tr->h/2;
1643 }
1644 else
1645 {
1646 y1 = fr->y - fr->h/2;
1647 y2 = tr->y + tr->h/2;
1648 }
1649 }
1650 x1 = fr->cx + fr->w/2;
1651 x2 = tr->cx + tr->w/2;
1652 }
1653 else
1654 {
1655 if(fr->branch == tr->branch)
1656 {
1657 x1 = fr->cx - fr->w/2;
1658 x2 = tr->cx - tr->w/2;
1659 }
1660 else
1661 {
1662 if(fr->cx < tr->cx)
1663 {
1664 x1 = fr->cx + fr->w/2;
1665 x2 = tr->cx - tr->w/2;
1666 }
1667 else
1668 {
1669 x1 = fr->cx - fr->w/2;
1670 x2 = tr->cx + tr->w/2;
1671 }
1672 }
1673 y1 = fr->y + rcs->merges[i].from->yofs;
1674 y2 = tr->y + rcs->merges[i].to->yofs;
1675 }
1676 if(dot && !conf.merge_arrows)
1677 {
1678 int o = conf.left_right ? 1 : 0;
1679 gdImageArc(im, x2, y2+o, 8, 8, 0, 360, conf.merge_color.id);
1680 gdImageFillToBorder(im, x2+1, y2+o+1, conf.merge_color.id, conf.merge_color.id);
1681 }
1682 else if(dot && conf.merge_arrows)
1683 {
1684 /*
1685 * Arrow patch from Haroon Rafique <haroon.rafique@utoronto.ca>
1686 * Slightly adapted to be more configurable.
1687 */
1688 int sx, sy; /* start point coordinates */
1689 int ex, ey; /* end point coordinates */
1690 double theta;
1691 double u1, v1, u2, v2;
1692 gdPoint p[3];
1693
1694 sx = x1; sy = y1;
1695 ex = x2; ey = y2;
1696 if(conf.left_right)
1697 {
1698 if(fr->branch == tr->branch)
1699 {
1700 int yy = (y1 < y2 ? y1 : y2) - 5;
1701 /* line from (x1,yy) to (x2,yy) */
1702 sy = ey = yy;
1703 }
1704 else
1705 {
1706 if(y1 > y2)
1707 {
1708 /* line from (x1,y1-3) to (x2,y2+3+1) */
1709 sy = y1-3;
1710 ey = y2+3+1;
1711 }
1712 else
1713 {
1714 /* line from (x1,y1+3+1) to (x2,y2-3) */
1715 sy = y1+3+1;
1716 ey = y2-3;
1717 }
1718 }
1719 }
1720 else
1721 {
1722 if(fr->branch == tr->branch)
1723 {
1724 int xx = (x1 < x2 ? x1 : x2) - 5;
1725 /* line from (xx,y1) to (xx,y2) */
1726 sx = ex = xx;
1727 }
1728 else
1729 {
1730 if(x1 > x2)
1731 {
1732 /* line from (x1-3,y1) to (x2+3,y2) */
1733 sx = x1-3;
1734 ex = x2+3;
1735 }
1736 else
1737 {
1738 /* line from (x1+3,y1) to (x2-3,y2) */
1739 sx = x1+3;
1740 ex = x2-3;
1741 }
1742 }
1743 }
1744 /*
1745 * inspiration for arrow code comes from arrows.c in the
1746 * graphviz package. Thank you, AT&T
1747 */
1748 /* theta in radians */
1749 theta = atan2((double)(sy-ey), (double)(sx-ex));
1750 u1 = (double)conf.arrow_length * cos(theta);
1751 v1 = (double)conf.arrow_length * sin(theta);
1752 u2 = (double)conf.arrow_width * cos(theta + M_PI/2.0);
1753 v2 = (double)conf.arrow_width * sin(theta + M_PI/2.0);
1754 /* points of polygon (triangle) */
1755 p[0].x = ROUND(ex + u1 - u2);
1756 p[0].y = ROUND(ey + v1 - v2);
1757 p[1].x = ex;
1758 p[1].y = ey;
1759 p[2].x = ROUND(ex + u1 + u2);
1760 p[2].y = ROUND(ey + v1 + v2);
1761 /* draw the polygon (triangle) */
1762 gdImageFilledPolygon(im, p, 3, conf.merge_color.id);
1763 }
1764 else
1765 {
1766 if(conf.left_right)
1767 {
1768 if(fr->branch == tr->branch)
1769 {
1770 int yy = (y1 < y2 ? y1 : y2) - 5;
1771 gdImageLine(im, x1, y1, x1, yy, conf.merge_color.id);
1772 gdImageLine(im, x2, y2, x2, yy, conf.merge_color.id);
1773 gdImageLine(im, x1, yy, x2, yy, conf.merge_color.id);
1774 }
1775 else
1776 {
1777 if(y1 > y2)
1778 {
1779 gdImageLine(im, x1, y1, x1, y1-3, conf.merge_color.id);
1780 gdImageLine(im, x2, y2+1, x2, y2+3+1, conf.merge_color.id);
1781 gdImageLine(im, x1, y1-3, x2, y2+3+1, conf.merge_color.id);
1782 }
1783 else
1784 {
1785 gdImageLine(im, x1, y1+1, x1, y1+3+1, conf.merge_color.id);
1786 gdImageLine(im, x2, y2, x2, y2-3, conf.merge_color.id);
1787 gdImageLine(im, x1, y1+3+1, x2, y2-3, conf.merge_color.id);
1788 }
1789 }
1790 }
1791 else
1792 {
1793 if(fr->branch == tr->branch)
1794 {
1795 int xx = (x1 < x2 ? x1 : x2) - 5;
1796 gdImageLine(im, xx, y1, x1, y1, conf.merge_color.id);
1797 gdImageLine(im, xx, y2, x2, y2, conf.merge_color.id);
1798 gdImageLine(im, xx, y1, xx, y2, conf.merge_color.id);
1799 }
1800 else
1801 {
1802 if(x1 > x2)
1803 {
1804 gdImageLine(im, x1, y1, x1-3, y1, conf.merge_color.id);
1805 gdImageLine(im, x2, y2, x2+3, y2, conf.merge_color.id);
1806 gdImageLine(im, x1-3, y1, x2+3, y2, conf.merge_color.id);
1807 }
1808 else
1809 {
1810 gdImageLine(im, x1, y1, x1+3, y1, conf.merge_color.id);
1811 gdImageLine(im, x2, y2, x2-3, y2, conf.merge_color.id);
1812 gdImageLine(im, x1+3, y1, x2-3, y2, conf.merge_color.id);
1813 }
1814 }
1815 }
1816 }
1817 }
1818 }
1819
1820 static void alloc_color(gdImagePtr im, color_t *c)
1821 {
1822 c->id = gdImageColorAllocate(im, c->r, c->g, c->b);
1823 }
1824
1825 gdImagePtr make_image(rcsfile_t *rcs)
1826 {
1827 gdImagePtr im;
1828 int i;
1829 char *cptr;
1830
1831 cptr = expand_string(conf.title, rcs, NULL, NULL, NULL, NULL);
1832 i = get_swidth(cptr, &conf.title_font);
1833 if(rcs->tw+conf.margin_left+conf.margin_right > i)
1834 i = rcs->tw+conf.margin_left+conf.margin_right;
1835 im = gdImageCreate(i, rcs->th+conf.margin_top+conf.margin_bottom);
1836 alloc_color(im, &conf.color_bg);
1837 alloc_color(im, &conf.tag_color);
1838 alloc_color(im, &conf.rev_color);
1839 alloc_color(im, &conf.rev_bgcolor);
1840 alloc_color(im, &conf.rev_text_color);
1841 alloc_color(im, &conf.branch_color);
1842 alloc_color(im, &conf.branch_tag_color);
1843 alloc_color(im, &conf.branch_bgcolor);
1844 alloc_color(im, &conf.title_color);
1845 alloc_color(im, &conf.merge_color);
1846 alloc_color(im, &black_color);
1847 alloc_color(im, &white_color);
1848
1849 if(conf.transparent_bg)
1850 gdImageColorTransparent(im, conf.color_bg.id);
1851
1852 if(!conf.merge_front)
1853 draw_merges(im, rcs, 0);
1854
1855 for(i = 0; i < rcs->nbranches; i++)
1856 {
1857 if(!rcs->branches[i]->folded)
1858 draw_branch(im, rcs->branches[i]);
1859 }
1860
1861 draw_merges(im, rcs, 1); /* The dots of the merge dest */
1862
1863 for(i = 0; i < rcs->nbranches; i++)
1864 {
1865 if(rcs->branches[i]->branchpoint)
1866 draw_connector(im, rcs->branches[i]);
1867 }
1868 draw_stringnl(im, cptr, &conf.title_font, conf.title_x, conf.title_y, conf.title_align, &conf.title_color);
1869 xfree(cptr);
1870
1871 if(conf.merge_front)
1872 draw_merges(im, rcs, 0);
1873
1874 return im;
1875 }
1876
1877 /*
1878 **************************************************************************
1879 * Layout routines
1880 *
1881 * Branch BBox:
1882 * left = center_x - total_width / 2 (cx-tw)/2
1883 * right = center_x + total_width / 2 (cx+tw)/2
1884 * top = y_pos (y)
1885 * bottom = y_pos + total_height (y+th)
1886 *
1887 * Margins of branches:
1888 *
1889 * . .
1890 * . .
1891 * +--------------+
1892 * ^
1893 * | branch_margin .
1894 * v .
1895 * ----------------+ .
1896 * | ^ |
1897 * | | branch_connect |
1898 * | v |
1899 *..-+ +t-----+------+ +------+------+
1900 * | l | | |
1901 * | <--> | branch bbox | <--> | branch bbox |
1902 * | | | r | | |
1903 *..-+ | +------------b+ | +-------------+
1904 * | ^ branch_margin
1905 * | | branch_margin
1906 * | v
1907 * | +-------------+
1908 * | . .
1909 * | . .
1910 * |
1911 * branch_margin
1912 *
1913 * FIXME: There are probable som +/-1 errors in the code...
1914 * (notably shadows are not calculated in the margins)
1915 **************************************************************************
1916 */
1917 static void move_branch(branch_t *b, int x, int y)
1918 {
1919 int i;
1920 b->cx += x;
1921 b->y += y;
1922 for(i = 0; i < b->nrevs; i++)
1923 {
1924 b->revs[i]->cx += x;
1925 b->revs[i]->y += y;
1926 }
1927 }
1928
1929 static void initial_reposition_branch(revision_t *r, int *x, int *w)
1930 {
1931 int i, j;
1932 for(j = 0; j < r->nbranches; j++)
1933 {
1934 branch_t *b = r->branches[j];
1935 *x += *w + conf.rev_minline + b->tw/2 - b->cx;
1936 *w = b->tw/2;
1937 move_branch(b, *x, r->y + r->h/2 + conf.branch_connect);
1938 *x = b->cx;
1939 /* Recurse to move branches of branched revisions */
1940 for(i = b->nrevs-1; i >= 0; i--)
1941 {
1942 initial_reposition_branch(b->revs[i], x, w);
1943 }
1944 }
1945 }
1946
1947 static void initial_reposition_branch_lr(revision_t *r, int *y, int *h)
1948 {
1949 int i, j;
1950 for(j = 0; j < r->nbranches; j++)
1951 {
1952 branch_t *b = r->branches[j];
1953 *y += *h + conf.rev_minline + b->th/2 - b->y;
1954 *h = b->th/2;
1955 move_branch(b, r->cx + r->w/2 + conf.branch_connect, *y);
1956 *y = b->y;
1957 /* Recurse to move branches of branched revisions */
1958 for(i = b->nrevs-1; i >= 0; i--)
1959 {
1960 initial_reposition_branch_lr(b->revs[i], y, h);
1961 }
1962 }
1963 }
1964
1965 static void rect_union(int *x, int *y, int *w, int *h, branch_t *b)
1966 {
1967 int x1 = *x;
1968 int x2 = x1 + *w;
1969 int y1 = *y;
1970 int y2 = y1 + *h;
1971 int xx1;
1972 int xx2;
1973 int yy1;
1974 int yy2;
1975
1976 if(conf.left_right)
1977 {
1978 xx1 = b->cx;
1979 yy1 = b->y - b->th/2;
1980 }
1981 else
1982 {
1983 xx1 = b->cx - b->tw/2;
1984 yy1 = b->y;
1985 }
1986 xx2 = xx1 + b->tw;
1987 yy2 = yy1 + b->th;
1988
1989 x1 = MIN(x1, xx1);
1990 x2 = MAX(x2, xx2);
1991 y1 = MIN(y1, yy1);
1992 y2 = MAX(y2, yy2);
1993 *x = x1;
1994 *y = y1;
1995 *w = x2 - x1;
1996 *h = y2 - y1;
1997 }
1998
1999 static int branch_intersects(int top, int bottom, int left, branch_t *b)
2000 {
2001 int br = b->cx + b->tw/2;
2002 int bt = b->y - conf.branch_connect - conf.branch_margin/2;
2003 int bb = b->y + b->th + conf.branch_margin/2;
2004 return !(bt > bottom || bb < top || br >= left);
2005 }
2006
2007 static int branch_intersects_lr(int left, int right, int top, branch_t *b)
2008 {
2009 int bt = b->y + b->th/2;
2010 int bl = b->cx - conf.branch_connect - conf.branch_margin/2;
2011 int br = b->cx + b->tw + conf.branch_margin/2;
2012 return !(bl > right || br < left || bt >= top);
2013 }
2014
2015 static int kern_branch(rcsfile_t *rcs, branch_t *b)
2016 {
2017 int left = b->cx - b->tw/2;
2018 int top = b->y - conf.branch_connect - conf.branch_margin/2;
2019 int bottom = b->y + b->th + conf.branch_margin/2;
2020 int i;
2021 int xpos = 0;
2022
2023 for(i = 0; i < rcs->nbranches; i++)
2024 {
2025 branch_t *bp = rcs->branches[i];
2026 if(bp == b)
2027 continue;
2028 if(branch_intersects(top, bottom, left, bp))
2029 {
2030 int m = bp->cx + bp->tw/2 + conf.branch_margin;
2031 if(m > xpos)
2032 xpos = m;
2033 }
2034 }
2035 if(xpos && (b->cx - b->tw/2) - xpos > 0)
2036 {
2037 move_branch(b, xpos - (b->cx - b->tw/2), 0);
2038 return 1;
2039 }
2040 return 0;
2041 }
2042
2043 static int kern_branch_lr(rcsfile_t *rcs, branch_t *b)
2044 {
2045 int top = b->y - b->th/2;
2046 int left = b->cx - conf.branch_connect - conf.branch_margin/2;
2047 int right = b->cx + b->tw + conf.branch_margin/2;
2048 int i;
2049 int ypos = 0;
2050
2051 for(i = 0; i < rcs->nbranches; i++)
2052 {
2053 branch_t *bp = rcs->branches[i];
2054 if(bp == b)
2055 continue;
2056 if(branch_intersects_lr(left, right, top, bp))
2057 {
2058 int m = bp->y + bp->th/2 + conf.branch_margin;
2059 if(m > ypos)
2060 ypos = m;
2061 }
2062 }
2063 if(ypos && (b->y - b->th/2) - ypos > 0)
2064 {
2065 move_branch(b, 0, ypos - (b->y - b->th/2));
2066 return 1;
2067 }
2068 return 0;
2069 }
2070
2071 static int kern_tree(rcsfile_t *rcs)
2072 {
2073 int i;
2074 int moved;
2075 int safeguard;
2076 int totalmoved = 0;
2077 for(moved = 1, safeguard = LOOPSAFEGUARD; moved && safeguard; safeguard--)
2078 {
2079 moved = 0;
2080 for(i = 1; i < rcs->nbranches; i++)
2081 {
2082 if(conf.left_right)
2083 moved += kern_branch_lr(rcs, rcs->branches[i]);
2084 else
2085 moved += kern_branch(rcs, rcs->branches[i]);
2086 }
2087 totalmoved += moved;
2088 #ifdef DEBUG
2089 fprintf(stderr, "kern_tree: moved=%d\n", moved);
2090 #endif
2091 }
2092 if(!quiet && !safeguard)
2093 fprintf(stderr, "kern_tree: safeguard terminated possible infinite loop; please report.\n");
2094 return totalmoved;
2095 }
2096
2097 static int index_of_revision(revision_t *r)
2098 {
2099 branch_t *b = r->branch;
2100 int i;
2101 for(i = 0; i < b->nrevs; i++)
2102 {
2103 if(r == b->revs[i])
2104 return i;
2105 }
2106 fprintf(stderr, "index_of_revision: Cannot find revision in branch\n");
2107 return 0;
2108 }
2109
2110 static void branch_bbox(branch_t *br, int *l, int *r, int *t, int *b)
2111 {
2112 if(l) *l = br->cx - br->tw/2;
2113 if(r) *r = br->cx + br->tw/2;
2114 if(t) *t = br->y;
2115 if(b) *b = br->y + br->th + ((conf.branch_dupbox && br->nrevs) ? conf.rev_minline + br->h : 0);
2116 }
2117
2118 static void branch_ext_bbox(branch_t *br, int *l, int *r, int *t, int *b)
2119 {
2120 int extra = conf.branch_margin & 1; /* Correct +/-1 error on div 2 */
2121 branch_bbox(br, l, r, t, b);
2122 if(l) *l -= conf.branch_margin/2;
2123 if(r) *r += conf.branch_margin/2 + extra;
2124 if(t) *t -= conf.branch_connect + conf.branch_margin/2;
2125 if(b) *b += conf.branch_margin/2 + extra;
2126 }
2127
2128 static int branch_distance(branch_t *br1, branch_t *br2)
2129 {
2130 int l1, r1, t1, b1;
2131 int l2, r2, t2, b2;
2132 assert(br1 != NULL);
2133 assert(br2 != NULL);
2134 branch_bbox(br1, &l1, &r1, NULL, NULL);
2135 branch_bbox(br2, &l2, &r2, NULL, NULL);
2136 branch_ext_bbox(br1, NULL, NULL, &t1, &b1);
2137 branch_ext_bbox(br2, NULL, NULL, &t2, &b2);
2138 /* Return:
2139 * - 0 if branches have no horizontal overlap
2140 * - positive if b1 is left of b2
2141 * - negative if b2 is left of b1
2142 */
2143 if((t1 > t2 && t1 < b2) || (b1 > t2 && b1 < b2))
2144 return l1 < l2 ? l2 - r1 : -(l1 - r2);
2145 else
2146 return 0;
2147 }
2148
2149 static int space_needed(branch_t *br1, branch_t *br2)
2150 {
2151 int t1, b1;
2152 int t2, b2;
2153 assert(br1 != NULL);
2154 assert(br2 != NULL);
2155 assert(br1->cx < br2->cx); /* br1 must be left of br2 */
2156 branch_ext_bbox(br1, NULL, NULL, &t1, &b1);
2157 branch_ext_bbox(br2, NULL, NULL, &t2, &b2);
2158 /* Return:
2159 * - positive if top br1 is located lower than br2
2160 * - negatve is top br2 is located lower than br1
2161 */
2162 if(t1 > t2)
2163 return -(t1 - b2);
2164 else
2165 return t2 - b1;
2166 }
2167
2168 static void move_yr_branch(branch_t *b, int dy)
2169 {
2170 int i, j;
2171 #ifdef DEBUG
2172 /* fprintf(stderr, "move_yr_branch: b=%s, dy=%d\n", b->branch->branch, dy);*/
2173 #endif
2174 b->y += dy;
2175 for(i = 0; i < b->nrevs; i++)
2176 {
2177 b->revs[i]->y += dy;
2178 for(j = 0; j < b->revs[i]->nbranches; j++)
2179 {
2180 #ifdef DEBUG
2181 /* fprintf(stderr, ".");*/
2182 #endif
2183 move_yr_branch(b->revs[i]->branches[j], dy);
2184 }
2185 }
2186 }
2187
2188 static void move_trunk(revision_t *r, int dy)
2189 {
2190 int i, j;
2191 branch_t *b = r->branch;
2192 b->th += dy;
2193 for(i = index_of_revision(r); i < b->nrevs; i++)
2194 {
2195 #ifdef DEBUG
2196 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);
2197 #endif
2198 b->revs[i]->y += dy;
2199 for(j = 0; j < b->revs[i]->nbranches; j++)
2200 {
2201 move_yr_branch(b->revs[i]->branches[j], dy);
2202 }
2203 }
2204 }
2205
2206 static int space_below(rcsfile_t *rcs, revision_t *r)
2207 {
2208 int i, j;
2209 int bl, br, bb;
2210 int space = INT_MAX;
2211 branch_t *b = r->branch;
2212 branch_t *minb = NULL;
2213
2214 branch_ext_bbox(b, &bl, &br, NULL, &bb);
2215 for(i = 0; i < rcs->nbranches; i++)
2216 {
2217 int tbl, tbr, tbt;
2218 branch_t *tb = rcs->branches[i];
2219 branch_ext_bbox(tb, &tbl, &tbr, &tbt, NULL);
2220 if(tb == b)
2221 continue;
2222 if(tbt > bb) /* Must be below our branch */
2223 {
2224 if(tb->branchpoint) /* Take account for the horiz connector */
2225 tbl = tb->branchpoint->cx + tb->branchpoint->branch->tw/2;
2226 if((bl >= tbl && bl <= tbr) || (br <= tbr && br >= tbl))
2227 {
2228 int s = tbt - bb - conf.branch_connect;
2229 if(s < space)
2230 {
2231 space = s;
2232 minb = tb;
2233 }
2234 }
2235 }
2236 }
2237 if(b->branchpoint)
2238 {
2239 for(i = index_of_revision(r); i < b->nrevs; i++)
2240 {
2241 for(j = 0; j < b->revs[i]->nbranches; j++)
2242 {
2243 int s = space_below(rcs, b->revs[i]->branches[j]->revs[0]);
2244 if(s < space)
2245 space = s;
2246 }
2247 }
2248 }
2249 #ifdef DEBUG
2250 fprintf(stderr, "space_below: from %s have %d to %s\n", b->branch->branch, space, minb ? minb->branch->branch : "<recursed>");
2251 #endif
2252 return space;
2253 }
2254
2255 static int space_available(rcsfile_t *rcs, branch_t *colbr, branch_t *tagbr, int *nl, revision_t **bpcommon)
2256 {
2257 int i;
2258 int space = 0;
2259 int nlinks = 0;
2260 revision_t *r;
2261 branch_t *b;
2262 branch_t *ancestor;
2263 revision_t *branchpoint;
2264
2265 if(!tagbr->branchpoint || !colbr->branchpoint)
2266 {
2267 if(!quiet)
2268 fprintf(stderr, "space_available: Trying to stretch the top?\n");
2269 return 0;
2270 }
2271
2272 r = colbr->branchpoint;
2273 b = r->branch;
2274 branchpoint = tagbr->branchpoint;
2275 ancestor = branchpoint->branch;
2276 assert(b != NULL);
2277 assert(ancestor != NULL);
2278
2279 while(1)
2280 {
2281 int s;
2282 int rtag = b == ancestor ? index_of_revision(branchpoint)+1 : 0;
2283 for(i = index_of_revision(r); i >= rtag; i--)
2284 {
2285 if(i > 0)
2286 s = b->revs[i]->y - (b->revs[i-1]->y + b->revs[i-1]->h);
2287 else
2288 s = b->revs[i]->y - (b->y + b->h);
2289 if(s < conf.rev_maxline)
2290 {
2291 space += conf.rev_maxline - s;
2292 nlinks++;
2293 }
2294 }
2295 s = space_below(rcs, r);
2296 if(s < space)
2297 space = s;
2298 #ifdef DEBUG
2299 if(space < 0)
2300 return -1;
2301 #endif
2302 if(b == ancestor)
2303 break;
2304 r = b->branchpoint;
2305 if(!r)
2306 {
2307 /* Not a common ancestor */
2308 r = colbr->branchpoint;
2309 b = r->branch;
2310 branchpoint = ancestor->branchpoint;
2311 if(!branchpoint)
2312 {
2313 if(!quiet)
2314 fprintf(stderr, "space_available: No common ancestor?\n");
2315 return 0;
2316 }
2317 ancestor = branchpoint->branch;
2318 assert(ancestor != NULL);
2319 nlinks = 0;
2320 space = 0;
2321 continue; /* Restart with a new ancestor */
2322 }
2323 b = r->branch;
2324 }
2325 if(nl)
2326 *nl = nlinks; /* Return the number of links that can stretch */
2327 if(bpcommon)
2328 *bpcommon = branchpoint; /* Return the ancestral branchpoint on the common branch */
2329 return space;
2330 }
2331
2332 static int stretch_branches(rcsfile_t *rcs, branch_t *br1, branch_t *br2, int totalstretch)
2333 {
2334 revision_t *r;
2335 revision_t *bpcommon = NULL;
2336 branch_t *ancestor = NULL;
2337 branch_t *b;
2338 int i;
2339 int space;
2340 int nlinks;
2341 int dy;
2342 int rest;
2343
2344 space = space_available(rcs, br1, br2, &nlinks, &bpcommon);
2345 if(bpcommon)
2346 ancestor = bpcommon->branch;
2347
2348 #ifdef DEBUG
2349 if(space == -1)
2350 return 0;
2351 fprintf(stderr, "stretch_branches: space available %d over %d links common %s\n", space, nlinks, ancestor->branch->branch);
2352 #endif
2353 if(space < totalstretch)
2354 return 0;
2355
2356 dy = totalstretch / nlinks;
2357 rest = totalstretch - dy * nlinks;
2358
2359 r = br1->branchpoint;
2360 b = r->branch;
2361 while(1)
2362 {
2363 int rtag = b == ancestor ? index_of_revision(bpcommon)+1 : 0;
2364 for(i = index_of_revision(r); i >= rtag; i--)
2365 {
2366 int s, q;
2367 if(i > 0)
2368 s = b->revs[i]->y - (b->revs[i-1]->y + b->revs[i-1]->h);
2369 else
2370 s = b->revs[i]->y - (b->y + b->h);
2371 q = conf.rev_maxline - s;
2372 if(q > 0)
2373 {
2374 int d = rest ? rest/nlinks+1 : 0;
2375 if(q >= dy+d)
2376 {
2377 move_trunk(b->revs[i], dy+d);
2378 }
2379 else
2380 {
2381 move_trunk(b->revs[i], q);
2382 rest += dy+d - q;
2383 }
2384 rest -= d;
2385 nlinks--;
2386 }
2387 }
2388 if(b == ancestor)
2389 break;
2390 r = b->branchpoint;
2391 assert(r != NULL); /* else 'space_available' wouldn't have returned positively */
2392 b = r->branch;
2393 }
2394 return 1;
2395 }
2396
2397 static branch_t *find_collision_branch(rcsfile_t *rcs, branch_t *b)
2398 {
2399 int i;
2400 int dist = INT_MAX;
2401 branch_t *col = NULL;
2402
2403 for(i = 0; i < rcs->nbranches; i++)
2404 {
2405 int t = branch_distance(rcs->branches[i], b);
2406 if(t > 0 && t < dist)
2407 {
2408 dist = t;
2409 col = rcs->branches[i];
2410 }
2411 }
2412 return col;
2413 }
2414
2415 void auto_stretch(rcsfile_t *rcs)
2416 {
2417 int i;
2418 int safeguard;
2419
2420 for(i = 0, safeguard = LOOPSAFEGUARD; i < rcs->nbranches && safeguard; i++)
2421 {
2422 int bl, pr;
2423 branch_t *b = rcs->branches[i];
2424 if(!b->branchpoint)
2425 continue;
2426 branch_bbox(b, &bl, NULL, NULL, NULL);
2427 branch_bbox(b->branchpoint->branch, NULL, &pr, NULL, NULL);
2428 if(bl - conf.branch_margin - pr > 0)
2429 {
2430 branch_t *col;
2431 int spaceneeded;
2432 /* There is a potential to move branch b further left.
2433 * All branches obstructing this one from moving further
2434 * left must be originating from revisions below
2435 * b->branchpoint until a common ancester.
2436 * So, we search all branches for a branch that lies left
2437 * of b and is closest to b. This is then the collission
2438 * branch that needs to be moved.
2439 */
2440 col = find_collision_branch(rcs, b);
2441 if(!col)
2442 continue;
2443 spaceneeded = space_needed(col, b);
2444 if(spaceneeded < 0)
2445 continue;
2446 #ifdef DEBUG
2447 fprintf(stderr, "auto_stretch: %s collides %s need %d\n", b->branch->branch, col->branch->branch, spaceneeded);
2448 #endif
2449 /* Trace the collision branch back to find the common ancester
2450 * of both col and b. All revisions encountered while traversing
2451 * backwards must be stretched, including all revisions on the
2452 * common ancester from where the branches sprout.
2453 */
2454 if(stretch_branches(rcs, col, b, spaceneeded))
2455 {
2456 if(kern_tree(rcs))
2457 {
2458 /* Restart the process because movement can
2459 * cause more movement.
2460 */
2461 i = 0 - 1; /* -1 for the i++ of the loop */
2462 safeguard--; /* Prevent infinite loop, just in case */
2463 }
2464 /*return;*/
2465 }
2466 }
2467 }
2468 if(!quiet && !safeguard)
2469 fprintf(stderr, "auto_stretch: safeguard terminated possible infinite loop; please report.\n");
2470 }
2471
2472 static void fold_branch(rcsfile_t *rcs, revision_t *r)
2473 {
2474 int i, j;
2475 branch_t *btag = NULL;
2476
2477 if(r->nbranches < 2)
2478 return; /* Should not happen... */
2479
2480 for(i = 0; i < r->nbranches; i++)
2481 {
2482 branch_t *b = r->branches[i];
2483 if(!b->nrevs && b->ntags < 2)
2484 {
2485 /* No commits in this branch and no duplicate tags */
2486 if(!btag)
2487 btag = b;
2488 else
2489 {
2490 /* We have consecutive empty branches, fold */
2491 b->folded = 1;
2492 for(j = 0; j < rcs->nbranches; j++)
2493 {
2494 if(b == rcs->branches[j])
2495 {
2496 /* Zap the branch from the admin */
2497 memmove(&rcs->branches[j],
2498 &rcs->branches[j+1],
2499 (rcs->nbranches - j - 1)*sizeof(rcs->branches[0]));
2500 rcs->nbranches--;
2501 break;
2502 }
2503
2504 }
2505 memmove(&r->branches[i], &r->branches[i+1], (r->nbranches - i - 1)*sizeof(r->branches[0]));
2506 r->nbranches--;
2507 i--; /* We have one less now */
2508
2509 /* Add to the fold-list */
2510 btag->folds = xrealloc(btag->folds, (btag->nfolds+1) * sizeof(btag->folds[0]));
2511 btag->folds[btag->nfolds] = b;
2512 btag->nfolds++;
2513 }
2514 }
2515 else
2516 {
2517 if(!conf.branch_foldall)
2518 btag = NULL; /* Start a new box */
2519 /* Recursively fold sub-branches */
2520 for(j = 0; j < b->nrevs; j++)
2521 fold_branch(rcs, b->revs[j]);
2522 }
2523 }
2524 }
2525
2526 void make_layout(rcsfile_t *rcs)
2527 {
2528 int i, j;
2529 int x, y;
2530 int w, h;
2531 int w2;
2532
2533 /* Remove all unwanted revisions */
2534 if(conf.strip_untagged)
2535 {
2536 int fr = conf.strip_first_rev ? 0 : 1;
2537 for(i = 0; i < rcs->nbranches; i++)
2538 {
2539 branch_t *bp = rcs->branches[i];
2540 for(j = fr; j < bp->nrevs-1; j++)
2541 {
2542 if(!bp->revs[j]->ntags && !bp->revs[j]->nbranches)
2543 {
2544 memmove(&bp->revs[j], &bp->revs[j+1], (bp->nrevs-j-1) * sizeof(bp->revs[0]));
2545 bp->nrevs--;
2546 bp->revs[j]->stripped = 1;
2547 j--;
2548 }
2549 }
2550 }
2551 }
2552
2553 /* Fold all empty branched in one box on the same branchpoint */
2554 if(conf.branch_fold)
2555 {
2556 for(i = 0; i < rcs->branches[0]->nrevs; i++)
2557 {
2558 if(rcs->branches[0]->revs[i]->nbranches > 1)
2559 fold_branch(rcs, rcs->branches[0]->revs[i]);
2560 }
2561 }
2562
2563 /* Calculate the box-sizes of the revisions */
2564 for(i = 0; i < rcs->nsrev; i++)
2565 {
2566 revision_t *rp;
2567 int w;
2568 int h;
2569 rp = rcs->srev[i];
2570 rp->revtext = expand_string(conf.rev_text, rcs, rp, rp->rev, NULL, rp->ntags ? rp->tags[0] : NULL);
2571 w = get_swidth(rp->revtext, &conf.rev_text_font);
2572 j = get_swidth(rp->rev->rev, &conf.rev_font);
2573 if(j > w)
2574 w = j;
2575 h = get_sheight(rp->revtext, &conf.rev_text_font) + get_sheight(rp->rev->rev, &conf.rev_font);
2576 for(j = 0; j < rp->ntags; j++)
2577 {
2578 int ww = get_swidth(rp->tags[j]->tag, &conf.tag_font);
2579 int th;
2580 if(ww > w) w = ww;
2581 th = get_sheight(rp->tags[j]->tag, &conf.tag_font) + conf.rev_separator;
2582 rp->tags[j]->yofs = h + th/2 + conf.rev_tspace;
2583 h += th;
2584 }
2585 rp->w = w + conf.rev_lspace + conf.rev_rspace;
2586 rp->h = h + conf.rev_tspace + conf.rev_bspace;
2587 }
2588
2589 /* Calculate the box-sizes of the branches */
2590 for(i = 0; i < rcs->nbranches; i++)
2591 {
2592 branch_t *bp = rcs->branches[i];
2593 int w;
2594 int h;
2595 if(!bp->nfolds)
2596 {
2597 w = get_swidth(bp->branch->branch, &conf.branch_font);
2598 h = get_sheight(bp->branch->branch, &conf.branch_font);
2599 for(j = 0; j < bp->ntags; j++)
2600 {
2601 int ww = get_swidth(bp->tags[j]->tag, &conf.branch_tag_font);
2602 if(ww > w) w = ww;
2603 h += get_sheight(bp->tags[j]->tag, &conf.branch_tag_font);
2604 }
2605 }
2606 else
2607 {
2608 int h1, h2;
2609 int w1, w2;
2610 int fw;
2611 w1 = get_swidth(bp->branch->branch, &conf.branch_font);
2612 w1 += get_swidth(" ", &conf.branch_font);
2613 w2 = get_swidth(bp->tags[0]->tag, &conf.branch_tag_font);
2614 fw = w1;
2615 w = w1 + w2;
2616 h1 = get_sheight(bp->branch->branch, &conf.branch_font);
2617 h2 = get_sheight(bp->tags[0]->tag, &conf.branch_tag_font);
2618 h = MAX(h1, h2);
2619 for(j = 0; j < bp->nfolds; j++)
2620 {
2621 w1 = get_swidth(bp->folds[j]->branch->branch, &conf.branch_font);
2622 w1 += get_swidth(" ", &conf.branch_font);
2623 w2 = get_swidth(bp->folds[j]->tags[0]->tag, &conf.branch_tag_font);
2624 if(w1 > fw)
2625 fw = w1;
2626 if(w1 + w2 > w)
2627 w = w1 + w2;
2628 h1 = get_sheight(bp->folds[j]->branch->branch, &conf.branch_font);
2629 h2 = get_sheight(bp->folds[j]->tags[0]->tag, &conf.branch_tag_font);
2630 h += MAX(h1, h2);
2631 }
2632 bp->fw = fw;
2633 }
2634 w += conf.branch_lspace + conf.branch_rspace;
2635 h += conf.branch_tspace + conf.branch_bspace;
2636 bp->w = w;
2637 bp->h = h;
2638 if(conf.left_right)
2639 {
2640 for(j = 0; j < bp->nrevs; j++)
2641 {
2642 if(bp->revs[j]->h > h)
2643 h = bp->revs[j]->h;
2644 w += bp->revs[j]->w + conf.rev_minline;
2645 }
2646 if(conf.branch_dupbox && bp->nrevs)
2647 w += bp->w + conf.rev_minline;
2648 }
2649 else
2650 {
2651 for(j = 0; j < bp->nrevs; j++)
2652 {
2653 if(bp->revs[j]->w > w)
2654 w = bp->revs[j]->w;
2655 h += bp->revs[j]->h + conf.rev_minline;
2656 }
2657 if(conf.branch_dupbox && bp->nrevs)
2658 h += bp->h + conf.rev_minline;
2659 }
2660 bp->th = h;
2661 bp->tw = w;
2662 }
2663
2664 /* Calculate the relative positions of revs in a branch */
2665 if(conf.left_right)
2666 {
2667 for(i = 0; i < rcs->nbranches; i++)
2668 {
2669 branch_t *b = rcs->branches[i];
2670 y = b->th/2;
2671 x = b->w;
2672 b->y = y;
2673 b->cx = 0;
2674 for(j = 0; j < b->nrevs; j++)
2675 {
2676 x += conf.rev_minline;
2677 b->revs[j]->y = y;
2678 b->revs[j]->cx = x;
2679 x += b->revs[j]->w;
2680 }
2681 }
2682 }
2683 else
2684 {
2685 for(i = 0; i < rcs->nbranches; i++)
2686 {
2687 branch_t *b = rcs->branches[i];
2688 x = b->tw/2;
2689 y = b->h;
2690 b->cx = x;
2691 b->y = 0;
2692 for(j = 0; j < b->nrevs; j++)
2693 {
2694 y += conf.rev_minline;
2695 b->revs[j]->cx = x;
2696 b->revs[j]->y = y;
2697 y += b->revs[j]->h;
2698 }
2699 }
2700 }
2701
2702 /* Initially reposition the branches from bottom to top progressively right */
2703 if(conf.left_right)
2704 {
2705 x = rcs->branches[0]->y;
2706 w2 = rcs->branches[0]->th / 2;
2707 for(i = rcs->branches[0]->nrevs-1; i >= 0; i--)
2708 {
2709 initial_reposition_branch_lr(rcs->branches[0]->revs[i], &x, &w2);
2710 }
2711 }
2712 else
2713 {
2714 x = rcs->branches[0]->cx;
2715 w2 = rcs->branches[0]->tw / 2;
2716 for(i = rcs->branches[0]->nrevs-1; i >= 0; i--)
2717 {
2718 initial_reposition_branch(rcs->branches[0]->revs[i], &x, &w2);
2719 }
2720 }
2721
2722 /* Initially move branches left if there is room */
2723 kern_tree(rcs);
2724
2725 /* Try to kern the branches more by expanding the inter-revision spacing */
2726 if(conf.auto_stretch && !conf.left_right)
2727 auto_stretch(rcs);
2728
2729 /* Move everything w.r.t. the top-left margin */
2730 for(i = 0; i < rcs->nbranches; i++)
2731 move_branch(rcs->branches[i], conf.margin_left, conf.margin_top);
2732
2733 /* Calculate overall image size */
2734 if(conf.left_right)
2735 {
2736 x = rcs->branches[0]->cx;
2737 y = rcs->branches[0]->y - rcs->branches[0]->th/2;
2738 }
2739 else
2740 {
2741 x = rcs->branches[0]->cx - rcs->branches[0]->tw/2;
2742 y = rcs->branches[0]->y;
2743 }
2744 w = rcs->branches[0]->tw;
2745 h = rcs->branches[0]->th;
2746 for(i = 1; i < rcs->nbranches; i++)
2747 rect_union(&x, &y, &w, &h, rcs->branches[i]);
2748 rcs->tw = w;
2749 rcs->th = h;
2750
2751 /* Flip the entire tree */
2752 if(conf.upside_down)
2753 {
2754 if(conf.left_right)
2755 {
2756 x += rcs->tw;
2757 for(i = 0; i < rcs->nbranches; i++)
2758 {
2759 branch_t *b = rcs->branches[i];
2760 for(j = 0; j < b->nrevs; j++)
2761 {
2762 revision_t *r = b->revs[j];
2763 r->cx = x - r->cx - r->w + conf.margin_left;
2764 }
2765 b->cx = x - b->cx - b->w + conf.margin_left;
2766 }
2767 }
2768 else
2769 {
2770 y += rcs->th;
2771 for(i = 0; i < rcs->nbranches; i++)
2772 {
2773 branch_t *b = rcs->branches[i];
2774 for(j = 0; j < b->nrevs; j++)
2775 {
2776 revision_t *r = b->revs[j];
2777 r->y = y - r->y - r->h + conf.margin_top;
2778 }
2779 b->y = y - b->y - b->h + conf.margin_top;
2780 }
2781 }
2782 }
2783 }
2784
2785 /*
2786 **************************************************************************
2787 * Imagemap functions
2788 **************************************************************************
2789 */
2790 void make_imagemap(rcsfile_t *rcs, FILE *fp, gdImagePtr im)
2791 {
2792 int i, j;
2793 const char *htp = conf.html_level == HTMLLEVEL_X ? " /" : "";
2794
2795 switch(conf.html_level)
2796 {
2797 case HTMLLEVEL_4:
2798 fprintf(fp, "<map name=\"%s\" id=\"%s\">\n", conf.map_name, conf.map_name);
2799 break;
2800 case HTMLLEVEL_X:
2801 fprintf(fp, "<map id=\"%s\">\n", conf.map_name);
2802 break;
2803 default:
2804 fprintf(fp, "<map name=\"%s\">\n", conf.map_name);
2805 }
2806
2807 for(i = 0; i < rcs->nbranches; i++)
2808 {
2809 branch_t *b = rcs->branches[i];
2810 tag_t *tag = b->ntags ? b->tags[0] : NULL;
2811 char *bhref = expand_string(conf.map_branch_href, rcs, NULL, b->branch, NULL, tag);
2812 char *balt = expand_string(conf.map_branch_alt, rcs, NULL, b->branch, NULL, tag);
2813 int x1;
2814 int x2;
2815 int y1;
2816 int y2;
2817
2818 if(!b->nfolds)
2819 {
2820 if(conf.left_right)
2821 {
2822 x1 = b->cx;
2823 y1 = b->y - b->h/2;
2824 x2 = b->cx + b->w;
2825 y2 = b->y + b->h/2;
2826 }
2827 else
2828 {
2829 x1 = b->cx - b->w/2;
2830 y1 = b->y;
2831 x2 = b->cx + b->w/2;
2832 y2 = b->y + b->h;
2833 }
2834 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s%s>\n",
2835 bhref, x1, y1, x2, y2, balt, htp);
2836 if(im)
2837 {
2838 gdImageFilledRectangle(im, x1-2, y1-2, x1+2, y1+2, conf.title_color.id);
2839 gdImageFilledRectangle(im, x2-2, y2-2, x2+2, y2+2, conf.tag_color.id);
2840 gdImageLine(im, x1, y1, x2, y2, conf.title_color.id);
2841 }
2842 }
2843 else
2844 {
2845 int yy1, yy2, yy;
2846 if(conf.left_right)
2847 {
2848 x1 = b->cx + conf.branch_lspace;
2849 y1 = b->y - b->h/2 + conf.branch_tspace;
2850 }
2851 else
2852 {
2853 x1 = b->cx - b->w/2 + conf.branch_lspace;
2854 y1 = b->y + conf.branch_tspace;
2855 }
2856 x2 = x1 + b->w - conf.branch_rspace;
2857
2858 yy1 = get_sheight(b->branch->branch, &conf.branch_font);
2859 yy2 = get_sheight(b->tags[0]->tag, &conf.branch_tag_font);
2860 yy = MAX(yy1, yy2);
2861 y2 = y1 + yy;
2862 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s%s>\n",
2863 bhref, x1, y1, x2, y2, balt, htp);
2864
2865 y1 += yy;
2866 y2 += yy;
2867 for(j = 0; j < b->nfolds; j++)
2868 {
2869 branch_t *fb = b->folds[j];
2870 tag_t *t = fb->tags[0];
2871 xfree(bhref);
2872 xfree(balt);
2873 bhref = expand_string(conf.map_branch_href, rcs, NULL, fb->branch, NULL, t);
2874 balt = expand_string(conf.map_branch_alt, rcs, NULL, fb->branch, NULL, t);
2875 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s%s>\n",
2876 bhref, x1, y1, x2, y2, balt, htp);
2877 yy1 = get_sheight(fb->branch->branch, &conf.branch_font);
2878 yy2 = get_sheight(fb->tags[0]->tag, &conf.branch_tag_font);
2879 yy = MAX(yy1, yy2);
2880 y1 += yy;
2881 y2 += yy;
2882 }
2883 }
2884
2885 for(j = 0; j < b->nrevs; j++)
2886 {
2887 revision_t *r = b->revs[j];
2888 revision_t* r1;
2889 int xoff = 1;
2890 int yoff = 1;
2891 char *href;
2892 char *alt;
2893
2894 tag = r->ntags ? r->tags[0] : NULL;
2895 href = expand_string(conf.map_rev_href, rcs, r, r->rev, NULL, tag);
2896 alt = expand_string(conf.map_rev_alt, rcs, r, r->rev, NULL, tag);
2897 if(conf.left_right)
2898 {
2899 x1 = r->cx;
2900 y1 = r->y - r->h/2;
2901 x2 = r->cx + r->w;
2902 y2 = r->y + r->h/2;
2903 }
2904 else
2905 {
2906 x1 = r->cx - r->w/2;
2907 y1 = r->y;
2908 x2 = r->cx + r->w/2;
2909 y2 = r->y + r->h;
2910 }
2911 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s%s>\n",
2912 href, x1, y1, x2, y2, alt, htp);
2913 if(im)
2914 {
2915 gdImageFilledRectangle(im, x1-2, y1-2, x1+2, y1+2, conf.title_color.id);
2916 gdImageFilledRectangle(im, x2-2, y2-2, x2+2, y2+2, conf.tag_color.id);
2917 gdImageLine(im, x1, y1, x2, y2, conf.title_color.id);
2918 }
2919 xfree(href);
2920 xfree(alt);
2921 if(j > 0 || b->branchpoint)
2922 {
2923 if(j > 0)
2924 {
2925 r1 = b->revs[j-1];
2926 if(conf.left_right)
2927 {
2928 yoff = MIN(r->h, r1->h)/4;
2929 x1 = conf.upside_down ? r1->cx : r1->cx + r1->w;
2930 }
2931 else
2932 {
2933 xoff = MIN(r->w, r1->w)/4;
2934 y1 = conf.upside_down ? r1->y : r1->y + r1->h;
2935 }
2936 }
2937 else
2938 {
2939 r1 = b->branchpoint;
2940 if(conf.left_right)
2941 {
2942 yoff = MIN(r->h, b->h)/4;
2943 x1 = conf.upside_down ? b->cx : b->cx + b->w;
2944 }
2945 else
2946 {
2947 xoff = MIN(r->w, b->w)/4;
2948 y1 = conf.upside_down ? b->y : b->y + b->h;
2949 }
2950 }
2951 if(conf.left_right)
2952 {
2953 y1 = r->y - yoff;
2954 y2 = r->y + yoff;
2955 x2 = conf.upside_down ? r->cx + r->w : r->cx;
2956 yoff = 0;
2957 }
2958 else
2959 {
2960 x1 = r->cx - xoff;
2961 x2 = r->cx + xoff;
2962 y2 = conf.upside_down ? r->y + r->h : r->y;
2963 xoff = 0;
2964 }
2965 if(x1 > x2)
2966 {
2967 int tt = x1;
2968 x1 = x2;
2969 x2 = tt;
2970 }
2971 if(y1 > y2)
2972 {
2973 int tt = y1;
2974 y1 = y2;
2975 y2 = tt;
2976 }
2977 href = expand_string(conf.map_diff_href, rcs, r, r->rev, r1->rev, tag);
2978 alt = expand_string(conf.map_diff_alt, rcs, r, r->rev, r1->rev, tag);
2979 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s%s>\n",
2980 href,
2981 x1+xoff, y1+yoff, x2-xoff, y2-yoff,
2982 alt, htp);
2983 if(im)
2984 {
2985 gdImageFilledRectangle(im, x1-2, y1-2, x1+2, y1+2, conf.title_color.id);
2986 gdImageFilledRectangle(im, x2-2, y2-2, x2+2, y2+2, conf.tag_color.id);
2987 gdImageLine(im, x1, y1, x2, y2, conf.title_color.id);
2988 }
2989 xfree(href);
2990 xfree(alt);
2991 }
2992 }
2993 if(conf.branch_dupbox && b->nrevs)
2994 {
2995 if(conf.left_right)
2996 {
2997 x1 = conf.upside_down ? b->cx + b->w - b->tw : b->cx - b->w + b->tw;
2998 y1 = b->y - b->h/2;
2999 x2 = x1 + b->w;
3000 y2 = b->y + b->h/2;
3001 }
3002 else
3003 {
3004 x1 = b->cx - b->w/2;
3005 y1 = conf.upside_down ? b->y + b->h - b->th : b->y - b->h + b->th;
3006 x2 = b->cx + b->w/2;
3007 y2 = y1 + b->h;
3008 }
3009 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s%s>\n",
3010 bhref, x1, y1, x2, y2, balt, htp);
3011 if(im)
3012 {
3013 gdImageFilledRectangle(im, x1-2, y1-2, x1+2, y1+2, conf.title_color.id);
3014 gdImageFilledRectangle(im, x2-2, y2-2, x2+2, y2+2, conf.tag_color.id);
3015 gdImageLine(im, x1, y1, x2, y2, conf.title_color.id);
3016 }
3017 }
3018 xfree(bhref);
3019 xfree(balt);
3020 }
3021 fprintf(fp, "</map>\n");
3022 }
3023
3024 /*
3025 **************************************************************************
3026 * Program entry
3027 **************************************************************************
3028 */
3029 static const char usage_str[] =
3030 "Usage: cvsgraph [options] <file>\n"
3031 " -b Add a branch box at both sides of the trunk (config value is negated)\n"
3032 " -c <file> Read alternative config from <file>\n"
3033 " -d <level> Enable debug mode at <level>\n"
3034 " -h This message\n"
3035 " -i Generate an imagemap instead of image\n"
3036 " -I <file> Also write the imagemap to <file>\n"
3037 " -k Auto stretch the tree (config value is negated)\n"
3038 " -M <name> Use <name> as imagemap name\n"
3039 " -m <mod> Use <mod> as cvs module\n"
3040 " -o <file> Output to <file>\n"
3041 " -O <opt=val> Set option opt to value val\n"
3042 " -q Be quiet (i.e. no warnings)\n"
3043 " -r <path> Use <path> as cvsroot path\n"
3044 " -s Strip untagged revisions (config value is negated)\n"
3045 " -S Also strip the first revision (config value is negated)\n"
3046 " -u Upside down image (mirror vertically; config value is negated)\n"
3047 " -V Print version and exit\n"
3048 " -x [34x] Specify level of HTML 3.2 (default), 4.0 or XHTML\n"
3049 " -[0-9] <txt> Use <txt> for expansion\n"
3050 ;
3051
3052 #define VERSION_STR "1.4.0"
3053 #define NOTICE_STR "Copyright (c) 2001,2002,2003 B.Stultiens"
3054
3055 static void append_slash(char **path)
3056 {
3057 int l;
3058 assert(path != NULL);
3059 assert(*path != NULL);
3060 l = strlen(*path);
3061 if(!l || (*path)[l-1] == '/')
3062 return;
3063 *path = xrealloc(*path, l+2);
3064 strcat(*path, "/");
3065 }
3066
3067 int main(int argc, char *argv[])
3068 {
3069 extern int rcs_flex_debug;
3070 extern int rcsdebug;
3071 int optc;
3072 char *confpath = NULL;
3073 char *outfile = NULL;
3074 char *cvsroot = NULL;
3075 char *cvsmodule = NULL;
3076 int imagemap = 0;
3077 int upsidedown = 0;
3078 int bdupbox = 0;
3079 int stripuntag = 0;
3080 int stripfirst = 0;
3081 int autostretch = 0;
3082 int htmllevel = 0;
3083 char *imgmapname = NULL;
3084 char *imgmapfile = NULL;
3085 int lose = 0;
3086 FILE *fp;
3087 char *rcsfilename;
3088 rcsfile_t *rcs;
3089 gdImagePtr im;
3090
3091 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)
3092 {
3093 switch(optc)
3094 {
3095 case 'b':
3096 bdupbox = 1;
3097 break;
3098 case 'c':
3099 confpath = xstrdup(optarg);
3100 break;
3101 case 'd':
3102 debuglevel = strtol(optarg, NULL, 0);
3103 break;
3104 case 'I':
3105 imgmapfile = xstrdup(optarg);
3106 break;
3107 case 'i':
3108 imagemap = 1;
3109 break;
3110 case 'k':
3111 autostretch = 1;
3112 break;
3113 case 'M':
3114 imgmapname = xstrdup(optarg);
3115 break;
3116 case 'm':
3117 cvsmodule = xstrdup(optarg);
3118 break;
3119 case 'O':
3120 stack_option(optarg);
3121 break;
3122 case 'o':
3123 outfile = xstrdup(optarg);
3124 break;
3125 case 'q':
3126 quiet = 1;
3127 break;
3128 case 'r':
3129 cvsroot = xstrdup(optarg);
3130 break;
3131 case 'S':
3132 stripfirst = 1;
3133 break;
3134 case 's':
3135 stripuntag = 1;
3136 break;
3137 case 'u':
3138 upsidedown = 1;
3139 break;
3140 case 'V':
3141 fprintf(stdout, "cvsgraph v%s, %s\n", VERSION_STR, NOTICE_STR);
3142 return 0;
3143 case 'x':
3144 switch(optarg[0])
3145 {
3146 case '3':
3147 htmllevel = HTMLLEVEL_3;
3148 break;
3149 case '4':
3150 htmllevel = HTMLLEVEL_4;
3151 break;
3152 case 'x':
3153 htmllevel = HTMLLEVEL_X;
3154 break;
3155 default:
3156 fprintf(stderr, "Invalid HTML level in -x\n");
3157 lose++;
3158 }
3159 break;
3160 case 'h':
3161 fprintf(stdout, "%s", usage_str);
3162 return 0;
3163 default:
3164 if(isdigit(optc))
3165 {
3166 conf.expand[optc-'0'] = xstrdup(optarg);
3167 }
3168 else
3169 lose++;
3170 }
3171 }
3172
3173 if(lose)
3174 {
3175 fprintf(stderr, "%s", usage_str);
3176 return 1;
3177 }
3178
3179 if(debuglevel)
3180 {
3181 setvbuf(stdout, NULL, 0, _IONBF);
3182 setvbuf(stderr, NULL, 0, _IONBF);
3183 }
3184 rcs_flex_debug = (debuglevel & DEBUG_RCS_LEX) != 0;
3185 rcsdebug = (debuglevel & DEBUG_RCS_YACC) != 0;
3186
3187 /* Set defaults */
3188 conf.tag_font.gdfont = gdFontTiny;
3189 conf.rev_font.gdfont = gdFontTiny;
3190 conf.branch_font.gdfont = gdFontTiny;
3191 conf.branch_tag_font.gdfont = gdFontTiny;
3192 conf.title_font.gdfont = gdFontTiny;
3193 conf.rev_text_font.gdfont = gdFontTiny;
3194
3195 conf.anti_alias = 1;
3196 conf.thick_lines = 1;
3197 conf.branch_fold = 1;
3198
3199 conf.cvsroot = xstrdup("");
3200 conf.cvsmodule = xstrdup("");
3201 conf.date_format = xstrdup("%d-%b-%Y %H:%M:%S");
3202 conf.title = xstrdup("");
3203 conf.map_name = xstrdup("CvsGraphImageMap");
3204 conf.map_branch_href = xstrdup("href=\"unset: conf.map_branch_href\"");
3205 conf.map_branch_alt = xstrdup("alt=\"%B\"");
3206 conf.map_rev_href = xstrdup("href=\"unset: conf.map_rev_href\"");
3207 conf.map_rev_alt = xstrdup("alt=\"%R\"");
3208 conf.map_diff_href = xstrdup("href=\"unset: conf.map_diff_href\"");
3209 conf.map_diff_alt = xstrdup("alt=\"%P &lt;-&gt; %R\"");
3210 conf.rev_text = xstrdup("%d");
3211 conf.merge_from = xstrdup("");
3212 conf.merge_to = xstrdup("");
3213 conf.merge_arrows = 1;
3214 conf.arrow_width = ARROW_WIDTH;
3215 conf.arrow_length = ARROW_LENGTH;
3216
3217 conf.color_bg = white_color;
3218 conf.branch_bgcolor = white_color;
3219 conf.branch_color = black_color;
3220 conf.branch_tag_color = black_color;
3221 conf.rev_color = black_color;
3222 conf.rev_bgcolor = white_color;
3223 conf.merge_color = black_color;
3224 conf.tag_color = black_color;
3225 conf.title_color = black_color;
3226 conf.rev_text_color = black_color;
3227
3228 conf.image_quality = 100;
3229 conf.rev_maxline = -1; /* Checked later to set to default */
3230
3231 read_config(confpath);
3232
3233 if(conf.rev_maxline == -1) conf.rev_maxline = 5 * conf.rev_minline;
3234
3235 /* Set overrides */
3236 if(cvsroot) conf.cvsroot = cvsroot;
3237 if(cvsmodule) conf.cvsmodule = cvsmodule;
3238 if(imgmapname) conf.map_name = imgmapname;
3239 if(upsidedown) conf.upside_down = !conf.upside_down;
3240 if(bdupbox) conf.branch_dupbox = !conf.branch_dupbox;
3241 if(stripuntag) conf.strip_untagged = !conf.strip_untagged;
3242 if(stripfirst) conf.strip_first_rev = !conf.strip_first_rev;
3243 if(autostretch) conf.auto_stretch = !conf.auto_stretch;
3244 if(htmllevel) conf.html_level = htmllevel;
3245
3246 if(conf.rev_minline >= conf.rev_maxline)
3247 {
3248 if(conf.auto_stretch && !quiet)
3249 fprintf(stderr, "Auto stretch is only possible if rev_minline < rev_maxline\n");
3250 conf.auto_stretch = 0;
3251 }
3252
3253 if(conf.thick_lines < 1)
3254 conf.thick_lines = 1;
3255 if(conf.thick_lines > 11)
3256 conf.thick_lines = 11;
3257
3258 append_slash(&conf.cvsroot);
3259 append_slash(&conf.cvsmodule);
3260
3261 if(optind >= argc)
3262 {
3263 #ifdef __WIN32__
3264 /* Bad hack for DOS/Windows */
3265 if(setmode(fileno(stdin), O_BINARY) == -1)
3266 {
3267 perror("Set binary mode for stdin");
3268 return 1;
3269 }
3270 #endif
3271 rcsfilename = NULL;
3272 }
3273 else
3274 rcsfilename = argv[optind];
3275
3276 rcs = get_rcsfile(conf.cvsroot, conf.cvsmodule, rcsfilename);
3277 if(!rcs)
3278 return 1;
3279
3280 if(debuglevel & DEBUG_RCS_FILE)
3281 dump_rcsfile(rcs);
3282
3283 if(!reorganise_branches(rcs))
3284 return 1;
3285
3286 assign_tags(rcs);
3287 find_merges(rcs);
3288
3289 if(outfile)
3290 {
3291 if((fp = fopen(outfile, "wb")) == NULL)
3292 {
3293 perror(outfile);
3294 return 1;
3295 }
3296 }
3297 else
3298 {
3299 fp = stdout;
3300 #ifdef __WIN32__
3301 /* Bad hack for DOS/Windows */
3302 if(setmode(fileno(fp), O_BINARY) == -1)
3303 {
3304 perror("Set binary mode for stdout");
3305 return 1;
3306 }
3307 #endif
3308 }
3309
3310 make_layout(rcs);
3311
3312 if(!imagemap)
3313 {
3314 /* Create an image */
3315 im = make_image(rcs);
3316 #ifdef DEBUG_IMAGEMAP
3317 {
3318 FILE *nulfile = fopen("/dev/null", "w");
3319 make_imagemap(rcs, nulfile, im);
3320 fclose(nulfile);
3321 }
3322 #endif
3323 switch(conf.image_type)
3324 {
3325 #ifdef HAVE_IMAGE_GIF
3326 # ifndef HAVE_IMAGE_PNG
3327 default:
3328 # endif
3329 case IMAGE_GIF:
3330 gdImageGif(im, fp);
3331 break;
3332 #endif
3333 #ifdef HAVE_IMAGE_PNG
3334 default:
3335 case IMAGE_PNG:
3336 gdImagePng(im, fp);
3337 break;
3338 #endif
3339 #ifdef HAVE_IMAGE_JPEG
3340 # if !defined(HAVE_IMAGE_GIF) && !defined(HAVE_IMAGE_PNG)
3341 default:
3342 # endif
3343 case IMAGE_JPEG:
3344 gdImageJpeg(im, fp, conf.image_quality);
3345 break;
3346 #endif
3347 }
3348
3349 gdImageDestroy(im);
3350 }
3351 else
3352 {
3353 /* Create an imagemap */
3354 make_imagemap(rcs, fp, NULL);
3355 }
3356
3357 /* Also create imagemap to file if requested */
3358 if(imgmapfile)
3359 {
3360 FILE *ifp = fopen(imgmapfile, "wb");
3361 if(!ifp)
3362 {
3363 perror(imgmapfile);
3364 return 1;
3365 }
3366 make_imagemap(rcs, ifp, NULL);
3367 fclose(ifp);
3368 }
3369
3370 if(outfile)
3371 fclose(fp);
3372
3373 return 0;
3374 }

  ViewVC Help
Powered by ViewVC 1.1.0 with CvsGraph 1.7.0