Use skip_prefix() to get rid of magic constants

Signed-off-by: Lukas Fleischer <lfleischer@lfos.de>
This commit is contained in:
Lukas Fleischer 2016-10-08 15:45:12 +02:00 committed by Jason A. Donenfeld
parent 5fe88a9c81
commit 32c27e8877
2 changed files with 34 additions and 28 deletions

56
cgit.c
View File

@ -31,6 +31,7 @@ static void process_cached_repolist(const char *path);
static void repo_config(struct cgit_repo *repo, const char *name, const char *value) static void repo_config(struct cgit_repo *repo, const char *name, const char *value)
{ {
const char *path;
struct string_list_item *item; struct string_list_item *item;
if (!strcmp(name, "name")) if (!strcmp(name, "name"))
@ -73,8 +74,8 @@ static void repo_config(struct cgit_repo *repo, const char *name, const char *va
repo->max_stats = cgit_find_stats_period(value, NULL); repo->max_stats = cgit_find_stats_period(value, NULL);
else if (!strcmp(name, "module-link")) else if (!strcmp(name, "module-link"))
repo->module_link= xstrdup(value); repo->module_link= xstrdup(value);
else if (starts_with(name, "module-link.")) { else if (skip_prefix(name, "module-link.", &path)) {
item = string_list_append(&repo->submodules, xstrdup(name + 12)); item = string_list_append(&repo->submodules, xstrdup(path));
item->util = xstrdup(value); item->util = xstrdup(value);
} else if (!strcmp(name, "section")) } else if (!strcmp(name, "section"))
repo->section = xstrdup(value); repo->section = xstrdup(value);
@ -106,14 +107,16 @@ static void repo_config(struct cgit_repo *repo, const char *name, const char *va
static void config_cb(const char *name, const char *value) static void config_cb(const char *name, const char *value)
{ {
const char *arg;
if (!strcmp(name, "section") || !strcmp(name, "repo.group")) if (!strcmp(name, "section") || !strcmp(name, "repo.group"))
ctx.cfg.section = xstrdup(value); ctx.cfg.section = xstrdup(value);
else if (!strcmp(name, "repo.url")) else if (!strcmp(name, "repo.url"))
ctx.repo = cgit_add_repo(value); ctx.repo = cgit_add_repo(value);
else if (ctx.repo && !strcmp(name, "repo.path")) else if (ctx.repo && !strcmp(name, "repo.path"))
ctx.repo->path = trim_end(value, '/'); ctx.repo->path = trim_end(value, '/');
else if (ctx.repo && starts_with(name, "repo.")) else if (ctx.repo && skip_prefix(name, "repo.", &arg))
repo_config(ctx.repo, name + 5, value); repo_config(ctx.repo, arg, value);
else if (!strcmp(name, "readme")) else if (!strcmp(name, "readme"))
string_list_append(&ctx.cfg.readme, xstrdup(value)); string_list_append(&ctx.cfg.readme, xstrdup(value));
else if (!strcmp(name, "root-title")) else if (!strcmp(name, "root-title"))
@ -280,8 +283,8 @@ static void config_cb(const char *name, const char *value)
ctx.cfg.branch_sort = 1; ctx.cfg.branch_sort = 1;
if (!strcmp(value, "name")) if (!strcmp(value, "name"))
ctx.cfg.branch_sort = 0; ctx.cfg.branch_sort = 0;
} else if (starts_with(name, "mimetype.")) } else if (skip_prefix(name, "mimetype.", &arg))
add_mimetype(name + 9, value); add_mimetype(arg, value);
else if (!strcmp(name, "include")) else if (!strcmp(name, "include"))
parse_configfile(expand_macros(value), config_cb); parse_configfile(expand_macros(value), config_cb);
} }
@ -470,13 +473,13 @@ static char *find_default_branch(struct cgit_repo *repo)
static char *guess_defbranch(void) static char *guess_defbranch(void)
{ {
const char *ref; const char *ref, *refname;
struct object_id oid; struct object_id oid;
ref = resolve_ref_unsafe("HEAD", 0, oid.hash, NULL); ref = resolve_ref_unsafe("HEAD", 0, oid.hash, NULL);
if (!ref || !starts_with(ref, "refs/heads/")) if (!ref || !skip_prefix(ref, "refs/heads/", &refname))
return "master"; return "master";
return xstrdup(ref + 11); return xstrdup(refname);
} }
/* The caller must free filename and ref after calling this. */ /* The caller must free filename and ref after calling this. */
@ -938,6 +941,7 @@ out:
static void cgit_parse_args(int argc, const char **argv) static void cgit_parse_args(int argc, const char **argv)
{ {
int i; int i;
const char *arg;
int scan = 0; int scan = 0;
for (i = 1; i < argc; i++) { for (i = 1; i < argc; i++) {
@ -958,28 +962,28 @@ static void cgit_parse_args(int argc, const char **argv)
exit(0); exit(0);
} }
if (starts_with(argv[i], "--cache=")) { if (skip_prefix(argv[i], "--cache=", &arg)) {
ctx.cfg.cache_root = xstrdup(argv[i] + 8); ctx.cfg.cache_root = xstrdup(arg);
} else if (!strcmp(argv[i], "--nocache")) { } else if (!strcmp(argv[i], "--nocache")) {
ctx.cfg.nocache = 1; ctx.cfg.nocache = 1;
} else if (!strcmp(argv[i], "--nohttp")) { } else if (!strcmp(argv[i], "--nohttp")) {
ctx.env.no_http = "1"; ctx.env.no_http = "1";
} else if (starts_with(argv[i], "--query=")) { } else if (skip_prefix(argv[i], "--query=", &arg)) {
ctx.qry.raw = xstrdup(argv[i] + 8); ctx.qry.raw = xstrdup(arg);
} else if (starts_with(argv[i], "--repo=")) { } else if (skip_prefix(argv[i], "--repo=", &arg)) {
ctx.qry.repo = xstrdup(argv[i] + 7); ctx.qry.repo = xstrdup(arg);
} else if (starts_with(argv[i], "--page=")) { } else if (skip_prefix(argv[i], "--page=", &arg)) {
ctx.qry.page = xstrdup(argv[i] + 7); ctx.qry.page = xstrdup(arg);
} else if (starts_with(argv[i], "--head=")) { } else if (skip_prefix(argv[i], "--head=", &arg)) {
ctx.qry.head = xstrdup(argv[i] + 7); ctx.qry.head = xstrdup(arg);
ctx.qry.has_symref = 1; ctx.qry.has_symref = 1;
} else if (starts_with(argv[i], "--sha1=")) { } else if (skip_prefix(argv[i], "--sha1=", &arg)) {
ctx.qry.sha1 = xstrdup(argv[i] + 7); ctx.qry.sha1 = xstrdup(arg);
ctx.qry.has_sha1 = 1; ctx.qry.has_sha1 = 1;
} else if (starts_with(argv[i], "--ofs=")) { } else if (skip_prefix(argv[i], "--ofs=", &arg)) {
ctx.qry.ofs = atoi(argv[i] + 6); ctx.qry.ofs = atoi(arg);
} else if (starts_with(argv[i], "--scan-tree=") || } else if (skip_prefix(argv[i], "--scan-tree=", &arg) ||
starts_with(argv[i], "--scan-path=")) { skip_prefix(argv[i], "--scan-path=", &arg)) {
/* /*
* HACK: The global snapshot bit mask defines the set * HACK: The global snapshot bit mask defines the set
* of allowed snapshot formats, but the config file * of allowed snapshot formats, but the config file
@ -993,7 +997,7 @@ static void cgit_parse_args(int argc, const char **argv)
*/ */
ctx.cfg.snapshots = 0xFF; ctx.cfg.snapshots = 0xFF;
scan++; scan++;
scan_tree(argv[i] + 12, repo_config); scan_tree(arg, repo_config);
} }
} }
if (scan) { if (scan) {

View File

@ -55,6 +55,8 @@ static void repo_config(const char *name, const char *value)
static int gitconfig_config(const char *key, const char *value, void *cb) static int gitconfig_config(const char *key, const char *value, void *cb)
{ {
const char *name;
if (!strcmp(key, "gitweb.owner")) if (!strcmp(key, "gitweb.owner"))
config_fn(repo, "owner", value); config_fn(repo, "owner", value);
else if (!strcmp(key, "gitweb.description")) else if (!strcmp(key, "gitweb.description"))
@ -63,8 +65,8 @@ static int gitconfig_config(const char *key, const char *value, void *cb)
config_fn(repo, "section", value); config_fn(repo, "section", value);
else if (!strcmp(key, "gitweb.homepage")) else if (!strcmp(key, "gitweb.homepage"))
config_fn(repo, "homepage", value); config_fn(repo, "homepage", value);
else if (starts_with(key, "cgit.")) else if (skip_prefix(key, "cgit.", &name))
config_fn(repo, key + 5, value); config_fn(repo, name, value);
return 0; return 0;
} }