summaryrefslogtreecommitdiffabout
authorLars Hjemli <hjemli@gmail.com>2009-08-19 15:47:24 (UTC)
committer Lars Hjemli <hjemli@gmail.com>2009-08-24 08:22:57 (UTC)
commit302a3efa261b1b6127b2a2189e25ab45019b1b54 (patch) (unidiff)
treeda2be8819777044cd16887da17528b1937be55ee
parent523a2161b9a21f5fa8526280bb914d7affb185b1 (diff)
downloadcgit-302a3efa261b1b6127b2a2189e25ab45019b1b54.zip
cgit-302a3efa261b1b6127b2a2189e25ab45019b1b54.tar.gz
cgit-302a3efa261b1b6127b2a2189e25ab45019b1b54.tar.bz2
cgit.c: make print_repolist() and print_repo() reusable for caching
Signed-off-by: Lars Hjemli <hjemli@gmail.com>
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--cgit.c24
1 files changed, 12 insertions, 12 deletions
diff --git a/cgit.c b/cgit.c
index 97f5e08..db96905 100644
--- a/cgit.c
+++ b/cgit.c
@@ -355,195 +355,195 @@ static int prepare_repo_cmd(struct cgit_context *ctx)
355 return 1; 355 return 1;
356 } 356 }
357 357
358 if (get_sha1(ctx->qry.head, sha1)) { 358 if (get_sha1(ctx->qry.head, sha1)) {
359 tmp = xstrdup(ctx->qry.head); 359 tmp = xstrdup(ctx->qry.head);
360 ctx->qry.head = ctx->repo->defbranch; 360 ctx->qry.head = ctx->repo->defbranch;
361 ctx->page.status = 404; 361 ctx->page.status = 404;
362 ctx->page.statusmsg = "not found"; 362 ctx->page.statusmsg = "not found";
363 cgit_print_http_headers(ctx); 363 cgit_print_http_headers(ctx);
364 cgit_print_docstart(ctx); 364 cgit_print_docstart(ctx);
365 cgit_print_pageheader(ctx); 365 cgit_print_pageheader(ctx);
366 cgit_print_error(fmt("Invalid branch: %s", tmp)); 366 cgit_print_error(fmt("Invalid branch: %s", tmp));
367 cgit_print_docend(); 367 cgit_print_docend();
368 return 1; 368 return 1;
369 } 369 }
370 return 0; 370 return 0;
371} 371}
372 372
373static void process_request(void *cbdata) 373static void process_request(void *cbdata)
374{ 374{
375 struct cgit_context *ctx = cbdata; 375 struct cgit_context *ctx = cbdata;
376 struct cgit_cmd *cmd; 376 struct cgit_cmd *cmd;
377 377
378 cmd = cgit_get_cmd(ctx); 378 cmd = cgit_get_cmd(ctx);
379 if (!cmd) { 379 if (!cmd) {
380 ctx->page.title = "cgit error"; 380 ctx->page.title = "cgit error";
381 cgit_print_http_headers(ctx); 381 cgit_print_http_headers(ctx);
382 cgit_print_docstart(ctx); 382 cgit_print_docstart(ctx);
383 cgit_print_pageheader(ctx); 383 cgit_print_pageheader(ctx);
384 cgit_print_error("Invalid request"); 384 cgit_print_error("Invalid request");
385 cgit_print_docend(); 385 cgit_print_docend();
386 return; 386 return;
387 } 387 }
388 388
389 if (cmd->want_repo && !ctx->repo) { 389 if (cmd->want_repo && !ctx->repo) {
390 cgit_print_http_headers(ctx); 390 cgit_print_http_headers(ctx);
391 cgit_print_docstart(ctx); 391 cgit_print_docstart(ctx);
392 cgit_print_pageheader(ctx); 392 cgit_print_pageheader(ctx);
393 cgit_print_error(fmt("No repository selected")); 393 cgit_print_error(fmt("No repository selected"));
394 cgit_print_docend(); 394 cgit_print_docend();
395 return; 395 return;
396 } 396 }
397 397
398 if (ctx->repo && prepare_repo_cmd(ctx)) 398 if (ctx->repo && prepare_repo_cmd(ctx))
399 return; 399 return;
400 400
401 if (cmd->want_layout) { 401 if (cmd->want_layout) {
402 cgit_print_http_headers(ctx); 402 cgit_print_http_headers(ctx);
403 cgit_print_docstart(ctx); 403 cgit_print_docstart(ctx);
404 cgit_print_pageheader(ctx); 404 cgit_print_pageheader(ctx);
405 } 405 }
406 406
407 cmd->fn(ctx); 407 cmd->fn(ctx);
408 408
409 if (cmd->want_layout) 409 if (cmd->want_layout)
410 cgit_print_docend(); 410 cgit_print_docend();
411} 411}
412 412
413int cmp_repos(const void *a, const void *b) 413int cmp_repos(const void *a, const void *b)
414{ 414{
415 const struct cgit_repo *ra = a, *rb = b; 415 const struct cgit_repo *ra = a, *rb = b;
416 return strcmp(ra->url, rb->url); 416 return strcmp(ra->url, rb->url);
417} 417}
418 418
419void print_repo(struct cgit_repo *repo) 419void print_repo(FILE *f, struct cgit_repo *repo)
420{ 420{
421 printf("repo.url=%s\n", repo->url); 421 fprintf(f, "repo.url=%s\n", repo->url);
422 printf("repo.name=%s\n", repo->name); 422 fprintf(f, "repo.name=%s\n", repo->name);
423 printf("repo.path=%s\n", repo->path); 423 fprintf(f, "repo.path=%s\n", repo->path);
424 if (repo->owner) 424 if (repo->owner)
425 printf("repo.owner=%s\n", repo->owner); 425 fprintf(f, "repo.owner=%s\n", repo->owner);
426 if (repo->desc) 426 if (repo->desc)
427 printf("repo.desc=%s\n", repo->desc); 427 fprintf(f, "repo.desc=%s\n", repo->desc);
428 if (repo->readme) 428 if (repo->readme)
429 printf("repo.readme=%s\n", repo->readme); 429 fprintf(f, "repo.readme=%s\n", repo->readme);
430 printf("\n"); 430 fprintf(f, "\n");
431} 431}
432 432
433void print_repolist(struct cgit_repolist *list) 433void print_repolist(FILE *f, struct cgit_repolist *list, int start)
434{ 434{
435 int i; 435 int i;
436 436
437 for(i = 0; i < list->count; i++) 437 for(i = start; i < list->count; i++)
438 print_repo(&list->repos[i]); 438 print_repo(f, &list->repos[i]);
439} 439}
440 440
441 441
442static void cgit_parse_args(int argc, const char **argv) 442static void cgit_parse_args(int argc, const char **argv)
443{ 443{
444 int i; 444 int i;
445 int scan = 0; 445 int scan = 0;
446 446
447 for (i = 1; i < argc; i++) { 447 for (i = 1; i < argc; i++) {
448 if (!strncmp(argv[i], "--cache=", 8)) { 448 if (!strncmp(argv[i], "--cache=", 8)) {
449 ctx.cfg.cache_root = xstrdup(argv[i]+8); 449 ctx.cfg.cache_root = xstrdup(argv[i]+8);
450 } 450 }
451 if (!strcmp(argv[i], "--nocache")) { 451 if (!strcmp(argv[i], "--nocache")) {
452 ctx.cfg.nocache = 1; 452 ctx.cfg.nocache = 1;
453 } 453 }
454 if (!strcmp(argv[i], "--nohttp")) { 454 if (!strcmp(argv[i], "--nohttp")) {
455 ctx.env.no_http = "1"; 455 ctx.env.no_http = "1";
456 } 456 }
457 if (!strncmp(argv[i], "--query=", 8)) { 457 if (!strncmp(argv[i], "--query=", 8)) {
458 ctx.qry.raw = xstrdup(argv[i]+8); 458 ctx.qry.raw = xstrdup(argv[i]+8);
459 } 459 }
460 if (!strncmp(argv[i], "--repo=", 7)) { 460 if (!strncmp(argv[i], "--repo=", 7)) {
461 ctx.qry.repo = xstrdup(argv[i]+7); 461 ctx.qry.repo = xstrdup(argv[i]+7);
462 } 462 }
463 if (!strncmp(argv[i], "--page=", 7)) { 463 if (!strncmp(argv[i], "--page=", 7)) {
464 ctx.qry.page = xstrdup(argv[i]+7); 464 ctx.qry.page = xstrdup(argv[i]+7);
465 } 465 }
466 if (!strncmp(argv[i], "--head=", 7)) { 466 if (!strncmp(argv[i], "--head=", 7)) {
467 ctx.qry.head = xstrdup(argv[i]+7); 467 ctx.qry.head = xstrdup(argv[i]+7);
468 ctx.qry.has_symref = 1; 468 ctx.qry.has_symref = 1;
469 } 469 }
470 if (!strncmp(argv[i], "--sha1=", 7)) { 470 if (!strncmp(argv[i], "--sha1=", 7)) {
471 ctx.qry.sha1 = xstrdup(argv[i]+7); 471 ctx.qry.sha1 = xstrdup(argv[i]+7);
472 ctx.qry.has_sha1 = 1; 472 ctx.qry.has_sha1 = 1;
473 } 473 }
474 if (!strncmp(argv[i], "--ofs=", 6)) { 474 if (!strncmp(argv[i], "--ofs=", 6)) {
475 ctx.qry.ofs = atoi(argv[i]+6); 475 ctx.qry.ofs = atoi(argv[i]+6);
476 } 476 }
477 if (!strncmp(argv[i], "--scan-tree=", 12)) { 477 if (!strncmp(argv[i], "--scan-tree=", 12)) {
478 scan++; 478 scan++;
479 scan_tree(argv[i] + 12); 479 scan_tree(argv[i] + 12);
480 } 480 }
481 } 481 }
482 if (scan) { 482 if (scan) {
483 qsort(cgit_repolist.repos, cgit_repolist.count, 483 qsort(cgit_repolist.repos, cgit_repolist.count,
484 sizeof(struct cgit_repo), cmp_repos); 484 sizeof(struct cgit_repo), cmp_repos);
485 print_repolist(&cgit_repolist); 485 print_repolist(stdout, &cgit_repolist, 0);
486 exit(0); 486 exit(0);
487 } 487 }
488} 488}
489 489
490static int calc_ttl() 490static int calc_ttl()
491{ 491{
492 if (!ctx.repo) 492 if (!ctx.repo)
493 return ctx.cfg.cache_root_ttl; 493 return ctx.cfg.cache_root_ttl;
494 494
495 if (!ctx.qry.page) 495 if (!ctx.qry.page)
496 return ctx.cfg.cache_repo_ttl; 496 return ctx.cfg.cache_repo_ttl;
497 497
498 if (ctx.qry.has_symref) 498 if (ctx.qry.has_symref)
499 return ctx.cfg.cache_dynamic_ttl; 499 return ctx.cfg.cache_dynamic_ttl;
500 500
501 if (ctx.qry.has_sha1) 501 if (ctx.qry.has_sha1)
502 return ctx.cfg.cache_static_ttl; 502 return ctx.cfg.cache_static_ttl;
503 503
504 return ctx.cfg.cache_repo_ttl; 504 return ctx.cfg.cache_repo_ttl;
505} 505}
506 506
507int main(int argc, const char **argv) 507int main(int argc, const char **argv)
508{ 508{
509 const char *path; 509 const char *path;
510 char *qry; 510 char *qry;
511 int err, ttl; 511 int err, ttl;
512 512
513 prepare_context(&ctx); 513 prepare_context(&ctx);
514 cgit_repolist.length = 0; 514 cgit_repolist.length = 0;
515 cgit_repolist.count = 0; 515 cgit_repolist.count = 0;
516 cgit_repolist.repos = NULL; 516 cgit_repolist.repos = NULL;
517 517
518 cgit_parse_args(argc, argv); 518 cgit_parse_args(argc, argv);
519 parse_configfile(ctx.env.cgit_config, config_cb); 519 parse_configfile(ctx.env.cgit_config, config_cb);
520 ctx.repo = NULL; 520 ctx.repo = NULL;
521 http_parse_querystring(ctx.qry.raw, querystring_cb); 521 http_parse_querystring(ctx.qry.raw, querystring_cb);
522 522
523 /* If virtual-root isn't specified in cgitrc, lets pretend 523 /* If virtual-root isn't specified in cgitrc, lets pretend
524 * that virtual-root equals SCRIPT_NAME. 524 * that virtual-root equals SCRIPT_NAME.
525 */ 525 */
526 if (!ctx.cfg.virtual_root) 526 if (!ctx.cfg.virtual_root)
527 ctx.cfg.virtual_root = ctx.cfg.script_name; 527 ctx.cfg.virtual_root = ctx.cfg.script_name;
528 528
529 /* If no url parameter is specified on the querystring, lets 529 /* If no url parameter is specified on the querystring, lets
530 * use PATH_INFO as url. This allows cgit to work with virtual 530 * use PATH_INFO as url. This allows cgit to work with virtual
531 * urls without the need for rewriterules in the webserver (as 531 * urls without the need for rewriterules in the webserver (as
532 * long as PATH_INFO is included in the cache lookup key). 532 * long as PATH_INFO is included in the cache lookup key).
533 */ 533 */
534 path = ctx.env.path_info; 534 path = ctx.env.path_info;
535 if (!ctx.qry.url && path) { 535 if (!ctx.qry.url && path) {
536 if (path[0] == '/') 536 if (path[0] == '/')
537 path++; 537 path++;
538 ctx.qry.url = xstrdup(path); 538 ctx.qry.url = xstrdup(path);
539 if (ctx.qry.raw) { 539 if (ctx.qry.raw) {
540 qry = ctx.qry.raw; 540 qry = ctx.qry.raw;
541 ctx.qry.raw = xstrdup(fmt("%s?%s", path, qry)); 541 ctx.qry.raw = xstrdup(fmt("%s?%s", path, qry));
542 free(qry); 542 free(qry);
543 } else 543 } else
544 ctx.qry.raw = xstrdup(ctx.qry.url); 544 ctx.qry.raw = xstrdup(ctx.qry.url);
545 cgit_parse_url(ctx.qry.url); 545 cgit_parse_url(ctx.qry.url);
546 } 546 }
547 547
548 ttl = calc_ttl(); 548 ttl = calc_ttl();
549 ctx.page.expires += ttl*60; 549 ctx.page.expires += ttl*60;