aboutsummaryrefslogtreecommitdiff
diff refs
from: back
to: back
| flip
diff options
context:
space:
mode:
-rw-r--r--cgit.c92
-rw-r--r--cgit.h4
-rw-r--r--cgitrc.5.txt5
-rw-r--r--cmd.c3
-rw-r--r--scan-tree.c8
-rw-r--r--ui-blob.c61
-rw-r--r--ui-blob.h6
-rw-r--r--ui-shared.c2
-rw-r--r--ui-summary.c100
-rw-r--r--ui-summary.h1
10 files changed, 122 insertions, 160 deletions
diff --git a/cgit.c b/cgit.c
index f0a9acf..f738b83 100644
--- a/cgit.c
+++ b/cgit.c
@@ -1,7 +1,7 @@
/* cgit.c: cgi for the git scm
*
* Copyright (C) 2006 Lars Hjemli
- * Copyright (C) 2010-2013 Jason A. Donenfeld <Jason@zx2c4.com>
+ * Copyright (C) 2010, 2012 Jason A. Donenfeld <Jason@zx2c4.com>
*
* Licensed under GNU General Public License v2
* (see COPYING for full license text)
@@ -101,15 +101,13 @@ static void repo_config(struct cgit_repo *repo, const char *name, const char *va
else if (!strcmp(name, "module-link"))
repo->module_link= xstrdup(value);
else if (!prefixcmp(name, "module-link.")) {
- item = string_list_append(&repo->submodules, xstrdup(name + 12));
+ item = string_list_append(&repo->submodules, name + 12);
item->util = xstrdup(value);
} else if (!strcmp(name, "section"))
repo->section = xstrdup(value);
- else if (!strcmp(name, "readme") && value != NULL) {
- if (repo->readme.items == ctx.cfg.readme.items)
- memset(&repo->readme, 0, sizeof(repo->readme));
- string_list_append(&repo->readme, xstrdup(value));
- } else if (!strcmp(name, "logo") && value != NULL)
+ else if (!strcmp(name, "readme") && value != NULL)
+ repo->readme = xstrdup(value);
+ else if (!strcmp(name, "logo") && value != NULL)
repo->logo = xstrdup(value);
else if (!strcmp(name, "logo-link") && value != NULL)
repo->logo_link = xstrdup(value);
@@ -133,8 +131,8 @@ static void config_cb(const char *name, const char *value)
ctx.repo->path = trim_end(value, '/');
else if (ctx.repo && !prefixcmp(name, "repo."))
repo_config(ctx.repo, name + 5, value);
- else if (!strcmp(name, "readme") && value != NULL)
- string_list_append(&ctx.cfg.readme, xstrdup(value));
+ else if (!strcmp(name, "readme"))
+ ctx.cfg.readme = xstrdup(value);
else if (!strcmp(name, "root-title"))
ctx.cfg.root_title = xstrdup(value);
else if (!strcmp(name, "root-desc"))
@@ -472,76 +470,37 @@ static char *guess_defbranch(void)
return "master";
return xstrdup(ref + 11);
}
-/* The caller must free filename and ref after calling this. */
-static inline void parse_readme(const char *readme, char **filename, char **ref, struct cgit_repo *repo)
-{
- const char *colon;
-
- *filename = NULL;
- *ref = NULL;
-
- if (!readme || !readme[0])
- return;
- /* Check if the readme is tracked in the git repo. */
- colon = strchr(readme, ':');
- if (colon && strlen(colon) > 1) {
- /* If it starts with a colon, we want to use
- * the default branch */
- if (colon == readme && repo->defbranch)
- *ref = xstrdup(repo->defbranch);
- else
- *ref = xstrndup(readme, colon - readme);
- readme = colon + 1;
- }
-
- /* Prepend repo path to relative readme path unless tracked. */
- if (!(*ref) && readme[0] != '/')
- *filename = fmtalloc("%s/%s", repo->path, readme);
- else
- *filename = xstrdup(readme);
-}
static void choose_readme(struct cgit_repo *repo)
{
- int found;
- char *filename, *ref;
- struct string_list_item *entry;
+ char *entry, *filename, *ref;
- if (!repo->readme.nr)
+ /* If there's no space, we skip the possibly expensive
+ * selection process. */
+ if (!repo->readme || !strchr(repo->readme, ' '))
return;
- found = 0;
- for_each_string_list_item(entry, &repo->readme) {
- parse_readme(entry->string, &filename, &ref, repo);
- if (!filename) {
+ for (entry = strtok(repo->readme, " "); entry; entry = strtok(NULL, " ")) {
+ cgit_parse_readme(entry, NULL, &filename, &ref, repo);
+ if (!(*filename)) {
free(filename);
free(ref);
continue;
}
- /* If there's only one item, we skip the possibly expensive
- * selection process. */
- if (repo->readme.nr == 1) {
- found = 1;
+ if (*ref && cgit_ref_path_exists(filename, ref)) {
+ free(filename);
+ free(ref);
break;
}
- if (ref) {
- if (cgit_ref_path_exists(filename, ref, 1)) {
- found = 1;
- break;
- }
- }
- else if (!access(filename, R_OK)) {
- found = 1;
+ if (!access(filename, R_OK)) {
+ free(filename);
+ free(ref);
break;
}
free(filename);
free(ref);
}
- repo->readme.strdup_strings = 1;
- string_list_clear(&repo->readme, 0);
- repo->readme.strdup_strings = 0;
- if (found)
- string_list_append(&repo->readme, filename)->util = ref;
+ repo->readme = entry;
}
static int prepare_repo_cmd(struct cgit_context *ctx)
@@ -701,7 +660,6 @@ static char *get_first_line(char *txt)
static void print_repo(FILE *f, struct cgit_repo *repo)
{
- struct string_list_item *item;
fprintf(f, "repo.url=%s\n", repo->url);
fprintf(f, "repo.name=%s\n", repo->name);
fprintf(f, "repo.path=%s\n", repo->path);
@@ -712,12 +670,8 @@ static void print_repo(FILE *f, struct cgit_repo *repo)
fprintf(f, "repo.desc=%s\n", tmp);
free(tmp);
}
- for_each_string_list_item(item, &repo->readme) {
- if (item->util)
- fprintf(f, "repo.readme=%s:%s\n", (char *)item->util, item->string);
- else
- fprintf(f, "repo.readme=%s\n", item->string);
- }
+ if (repo->readme)
+ fprintf(f, "repo.readme=%s\n", repo->readme);
if (repo->defbranch)
fprintf(f, "repo.defbranch=%s\n", repo->defbranch);
if (repo->module_link)
diff --git a/cgit.h b/cgit.h
index f28cf30..850b792 100644
--- a/cgit.h
+++ b/cgit.h
@@ -73,7 +73,7 @@ struct cgit_repo {
char *owner;
char *defbranch;
char *module_link;
- struct string_list readme;
+ char *readme;
char *section;
char *clone_url;
char *logo;
@@ -183,7 +183,7 @@ struct cgit_config {
char *mimetype_file;
char *module_link;
char *project_list;
- struct string_list readme;
+ char *readme;
char *robots;
char *root_title;
char *root_desc;
diff --git a/cgitrc.5.txt b/cgitrc.5.txt
index 6437ef4..12a843b 100644
--- a/cgitrc.5.txt
+++ b/cgitrc.5.txt
@@ -290,9 +290,8 @@ project-list::
readme::
Text which will be used as default value for "repo.readme". Multiple
- config keys may be specified, and cgit will use the first found file
- in this list. This is useful in conjunction with scan-path. Default
- value: none. See also: scan-path, repo.readme.
+ files may be specified, separated by a space, and cgit will use the
+ first found file in this list. Default value: none.
remove-suffix::
If set to "1" and scan-path is enabled, if any repositories are found
diff --git a/cmd.c b/cmd.c
index abe8e46..198bf2f 100644
--- a/cmd.c
+++ b/cmd.c
@@ -1,7 +1,6 @@
/* cmd.c: the cgit command dispatcher
*
* Copyright (C) 2008 Lars Hjemli
- * Copyright (C) 2013 Jason A. Donenfeld <Jason@zx2c4.com>.
*
* Licensed under GNU General Public License v2
* (see COPYING for full license text)
@@ -47,7 +46,7 @@ static void about_fn(struct cgit_context *ctx)
static void blob_fn(struct cgit_context *ctx)
{
- cgit_print_blob(ctx->qry.sha1, ctx->qry.path, ctx->qry.head, 0);
+ cgit_print_blob(ctx->qry.sha1, ctx->qry.path, ctx->qry.head);
}
static void commit_fn(struct cgit_context *ctx)
diff --git a/scan-tree.c b/scan-tree.c
index 2684b44..a1ec8fb 100644
--- a/scan-tree.c
+++ b/scan-tree.c
@@ -1,7 +1,7 @@
/* scan-tree.c
*
* Copyright (C) 2008-2009 Lars Hjemli
- * Copyright (C) 2010-2013 Jason A. Donenfeld <Jason@zx2c4.com>
+ * Copyright (C) 2010, 2012 Jason A. Donenfeld <Jason@zx2c4.com>
*
* Licensed under GNU General Public License v2
* (see COPYING for full license text)
@@ -147,6 +147,12 @@ static void add_repo(const char *base, struct strbuf *path, repo_config_fn fn)
strbuf_setlen(path, pathlen);
}
+ if (!repo->readme) {
+ strbuf_addstr(path, "README.html");
+ if (!stat(path->buf, &st))
+ repo->readme = "README.html";
+ strbuf_setlen(path, pathlen);
+ }
if (ctx.cfg.section_from_path) {
n = ctx.cfg.section_from_path;
if (n > 0) {
diff --git a/ui-blob.c b/ui-blob.c
index eb14a75..b4be139 100644
--- a/ui-blob.c
+++ b/ui-blob.c
@@ -1,7 +1,7 @@
/* ui-blob.c: show blob content
*
* Copyright (C) 2008 Lars Hjemli
- * Copyright (C) 2010-2013 Jason A. Donenfeld <Jason@zx2c4.com>
+ * Copyright (C) 2010 Jason A. Donenfeld <Jason@zx2c4.com>
*
* Licensed under GNU General Public License v2
* (see COPYING for full license text)
@@ -15,8 +15,7 @@
struct walk_tree_context {
const char *match_path;
unsigned char *matched_sha1;
- int found_path:1;
- int file_only:1;
+ int found_path;
};
static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
@@ -24,8 +23,6 @@ static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
{
struct walk_tree_context *walk_tree_ctx = cbdata;
- if (walk_tree_ctx->file_only && !S_ISREG(mode))
- return READ_TREE_RECURSIVE;
if (strncmp(base, walk_tree_ctx->match_path, baselen)
|| strcmp(walk_tree_ctx->match_path + baselen, pathname))
return READ_TREE_RECURSIVE;
@@ -34,34 +31,33 @@ static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
return 0;
}
-int cgit_ref_path_exists(const char *path, const char *ref, int file_only)
+int cgit_ref_path_exists(const char *path, const char *ref)
{
- unsigned char sha1[20];
- unsigned long size;
- struct pathspec_item path_items = {
- .match = path,
- .len = strlen(path)
- };
- struct pathspec paths = {
- .nr = 1,
- .items = &path_items
- };
- struct walk_tree_context walk_tree_ctx = {
- .match_path = path,
- .matched_sha1 = sha1,
- .found_path = 0,
- .file_only = file_only
- };
+ unsigned char sha1[20];
+ unsigned long size;
+ struct pathspec_item path_items = {
+ .match = path,
+ .len = strlen(path)
+ };
+ struct pathspec paths = {
+ .nr = 1,
+ .items = &path_items
+ };
+ struct walk_tree_context walk_tree_ctx = {
+ .match_path = path,
+ .matched_sha1 = sha1,
+ .found_path = 0
+ };
- if (get_sha1(ref, sha1))
- return 0;
- if (sha1_object_info(sha1, &size) != OBJ_COMMIT)
- return 0;
- read_tree_recursive(lookup_commit_reference(sha1)->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
- return walk_tree_ctx.found_path;
+ if (get_sha1(ref, sha1))
+ return 0;
+ if (sha1_object_info(sha1, &size) != OBJ_COMMIT)
+ return 0;
+ read_tree_recursive(lookup_commit_reference(sha1)->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
+ return walk_tree_ctx.found_path;
}
-int cgit_print_file(char *path, const char *head, int file_only)
+int cgit_print_file(char *path, const char *head)
{
unsigned char sha1[20];
enum object_type type;
@@ -79,8 +75,7 @@ int cgit_print_file(char *path, const char *head, int file_only)
struct walk_tree_context walk_tree_ctx = {
.match_path = path,
.matched_sha1 = sha1,
- .found_path = 0,
- .file_only = file_only
+ .found_path = 0
};
if (get_sha1(head, sha1))
@@ -103,7 +98,7 @@ int cgit_print_file(char *path, const char *head, int file_only)
return 0;
}
-void cgit_print_blob(const char *hex, char *path, const char *head, int file_only)
+void cgit_print_blob(const char *hex, char *path, const char *head)
{
unsigned char sha1[20];
enum object_type type;
@@ -121,8 +116,6 @@ void cgit_print_blob(const char *hex, char *path, const char *head, int file_onl
struct walk_tree_context walk_tree_ctx = {
.match_path = path,
.matched_sha1 = sha1,
- .found_path = 0,
- .file_only = file_only
};
if (hex) {
diff --git a/ui-blob.h b/ui-blob.h
index 16847b2..ce3649f 100644
--- a/ui-blob.h
+++ b/ui-blob.h
@@ -1,8 +1,8 @@
#ifndef UI_BLOB_H
#define UI_BLOB_H
-extern int cgit_ref_path_exists(const char *path, const char *ref, int file_only);
-extern int cgit_print_file(char *path, const char *head, int file_only);
-extern void cgit_print_blob(const char *hex, char *path, const char *head, int file_only);
+extern int cgit_ref_path_exists(const char *path, const char *ref);
+extern int cgit_print_file(char *path, const char *head);
+extern void cgit_print_blob(const char *hex, char *path, const char *head);
#endif /* UI_BLOB_H */
diff --git a/ui-shared.c b/ui-shared.c
index 7ab2ab1..519eef7 100644
--- a/ui-shared.c
+++ b/ui-shared.c
@@ -864,7 +864,7 @@ void cgit_print_pageheader(struct cgit_context *ctx)
if (ctx->repo->max_stats)
cgit_stats_link("stats", NULL, hc(ctx, "stats"),
ctx->qry.head, ctx->qry.vpath);
- if (ctx->repo->readme.nr)
+ if (ctx->repo->readme)
reporevlink("about", "about", NULL,
hc(ctx, "about"), ctx->qry.head, NULL,
NULL);
diff --git a/ui-summary.c b/ui-summary.c
index d8500d6..57206dd 100644
--- a/ui-summary.c
+++ b/ui-summary.c
@@ -1,7 +1,7 @@
/* ui-summary.c: functions for generating repo summary page
*
* Copyright (C) 2006 Lars Hjemli
- * Copyright (C) 2010-2013 Jason A. Donenfeld <Jason@zx2c4.com>
+ * Copyright (C) 2010 Jason A. Donenfeld <Jason@zx2c4.com>
*
* Licensed under GNU General Public License v2
* (see COPYING for full license text)
@@ -13,7 +13,6 @@
#include "ui-log.h"
#include "ui-refs.h"
#include "ui-blob.h"
-#include <libgen.h>
static void print_url(char *base, char *suffix)
{
@@ -96,57 +95,69 @@ void cgit_print_summary()
html("</table>");
}
-/* The caller must free the return value. */
-static char* append_readme_path(const char *filename, const char *ref, const char *path)
+/* The caller must free filename and ref after calling this. */
+void cgit_parse_readme(const char *readme, const char *path, char **filename, char **ref, struct cgit_repo *repo)
{
- char *file, *base_dir, *full_path, *resolved_base = NULL, *resolved_full = NULL;
- /* If a subpath is specified for the about page, make it relative
- * to the directory containing the configured readme. */
+ const char *slash, *colon;
+ char *resolved_base, *resolved_full;
- file = xstrdup(filename);
- base_dir = dirname(file);
- if (!strcmp(base_dir, ".") || !strcmp(base_dir, "..")) {
- if (!ref) {
- free(file);
- return NULL;
- }
- full_path = xstrdup(path);
- } else
- full_path = fmtalloc("%s/%s", base_dir, path);
-
- if (!ref) {
- resolved_base = realpath(base_dir, NULL);
- resolved_full = realpath(full_path, NULL);
- if (!resolved_base || !resolved_full || strncmp(resolved_base, resolved_full, strlen(resolved_base))) {
- free(full_path);
- full_path = NULL;
- }
+ *filename = NULL;
+ *ref = NULL;
+
+ if (!readme || !(*readme))
+ return;
+
+ /* Check if the readme is tracked in the git repo. */
+ colon = strchr(readme, ':');
+ if (colon && strlen(colon) > 1) {
+ /* If it starts with a colon, we want to use
+ * the default branch */
+ if (colon == readme && repo->defbranch)
+ *ref = xstrdup(repo->defbranch);
+ else
+ *ref = xstrndup(readme, colon - readme);
+ readme = colon + 1;
}
- free(file);
- free(resolved_base);
- free(resolved_full);
+ /* Prepend repo path to relative readme path unless tracked. */
+ if (!(*ref) && *readme != '/')
+ readme = fmtalloc("%s/%s", repo->path, readme);
- return full_path;
+ /* If a subpath is specified for the about page, make it relative
+ * to the directory containing the configured readme. */
+ if (path) {
+ slash = strrchr(readme, '/');
+ if (!slash) {
+ if (!colon)
+ return;
+ slash = colon;
+ }
+ *filename = xmalloc(slash - readme + 1 + strlen(path) + 1);
+ strncpy(*filename, readme, slash - readme + 1);
+ if (!(*ref))
+ resolved_base = realpath(*filename, NULL);
+ strcpy(*filename + (slash - readme + 1), path);
+ if (!(*ref))
+ resolved_full = realpath(*filename, NULL);
+ if (!(*ref) && (!resolved_base || !resolved_full || strstr(resolved_full, resolved_base) != resolved_full)) {
+ free(*filename);
+ *filename = NULL;
+ }
+ if (!(*ref)) {
+ free(resolved_base);
+ free(resolved_full);
+ }
+ } else
+ *filename = xstrdup(readme);
}
void cgit_print_repo_readme(char *path)
{
char *filename, *ref;
- int free_filename = 0;
+ cgit_parse_readme(ctx.repo->readme, path, &filename, &ref, ctx.repo);
- if (ctx.repo->readme.nr == 0)
+ if (!filename)
return;
-
- filename = ctx.repo->readme.items[0].string;
- ref = ctx.repo->readme.items[0].util;
-
- if (path) {
- free_filename = 1;
- filename = append_readme_path(filename, ref, path);
- if (!filename)
- return;
- }
/* Print the calculated readme, either from the git repo or from the
* filesystem, while applying the about-filter.
@@ -157,15 +168,14 @@ void cgit_print_repo_readme(char *path)
cgit_open_filter(ctx.repo->about_filter);
}
if (ref)
- cgit_print_file(filename, ref, 1);
+ cgit_print_file(filename, ref);
else
html_include(filename);
if (ctx.repo->about_filter) {
cgit_close_filter(ctx.repo->about_filter);
ctx.repo->about_filter->argv[1] = NULL;
- free(ref);
}
html("</div>");
- if (free_filename)
- free(filename);
+ free(filename);
+ free(ref);
}
diff --git a/ui-summary.h b/ui-summary.h
index c01f560..d6dc5ba 100644
--- a/ui-summary.h
+++ b/ui-summary.h
@@ -1,6 +1,7 @@
#ifndef UI_SUMMARY_H
#define UI_SUMMARY_H
+extern void cgit_parse_readme(const char *readme, const char *path, char **filename, char **ref, struct cgit_repo *repo);
extern void cgit_print_summary();
extern void cgit_print_repo_readme(char *path);