diff options
| author | 2008-04-09 04:29:21 (JST) | |
|---|---|---|
| committer | 2008-04-09 04:29:21 (JST) | |
| commit | 23296ad648c0e2a9e3cf40a3de322b10ad25cce3 (patch) | |
| tree | 136493d8228b0ff4971feb06b0e8aee296367b00 | |
| parent | e2a44cf0923398396b7a321d5ce894ad3bf6f580 (diff) | |
| parent | c6f747649ace1a92ed5dfaae9cc1ea3affe0bf51 (diff) | |
| download | cgit-23296ad648c0e2a9e3cf40a3de322b10ad25cce3.zip cgit-23296ad648c0e2a9e3cf40a3de322b10ad25cce3.tar.gz | |
Merge branch 'lh/cleanup'
* lh/cleanup: (21 commits)
Reset ctx.repo to NULL when the config parser is finished
Move cgit_parse_query() from parsing.c to html.c as http_parse_querystring()
Move function for configfile parsing into configfile.[ch]
Add cache.h
Remove global and obsolete cgit_cmd
Makefile: copy the QUIET constructs from the Makefile in git.git
Move cgit_version from shared.c to cgit.c
Makefile: autobuild dependency rules
Initial Makefile cleanup
Move non-generic functions from shared.c to cgit.c
Add ui-shared.h
Add separate header-files for each page/view
Refactor snapshot support
Add command dispatcher
Remove obsolete cacheitem parameter to ui-functions
Add struct cgit_page to cgit_context
Introduce html.h
Improve initialization of git directory
Move cgit_repo into cgit_context
Add all config variables into struct cgit_context
...
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | Makefile | 75 | ||||
| -rw-r--r-- | cache.c | 21 | ||||
| -rw-r--r-- | cache.h | 23 | ||||
| -rw-r--r-- | cgit.c | 391 | ||||
| -rw-r--r-- | cgit.h | 237 | ||||
| -rw-r--r-- | cmd.c | 112 | ||||
| -rw-r--r-- | cmd.h | 15 | ||||
| -rw-r--r-- | configfile.c | 87 | ||||
| -rw-r--r-- | configfile.h | 8 | ||||
| -rw-r--r-- | html.c | 95 | ||||
| -rw-r--r-- | html.h | 20 | ||||
| -rw-r--r-- | parsing.c | 146 | ||||
| -rw-r--r-- | shared.c | 257 | ||||
| -rw-r--r-- | ui-blob.c | 16 | ||||
| -rw-r--r-- | ui-blob.h | 6 | ||||
| -rw-r--r-- | ui-commit.c | 26 | ||||
| -rw-r--r-- | ui-commit.h | 6 | ||||
| -rw-r--r-- | ui-diff.c | 9 | ||||
| -rw-r--r-- | ui-diff.h | 7 | ||||
| -rw-r--r-- | ui-log.c | 32 | ||||
| -rw-r--r-- | ui-log.h | 7 | ||||
| -rw-r--r-- | ui-patch.c | 10 | ||||
| -rw-r--r-- | ui-patch.h | 6 | ||||
| -rw-r--r-- | ui-refs.c | 175 | ||||
| -rw-r--r-- | ui-refs.h | 8 | ||||
| -rw-r--r-- | ui-repolist.c | 54 | ||||
| -rw-r--r-- | ui-repolist.h | 6 | ||||
| -rw-r--r-- | ui-shared.c | 237 | ||||
| -rw-r--r-- | ui-shared.h | 36 | ||||
| -rw-r--r-- | ui-snapshot.c | 123 | ||||
| -rw-r--r-- | ui-snapshot.h | 8 | ||||
| -rw-r--r-- | ui-summary.c | 189 | ||||
| -rw-r--r-- | ui-summary.h | 6 | ||||
| -rw-r--r-- | ui-tag.c | 3 | ||||
| -rw-r--r-- | ui-tag.h | 6 | ||||
| -rw-r--r-- | ui-tree.c | 26 | ||||
| -rw-r--r-- | ui-tree.h | 6 |
38 files changed, 1379 insertions, 1117 deletions
| @@ -3,3 +3,4 @@ cgit | |||
| 3 | cgit.conf | 3 | cgit.conf |
| 4 | VERSION | 4 | VERSION |
| 5 | *.o | 5 | *.o |
| 6 | *.d | ||
| @@ -12,13 +12,62 @@ GIT_URL = http://www.kernel.org/pub/software/scm/git/git-$(GIT_VER).tar.bz2 | |||
| 12 | # | 12 | # |
| 13 | -include cgit.conf | 13 | -include cgit.conf |
| 14 | 14 | ||
| 15 | # | ||
| 16 | # Define a way to invoke make in subdirs quietly, shamelessly ripped | ||
| 17 | # from git.git | ||
| 18 | # | ||
| 19 | QUIET_SUBDIR0 = +$(MAKE) -C # space to separate -C and subdir | ||
| 20 | QUIET_SUBDIR1 = | ||
| 15 | 21 | ||
| 16 | EXTLIBS = git/libgit.a git/xdiff/lib.a -lz -lcrypto | 22 | ifneq ($(findstring $(MAKEFLAGS),w),w) |
| 17 | OBJECTS = shared.o cache.o parsing.o html.o ui-shared.o ui-repolist.o \ | 23 | PRINT_DIR = --no-print-directory |
| 18 | ui-summary.o ui-log.o ui-tree.o ui-commit.o ui-diff.o \ | 24 | else # "make -w" |
| 19 | ui-snapshot.o ui-blob.o ui-tag.o ui-refs.o ui-patch.o | 25 | NO_SUBDIR = : |
| 26 | endif | ||
| 27 | |||
| 28 | ifndef V | ||
| 29 | QUIET_CC = @echo ' ' CC $@; | ||
| 30 | QUIET_MM = @echo ' ' MM $@; | ||
| 31 | QUIET_SUBDIR0 = +@subdir= | ||
| 32 | QUIET_SUBDIR1 = ;$(NO_SUBDIR) echo ' ' SUBDIR $$subdir; \ | ||
| 33 | $(MAKE) $(PRINT_DIR) -C $$subdir | ||
| 34 | endif | ||
| 35 | |||
| 36 | # | ||
| 37 | # Define a pattern rule for automatic dependency building | ||
| 38 | # | ||
| 39 | %.d: %.c | ||
| 40 | $(QUIET_MM)$(CC) $(CFLAGS) -MM $< | sed -e 's/\($*\)\.o:/\1.o $@:/g' >$@ | ||
| 41 | |||
| 42 | # | ||
| 43 | # Define a pattern rule for silent object building | ||
| 44 | # | ||
| 45 | %.o: %.c | ||
| 46 | $(QUIET_CC)$(CC) -o $*.o -c $(CFLAGS) $< | ||
| 20 | 47 | ||
| 21 | 48 | ||
| 49 | EXTLIBS = git/libgit.a git/xdiff/lib.a -lz -lcrypto | ||
| 50 | OBJECTS = | ||
| 51 | OBJECTS += cache.o | ||
| 52 | OBJECTS += cgit.o | ||
| 53 | OBJECTS += cmd.o | ||
| 54 | OBJECTS += configfile.o | ||
| 55 | OBJECTS += html.o | ||
| 56 | OBJECTS += parsing.o | ||
| 57 | OBJECTS += shared.o | ||
| 58 | OBJECTS += ui-blob.o | ||
| 59 | OBJECTS += ui-commit.o | ||
| 60 | OBJECTS += ui-diff.o | ||
| 61 | OBJECTS += ui-log.o | ||
| 62 | OBJECTS += ui-patch.o | ||
| 63 | OBJECTS += ui-refs.o | ||
| 64 | OBJECTS += ui-repolist.o | ||
| 65 | OBJECTS += ui-shared.o | ||
| 66 | OBJECTS += ui-snapshot.o | ||
| 67 | OBJECTS += ui-summary.o | ||
| 68 | OBJECTS += ui-tag.o | ||
| 69 | OBJECTS += ui-tree.o | ||
| 70 | |||
| 22 | ifdef NEEDS_LIBICONV | 71 | ifdef NEEDS_LIBICONV |
| 23 | EXTLIBS += -liconv | 72 | EXTLIBS += -liconv |
| 24 | endif | 73 | endif |
| @@ -41,21 +90,25 @@ CFLAGS += -DCGIT_SCRIPT_NAME='"$(CGIT_SCRIPT_NAME)"' | |||
| 41 | CFLAGS += -DCGIT_CACHE_ROOT='"$(CACHE_ROOT)"' | 90 | CFLAGS += -DCGIT_CACHE_ROOT='"$(CACHE_ROOT)"' |
| 42 | 91 | ||
| 43 | 92 | ||
| 44 | cgit: cgit.c $(OBJECTS) | 93 | cgit: $(OBJECTS) |
| 45 | $(CC) $(CFLAGS) cgit.c -o cgit $(OBJECTS) $(EXTLIBS) | 94 | $(QUIET_CC)$(CC) $(CFLAGS) -o cgit $(OBJECTS) $(EXTLIBS) |
| 95 | |||
| 96 | $(OBJECTS): git/xdiff/lib.a git/libgit.a | ||
| 97 | |||
| 98 | cgit.o: VERSION | ||
| 46 | 99 | ||
| 47 | $(OBJECTS): cgit.h git/xdiff/lib.a git/libgit.a VERSION | 100 | -include $(OBJECTS:.o=.d) |
| 48 | 101 | ||
| 49 | git/xdiff/lib.a: | git | 102 | git/xdiff/lib.a: | git |
| 50 | 103 | ||
| 51 | git/libgit.a: | git | 104 | git/libgit.a: | git |
| 52 | 105 | ||
| 53 | git: | 106 | git: |
| 54 | cd git && $(MAKE) xdiff/lib.a | 107 | $(QUIET_SUBDIR0)git $(QUIET_SUBDIR1) xdiff/lib.a |
| 55 | cd git && $(MAKE) libgit.a | 108 | $(QUIET_SUBDIR0)git $(QUIET_SUBDIR1) libgit.a |
| 56 | 109 | ||
| 57 | test: all | 110 | test: all |
| 58 | $(MAKE) -C tests | 111 | $(QUIET_SUBDIR0)tests $(QUIET_SUBDIR1) all |
| 59 | 112 | ||
| 60 | install: all | 113 | install: all |
| 61 | mkdir -p $(DESTDIR)$(CGIT_SCRIPT_PATH) | 114 | mkdir -p $(DESTDIR)$(CGIT_SCRIPT_PATH) |
| @@ -69,7 +122,7 @@ uninstall: | |||
| 69 | rm -f $(CGIT_SCRIPT_PATH)/cgit.png | 122 | rm -f $(CGIT_SCRIPT_PATH)/cgit.png |
| 70 | 123 | ||
| 71 | clean: | 124 | clean: |
| 72 | rm -f cgit VERSION *.o | 125 | rm -f cgit VERSION *.o *.d |
| 73 | cd git && $(MAKE) clean | 126 | cd git && $(MAKE) clean |
| 74 | 127 | ||
| 75 | distclean: clean | 128 | distclean: clean |
| @@ -7,6 +7,7 @@ | |||
| 7 | */ | 7 | */ |
| 8 | 8 | ||
| 9 | #include "cgit.h" | 9 | #include "cgit.h" |
| 10 | #include "cache.h" | ||
| 10 | 11 | ||
| 11 | const int NOLOCK = -1; | 12 | const int NOLOCK = -1; |
| 12 | 13 | ||
| @@ -44,23 +45,23 @@ int cache_create_dirs() | |||
| 44 | { | 45 | { |
| 45 | char *path; | 46 | char *path; |
| 46 | 47 | ||
| 47 | path = fmt("%s", cgit_cache_root); | 48 | path = fmt("%s", ctx.cfg.cache_root); |
| 48 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) | 49 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) |
| 49 | return 0; | 50 | return 0; |
| 50 | 51 | ||
| 51 | if (!cgit_repo) | 52 | if (!ctx.repo) |
| 52 | return 0; | 53 | return 0; |
| 53 | 54 | ||
| 54 | path = fmt("%s/%s", cgit_cache_root, | 55 | path = fmt("%s/%s", ctx.cfg.cache_root, |
| 55 | cache_safe_filename(cgit_repo->url)); | 56 | cache_safe_filename(ctx.repo->url)); |
| 56 | 57 | ||
| 57 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) | 58 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) |
| 58 | return 0; | 59 | return 0; |
| 59 | 60 | ||
| 60 | if (cgit_query_page) { | 61 | if (ctx.qry.page) { |
| 61 | path = fmt("%s/%s/%s", cgit_cache_root, | 62 | path = fmt("%s/%s/%s", ctx.cfg.cache_root, |
| 62 | cache_safe_filename(cgit_repo->url), | 63 | cache_safe_filename(ctx.repo->url), |
| 63 | cgit_query_page); | 64 | ctx.qry.page); |
| 64 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) | 65 | if (mkdir(path, S_IRWXU) && errno!=EEXIST) |
| 65 | return 0; | 66 | return 0; |
| 66 | } | 67 | } |
| @@ -74,7 +75,7 @@ int cache_refill_overdue(const char *lockfile) | |||
| 74 | if (stat(lockfile, &st)) | 75 | if (stat(lockfile, &st)) |
| 75 | return 0; | 76 | return 0; |
| 76 | else | 77 | else |
| 77 | return (time(NULL) - st.st_mtime > cgit_cache_max_create_time); | 78 | return (time(NULL) - st.st_mtime > ctx.cfg.cache_max_create_time); |
| 78 | } | 79 | } |
| 79 | 80 | ||
| 80 | int cache_lock(struct cacheitem *item) | 81 | int cache_lock(struct cacheitem *item) |
| @@ -83,7 +84,7 @@ int cache_lock(struct cacheitem *item) | |||
| 83 | char *lockfile = xstrdup(fmt("%s.lock", item->name)); | 84 | char *lockfile = xstrdup(fmt("%s.lock", item->name)); |
| 84 | 85 | ||
| 85 | top: | 86 | top: |
| 86 | if (++i > cgit_max_lock_attempts) | 87 | if (++i > ctx.cfg.max_lock_attempts) |
| 87 | die("cache_lock: unable to lock %s: %s", | 88 | die("cache_lock: unable to lock %s: %s", |
| 88 | item->name, strerror(errno)); | 89 | item->name, strerror(errno)); |
| 89 | 90 | ||
| @@ -0,0 +1,23 @@ | |||
| 1 | /* | ||
| 2 | * Since git has it's own cache.h which we include, | ||
| 3 | * lets test on CGIT_CACHE_H to avoid confusion | ||
| 4 | */ | ||
| 5 | |||
| 6 | #ifndef CGIT_CACHE_H | ||
| 7 | #define CGIT_CACHE_H | ||
| 8 | |||
| 9 | struct cacheitem { | ||
| 10 | char *name; | ||
| 11 | struct stat st; | ||
| 12 | int ttl; | ||
| 13 | int fd; | ||
| 14 | }; | ||
| 15 | |||
| 16 | extern char *cache_safe_filename(const char *unsafe); | ||
| 17 | extern int cache_lock(struct cacheitem *item); | ||
| 18 | extern int cache_unlock(struct cacheitem *item); | ||
| 19 | extern int cache_cancel_lock(struct cacheitem *item); | ||
| 20 | extern int cache_exist(struct cacheitem *item); | ||
| 21 | extern int cache_expired(struct cacheitem *item); | ||
| 22 | |||
| 23 | #endif /* CGIT_CACHE_H */ | ||
| @@ -7,40 +7,199 @@ | |||
| 7 | */ | 7 | */ |
| 8 | 8 | ||
| 9 | #include "cgit.h" | 9 | #include "cgit.h" |
| 10 | #include "cache.h" | ||
| 11 | #include "cmd.h" | ||
| 12 | #include "configfile.h" | ||
| 13 | #include "html.h" | ||
| 14 | #include "ui-shared.h" | ||
| 15 | |||
| 16 | const char *cgit_version = CGIT_VERSION; | ||
| 17 | |||
| 18 | void config_cb(const char *name, const char *value) | ||
| 19 | { | ||
| 20 | if (!strcmp(name, "root-title")) | ||
| 21 | ctx.cfg.root_title = xstrdup(value); | ||
| 22 | else if (!strcmp(name, "css")) | ||
| 23 | ctx.cfg.css = xstrdup(value); | ||
| 24 | else if (!strcmp(name, "logo")) | ||
| 25 | ctx.cfg.logo = xstrdup(value); | ||
| 26 | else if (!strcmp(name, "index-header")) | ||
| 27 | ctx.cfg.index_header = xstrdup(value); | ||
| 28 | else if (!strcmp(name, "index-info")) | ||
| 29 | ctx.cfg.index_info = xstrdup(value); | ||
| 30 | else if (!strcmp(name, "logo-link")) | ||
| 31 | ctx.cfg.logo_link = xstrdup(value); | ||
| 32 | else if (!strcmp(name, "module-link")) | ||
| 33 | ctx.cfg.module_link = xstrdup(value); | ||
| 34 | else if (!strcmp(name, "virtual-root")) { | ||
| 35 | ctx.cfg.virtual_root = trim_end(value, '/'); | ||
| 36 | if (!ctx.cfg.virtual_root && (!strcmp(value, "/"))) | ||
| 37 | ctx.cfg.virtual_root = ""; | ||
| 38 | } else if (!strcmp(name, "nocache")) | ||
| 39 | ctx.cfg.nocache = atoi(value); | ||
| 40 | else if (!strcmp(name, "snapshots")) | ||
| 41 | ctx.cfg.snapshots = cgit_parse_snapshots_mask(value); | ||
| 42 | else if (!strcmp(name, "enable-index-links")) | ||
| 43 | ctx.cfg.enable_index_links = atoi(value); | ||
| 44 | else if (!strcmp(name, "enable-log-filecount")) | ||
| 45 | ctx.cfg.enable_log_filecount = atoi(value); | ||
| 46 | else if (!strcmp(name, "enable-log-linecount")) | ||
| 47 | ctx.cfg.enable_log_linecount = atoi(value); | ||
| 48 | else if (!strcmp(name, "cache-root")) | ||
| 49 | ctx.cfg.cache_root = xstrdup(value); | ||
| 50 | else if (!strcmp(name, "cache-root-ttl")) | ||
| 51 | ctx.cfg.cache_root_ttl = atoi(value); | ||
| 52 | else if (!strcmp(name, "cache-repo-ttl")) | ||
| 53 | ctx.cfg.cache_repo_ttl = atoi(value); | ||
| 54 | else if (!strcmp(name, "cache-static-ttl")) | ||
| 55 | ctx.cfg.cache_static_ttl = atoi(value); | ||
| 56 | else if (!strcmp(name, "cache-dynamic-ttl")) | ||
| 57 | ctx.cfg.cache_dynamic_ttl = atoi(value); | ||
| 58 | else if (!strcmp(name, "max-message-length")) | ||
| 59 | ctx.cfg.max_msg_len = atoi(value); | ||
| 60 | else if (!strcmp(name, "max-repodesc-length")) | ||
| 61 | ctx.cfg.max_repodesc_len = atoi(value); | ||
| 62 | else if (!strcmp(name, "max-commit-count")) | ||
| 63 | ctx.cfg.max_commit_count = atoi(value); | ||
| 64 | else if (!strcmp(name, "summary-log")) | ||
| 65 | ctx.cfg.summary_log = atoi(value); | ||
| 66 | else if (!strcmp(name, "summary-branches")) | ||
| 67 | ctx.cfg.summary_branches = atoi(value); | ||
| 68 | else if (!strcmp(name, "summary-tags")) | ||
| 69 | ctx.cfg.summary_tags = atoi(value); | ||
| 70 | else if (!strcmp(name, "agefile")) | ||
| 71 | ctx.cfg.agefile = xstrdup(value); | ||
| 72 | else if (!strcmp(name, "renamelimit")) | ||
| 73 | ctx.cfg.renamelimit = atoi(value); | ||
| 74 | else if (!strcmp(name, "robots")) | ||
| 75 | ctx.cfg.robots = xstrdup(value); | ||
| 76 | else if (!strcmp(name, "clone-prefix")) | ||
| 77 | ctx.cfg.clone_prefix = xstrdup(value); | ||
| 78 | else if (!strcmp(name, "repo.group")) | ||
| 79 | ctx.cfg.repo_group = xstrdup(value); | ||
| 80 | else if (!strcmp(name, "repo.url")) | ||
| 81 | ctx.repo = cgit_add_repo(value); | ||
| 82 | else if (!strcmp(name, "repo.name")) | ||
| 83 | ctx.repo->name = xstrdup(value); | ||
| 84 | else if (ctx.repo && !strcmp(name, "repo.path")) | ||
| 85 | ctx.repo->path = trim_end(value, '/'); | ||
| 86 | else if (ctx.repo && !strcmp(name, "repo.clone-url")) | ||
| 87 | ctx.repo->clone_url = xstrdup(value); | ||
| 88 | else if (ctx.repo && !strcmp(name, "repo.desc")) | ||
| 89 | ctx.repo->desc = xstrdup(value); | ||
| 90 | else if (ctx.repo && !strcmp(name, "repo.owner")) | ||
| 91 | ctx.repo->owner = xstrdup(value); | ||
| 92 | else if (ctx.repo && !strcmp(name, "repo.defbranch")) | ||
| 93 | ctx.repo->defbranch = xstrdup(value); | ||
| 94 | else if (ctx.repo && !strcmp(name, "repo.snapshots")) | ||
| 95 | ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */ | ||
| 96 | else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount")) | ||
| 97 | ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value); | ||
| 98 | else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount")) | ||
| 99 | ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value); | ||
| 100 | else if (ctx.repo && !strcmp(name, "repo.module-link")) | ||
| 101 | ctx.repo->module_link= xstrdup(value); | ||
| 102 | else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) { | ||
| 103 | if (*value == '/') | ||
| 104 | ctx.repo->readme = xstrdup(value); | ||
| 105 | else | ||
| 106 | ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value)); | ||
| 107 | } else if (!strcmp(name, "include")) | ||
| 108 | parse_configfile(value, config_cb); | ||
| 109 | } | ||
| 110 | |||
| 111 | static void querystring_cb(const char *name, const char *value) | ||
| 112 | { | ||
| 113 | if (!strcmp(name,"r")) { | ||
| 114 | ctx.qry.repo = xstrdup(value); | ||
| 115 | ctx.repo = cgit_get_repoinfo(value); | ||
| 116 | } else if (!strcmp(name, "p")) { | ||
| 117 | ctx.qry.page = xstrdup(value); | ||
| 118 | } else if (!strcmp(name, "url")) { | ||
| 119 | cgit_parse_url(value); | ||
| 120 | } else if (!strcmp(name, "qt")) { | ||
| 121 | ctx.qry.grep = xstrdup(value); | ||
| 122 | } else if (!strcmp(name, "q")) { | ||
| 123 | ctx.qry.search = xstrdup(value); | ||
| 124 | } else if (!strcmp(name, "h")) { | ||
| 125 | ctx.qry.head = xstrdup(value); | ||
| 126 | ctx.qry.has_symref = 1; | ||
| 127 | } else if (!strcmp(name, "id")) { | ||
| 128 | ctx.qry.sha1 = xstrdup(value); | ||
| 129 | ctx.qry.has_sha1 = 1; | ||
| 130 | } else if (!strcmp(name, "id2")) { | ||
| 131 | ctx.qry.sha2 = xstrdup(value); | ||
| 132 | ctx.qry.has_sha1 = 1; | ||
| 133 | } else if (!strcmp(name, "ofs")) { | ||
| 134 | ctx.qry.ofs = atoi(value); | ||
| 135 | } else if (!strcmp(name, "path")) { | ||
| 136 | ctx.qry.path = trim_end(value, '/'); | ||
| 137 | } else if (!strcmp(name, "name")) { | ||
| 138 | ctx.qry.name = xstrdup(value); | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | static void prepare_context(struct cgit_context *ctx) | ||
| 143 | { | ||
| 144 | memset(ctx, 0, sizeof(ctx)); | ||
| 145 | ctx->cfg.agefile = "info/web/last-modified"; | ||
| 146 | ctx->cfg.cache_dynamic_ttl = 5; | ||
| 147 | ctx->cfg.cache_max_create_time = 5; | ||
| 148 | ctx->cfg.cache_repo_ttl = 5; | ||
| 149 | ctx->cfg.cache_root = CGIT_CACHE_ROOT; | ||
| 150 | ctx->cfg.cache_root_ttl = 5; | ||
| 151 | ctx->cfg.cache_static_ttl = -1; | ||
| 152 | ctx->cfg.css = "/cgit.css"; | ||
| 153 | ctx->cfg.logo = "/git-logo.png"; | ||
| 154 | ctx->cfg.max_commit_count = 50; | ||
| 155 | ctx->cfg.max_lock_attempts = 5; | ||
| 156 | ctx->cfg.max_msg_len = 60; | ||
| 157 | ctx->cfg.max_repodesc_len = 60; | ||
| 158 | ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s"; | ||
| 159 | ctx->cfg.renamelimit = -1; | ||
| 160 | ctx->cfg.robots = "index, nofollow"; | ||
| 161 | ctx->cfg.root_title = "Git repository browser"; | ||
| 162 | ctx->cfg.script_name = CGIT_SCRIPT_NAME; | ||
| 163 | ctx->page.mimetype = "text/html"; | ||
| 164 | ctx->page.charset = PAGE_ENCODING; | ||
| 165 | ctx->page.filename = NULL; | ||
| 166 | } | ||
| 10 | 167 | ||
| 11 | static int cgit_prepare_cache(struct cacheitem *item) | 168 | static int cgit_prepare_cache(struct cacheitem *item) |
| 12 | { | 169 | { |
| 13 | if (!cgit_repo && cgit_query_repo) { | 170 | if (!ctx.repo && ctx.qry.repo) { |
| 14 | char *title = fmt("%s - %s", cgit_root_title, "Bad request"); | 171 | ctx.page.title = fmt("%s - %s", ctx.cfg.root_title, |
| 15 | cgit_print_docstart(title, item); | 172 | "Bad request"); |
| 16 | cgit_print_pageheader(title, 0); | 173 | cgit_print_http_headers(&ctx); |
| 17 | cgit_print_error(fmt("Unknown repo: %s", cgit_query_repo)); | 174 | cgit_print_docstart(&ctx); |
| 175 | cgit_print_pageheader(&ctx); | ||
| 176 | cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo)); | ||
| 18 | cgit_print_docend(); | 177 | cgit_print_docend(); |
| 19 | return 0; | 178 | return 0; |
| 20 | } | 179 | } |
| 21 | 180 | ||
| 22 | if (!cgit_repo) { | 181 | if (!ctx.repo) { |
| 23 | item->name = xstrdup(fmt("%s/index.html", cgit_cache_root)); | 182 | item->name = xstrdup(fmt("%s/index.html", ctx.cfg.cache_root)); |
| 24 | item->ttl = cgit_cache_root_ttl; | 183 | item->ttl = ctx.cfg.cache_root_ttl; |
| 25 | return 1; | 184 | return 1; |
| 26 | } | 185 | } |
| 27 | 186 | ||
| 28 | if (!cgit_cmd) { | 187 | if (!ctx.qry.page) { |
| 29 | item->name = xstrdup(fmt("%s/%s/index.%s.html", cgit_cache_root, | 188 | item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root, |
| 30 | cache_safe_filename(cgit_repo->url), | 189 | cache_safe_filename(ctx.repo->url), |
| 31 | cache_safe_filename(cgit_querystring))); | 190 | cache_safe_filename(ctx.qry.raw))); |
| 32 | item->ttl = cgit_cache_repo_ttl; | 191 | item->ttl = ctx.cfg.cache_repo_ttl; |
| 33 | } else { | 192 | } else { |
| 34 | item->name = xstrdup(fmt("%s/%s/%s/%s.html", cgit_cache_root, | 193 | item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root, |
| 35 | cache_safe_filename(cgit_repo->url), | 194 | cache_safe_filename(ctx.repo->url), |
| 36 | cgit_query_page, | 195 | ctx.qry.page, |
| 37 | cache_safe_filename(cgit_querystring))); | 196 | cache_safe_filename(ctx.qry.raw))); |
| 38 | if (cgit_query_has_symref) | 197 | if (ctx.qry.has_symref) |
| 39 | item->ttl = cgit_cache_dynamic_ttl; | 198 | item->ttl = ctx.cfg.cache_dynamic_ttl; |
| 40 | else if (cgit_query_has_sha1) | 199 | else if (ctx.qry.has_sha1) |
| 41 | item->ttl = cgit_cache_static_ttl; | 200 | item->ttl = ctx.cfg.cache_static_ttl; |
| 42 | else | 201 | else |
| 43 | item->ttl = cgit_cache_repo_ttl; | 202 | item->ttl = ctx.cfg.cache_repo_ttl; |
| 44 | } | 203 | } |
| 45 | return 1; | 204 | return 1; |
| 46 | } | 205 | } |
| @@ -64,7 +223,7 @@ int find_current_ref(const char *refname, const unsigned char *sha1, | |||
| 64 | return info->match; | 223 | return info->match; |
| 65 | } | 224 | } |
| 66 | 225 | ||
| 67 | char *find_default_branch(struct repoinfo *repo) | 226 | char *find_default_branch(struct cgit_repo *repo) |
| 68 | { | 227 | { |
| 69 | struct refmatch info; | 228 | struct refmatch info; |
| 70 | 229 | ||
| @@ -78,113 +237,98 @@ char *find_default_branch(struct repoinfo *repo) | |||
| 78 | return info.first_ref; | 237 | return info.first_ref; |
| 79 | } | 238 | } |
| 80 | 239 | ||
| 81 | static void cgit_print_repo_page(struct cacheitem *item) | 240 | static int prepare_repo_cmd(struct cgit_context *ctx) |
| 82 | { | 241 | { |
| 83 | char *title, *tmp; | 242 | char *tmp; |
| 84 | int show_search; | ||
| 85 | unsigned char sha1[20]; | 243 | unsigned char sha1[20]; |
| 86 | 244 | int nongit = 0; | |
| 87 | if (chdir(cgit_repo->path)) { | 245 | |
| 88 | title = fmt("%s - %s", cgit_root_title, "Bad request"); | 246 | setenv("GIT_DIR", ctx->repo->path, 1); |
| 89 | cgit_print_docstart(title, item); | 247 | setup_git_directory_gently(&nongit); |
| 90 | cgit_print_pageheader(title, 0); | 248 | if (nongit) { |
| 91 | cgit_print_error(fmt("Unable to scan repository: %s", | 249 | ctx->page.title = fmt("%s - %s", ctx->cfg.root_title, |
| 92 | strerror(errno))); | 250 | "config error"); |
| 251 | tmp = fmt("Not a git repository: '%s'", ctx->repo->path); | ||
| 252 | ctx->repo = NULL; | ||
| 253 | cgit_print_http_headers(ctx); | ||
| 254 | cgit_print_docstart(ctx); | ||
| 255 | cgit_print_pageheader(ctx); | ||
| 256 | cgit_print_error(tmp); | ||
| 93 | cgit_print_docend(); | 257 | cgit_print_docend(); |
| 94 | return; | 258 | return 1; |
| 95 | } | 259 | } |
| 260 | ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc); | ||
| 96 | 261 | ||
| 97 | title = fmt("%s - %s", cgit_repo->name, cgit_repo->desc); | 262 | if (!ctx->qry.head) { |
| 98 | show_search = 0; | 263 | ctx->qry.head = xstrdup(find_default_branch(ctx->repo)); |
| 99 | setenv("GIT_DIR", cgit_repo->path, 1); | 264 | ctx->repo->defbranch = ctx->qry.head; |
| 100 | |||
| 101 | if (!cgit_query_head) { | ||
| 102 | cgit_query_head = xstrdup(find_default_branch(cgit_repo)); | ||
| 103 | cgit_repo->defbranch = cgit_query_head; | ||
| 104 | } | 265 | } |
| 105 | 266 | ||
| 106 | if (!cgit_query_head) { | 267 | if (!ctx->qry.head) { |
| 107 | cgit_print_docstart(title, item); | 268 | cgit_print_http_headers(ctx); |
| 108 | cgit_print_pageheader(title, 0); | 269 | cgit_print_docstart(ctx); |
| 270 | cgit_print_pageheader(ctx); | ||
| 109 | cgit_print_error("Repository seems to be empty"); | 271 | cgit_print_error("Repository seems to be empty"); |
| 110 | cgit_print_docend(); | 272 | cgit_print_docend(); |
| 111 | return; | 273 | return 1; |
| 112 | } | 274 | } |
| 113 | 275 | ||
| 114 | if (get_sha1(cgit_query_head, sha1)) { | 276 | if (get_sha1(ctx->qry.head, sha1)) { |
| 115 | tmp = xstrdup(cgit_query_head); | 277 | tmp = xstrdup(ctx->qry.head); |
| 116 | cgit_query_head = cgit_repo->defbranch; | 278 | ctx->qry.head = ctx->repo->defbranch; |
| 117 | cgit_print_docstart(title, item); | 279 | cgit_print_http_headers(ctx); |
| 118 | cgit_print_pageheader(title, 0); | 280 | cgit_print_docstart(ctx); |
| 281 | cgit_print_pageheader(ctx); | ||
| 119 | cgit_print_error(fmt("Invalid branch: %s", tmp)); | 282 | cgit_print_error(fmt("Invalid branch: %s", tmp)); |
| 120 | cgit_print_docend(); | 283 | cgit_print_docend(); |
| 121 | return; | 284 | return 1; |
| 122 | } | 285 | } |
| 286 | return 0; | ||
| 287 | } | ||
| 123 | 288 | ||
| 124 | if ((cgit_cmd == CMD_SNAPSHOT) && cgit_repo->snapshots) { | 289 | static void process_request(struct cgit_context *ctx) |
| 125 | cgit_print_snapshot(item, cgit_query_head, cgit_query_sha1, | 290 | { |
| 126 | cgit_repobasename(cgit_repo->url), | 291 | struct cgit_cmd *cmd; |
| 127 | cgit_query_path, | 292 | |
| 128 | cgit_repo->snapshots ); | 293 | cmd = cgit_get_cmd(ctx); |
| 294 | if (!cmd) { | ||
| 295 | ctx->page.title = "cgit error"; | ||
| 296 | ctx->repo = NULL; | ||
| 297 | cgit_print_http_headers(ctx); | ||
| 298 | cgit_print_docstart(ctx); | ||
| 299 | cgit_print_pageheader(ctx); | ||
| 300 | cgit_print_error("Invalid request"); | ||
| 301 | cgit_print_docend(); | ||
| 129 | return; | 302 | return; |
| 130 | } | 303 | } |
| 131 | 304 | ||
| 132 | if (cgit_cmd == CMD_PATCH) { | 305 | if (cmd->want_repo && prepare_repo_cmd(ctx)) |
| 133 | cgit_print_patch(cgit_query_sha1, item); | ||
| 134 | return; | 306 | return; |
| 135 | } | ||
| 136 | 307 | ||
| 137 | if (cgit_cmd == CMD_BLOB) { | 308 | if (cmd->want_layout) { |
| 138 | cgit_print_blob(item, cgit_query_sha1, cgit_query_path); | 309 | cgit_print_http_headers(ctx); |
| 139 | return; | 310 | cgit_print_docstart(ctx); |
| 311 | cgit_print_pageheader(ctx); | ||
| 140 | } | 312 | } |
| 141 | 313 | ||
| 142 | show_search = (cgit_cmd == CMD_LOG); | 314 | cmd->fn(ctx); |
| 143 | cgit_print_docstart(title, item); | 315 | |
| 144 | if (!cgit_cmd) { | 316 | if (cmd->want_layout) |
| 145 | cgit_print_pageheader("summary", show_search); | ||
| 146 | cgit_print_summary(); | ||
| 147 | cgit_print_docend(); | 317 | cgit_print_docend(); |
| 148 | return; | 318 | } |
| 149 | } | ||
| 150 | 319 | ||
| 151 | cgit_print_pageheader(cgit_query_page, show_search); | 320 | static long ttl_seconds(long ttl) |
| 152 | 321 | { | |
| 153 | switch(cgit_cmd) { | 322 | if (ttl<0) |
| 154 | case CMD_LOG: | 323 | return 60 * 60 * 24 * 365; |
| 155 | cgit_print_log(cgit_query_sha1, cgit_query_ofs, | 324 | else |
| 156 | cgit_max_commit_count, cgit_query_grep, cgit_query_search, | 325 | return ttl * 60; |
| 157 | cgit_query_path, 1); | ||
| 158 | break; | ||
| 159 | case CMD_TREE: | ||
| 160 | cgit_print_tree(cgit_query_sha1, cgit_query_path); | ||
| 161 | break; | ||
| 162 | case CMD_COMMIT: | ||
| 163 | cgit_print_commit(cgit_query_sha1); | ||
| 164 | break; | ||
| 165 | case CMD_REFS: | ||
| 166 | cgit_print_refs(); | ||
| 167 | break; | ||
| 168 | case CMD_TAG: | ||
| 169 | cgit_print_tag(cgit_query_sha1); | ||
| 170 | break; | ||
| 171 | case CMD_DIFF: | ||
| 172 | cgit_print_diff(cgit_query_sha1, cgit_query_sha2, cgit_query_path); | ||
| 173 | break; | ||
| 174 | default: | ||
| 175 | cgit_print_error("Invalid request"); | ||
| 176 | } | ||
| 177 | cgit_print_docend(); | ||
| 178 | } | 326 | } |
| 179 | 327 | ||
| 180 | static void cgit_fill_cache(struct cacheitem *item, int use_cache) | 328 | static void cgit_fill_cache(struct cacheitem *item, int use_cache) |
| 181 | { | 329 | { |
| 182 | static char buf[PATH_MAX]; | ||
| 183 | int stdout2; | 330 | int stdout2; |
| 184 | 331 | ||
| 185 | getcwd(buf, sizeof(buf)); | ||
| 186 | item->st.st_mtime = time(NULL); | ||
| 187 | |||
| 188 | if (use_cache) { | 332 | if (use_cache) { |
| 189 | stdout2 = chk_positive(dup(STDOUT_FILENO), | 333 | stdout2 = chk_positive(dup(STDOUT_FILENO), |
| 190 | "Preserving STDOUT"); | 334 | "Preserving STDOUT"); |
| @@ -192,10 +336,9 @@ static void cgit_fill_cache(struct cacheitem *item, int use_cache) | |||
| 192 | chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); | 336 | chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); |
| 193 | } | 337 | } |
| 194 | 338 | ||
| 195 | if (cgit_repo) | 339 | ctx.page.modified = time(NULL); |
| 196 | cgit_print_repo_page(item); | 340 | ctx.page.expires = ctx.page.modified + ttl_seconds(item->ttl); |
| 197 | else | 341 | process_request(&ctx); |
| 198 | cgit_print_repolist(item); | ||
| 199 | 342 | ||
| 200 | if (use_cache) { | 343 | if (use_cache) { |
| 201 | chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); | 344 | chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); |
| @@ -203,8 +346,6 @@ static void cgit_fill_cache(struct cacheitem *item, int use_cache) | |||
| 203 | "Restoring original STDOUT"); | 346 | "Restoring original STDOUT"); |
| 204 | chk_zero(close(stdout2), "Closing temporary STDOUT"); | 347 | chk_zero(close(stdout2), "Closing temporary STDOUT"); |
| 205 | } | 348 | } |
| 206 | |||
| 207 | chdir(buf); | ||
| 208 | } | 349 | } |
| 209 | 350 | ||
| 210 | static void cgit_check_cache(struct cacheitem *item) | 351 | static void cgit_check_cache(struct cacheitem *item) |
| @@ -212,7 +353,7 @@ static void cgit_check_cache(struct cacheitem *item) | |||
| 212 | int i = 0; | 353 | int i = 0; |
| 213 | 354 | ||
| 214 | top: | 355 | top: |
| 215 | if (++i > cgit_max_lock_attempts) { | 356 | if (++i > ctx.cfg.max_lock_attempts) { |
| 216 | die("cgit_refresh_cache: unable to lock %s: %s", | 357 | die("cgit_refresh_cache: unable to lock %s: %s", |
| 217 | item->name, strerror(errno)); | 358 | item->name, strerror(errno)); |
| 218 | } | 359 | } |
| @@ -258,30 +399,30 @@ static void cgit_parse_args(int argc, const char **argv) | |||
| 258 | 399 | ||
| 259 | for (i = 1; i < argc; i++) { | 400 | for (i = 1; i < argc; i++) { |
| 260 | if (!strncmp(argv[i], "--cache=", 8)) { | 401 | if (!strncmp(argv[i], "--cache=", 8)) { |
| 261 | cgit_cache_root = xstrdup(argv[i]+8); | 402 | ctx.cfg.cache_root = xstrdup(argv[i]+8); |
| 262 | } | 403 | } |
| 263 | if (!strcmp(argv[i], "--nocache")) { | 404 | if (!strcmp(argv[i], "--nocache")) { |
| 264 | cgit_nocache = 1; | 405 | ctx.cfg.nocache = 1; |
| 265 | } | 406 | } |
| 266 | if (!strncmp(argv[i], "--query=", 8)) { | 407 | if (!strncmp(argv[i], "--query=", 8)) { |
| 267 | cgit_querystring = xstrdup(argv[i]+8); | 408 | ctx.qry.raw = xstrdup(argv[i]+8); |
| 268 | } | 409 | } |
| 269 | if (!strncmp(argv[i], "--repo=", 7)) { | 410 | if (!strncmp(argv[i], "--repo=", 7)) { |
| 270 | cgit_query_repo = xstrdup(argv[i]+7); | 411 | ctx.qry.repo = xstrdup(argv[i]+7); |
| 271 | } | 412 | } |
| 272 | if (!strncmp(argv[i], "--page=", 7)) { | 413 | if (!strncmp(argv[i], "--page=", 7)) { |
| 273 | cgit_query_page = xstrdup(argv[i]+7); | 414 | ctx.qry.page = xstrdup(argv[i]+7); |
| 274 | } | 415 | } |
| 275 | if (!strncmp(argv[i], "--head=", 7)) { | 416 | if (!strncmp(argv[i], "--head=", 7)) { |
| 276 | cgit_query_head = xstrdup(argv[i]+7); | 417 | ctx.qry.head = xstrdup(argv[i]+7); |
| 277 | cgit_query_has_symref = 1; | 418 | ctx.qry.has_symref = 1; |
| 278 | } | 419 | } |
| 279 | if (!strncmp(argv[i], "--sha1=", 7)) { | 420 | if (!strncmp(argv[i], "--sha1=", 7)) { |
