/* shared.c: global vars + some callback functions * * Copyright (C) 2006-2014 cgit Development Team * * Licensed under GNU General Public License v2 * (see COPYING for full license text) */ #include "cgit.h" #include struct cgit_repolist cgit_repolist; struct cgit_context ctx; int chk_zero(int result, char *msg) { if (result != 0) die_errno("%s", msg); return result; } int chk_positive(int result, char *msg) { if (result <= 0) die_errno("%s", msg); return result; } int chk_non_negative(int result, char *msg) { if (result < 0) die_errno("%s", msg); return result; } char *cgit_default_repo_desc = "[no description]"; struct cgit_repo *cgit_add_repo(const char *url) { struct cgit_repo *ret; if (++cgit_repolist.count > cgit_repolist.length) { if (cgit_repolist.length == 0) cgit_repolist.length = 8; else cgit_repolist.length *= 2; cgit_repolist.repos = xrealloc(cgit_repolist.repos, cgit_repolist.length * sizeof(struct cgit_repo)); } ret = &cgit_repolist.repos[cgit_repolist.count-1]; memset(ret, 0, sizeof(struct cgit_repo)); ret->url = trim_end(url, '/'); ret->name = ret->url; ret->path = NULL; ret->desc = cgit_default_repo_desc; ret->owner = NULL; ret->section = ctx.cfg.section; ret->snapshots = ctx.cfg.snapshots; ret->enable_commit_graph = ctx.cfg.enable_commit_graph; ret->enable_log_filecount = ctx.cfg.enable_log_filecount; ret->enable_log_linecount = ctx.cfg.enable_log_linecount; ret->enable_remote_branches = ctx.cfg.enable_remote_branches; ret->enable_subject_links = ctx.cfg.enable_subject_links; ret->max_stats = ctx.cfg.max_stats; ret->branch_sort = ctx.cfg.branch_sort; ret->commit_sort = ctx.cfg.commit_sort; ret->module_link = ctx.cfg.module_link; ret->readme = ctx.cfg.readme; ret->mtime = -1; ret->about_filter = ctx.cfg.about_filter; ret->commit_filter = ctx.cfg.commit_filter; ret->source_filter = ctx.cfg.source_filter; ret->clone_url = ctx.cfg.clone_url; ret->submodules.strdup_strings = 1; return ret; } struct cgit_repo *cgit_get_repoinfo(const char *url) { int i; struct cgit_repo *repo; for (i = 0; i < cgit_repolist.count; i++) { repo = &cgit_repolist.repos[i]; if (!strcmp(repo->url, url)) return repo; } return NULL; } void *cgit_free_commitinfo(struct commitinfo *info) { free(info->author); free(info->author_email); free(info->committer); free(info->committer_email); free(info->subject); free(info->msg); free(info->msg_encoding); free(info); return NULL; } char *trim_end(const char *str, char c) { int len; if (str == NULL) return NULL; len = strlen(str); while (len > 0 && str[len - 1] == c) len--; if (len == 0) return NULL; return xstrndup(str, len); } char *ensure_end(const char *str, char c) { size_t len = strlen(str); char *result; if (len && str[len - 1] == c) return xstrndup(str, len); result = xmalloc(len + 2); memcpy(result, str, len); result[len] = '/'; result[len + 1] = '\0'; return result; } void strbuf_ensure_end(struct strbuf *sb, char c) { if (!sb->len || sb->buf[sb->len - 1] != c) strbu
# Files I don't care to see in git-status/commit
/cgit
cgit.conf
CGIT-CFLAGS
VERSION
cgitrc.5
cgitrc.5.fo
cgitrc.5.html
cgitrc.5.pdf
cgitrc.5.xml
*.o
*.d
ot_tree_sha1(new_sha1, "", &opt); diffcore_std(&opt); diff_flush(&opt); } void cgit_diff_commit(struct commit *commit, filepair_fn fn, const char *prefix) { unsigned char *old_sha1 = NULL; if (commit->parents) old_sha1 = commit->parents->item->object.sha1; cgit_diff_tree(old_sha1, commit->object.sha1, fn, prefix, ctx.qry.ignorews); } int cgit_parse_snapshots_mask(const char *str) { struct string_list tokens = STRING_LIST_INIT_DUP; struct string_list_item *item; const struct cgit_snapshot_format *f; int rv = 0; /* favor legacy setting */ if (atoi(str)) return 1; string_list_split(&tokens, str, ' ', -1); string_list_remove_empty_items(&tokens, 0); for_each_string_list_item(item, &tokens) { for (f = cgit_snapshot_formats; f->suffix; f++) { if (!strcmp(item->string, f->suffix) || !strcmp(item->string, f->suffix + 1)) { rv |= f->bit; break; } } } string_list_clear(&tokens, 0); return rv; } typedef struct { char * name; char * value; } cgit_env_var; void cgit_prepare_repo_env(struct cgit_repo * repo) { cgit_env_var env_vars[] = { { .name = "CGIT_REPO_URL", .value = repo->url }, { .name = "CGIT_REPO_NAME", .value = repo->name }, { .name = "CGIT_REPO_PATH", .value = repo->path }, { .name = "CGIT_REPO_OWNER", .value = repo->owner }, { .name = "CGIT_REPO_DEFBRANCH", .value = repo->defbranch }, { .name = "CGIT_REPO_SECTION", .value = repo->section }, { .name = "CGIT_REPO_CLONE_URL", .value = repo->clone_url } }; int env_var_count = ARRAY_SIZE(env_vars); cgit_env_var *p, *q; static char *warn = "cgit warning: failed to set env: %s=%s\n"; p = env_vars; q = p + env_var_count; for (; p < q; p++) if (p->value && setenv(p->name, p->value, 1)) fprintf(stderr, warn, p->name, p->value); } /* Read the content of the specified file into a newly allocated buffer, * zeroterminate the buffer and return 0 on success, errno otherwise. */ int readfile(const char *path, char **buf, size_t *size) { int fd, e; struct stat st; fd = open(path, O_RDONLY); if (fd == -1) return errno; if (fstat(fd, &st)) { e = errno; close(fd); return e; } if (!S_ISREG(st.st_mode)) { close(fd); return EISDIR; } *buf = xmalloc(st.st_size + 1); *size = read_in_full(fd, *buf, st.st_size); e = errno; (*buf)[*size] = '\0'; close(fd); return (*size == st.st_size ? 0 : e); } static int is_token_char(char c) { return isalnum(c) || c == '_'; } /* Replace name with getenv(name), return pointer to zero-terminating char */ static char *expand_macro(char *name, int maxlength) { char *value; int len; len = 0; value = getenv(name); if (value) { len = strlen(value); if (len > maxlength) len = maxlength; strncpy(name, value, len); } return name + len; } #define EXPBUFSIZE (1024 * 8) /* Replace all tokens prefixed by '$' in the specified text with the * value of the named environment variable. * NB: the return value is a static buffer, i.e. it must be strdup'd * by the caller. */ char *expand_macros(const char *txt) { static char result[EXPBUFSIZE]; char *p, *start; int len; p = result; start = NULL; while (p < result + EXPBUFSIZE - 1 && txt && *txt) { *p = *txt; if (start) { if (!is_token_char(*txt)) { if (p - start > 0) { *p = '\0'; len = result + EXPBUFSIZE - start - 1; p = expand_macro(start, len) - 1; } start = NULL; txt--; } p++; txt++; continue; } if (*txt == '$') { start = p; txt++; continue; } p++; txt++; } *p = '\0'; if (start && p - start > 0) { len = result + EXPBUFSIZE - start - 1; p = expand_macro(start, len); *p = '\0'; } return result; }