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

  ViewVC Help
Powered by ViewVC 1.1.0 with CvsGraph 1.7.0