use struct strbuf instead of static buffers
Use "struct strbuf" from Git to remove the limit on file path length. Notes on scan-tree: This is slightly involved since I decided to pass the strbuf into add_repo() and modify if whenever a new file name is required, which should avoid any extra allocations within that function. The pattern there is to append the filename, use it and then reset the buffer to its original length (retaining a trailing '/'). Notes on ui-snapshot: Since write_archive modifies the argv array passed to it we copy the argv_array values into a new array of char* and then free the original argv_array structure and the new array without worrying about what the values now look like. Signed-off-by: John Keeping <john@keeping.me.uk>
Cette révision appartient à :
Parent
42d5476f25
révision
fb3655df3b
57
cache.c
57
cache.c
@ -312,9 +312,9 @@ int cache_process(int size, const char *path, const char *key, int ttl,
|
|||||||
cache_fill_fn fn, void *cbdata)
|
cache_fill_fn fn, void *cbdata)
|
||||||
{
|
{
|
||||||
unsigned long hash;
|
unsigned long hash;
|
||||||
int len, i;
|
int i;
|
||||||
char filename[1024];
|
struct strbuf filename = STRBUF_INIT;
|
||||||
char lockname[1024 + 5]; /* 5 = ".lock" */
|
struct strbuf lockname = STRBUF_INIT;
|
||||||
struct cache_slot slot;
|
struct cache_slot slot;
|
||||||
|
|
||||||
/* If the cache is disabled, just generate the content */
|
/* If the cache is disabled, just generate the content */
|
||||||
@ -329,32 +329,22 @@ int cache_process(int size, const char *path, const char *key, int ttl,
|
|||||||
fn(cbdata);
|
fn(cbdata);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
len = strlen(path);
|
|
||||||
if (len > sizeof(filename) - 10) { /* 10 = "/01234567\0" */
|
|
||||||
cache_log("[cgit] Cache path too long, caching is disabled: %s\n",
|
|
||||||
path);
|
|
||||||
fn(cbdata);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
if (!key)
|
if (!key)
|
||||||
key = "";
|
key = "";
|
||||||
hash = hash_str(key) % size;
|
hash = hash_str(key) % size;
|
||||||
strcpy(filename, path);
|
strbuf_addstr(&filename, path);
|
||||||
if (filename[len - 1] != '/')
|
strbuf_ensure_end(&filename, '/');
|
||||||
filename[len++] = '/';
|
|
||||||
for (i = 0; i < 8; i++) {
|
for (i = 0; i < 8; i++) {
|
||||||
sprintf(filename + len++, "%x",
|
strbuf_addf(&filename, "%x", (unsigned char)(hash & 0xf));
|
||||||
(unsigned char)(hash & 0xf));
|
|
||||||
hash >>= 4;
|
hash >>= 4;
|
||||||
}
|
}
|
||||||
filename[len] = '\0';
|
strbuf_addbuf(&lockname, &filename);
|
||||||
strcpy(lockname, filename);
|
strbuf_addstr(&lockname, ".lock");
|
||||||
strcpy(lockname + len, ".lock");
|
|
||||||
slot.fn = fn;
|
slot.fn = fn;
|
||||||
slot.cbdata = cbdata;
|
slot.cbdata = cbdata;
|
||||||
slot.ttl = ttl;
|
slot.ttl = ttl;
|
||||||
slot.cache_name = filename;
|
slot.cache_name = strbuf_detach(&filename, NULL);
|
||||||
slot.lock_name = lockname;
|
slot.lock_name = strbuf_detach(&lockname, NULL);
|
||||||
slot.key = key;
|
slot.key = key;
|
||||||
slot.keylen = strlen(key);
|
slot.keylen = strlen(key);
|
||||||
return process_slot(&slot);
|
return process_slot(&slot);
|
||||||
@ -381,18 +371,13 @@ int cache_ls(const char *path)
|
|||||||
struct dirent *ent;
|
struct dirent *ent;
|
||||||
int err = 0;
|
int err = 0;
|
||||||
struct cache_slot slot;
|
struct cache_slot slot;
|
||||||
char fullname[1024];
|
struct strbuf fullname = STRBUF_INIT;
|
||||||
char *name;
|
size_t prefixlen;
|
||||||
|
|
||||||
if (!path) {
|
if (!path) {
|
||||||
cache_log("[cgit] cache path not specified\n");
|
cache_log("[cgit] cache path not specified\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (strlen(path) > 1024 - 10) {
|
|
||||||
cache_log("[cgit] cache path too long: %s\n",
|
|
||||||
path);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
dir = opendir(path);
|
dir = opendir(path);
|
||||||
if (!dir) {
|
if (!dir) {
|
||||||
err = errno;
|
err = errno;
|
||||||
@ -400,30 +385,28 @@ int cache_ls(const char *path)
|
|||||||
path, strerror(err), err);
|
path, strerror(err), err);
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
strcpy(fullname, path);
|
strbuf_addstr(&fullname, path);
|
||||||
name = fullname + strlen(path);
|
strbuf_ensure_end(&fullname, '/');
|
||||||
if (*(name - 1) != '/') {
|
prefixlen = fullname.len;
|
||||||
*name++ = '/';
|
|
||||||
*name = '\0';
|
|
||||||
}
|
|
||||||
slot.cache_name = fullname;
|
|
||||||
while ((ent = readdir(dir)) != NULL) {
|
while ((ent = readdir(dir)) != NULL) {
|
||||||
if (strlen(ent->d_name) != 8)
|
if (strlen(ent->d_name) != 8)
|
||||||
continue;
|
continue;
|
||||||
strcpy(name, ent->d_name);
|
strbuf_setlen(&fullname, prefixlen);
|
||||||
|
strbuf_addstr(&fullname, ent->d_name);
|
||||||
if ((err = open_slot(&slot)) != 0) {
|
if ((err = open_slot(&slot)) != 0) {
|
||||||
cache_log("[cgit] unable to open path %s: %s (%d)\n",
|
cache_log("[cgit] unable to open path %s: %s (%d)\n",
|
||||||
fullname, strerror(err), err);
|
fullname.buf, strerror(err), err);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
printf("%s %s %10"PRIuMAX" %s\n",
|
printf("%s %s %10"PRIuMAX" %s\n",
|
||||||
name,
|
fullname.buf,
|
||||||
sprintftime("%Y-%m-%d %H:%M:%S",
|
sprintftime("%Y-%m-%d %H:%M:%S",
|
||||||
slot.cache_st.st_mtime),
|
slot.cache_st.st_mtime),
|
||||||
(uintmax_t)slot.cache_st.st_size,
|
(uintmax_t)slot.cache_st.st_size,
|
||||||
slot.buf);
|
slot.buf);
|
||||||
close_slot(&slot);
|
close_slot(&slot);
|
||||||
}
|
}
|
||||||
|
slot.cache_name = strbuf_detach(&fullname, NULL);
|
||||||
closedir(dir);
|
closedir(dir);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
70
cgit.c
70
cgit.c
@ -468,7 +468,7 @@ static int prepare_repo_cmd(struct cgit_context *ctx)
|
|||||||
if (nongit) {
|
if (nongit) {
|
||||||
const char *name = ctx->repo->name;
|
const char *name = ctx->repo->name;
|
||||||
rc = errno;
|
rc = errno;
|
||||||
ctx->page.title = fmt("%s - %s", ctx->cfg.root_title,
|
ctx->page.title = fmtalloc("%s - %s", ctx->cfg.root_title,
|
||||||
"config error");
|
"config error");
|
||||||
ctx->repo = NULL;
|
ctx->repo = NULL;
|
||||||
cgit_print_http_headers(ctx);
|
cgit_print_http_headers(ctx);
|
||||||
@ -479,7 +479,7 @@ static int prepare_repo_cmd(struct cgit_context *ctx)
|
|||||||
cgit_print_docend();
|
cgit_print_docend();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc);
|
ctx->page.title = fmtalloc("%s - %s", ctx->repo->name, ctx->repo->desc);
|
||||||
|
|
||||||
if (!ctx->repo->defbranch)
|
if (!ctx->repo->defbranch)
|
||||||
ctx->repo->defbranch = guess_defbranch();
|
ctx->repo->defbranch = guess_defbranch();
|
||||||
@ -577,21 +577,16 @@ static int cmp_repos(const void *a, const void *b)
|
|||||||
static char *build_snapshot_setting(int bitmap)
|
static char *build_snapshot_setting(int bitmap)
|
||||||
{
|
{
|
||||||
const struct cgit_snapshot_format *f;
|
const struct cgit_snapshot_format *f;
|
||||||
char *result = xstrdup("");
|
struct strbuf result = STRBUF_INIT;
|
||||||
char *tmp;
|
|
||||||
int len;
|
|
||||||
|
|
||||||
for (f = cgit_snapshot_formats; f->suffix; f++) {
|
for (f = cgit_snapshot_formats; f->suffix; f++) {
|
||||||
if (f->bit & bitmap) {
|
if (f->bit & bitmap) {
|
||||||
tmp = result;
|
if (result.len)
|
||||||
result = xstrdup(fmt("%s%s ", tmp, f->suffix));
|
strbuf_addch(&result, ' ');
|
||||||
free(tmp);
|
strbuf_addstr(&result, f->suffix);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
len = strlen(result);
|
return strbuf_detach(&result, NULL);
|
||||||
if (len)
|
|
||||||
result[len - 1] = '\0';
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *get_first_line(char *txt)
|
static char *get_first_line(char *txt)
|
||||||
@ -639,7 +634,7 @@ static void print_repo(FILE *f, struct cgit_repo *repo)
|
|||||||
fprintf(f, "repo.source-filter=%s\n", repo->source_filter->cmd);
|
fprintf(f, "repo.source-filter=%s\n", repo->source_filter->cmd);
|
||||||
if (repo->snapshots != ctx.cfg.snapshots) {
|
if (repo->snapshots != ctx.cfg.snapshots) {
|
||||||
char *tmp = build_snapshot_setting(repo->snapshots);
|
char *tmp = build_snapshot_setting(repo->snapshots);
|
||||||
fprintf(f, "repo.snapshots=%s\n", tmp);
|
fprintf(f, "repo.snapshots=%s\n", tmp ? tmp : "");
|
||||||
free(tmp);
|
free(tmp);
|
||||||
}
|
}
|
||||||
if (repo->max_stats != ctx.cfg.max_stats)
|
if (repo->max_stats != ctx.cfg.max_stats)
|
||||||
@ -661,20 +656,22 @@ static void print_repolist(FILE *f, struct cgit_repolist *list, int start)
|
|||||||
*/
|
*/
|
||||||
static int generate_cached_repolist(const char *path, const char *cached_rc)
|
static int generate_cached_repolist(const char *path, const char *cached_rc)
|
||||||
{
|
{
|
||||||
char *locked_rc;
|
struct strbuf locked_rc = STRBUF_INIT;
|
||||||
|
int result = 0;
|
||||||
int idx;
|
int idx;
|
||||||
FILE *f;
|
FILE *f;
|
||||||
|
|
||||||
locked_rc = xstrdup(fmt("%s.lock", cached_rc));
|
strbuf_addf(&locked_rc, "%s.lock", cached_rc);
|
||||||
f = fopen(locked_rc, "wx");
|
f = fopen(locked_rc.buf, "wx");
|
||||||
if (!f) {
|
if (!f) {
|
||||||
/* Inform about the error unless the lockfile already existed,
|
/* Inform about the error unless the lockfile already existed,
|
||||||
* since that only means we've got concurrent requests.
|
* since that only means we've got concurrent requests.
|
||||||
*/
|
*/
|
||||||
if (errno != EEXIST)
|
result = errno;
|
||||||
|
if (result != EEXIST)
|
||||||
fprintf(stderr, "[cgit] Error opening %s: %s (%d)\n",
|
fprintf(stderr, "[cgit] Error opening %s: %s (%d)\n",
|
||||||
locked_rc, strerror(errno), errno);
|
locked_rc.buf, strerror(result), result);
|
||||||
return errno;
|
goto out;
|
||||||
}
|
}
|
||||||
idx = cgit_repolist.count;
|
idx = cgit_repolist.count;
|
||||||
if (ctx.cfg.project_list)
|
if (ctx.cfg.project_list)
|
||||||
@ -682,55 +679,59 @@ static int generate_cached_repolist(const char *path, const char *cached_rc)
|
|||||||
else
|
else
|
||||||
scan_tree(path, repo_config);
|
scan_tree(path, repo_config);
|
||||||
print_repolist(f, &cgit_repolist, idx);
|
print_repolist(f, &cgit_repolist, idx);
|
||||||
if (rename(locked_rc, cached_rc))
|
if (rename(locked_rc.buf, cached_rc))
|
||||||
fprintf(stderr, "[cgit] Error renaming %s to %s: %s (%d)\n",
|
fprintf(stderr, "[cgit] Error renaming %s to %s: %s (%d)\n",
|
||||||
locked_rc, cached_rc, strerror(errno), errno);
|
locked_rc.buf, cached_rc, strerror(errno), errno);
|
||||||
fclose(f);
|
fclose(f);
|
||||||
return 0;
|
out:
|
||||||
|
strbuf_release(&locked_rc);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void process_cached_repolist(const char *path)
|
static void process_cached_repolist(const char *path)
|
||||||
{
|
{
|
||||||
struct stat st;
|
struct stat st;
|
||||||
char *cached_rc;
|
struct strbuf cached_rc = STRBUF_INIT;
|
||||||
time_t age;
|
time_t age;
|
||||||
unsigned long hash;
|
unsigned long hash;
|
||||||
|
|
||||||
hash = hash_str(path);
|
hash = hash_str(path);
|
||||||
if (ctx.cfg.project_list)
|
if (ctx.cfg.project_list)
|
||||||
hash += hash_str(ctx.cfg.project_list);
|
hash += hash_str(ctx.cfg.project_list);
|
||||||
cached_rc = xstrdup(fmt("%s/rc-%8lx", ctx.cfg.cache_root, hash));
|
strbuf_addf(&cached_rc, "%s/rc-%8lx", ctx.cfg.cache_root, hash);
|
||||||
|
|
||||||
if (stat(cached_rc, &st)) {
|
if (stat(cached_rc.buf, &st)) {
|
||||||
/* Nothing is cached, we need to scan without forking. And
|
/* Nothing is cached, we need to scan without forking. And
|
||||||
* if we fail to generate a cached repolist, we need to
|
* if we fail to generate a cached repolist, we need to
|
||||||
* invoke scan_tree manually.
|
* invoke scan_tree manually.
|
||||||
*/
|
*/
|
||||||
if (generate_cached_repolist(path, cached_rc)) {
|
if (generate_cached_repolist(path, cached_rc.buf)) {
|
||||||
if (ctx.cfg.project_list)
|
if (ctx.cfg.project_list)
|
||||||
scan_projects(path, ctx.cfg.project_list,
|
scan_projects(path, ctx.cfg.project_list,
|
||||||
repo_config);
|
repo_config);
|
||||||
else
|
else
|
||||||
scan_tree(path, repo_config);
|
scan_tree(path, repo_config);
|
||||||
}
|
}
|
||||||
return;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
parse_configfile(cached_rc, config_cb);
|
parse_configfile(cached_rc.buf, config_cb);
|
||||||
|
|
||||||
/* If the cached configfile hasn't expired, lets exit now */
|
/* If the cached configfile hasn't expired, lets exit now */
|
||||||
age = time(NULL) - st.st_mtime;
|
age = time(NULL) - st.st_mtime;
|
||||||
if (age <= (ctx.cfg.cache_scanrc_ttl * 60))
|
if (age <= (ctx.cfg.cache_scanrc_ttl * 60))
|
||||||
return;
|
goto out;
|
||||||
|
|
||||||
/* The cached repolist has been parsed, but it was old. So lets
|
/* The cached repolist has been parsed, but it was old. So lets
|
||||||
* rescan the specified path and generate a new cached repolist
|
* rescan the specified path and generate a new cached repolist
|
||||||
* in a child-process to avoid latency for the current request.
|
* in a child-process to avoid latency for the current request.
|
||||||
*/
|
*/
|
||||||
if (fork())
|
if (fork())
|
||||||
return;
|
goto out;
|
||||||
|
|
||||||
exit(generate_cached_repolist(path, cached_rc));
|
exit(generate_cached_repolist(path, cached_rc.buf));
|
||||||
|
out:
|
||||||
|
strbuf_release(&cached_rc);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void cgit_parse_args(int argc, const char **argv)
|
static void cgit_parse_args(int argc, const char **argv)
|
||||||
@ -812,7 +813,6 @@ static int calc_ttl()
|
|||||||
int main(int argc, const char **argv)
|
int main(int argc, const char **argv)
|
||||||
{
|
{
|
||||||
const char *path;
|
const char *path;
|
||||||
char *qry;
|
|
||||||
int err, ttl;
|
int err, ttl;
|
||||||
|
|
||||||
prepare_context(&ctx);
|
prepare_context(&ctx);
|
||||||
@ -843,9 +843,9 @@ int main(int argc, const char **argv)
|
|||||||
path++;
|
path++;
|
||||||
ctx.qry.url = xstrdup(path);
|
ctx.qry.url = xstrdup(path);
|
||||||
if (ctx.qry.raw) {
|
if (ctx.qry.raw) {
|
||||||
qry = ctx.qry.raw;
|
char *newqry = fmtalloc("%s?%s", path, ctx.qry.raw);
|
||||||
ctx.qry.raw = xstrdup(fmt("%s?%s", path, qry));
|
free(ctx.qry.raw);
|
||||||
free(qry);
|
ctx.qry.raw = newqry;
|
||||||
} else
|
} else
|
||||||
ctx.qry.raw = xstrdup(ctx.qry.url);
|
ctx.qry.raw = xstrdup(ctx.qry.url);
|
||||||
cgit_parse_url(ctx.qry.url);
|
cgit_parse_url(ctx.qry.url);
|
||||||
|
160
scan-tree.c
160
scan-tree.c
@ -12,38 +12,38 @@
|
|||||||
#include "configfile.h"
|
#include "configfile.h"
|
||||||
#include "html.h"
|
#include "html.h"
|
||||||
|
|
||||||
#define MAX_PATH 4096
|
|
||||||
|
|
||||||
/* return 1 if path contains a objects/ directory and a HEAD file */
|
/* return 1 if path contains a objects/ directory and a HEAD file */
|
||||||
static int is_git_dir(const char *path)
|
static int is_git_dir(const char *path)
|
||||||
{
|
{
|
||||||
struct stat st;
|
struct stat st;
|
||||||
static char buf[MAX_PATH];
|
struct strbuf pathbuf = STRBUF_INIT;
|
||||||
|
int result = 0;
|
||||||
|
|
||||||
if (snprintf(buf, MAX_PATH, "%s/objects", path) >= MAX_PATH) {
|
strbuf_addf(&pathbuf, "%s/objects", path);
|
||||||
fprintf(stderr, "Insanely long path: %s\n", path);
|
if (stat(pathbuf.buf, &st)) {
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
if (stat(buf, &st)) {
|
|
||||||
if (errno != ENOENT)
|
if (errno != ENOENT)
|
||||||
fprintf(stderr, "Error checking path %s: %s (%d)\n",
|
fprintf(stderr, "Error checking path %s: %s (%d)\n",
|
||||||
path, strerror(errno), errno);
|
path, strerror(errno), errno);
|
||||||
return 0;
|
goto out;
|
||||||
}
|
}
|
||||||
if (!S_ISDIR(st.st_mode))
|
if (!S_ISDIR(st.st_mode))
|
||||||
return 0;
|
goto out;
|
||||||
|
|
||||||
sprintf(buf, "%s/HEAD", path);
|
strbuf_reset(&pathbuf);
|
||||||
if (stat(buf, &st)) {
|
strbuf_addf(&pathbuf, "%s/HEAD", path);
|
||||||
|
if (stat(pathbuf.buf, &st)) {
|
||||||
if (errno != ENOENT)
|
if (errno != ENOENT)
|
||||||
fprintf(stderr, "Error checking path %s: %s (%d)\n",
|
fprintf(stderr, "Error checking path %s: %s (%d)\n",
|
||||||
path, strerror(errno), errno);
|
path, strerror(errno), errno);
|
||||||
return 0;
|
goto out;
|
||||||
}
|
}
|
||||||
if (!S_ISREG(st.st_mode))
|
if (!S_ISREG(st.st_mode))
|
||||||
return 0;
|
goto out;
|
||||||
|
|
||||||
return 1;
|
result = 1;
|
||||||
|
out:
|
||||||
|
strbuf_release(&pathbuf);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct cgit_repo *repo;
|
struct cgit_repo *repo;
|
||||||
@ -75,47 +75,61 @@ static char *xstrrchr(char *s, char *from, int c)
|
|||||||
return from < s ? NULL : from;
|
return from < s ? NULL : from;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void add_repo(const char *base, const char *path, repo_config_fn fn)
|
static void add_repo(const char *base, struct strbuf *path, repo_config_fn fn)
|
||||||
{
|
{
|
||||||
struct stat st;
|
struct stat st;
|
||||||
struct passwd *pwd;
|
struct passwd *pwd;
|
||||||
char *rel, *p, *slash;
|
size_t pathlen;
|
||||||
|
struct strbuf rel = STRBUF_INIT;
|
||||||
|
char *p, *slash;
|
||||||
int n;
|
int n;
|
||||||
size_t size;
|
size_t size;
|
||||||
|
|
||||||
if (stat(path, &st)) {
|
if (stat(path->buf, &st)) {
|
||||||
fprintf(stderr, "Error accessing %s: %s (%d)\n",
|
fprintf(stderr, "Error accessing %s: %s (%d)\n",
|
||||||
path, strerror(errno), errno);
|
path->buf, strerror(errno), errno);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ctx.cfg.strict_export && stat(fmt("%s/%s", path, ctx.cfg.strict_export), &st))
|
strbuf_addch(path, '/');
|
||||||
return;
|
pathlen = path->len;
|
||||||
|
|
||||||
if (!stat(fmt("%s/noweb", path), &st))
|
if (ctx.cfg.strict_export) {
|
||||||
|
strbuf_addstr(path, ctx.cfg.strict_export);
|
||||||
|
if(stat(path->buf, &st))
|
||||||
return;
|
return;
|
||||||
|
strbuf_setlen(path, pathlen);
|
||||||
|
}
|
||||||
|
|
||||||
if (base == path)
|
strbuf_addstr(path, "noweb");
|
||||||
rel = xstrdup(path);
|
if (!stat(path->buf, &st))
|
||||||
|
return;
|
||||||
|
strbuf_setlen(path, pathlen);
|
||||||
|
|
||||||
|
if (strncmp(base, path->buf, strlen(base)))
|
||||||
|
strbuf_addbuf(&rel, path);
|
||||||
else
|
else
|
||||||
rel = xstrdup(path + strlen(base) + 1);
|
strbuf_addstr(&rel, path->buf + strlen(base) + 1);
|
||||||
|
|
||||||
if (!strcmp(rel + strlen(rel) - 5, "/.git"))
|
if (!strcmp(rel.buf + rel.len - 5, "/.git"))
|
||||||
rel[strlen(rel) - 5] = '\0';
|
strbuf_setlen(&rel, rel.len - 5);
|
||||||
|
|
||||||
repo = cgit_add_repo(rel);
|
repo = cgit_add_repo(rel.buf);
|
||||||
config_fn = fn;
|
config_fn = fn;
|
||||||
if (ctx.cfg.enable_git_config)
|
if (ctx.cfg.enable_git_config) {
|
||||||
git_config_from_file(gitconfig_config, fmt("%s/config", path), NULL);
|
strbuf_addstr(path, "config");
|
||||||
|
git_config_from_file(gitconfig_config, path->buf, NULL);
|
||||||
|
strbuf_setlen(path, pathlen);
|
||||||
|
}
|
||||||
|
|
||||||
if (ctx.cfg.remove_suffix)
|
if (ctx.cfg.remove_suffix)
|
||||||
if ((p = strrchr(repo->url, '.')) && !strcmp(p, ".git"))
|
if ((p = strrchr(repo->url, '.')) && !strcmp(p, ".git"))
|
||||||
*p = '\0';
|
*p = '\0';
|
||||||
repo->path = xstrdup(path);
|
repo->path = xstrdup(path->buf);
|
||||||
while (!repo->owner) {
|
while (!repo->owner) {
|
||||||
if ((pwd = getpwuid(st.st_uid)) == NULL) {
|
if ((pwd = getpwuid(st.st_uid)) == NULL) {
|
||||||
fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n",
|
fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n",
|
||||||
path, strerror(errno), errno);
|
path->buf, strerror(errno), errno);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (pwd->pw_gecos)
|
if (pwd->pw_gecos)
|
||||||
@ -125,30 +139,32 @@ static void add_repo(const char *base, const char *path, repo_config_fn fn)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (repo->desc == cgit_default_repo_desc || !repo->desc) {
|
if (repo->desc == cgit_default_repo_desc || !repo->desc) {
|
||||||
p = fmt("%s/description", path);
|
strbuf_addstr(path, "description");
|
||||||
if (!stat(p, &st))
|
if (!stat(path->buf, &st))
|
||||||
readfile(p, &repo->desc, &size);
|
readfile(path->buf, &repo->desc, &size);
|
||||||
|
strbuf_setlen(path, pathlen);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!repo->readme) {
|
if (!repo->readme) {
|
||||||
p = fmt("%s/README.html", path);
|
strbuf_addstr(path, "README.html");
|
||||||
if (!stat(p, &st))
|
if (!stat(path->buf, &st))
|
||||||
repo->readme = "README.html";
|
repo->readme = "README.html";
|
||||||
|
strbuf_setlen(path, pathlen);
|
||||||
}
|
}
|
||||||
if (ctx.cfg.section_from_path) {
|
if (ctx.cfg.section_from_path) {
|
||||||
n = ctx.cfg.section_from_path;
|
n = ctx.cfg.section_from_path;
|
||||||
if (n > 0) {
|
if (n > 0) {
|
||||||
slash = rel;
|
slash = rel.buf;
|
||||||
while (slash && n && (slash = strchr(slash, '/')))
|
while (slash && n && (slash = strchr(slash, '/')))
|
||||||
n--;
|
n--;
|
||||||
} else {
|
} else {
|
||||||
slash = rel + strlen(rel);
|
slash = rel.buf + rel.len;
|
||||||
while (slash && n && (slash = xstrrchr(rel, slash, '/')))
|
while (slash && n && (slash = xstrrchr(rel.buf, slash, '/')))
|
||||||
n++;
|
n++;
|
||||||
}
|
}
|
||||||
if (slash && !n) {
|
if (slash && !n) {
|
||||||
*slash = '\0';
|
*slash = '\0';
|
||||||
repo->section = xstrdup(rel);
|
repo->section = xstrdup(rel.buf);
|
||||||
*slash = '/';
|
*slash = '/';
|
||||||
if (!prefixcmp(repo->name, repo->section)) {
|
if (!prefixcmp(repo->name, repo->section)) {
|
||||||
repo->name += strlen(repo->section);
|
repo->name += strlen(repo->section);
|
||||||
@ -158,19 +174,19 @@ static void add_repo(const char *base, const char *path, repo_config_fn fn)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
p = fmt("%s/cgitrc", path);
|
strbuf_addstr(path, "cgitrc");
|
||||||
if (!stat(p, &st))
|
if (!stat(path->buf, &st))
|
||||||
parse_configfile(xstrdup(p), &repo_config);
|
parse_configfile(xstrdup(path->buf), &repo_config);
|
||||||
|
|
||||||
|
strbuf_release(&rel);
|
||||||
free(rel);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void scan_path(const char *base, const char *path, repo_config_fn fn)
|
static void scan_path(const char *base, const char *path, repo_config_fn fn)
|
||||||
{
|
{
|
||||||
DIR *dir = opendir(path);
|
DIR *dir = opendir(path);
|
||||||
struct dirent *ent;
|
struct dirent *ent;
|
||||||
char *buf;
|
struct strbuf pathbuf = STRBUF_INIT;
|
||||||
|
size_t pathlen = strlen(path);
|
||||||
struct stat st;
|
struct stat st;
|
||||||
|
|
||||||
if (!dir) {
|
if (!dir) {
|
||||||
@ -178,14 +194,22 @@ static void scan_path(const char *base, const char *path, repo_config_fn fn)
|
|||||||
path, strerror(errno), errno);
|
path, strerror(errno), errno);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (is_git_dir(path)) {
|
|
||||||
add_repo(base, path, fn);
|
strbuf_add(&pathbuf, path, strlen(path));
|
||||||
|
if (is_git_dir(pathbuf.buf)) {
|
||||||
|
add_repo(base, &pathbuf, fn);
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
if (is_git_dir(fmt("%s/.git", path))) {
|
strbuf_addstr(&pathbuf, "/.git");
|
||||||
add_repo(base, fmt("%s/.git", path), fn);
|
if (is_git_dir(pathbuf.buf)) {
|
||||||
|
add_repo(base, &pathbuf, fn);
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
|
* Add one because we don't want to lose the trailing '/' when we
|
||||||
|
* reset the length of pathbuf in the loop below.
|
||||||
|
*/
|
||||||
|
pathlen++;
|
||||||
while ((ent = readdir(dir)) != NULL) {
|
while ((ent = readdir(dir)) != NULL) {
|
||||||
if (ent->d_name[0] == '.') {
|
if (ent->d_name[0] == '.') {
|
||||||
if (ent->d_name[1] == '\0')
|
if (ent->d_name[1] == '\0')
|
||||||
@ -195,24 +219,18 @@ static void scan_path(const char *base, const char *path, repo_config_fn fn)
|
|||||||
if (!ctx.cfg.scan_hidden_path)
|
if (!ctx.cfg.scan_hidden_path)
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
buf = malloc(strlen(path) + strlen(ent->d_name) + 2);
|
strbuf_setlen(&pathbuf, pathlen);
|
||||||
if (!buf) {
|
strbuf_addstr(&pathbuf, ent->d_name);
|
||||||
fprintf(stderr, "Alloc error on %s: %s (%d)\n",
|
if (stat(pathbuf.buf, &st)) {
|
||||||
path, strerror(errno), errno);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
sprintf(buf, "%s/%s", path, ent->d_name);
|
|
||||||
if (stat(buf, &st)) {
|
|
||||||
fprintf(stderr, "Error checking path %s: %s (%d)\n",
|
fprintf(stderr, "Error checking path %s: %s (%d)\n",
|
||||||
buf, strerror(errno), errno);
|
pathbuf.buf, strerror(errno), errno);
|
||||||
free(buf);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (S_ISDIR(st.st_mode))
|
if (S_ISDIR(st.st_mode))
|
||||||
scan_path(base, buf, fn);
|
scan_path(base, pathbuf.buf, fn);
|
||||||
free(buf);
|
|
||||||
}
|
}
|
||||||
end:
|
end:
|
||||||
|
strbuf_release(&pathbuf);
|
||||||
closedir(dir);
|
closedir(dir);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -220,7 +238,7 @@ end:
|
|||||||
|
|
||||||
void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn)
|
void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn)
|
||||||
{
|
{
|
||||||
char line[MAX_PATH * 2], *z;
|
struct strbuf line = STRBUF_INIT;
|
||||||
FILE *projects;
|
FILE *projects;
|
||||||
int err;
|
int err;
|
||||||
|
|
||||||
@ -230,19 +248,19 @@ void scan_projects(const char *path, const char *projectsfile, repo_config_fn fn
|
|||||||
projectsfile, strerror(errno), errno);
|
projectsfile, strerror(errno), errno);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
while (fgets(line, sizeof(line), projects) != NULL) {
|
while (strbuf_getline(&line, projects, '\n') != EOF) {
|
||||||
for (z = &lastc(line);
|
if (!line.len)
|
||||||
strlen(line) && strchr("\n\r", *z);
|
continue;
|
||||||
z = &lastc(line))
|
strbuf_insert(&line, 0, "/", 1);
|
||||||
*z = '\0';
|
strbuf_insert(&line, 0, path, strlen(path));
|
||||||
if (strlen(line))
|
scan_path(path, line.buf, fn);
|
||||||
scan_path(path, fmt("%s/%s", path, line), fn);
|
|
||||||
}
|
}
|
||||||
if ((err = ferror(projects))) {
|
if ((err = ferror(projects))) {
|
||||||
fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n",
|
fprintf(stderr, "Error reading from projectsfile %s: %s (%d)\n",
|
||||||
projectsfile, strerror(err), err);
|
projectsfile, strerror(err), err);
|
||||||
}
|
}
|
||||||
fclose(projects);
|
fclose(projects);
|
||||||
|
strbuf_release(&line);
|
||||||
}
|
}
|
||||||
|
|
||||||
void scan_tree(const char *path, repo_config_fn fn)
|
void scan_tree(const char *path, repo_config_fn fn)
|
||||||
|
33
ui-log.c
33
ui-log.c
@ -243,15 +243,19 @@ static void print_commit(struct commit *commit, struct rev_info *revs)
|
|||||||
cgit_free_commitinfo(info);
|
cgit_free_commitinfo(info);
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char *disambiguate_ref(const char *ref)
|
static const char *disambiguate_ref(const char *ref, int *must_free_result)
|
||||||
{
|
{
|
||||||
unsigned char sha1[20];
|
unsigned char sha1[20];
|
||||||
const char *longref;
|
struct strbuf longref = STRBUF_INIT;
|
||||||
|
|
||||||
longref = fmt("refs/heads/%s", ref);
|
strbuf_addf(&longref, "refs/heads/%s", ref);
|
||||||
if (get_sha1(longref, sha1) == 0)
|
if (get_sha1(longref.buf, sha1) == 0) {
|
||||||
return longref;
|
*must_free_result = 1;
|
||||||
|
return strbuf_detach(&longref, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
*must_free_result = 0;
|
||||||
|
strbuf_release(&longref);
|
||||||
return ref;
|
return ref;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -284,24 +288,26 @@ void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, char *pattern
|
|||||||
struct commit *commit;
|
struct commit *commit;
|
||||||
struct vector vec = VECTOR_INIT(char *);
|
struct vector vec = VECTOR_INIT(char *);
|
||||||
int i, columns = commit_graph ? 4 : 3;
|
int i, columns = commit_graph ? 4 : 3;
|
||||||
char *arg;
|
int must_free_tip = 0;
|
||||||
|
struct strbuf argbuf = STRBUF_INIT;
|
||||||
|
|
||||||
/* First argv is NULL */
|
/* First argv is NULL */
|
||||||
vector_push(&vec, NULL, 0);
|
vector_push(&vec, NULL, 0);
|
||||||
|
|
||||||
if (!tip)
|
if (!tip)
|
||||||
tip = ctx.qry.head;
|
tip = ctx.qry.head;
|
||||||
tip = disambiguate_ref(tip);
|
tip = disambiguate_ref(tip, &must_free_tip);
|
||||||
vector_push(&vec, &tip, 0);
|
vector_push(&vec, &tip, 0);
|
||||||
|
|
||||||
if (grep && pattern && *pattern) {
|
if (grep && pattern && *pattern) {
|
||||||
pattern = xstrdup(pattern);
|
pattern = xstrdup(pattern);
|
||||||
if (!strcmp(grep, "grep") || !strcmp(grep, "author") ||
|
if (!strcmp(grep, "grep") || !strcmp(grep, "author") ||
|
||||||
!strcmp(grep, "committer")) {
|
!strcmp(grep, "committer")) {
|
||||||
arg = fmt("--%s=%s", grep, pattern);
|
strbuf_addf(&argbuf, "--%s=%s", grep, pattern);
|
||||||
vector_push(&vec, &arg, 0);
|
vector_push(&vec, &argbuf.buf, 0);
|
||||||
}
|
}
|
||||||
if (!strcmp(grep, "range")) {
|
if (!strcmp(grep, "range")) {
|
||||||
|
char *arg;
|
||||||
/* Split the pattern at whitespace and add each token
|
/* Split the pattern at whitespace and add each token
|
||||||
* as a revision expression. Do not accept other
|
* as a revision expression. Do not accept other
|
||||||
* rev-list options. Also, replace the previously
|
* rev-list options. Also, replace the previously
|
||||||
@ -336,8 +342,8 @@ void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, char *pattern
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (path) {
|
if (path) {
|
||||||
arg = "--";
|
static const char *double_dash_arg = "--";
|
||||||
vector_push(&vec, &arg, 0);
|
vector_push(&vec, &double_dash_arg, 0);
|
||||||
vector_push(&vec, &path, 0);
|
vector_push(&vec, &path, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -430,4 +436,9 @@ void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, char *pattern
|
|||||||
ctx.qry.vpath, 0, NULL, NULL, ctx.qry.showmsg);
|
ctx.qry.vpath, 0, NULL, NULL, ctx.qry.showmsg);
|
||||||
html("</td></tr>\n");
|
html("</td></tr>\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* If we allocated tip then it is safe to cast away const. */
|
||||||
|
if (must_free_tip)
|
||||||
|
free((char*) tip);
|
||||||
|
strbuf_release(&argbuf);
|
||||||
}
|
}
|
||||||
|
@ -109,9 +109,9 @@ static int print_object(const unsigned char *sha1, const char *path)
|
|||||||
static char *buildpath(const char *base, int baselen, const char *path)
|
static char *buildpath(const char *base, int baselen, const char *path)
|
||||||
{
|
{
|
||||||
if (path[0])
|
if (path[0])
|
||||||
return fmt("%.*s%s/", baselen, base, path);
|
return fmtalloc("%.*s%s/", baselen, base, path);
|
||||||
else
|
else
|
||||||
return fmt("%.*s/", baselen, base);
|
return fmtalloc("%.*s/", baselen, base);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void print_dir(const unsigned char *sha1, const char *base,
|
static void print_dir(const unsigned char *sha1, const char *base,
|
||||||
@ -142,6 +142,7 @@ static void print_dir(const unsigned char *sha1, const char *base,
|
|||||||
fullpath);
|
fullpath);
|
||||||
html("</li>\n");
|
html("</li>\n");
|
||||||
}
|
}
|
||||||
|
free(fullpath);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void print_dir_entry(const unsigned char *sha1, const char *base,
|
static void print_dir_entry(const unsigned char *sha1, const char *base,
|
||||||
@ -159,6 +160,7 @@ static void print_dir_entry(const unsigned char *sha1, const char *base,
|
|||||||
cgit_plain_link(path, NULL, NULL, ctx.qry.head, ctx.qry.sha1,
|
cgit_plain_link(path, NULL, NULL, ctx.qry.head, ctx.qry.sha1,
|
||||||
fullpath);
|
fullpath);
|
||||||
html("</li>\n");
|
html("</li>\n");
|
||||||
|
free(fullpath);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void print_dir_tail(void)
|
static void print_dir_tail(void)
|
||||||
|
10
ui-refs.c
10
ui-refs.c
@ -99,7 +99,7 @@ static void print_tag_header()
|
|||||||
static void print_tag_downloads(const struct cgit_repo *repo, const char *ref)
|
static void print_tag_downloads(const struct cgit_repo *repo, const char *ref)
|
||||||
{
|
{
|
||||||
const struct cgit_snapshot_format* f;
|
const struct cgit_snapshot_format* f;
|
||||||
char *filename;
|
struct strbuf filename = STRBUF_INIT;
|
||||||
const char *basename;
|
const char *basename;
|
||||||
int free_ref = 0;
|
int free_ref = 0;
|
||||||
|
|
||||||
@ -111,7 +111,7 @@ static void print_tag_downloads(const struct cgit_repo *repo, const char *ref)
|
|||||||
if ((ref[0] == 'v' || ref[0] == 'V') && isdigit(ref[1]))
|
if ((ref[0] == 'v' || ref[0] == 'V') && isdigit(ref[1]))
|
||||||
ref++;
|
ref++;
|
||||||
if (isdigit(ref[0])) {
|
if (isdigit(ref[0])) {
|
||||||
ref = xstrdup(fmt("%s-%s", basename, ref));
|
ref = fmtalloc("%s-%s", basename, ref);
|
||||||
free_ref = 1;
|
free_ref = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -119,13 +119,15 @@ static void print_tag_downloads(const struct cgit_repo *repo, const char *ref)
|
|||||||
for (f = cgit_snapshot_formats; f->suffix; f++) {
|
for (f = cgit_snapshot_formats; f->suffix; f++) {
|
||||||
if (!(repo->snapshots & f->bit))
|
if (!(repo->snapshots & f->bit))
|
||||||
continue;
|
continue;
|
||||||
filename = fmt("%s%s", ref, f->suffix);
|
strbuf_reset(&filename);
|
||||||
cgit_snapshot_link(filename, NULL, NULL, NULL, NULL, filename);
|
strbuf_addf(&filename, "%s%s", ref, f->suffix);
|
||||||
|
cgit_snapshot_link(filename.buf, NULL, NULL, NULL, NULL, filename.buf);
|
||||||
html(" ");
|
html(" ");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (free_ref)
|
if (free_ref)
|
||||||
free((char *)ref);
|
free((char *)ref);
|
||||||
|
strbuf_release(&filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int print_tag(struct refinfo *ref)
|
static int print_tag(struct refinfo *ref)
|
||||||
|
@ -33,7 +33,7 @@ static time_t read_agefile(char *path)
|
|||||||
|
|
||||||
static int get_repo_modtime(const struct cgit_repo *repo, time_t *mtime)
|
static int get_repo_modtime(const struct cgit_repo *repo, time_t *mtime)
|
||||||
{
|
{
|
||||||
char *path;
|
struct strbuf path = STRBUF_INIT;
|
||||||
struct stat s;
|
struct stat s;
|
||||||
struct cgit_repo *r = (struct cgit_repo *)repo;
|
struct cgit_repo *r = (struct cgit_repo *)repo;
|
||||||
|
|
||||||
@ -41,32 +41,36 @@ static int get_repo_modtime(const struct cgit_repo *repo, time_t *mtime)
|
|||||||
*mtime = repo->mtime;
|
*mtime = repo->mtime;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
path = fmt("%s/%s", repo->path, ctx.cfg.agefile);
|
strbuf_addf(&path, "%s/%s", repo->path, ctx.cfg.agefile);
|
||||||
if (stat(path, &s) == 0) {
|
if (stat(path.buf, &s) == 0) {
|
||||||
*mtime = read_agefile(path);
|
*mtime = read_agefile(path.buf);
|
||||||
if (*mtime) {
|
if (*mtime) {
|
||||||
r->mtime = *mtime;
|
r->mtime = *mtime;
|
||||||
return 1;
|
goto end;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
path = fmt("%s/refs/heads/%s", repo->path, repo->defbranch ?
|
strbuf_reset(&path);
|
||||||
repo->defbranch : "master");
|
strbuf_addf(&path, "%s/refs/heads/%s", repo->path,
|
||||||
if (stat(path, &s) == 0) {
|
repo->defbranch ? repo->defbranch : "master");
|
||||||
|
if (stat(path.buf, &s) == 0) {
|
||||||
*mtime = s.st_mtime;
|
*mtime = s.st_mtime;
|
||||||
r->mtime = *mtime;
|
r->mtime = *mtime;
|
||||||
return 1;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
path = fmt("%s/%s", repo->path, "packed-refs");
|
strbuf_reset(&path);
|
||||||
if (stat(path, &s) == 0) {
|
strbuf_addf(&path, "%s/%s", repo->path, "packed-refs");
|
||||||
|
if (stat(path.buf, &s) == 0) {
|
||||||
*mtime = s.st_mtime;
|
*mtime = s.st_mtime;
|
||||||
r->mtime = *mtime;
|
r->mtime = *mtime;
|
||||||
return 1;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
*mtime = 0;
|
*mtime = 0;
|
||||||
r->mtime = *mtime;
|
r->mtime = *mtime;
|
||||||
|
end:
|
||||||
|
strbuf_release(&path);
|
||||||
return (r->mtime != 0);
|
return (r->mtime != 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
59
ui-shared.c
59
ui-shared.c
@ -62,7 +62,7 @@ const char *cgit_hosturl()
|
|||||||
return NULL;
|
return NULL;
|
||||||
if (!ctx.env.server_port || atoi(ctx.env.server_port) == 80)
|
if (!ctx.env.server_port || atoi(ctx.env.server_port) == 80)
|
||||||
return ctx.env.server_name;
|
return ctx.env.server_name;
|
||||||
return xstrdup(fmt("%s:%s", ctx.env.server_name, ctx.env.server_port));
|
return fmtalloc("%s:%s", ctx.env.server_name, ctx.env.server_port);
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *cgit_rooturl()
|
const char *cgit_rooturl()
|
||||||
@ -75,31 +75,30 @@ const char *cgit_rooturl()
|
|||||||
|
|
||||||
char *cgit_repourl(const char *reponame)
|
char *cgit_repourl(const char *reponame)
|
||||||
{
|
{
|
||||||
if (ctx.cfg.virtual_root) {
|
if (ctx.cfg.virtual_root)
|
||||||
return fmt("%s%s/", ctx.cfg.virtual_root, reponame);
|
return fmtalloc("%s%s/", ctx.cfg.virtual_root, reponame);
|
||||||
} else {
|
else
|
||||||
return fmt("?r=%s", reponame);
|
return fmtalloc("?r=%s", reponame);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char *cgit_fileurl(const char *reponame, const char *pagename,
|
char *cgit_fileurl(const char *reponame, const char *pagename,
|
||||||
const char *filename, const char *query)
|
const char *filename, const char *query)
|
||||||
{
|
{
|
||||||
char *tmp;
|
struct strbuf sb = STRBUF_INIT;
|
||||||
char *delim;
|
char *delim;
|
||||||
|
|
||||||
if (ctx.cfg.virtual_root) {
|
if (ctx.cfg.virtual_root) {
|
||||||
tmp = fmt("%s%s/%s/%s", ctx.cfg.virtual_root, reponame,
|
strbuf_addf(&sb, "%s%s/%s/%s", ctx.cfg.virtual_root, reponame,
|
||||||
pagename, (filename ? filename:""));
|
pagename, (filename ? filename:""));
|
||||||
delim = "?";
|
delim = "?";
|
||||||
} else {
|
} else {
|
||||||
tmp = fmt("?url=%s/%s/%s", reponame, pagename,
|
strbuf_addf(&sb, "?url=%s/%s/%s", reponame, pagename,
|
||||||
(filename ? filename : ""));
|
(filename ? filename : ""));
|
||||||
delim = "&";
|
delim = "&";
|
||||||
}
|
}
|
||||||
if (query)
|
if (query)
|
||||||
tmp = fmt("%s%s%s", tmp, delim, query);
|
strbuf_addf(&sb, "%s%s", delim, query);
|
||||||
return tmp;
|
return strbuf_detach(&sb, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *cgit_pageurl(const char *reponame, const char *pagename,
|
char *cgit_pageurl(const char *reponame, const char *pagename,
|
||||||
@ -548,21 +547,21 @@ void cgit_submodule_link(const char *class, char *path, const char *rev)
|
|||||||
htmlf("class='%s' ", class);
|
htmlf("class='%s' ", class);
|
||||||
html("href='");
|
html("href='");
|
||||||
if (item) {
|
if (item) {
|
||||||
html_attr(fmt(item->util, rev));
|
html_attrf(item->util, rev);
|
||||||
} else if (ctx.repo->module_link) {
|
} else if (ctx.repo->module_link) {
|
||||||
dir = strrchr(path, '/');
|
dir = strrchr(path, '/');
|
||||||
if (dir)
|
if (dir)
|
||||||
dir++;
|
dir++;
|
||||||
else
|
else
|
||||||
dir = path;
|
dir = path;
|
||||||
html_attr(fmt(ctx.repo->module_link, dir, rev));
|
html_attrf(ctx.repo->module_link, dir, rev);
|
||||||
} else {
|
} else {
|
||||||
html("#");
|
html("#");
|
||||||
}
|
}
|
||||||
html("'>");
|
html("'>");
|
||||||
html_txt(path);
|
html_txt(path);
|
||||||
html("</a>");
|
html("</a>");
|
||||||
html_txt(fmt(" @ %.7s", rev));
|
html_txtf(" @ %.7s", rev);
|
||||||
if (item && tail)
|
if (item && tail)
|
||||||
path[len - 1] = tail;
|
path[len - 1] = tail;
|
||||||
}
|
}
|
||||||
@ -678,12 +677,16 @@ void cgit_print_docstart(struct cgit_context *ctx)
|
|||||||
html("'/>\n");
|
html("'/>\n");
|
||||||
}
|
}
|
||||||
if (host && ctx->repo && ctx->qry.head) {
|
if (host && ctx->repo && ctx->qry.head) {
|
||||||
|
struct strbuf sb = STRBUF_INIT;
|
||||||
|
strbuf_addf(&sb, "h=%s", ctx->qry.head);
|
||||||
|
|
||||||
html("<link rel='alternate' title='Atom feed' href='");
|
html("<link rel='alternate' title='Atom feed' href='");
|
||||||
html(cgit_httpscheme());
|
html(cgit_httpscheme());
|
||||||
html_attr(cgit_hosturl());
|
html_attr(cgit_hosturl());
|
||||||
html_attr(cgit_fileurl(ctx->repo->url, "atom", ctx->qry.vpath,
|
html_attr(cgit_fileurl(ctx->repo->url, "atom", ctx->qry.vpath,
|
||||||
fmt("h=%s", ctx->qry.head)));
|
sb.buf));
|
||||||
html("' type='application/atom+xml'/>\n");
|
html("' type='application/atom+xml'/>\n");
|
||||||
|
strbuf_release(&sb);
|
||||||
}
|
}
|
||||||
if (ctx->cfg.head_include)
|
if (ctx->cfg.head_include)
|
||||||
html_include(ctx->cfg.head_include);
|
html_include(ctx->cfg.head_include);
|
||||||
@ -725,13 +728,14 @@ static int print_branch_option(const char *refname, const unsigned char *sha1,
|
|||||||
void cgit_add_hidden_formfields(int incl_head, int incl_search,
|
void cgit_add_hidden_formfields(int incl_head, int incl_search,
|
||||||
const char *page)
|
const char *page)
|
||||||
{
|
{
|
||||||
char *url;
|
|
||||||
|
|
||||||
if (!ctx.cfg.virtual_root) {
|
if (!ctx.cfg.virtual_root) {
|
||||||
url = fmt("%s/%s", ctx.qry.repo, page);
|
struct strbuf url = STRBUF_INIT;
|
||||||
|
|
||||||
|
strbuf_addf(&url, "%s/%s", ctx.qry.repo, page);
|
||||||
if (ctx.qry.vpath)
|
if (ctx.qry.vpath)
|
||||||
url = fmt("%s/%s", url, ctx.qry.vpath);
|
strbuf_addf(&url, "/%s", ctx.qry.vpath);
|
||||||
html_hidden("url", url);
|
html_hidden("url", url.buf);
|
||||||
|
strbuf_release(&url);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (incl_head && ctx.qry.head && ctx.repo->defbranch &&
|
if (incl_head && ctx.qry.head && ctx.repo->defbranch &&
|
||||||
@ -926,20 +930,23 @@ void cgit_print_snapshot_links(const char *repo, const char *head,
|
|||||||
const char *hex, int snapshots)
|
const char *hex, int snapshots)
|
||||||
{
|
{
|
||||||
const struct cgit_snapshot_format* f;
|
const struct cgit_snapshot_format* f;
|
||||||
char *prefix;
|
struct strbuf filename = STRBUF_INIT;
|
||||||
char *filename;
|
size_t prefixlen;
|
||||||
unsigned char sha1[20];
|
unsigned char sha1[20];
|
||||||
|
|
||||||
if (get_sha1(fmt("refs/tags/%s", hex), sha1) == 0 &&
|
if (get_sha1(fmt("refs/tags/%s", hex), sha1) == 0 &&
|
||||||
(hex[0] == 'v' || hex[0] == 'V') && isdigit(hex[1]))
|
(hex[0] == 'v' || hex[0] == 'V') && isdigit(hex[1]))
|
||||||
hex++;
|
hex++;
|
||||||
prefix = xstrdup(fmt("%s-%s", cgit_repobasename(repo), hex));
|
strbuf_addf(&filename, "%s-%s", cgit_repobasename(repo), hex);
|
||||||
|
prefixlen = filename.len;
|
||||||
for (f = cgit_snapshot_formats; f->suffix; f++) {
|
for (f = cgit_snapshot_formats; f->suffix; f++) {
|
||||||
if (!(snapshots & f->bit))
|
if (!(snapshots & f->bit))
|
||||||
continue;
|
continue;
|
||||||
filename = fmt("%s%s", prefix, f->suffix);
|
strbuf_setlen(&filename, prefixlen);
|
||||||
cgit_snapshot_link(filename, NULL, NULL, NULL, NULL, filename);
|
strbuf_addstr(&filename, f->suffix);
|
||||||
|
cgit_snapshot_link(filename.buf, NULL, NULL, NULL, NULL,
|
||||||
|
filename.buf);
|
||||||
html("<br/>");
|
html("<br/>");
|
||||||
}
|
}
|
||||||
free(prefix);
|
strbuf_release(&filename);
|
||||||
}
|
}
|
||||||
|
@ -15,14 +15,33 @@
|
|||||||
static int write_archive_type(const char *format, const char *hex, const char *prefix)
|
static int write_archive_type(const char *format, const char *hex, const char *prefix)
|
||||||
{
|
{
|
||||||
struct argv_array argv = ARGV_ARRAY_INIT;
|
struct argv_array argv = ARGV_ARRAY_INIT;
|
||||||
|
const char **nargv;
|
||||||
|
int result;
|
||||||
argv_array_push(&argv, "snapshot");
|
argv_array_push(&argv, "snapshot");
|
||||||
argv_array_push(&argv, format);
|
argv_array_push(&argv, format);
|
||||||
if (prefix) {
|
if (prefix) {
|
||||||
|
struct strbuf buf = STRBUF_INIT;
|
||||||
|
strbuf_addstr(&buf, prefix);
|
||||||
|
strbuf_addch(&buf, '/');
|
||||||
argv_array_push(&argv, "--prefix");
|
argv_array_push(&argv, "--prefix");
|
||||||
argv_array_push(&argv, fmt("%s/", prefix));
|
argv_array_push(&argv, buf.buf);
|
||||||
|
strbuf_release(&buf);
|
||||||
}
|
}
|
||||||
argv_array_push(&argv, hex);
|
argv_array_push(&argv, hex);
|
||||||
return write_archive(argv.argc, argv.argv, NULL, 1, NULL, 0);
|
/*
|
||||||
|
* Now we need to copy the pointers to arguments into a new
|
||||||
|
* structure because write_archive will rearrange its arguments
|
||||||
|
* which may result in duplicated/missing entries causing leaks
|
||||||
|
* or double-frees in argv_array_clear.
|
||||||
|
*/
|
||||||
|
nargv = xmalloc(sizeof(char *) * (argv.argc + 1));
|
||||||
|
/* argv_array guarantees a trailing NULL entry. */
|
||||||
|
memcpy(nargv, argv.argv, sizeof(char *) * (argv.argc + 1));
|
||||||
|
|
||||||
|
result = write_archive(argv.argc, nargv, NULL, 1, NULL, 0);
|
||||||
|
argv_array_clear(&argv);
|
||||||
|
free(nargv);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int write_tar_archive(const char *hex, const char *prefix)
|
static int write_tar_archive(const char *hex, const char *prefix)
|
||||||
@ -129,29 +148,36 @@ static const char *get_ref_from_filename(const char *url, const char *filename,
|
|||||||
{
|
{
|
||||||
const char *reponame;
|
const char *reponame;
|
||||||
unsigned char sha1[20];
|
unsigned char sha1[20];
|
||||||
char *snapshot;
|
struct strbuf snapshot = STRBUF_INIT;
|
||||||
|
int result = 1;
|
||||||
|
|
||||||
snapshot = xstrdup(filename);
|
strbuf_addstr(&snapshot, filename);
|
||||||
snapshot[strlen(snapshot) - strlen(format->suffix)] = '\0';
|
strbuf_setlen(&snapshot, snapshot.len - strlen(format->suffix));
|
||||||
|
|
||||||
if (get_sha1(snapshot, sha1) == 0)
|
if (get_sha1(snapshot.buf, sha1) == 0)
|
||||||
return snapshot;
|
goto out;
|
||||||
|
|
||||||
reponame = cgit_repobasename(url);
|
reponame = cgit_repobasename(url);
|
||||||
if (prefixcmp(snapshot, reponame) == 0) {
|
if (prefixcmp(snapshot.buf, reponame) == 0) {
|
||||||
snapshot += strlen(reponame);
|
const char *new_start = snapshot.buf;
|
||||||
while (snapshot && (*snapshot == '-' || *snapshot == '_'))
|
new_start += strlen(reponame);
|
||||||
snapshot++;
|
while (new_start && (*new_start == '-' || *new_start == '_'))
|
||||||
|
new_start++;
|
||||||
|
strbuf_splice(&snapshot, 0, new_start - snapshot.buf, "", 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (get_sha1(snapshot, sha1) == 0)
|
if (get_sha1(snapshot.buf, sha1) == 0)
|
||||||
return snapshot;
|
goto out;
|
||||||
|
|
||||||
snapshot = fmt("v%s", snapshot);
|
strbuf_insert(&snapshot, 0, "v", 1);
|
||||||
if (get_sha1(snapshot, sha1) == 0)
|
if (get_sha1(snapshot.buf, sha1) == 0)
|
||||||
return snapshot;
|
goto out;
|
||||||
|
|
||||||
return NULL;
|
result = 0;
|
||||||
|
strbuf_release(&snapshot);
|
||||||
|
|
||||||
|
out:
|
||||||
|
return result ? strbuf_detach(&snapshot, NULL) : NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
__attribute__((format (printf, 1, 2)))
|
__attribute__((format (printf, 1, 2)))
|
||||||
|
12
ui-summary.c
12
ui-summary.c
@ -17,6 +17,7 @@
|
|||||||
static void print_url(char *base, char *suffix)
|
static void print_url(char *base, char *suffix)
|
||||||
{
|
{
|
||||||
int columns = 3;
|
int columns = 3;
|
||||||
|
struct strbuf basebuf = STRBUF_INIT;
|
||||||
|
|
||||||
if (ctx.repo->enable_log_filecount)
|
if (ctx.repo->enable_log_filecount)
|
||||||
columns++;
|
columns++;
|
||||||
@ -25,13 +26,16 @@ static void print_url(char *base, char *suffix)
|
|||||||
|
|
||||||
if (!base || !*base)
|
if (!base || !*base)
|
||||||
return;
|
return;
|
||||||
if (suffix && *suffix)
|
if (suffix && *suffix) {
|
||||||
base = fmt("%s/%s", base, suffix);
|
strbuf_addf(&basebuf, "%s/%s", base, suffix);
|
||||||
|
base = basebuf.buf;
|
||||||
|
}
|
||||||
htmlf("<tr><td colspan='%d'><a href='", columns);
|
htmlf("<tr><td colspan='%d'><a href='", columns);
|
||||||
html_url_path(base);
|
html_url_path(base);
|
||||||
html("'>");
|
html("'>");
|
||||||
html_txt(base);
|
html_txt(base);
|
||||||
html("</a></td></tr>\n");
|
html("</a></td></tr>\n");
|
||||||
|
strbuf_release(&basebuf);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void print_urls(char *txt, char *suffix)
|
static void print_urls(char *txt, char *suffix)
|
||||||
@ -112,8 +116,8 @@ void cgit_print_repo_readme(char *path)
|
|||||||
|
|
||||||
/* Prepend repo path to relative readme path unless tracked. */
|
/* Prepend repo path to relative readme path unless tracked. */
|
||||||
if (!ref && *ctx.repo->readme != '/')
|
if (!ref && *ctx.repo->readme != '/')
|
||||||
ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path,
|
ctx.repo->readme = fmtalloc("%s/%s", ctx.repo->path,
|
||||||
ctx.repo->readme));
|
ctx.repo->readme);
|
||||||
|
|
||||||
/* If a subpath is specified for the about page, make it relative
|
/* If a subpath is specified for the about page, make it relative
|
||||||
* to the directory containing the configured readme.
|
* to the directory containing the configured readme.
|
||||||
|
14
ui-tag.c
14
ui-tag.c
@ -41,6 +41,7 @@ static void print_download_links(char *revname)
|
|||||||
|
|
||||||
void cgit_print_tag(char *revname)
|
void cgit_print_tag(char *revname)
|
||||||
{
|
{
|
||||||
|
struct strbuf fullref = STRBUF_INIT;
|
||||||
unsigned char sha1[20];
|
unsigned char sha1[20];
|
||||||
struct object *obj;
|
struct object *obj;
|
||||||
struct tag *tag;
|
struct tag *tag;
|
||||||
@ -49,20 +50,21 @@ void cgit_print_tag(char *revname)
|
|||||||
if (!revname)
|
if (!revname)
|
||||||
revname = ctx.qry.head;
|
revname = ctx.qry.head;
|
||||||
|
|
||||||
if (get_sha1(fmt("refs/tags/%s", revname), sha1)) {
|
strbuf_addf(&fullref, "refs/tags/%s", revname);
|
||||||
|
if (get_sha1(fullref.buf, sha1)) {
|
||||||
cgit_print_error("Bad tag reference: %s", revname);
|
cgit_print_error("Bad tag reference: %s", revname);
|
||||||
return;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
obj = parse_object(sha1);
|
obj = parse_object(sha1);
|
||||||
if (!obj) {
|
if (!obj) {
|
||||||
cgit_print_error("Bad object id: %s", sha1_to_hex(sha1));
|
cgit_print_error("Bad object id: %s", sha1_to_hex(sha1));
|
||||||
return;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
if (obj->type == OBJ_TAG) {
|
if (obj->type == OBJ_TAG) {
|
||||||
tag = lookup_tag(sha1);
|
tag = lookup_tag(sha1);
|
||||||
if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag))) {
|
if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag))) {
|
||||||
cgit_print_error("Bad tag object: %s", revname);
|
cgit_print_error("Bad tag object: %s", revname);
|
||||||
return;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
html("<table class='commit-info'>\n");
|
html("<table class='commit-info'>\n");
|
||||||
htmlf("<tr><td>tag name</td><td>");
|
htmlf("<tr><td>tag name</td><td>");
|
||||||
@ -101,5 +103,7 @@ void cgit_print_tag(char *revname)
|
|||||||
print_download_links(revname);
|
print_download_links(revname);
|
||||||
html("</table>\n");
|
html("</table>\n");
|
||||||
}
|
}
|
||||||
return;
|
|
||||||
|
cleanup:
|
||||||
|
strbuf_release(&fullref);
|
||||||
}
|
}
|
||||||
|
31
ui-tree.c
31
ui-tree.c
@ -129,13 +129,13 @@ static int ls_item(const unsigned char *sha1, const char *base, int baselen,
|
|||||||
{
|
{
|
||||||
struct walk_tree_context *walk_tree_ctx = cbdata;
|
struct walk_tree_context *walk_tree_ctx = cbdata;
|
||||||
char *name;
|
char *name;
|
||||||
char *fullpath;
|
struct strbuf fullpath = STRBUF_INIT;
|
||||||
char *class;
|
struct strbuf class = STRBUF_INIT;
|
||||||
enum object_type type;
|
enum object_type type;
|
||||||
unsigned long size = 0;
|
unsigned long size = 0;
|
||||||
|
|
||||||
name = xstrdup(pathname);
|
name = xstrdup(pathname);
|
||||||
fullpath = fmt("%s%s%s", ctx.qry.path ? ctx.qry.path : "",
|
strbuf_addf(&fullpath, "%s%s%s", ctx.qry.path ? ctx.qry.path : "",
|
||||||
ctx.qry.path ? "/" : "", name);
|
ctx.qry.path ? "/" : "", name);
|
||||||
|
|
||||||
if (!S_ISGITLINK(mode)) {
|
if (!S_ISGITLINK(mode)) {
|
||||||
@ -152,33 +152,34 @@ static int ls_item(const unsigned char *sha1, const char *base, int baselen,
|
|||||||
cgit_print_filemode(mode);
|
cgit_print_filemode(mode);
|
||||||
html("</td><td>");
|
html("</td><td>");
|
||||||
if (S_ISGITLINK(mode)) {
|
if (S_ISGITLINK(mode)) {
|
||||||
cgit_submodule_link("ls-mod", fullpath, sha1_to_hex(sha1));
|
cgit_submodule_link("ls-mod", fullpath.buf, sha1_to_hex(sha1));
|
||||||
} else if (S_ISDIR(mode)) {
|
} else if (S_ISDIR(mode)) {
|
||||||
cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head,
|
cgit_tree_link(name, NULL, "ls-dir", ctx.qry.head,
|
||||||
walk_tree_ctx->curr_rev, fullpath);
|
walk_tree_ctx->curr_rev, fullpath.buf);
|
||||||
} else {
|
} else {
|
||||||
class = strrchr(name, '.');
|
char *ext = strrchr(name, '.');
|
||||||
if (class != NULL) {
|
strbuf_addstr(&class, "ls-blob");
|
||||||
class = fmt("ls-blob %s", class + 1);
|
if (ext)
|
||||||
} else
|
strbuf_addf(&class, " %s", ext + 1);
|
||||||
class = "ls-blob";
|
cgit_tree_link(name, NULL, class.buf, ctx.qry.head,
|
||||||
cgit_tree_link(name, NULL, class, ctx.qry.head,
|
walk_tree_ctx->curr_rev, fullpath.buf);
|
||||||
walk_tree_ctx->curr_rev, fullpath);
|
|
||||||
}
|
}
|
||||||
htmlf("</td><td class='ls-size'>%li</td>", size);
|
htmlf("</td><td class='ls-size'>%li</td>", size);
|
||||||
|
|
||||||
html("<td>");
|
html("<td>");
|
||||||
cgit_log_link("log", NULL, "button", ctx.qry.head,
|
cgit_log_link("log", NULL, "button", ctx.qry.head,
|
||||||
walk_tree_ctx->curr_rev, fullpath, 0, NULL, NULL,
|
walk_tree_ctx->curr_rev, fullpath.buf, 0, NULL, NULL,
|
||||||
ctx.qry.showmsg);
|
ctx.qry.showmsg);
|
||||||
if (ctx.repo->max_stats)
|
if (ctx.repo->max_stats)
|
||||||
cgit_stats_link("stats", NULL, "button", ctx.qry.head,
|
cgit_stats_link("stats", NULL, "button", ctx.qry.head,
|
||||||
fullpath);
|
fullpath.buf);
|
||||||
if (!S_ISGITLINK(mode))
|
if (!S_ISGITLINK(mode))
|
||||||
cgit_plain_link("plain", NULL, "button", ctx.qry.head,
|
cgit_plain_link("plain", NULL, "button", ctx.qry.head,
|
||||||
walk_tree_ctx->curr_rev, fullpath);
|
walk_tree_ctx->curr_rev, fullpath.buf);
|
||||||
html("</td></tr>\n");
|
html("</td></tr>\n");
|
||||||
free(name);
|
free(name);
|
||||||
|
strbuf_release(&fullpath);
|
||||||
|
strbuf_release(&class);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Chargement…
Référencer dans un nouveau ticket
Block a user