author | Lars Hjemli <hjemli@gmail.com> | 2008-01-04 12:43:40 (UTC) |
---|---|---|
committer | Lars Hjemli <hjemli@gmail.com> | 2008-01-04 12:43:40 (UTC) |
commit | f80ff37a1706e6774ca21a3ce1fceeb17f89a37a (patch) (unidiff) | |
tree | 49558c7d594c054a9523f332de33112853e2c125 | |
parent | 620bb3e5e4ff87da740fe7232ba74330b5f862d4 (diff) | |
download | cgit-f80ff37a1706e6774ca21a3ce1fceeb17f89a37a.zip cgit-f80ff37a1706e6774ca21a3ce1fceeb17f89a37a.tar.gz cgit-f80ff37a1706e6774ca21a3ce1fceeb17f89a37a.tar.bz2 |
Handle missing default branch and error out on invalid branch names
When no branch is specified and the repository does not have a default branch,
use the first branch.
Also, print sensible errormessages when the repository does not contain any
branches and when invalid branchnames are specified.
Signed-off-by: Lars Hjemli <hjemli@gmail.com>
-rw-r--r-- | cgit.c | 62 |
1 files changed, 58 insertions, 4 deletions
@@ -24,70 +24,124 @@ static int cgit_prepare_cache(struct cacheitem *item) | |||
24 | item->ttl = cgit_cache_root_ttl; | 24 | item->ttl = cgit_cache_root_ttl; |
25 | return 1; | 25 | return 1; |
26 | } | 26 | } |
27 | 27 | ||
28 | if (!cgit_cmd) { | 28 | if (!cgit_cmd) { |
29 | item->name = xstrdup(fmt("%s/%s/index.%s.html", cgit_cache_root, | 29 | item->name = xstrdup(fmt("%s/%s/index.%s.html", cgit_cache_root, |
30 | cache_safe_filename(cgit_repo->url), | 30 | cache_safe_filename(cgit_repo->url), |
31 | cache_safe_filename(cgit_querystring))); | 31 | cache_safe_filename(cgit_querystring))); |
32 | item->ttl = cgit_cache_repo_ttl; | 32 | item->ttl = cgit_cache_repo_ttl; |
33 | } else { | 33 | } else { |
34 | item->name = xstrdup(fmt("%s/%s/%s/%s.html", cgit_cache_root, | 34 | item->name = xstrdup(fmt("%s/%s/%s/%s.html", cgit_cache_root, |
35 | cache_safe_filename(cgit_repo->url), | 35 | cache_safe_filename(cgit_repo->url), |
36 | cgit_query_page, | 36 | cgit_query_page, |
37 | cache_safe_filename(cgit_querystring))); | 37 | cache_safe_filename(cgit_querystring))); |
38 | if (cgit_query_has_symref) | 38 | if (cgit_query_has_symref) |
39 | item->ttl = cgit_cache_dynamic_ttl; | 39 | item->ttl = cgit_cache_dynamic_ttl; |
40 | else if (cgit_query_has_sha1) | 40 | else if (cgit_query_has_sha1) |
41 | item->ttl = cgit_cache_static_ttl; | 41 | item->ttl = cgit_cache_static_ttl; |
42 | else | 42 | else |
43 | item->ttl = cgit_cache_repo_ttl; | 43 | item->ttl = cgit_cache_repo_ttl; |
44 | } | 44 | } |
45 | return 1; | 45 | return 1; |
46 | } | 46 | } |
47 | 47 | ||
48 | struct refmatch { | ||
49 | char *req_ref; | ||
50 | char *first_ref; | ||
51 | int match; | ||
52 | }; | ||
53 | |||
54 | int find_current_ref(const char *refname, const unsigned char *sha1, | ||
55 | int flags, void *cb_data) | ||
56 | { | ||
57 | struct refmatch *info; | ||
58 | |||
59 | info = (struct refmatch *)cb_data; | ||
60 | if (!strcmp(refname, info->req_ref)) | ||
61 | info->match = 1; | ||
62 | if (!info->first_ref) | ||
63 | info->first_ref = xstrdup(refname); | ||
64 | return info->match; | ||
65 | } | ||
66 | |||
67 | char *find_default_branch(struct repoinfo *repo) | ||
68 | { | ||
69 | struct refmatch info; | ||
70 | |||
71 | info.req_ref = repo->defbranch; | ||
72 | info.first_ref = NULL; | ||
73 | info.match = 0; | ||
74 | for_each_branch_ref(find_current_ref, &info); | ||
75 | if (info.match) | ||
76 | return info.req_ref; | ||
77 | else | ||
78 | return info.first_ref; | ||
79 | } | ||
80 | |||
48 | static void cgit_print_repo_page(struct cacheitem *item) | 81 | static void cgit_print_repo_page(struct cacheitem *item) |
49 | { | 82 | { |
50 | char *title; | 83 | char *title, *tmp; |
51 | int show_search; | 84 | int show_search; |
52 | 85 | unsigned char sha1[20]; | |
53 | if (!cgit_query_head) | ||
54 | cgit_query_head = cgit_repo->defbranch; | ||
55 | 86 | ||
56 | if (chdir(cgit_repo->path)) { | 87 | if (chdir(cgit_repo->path)) { |
57 | title = fmt("%s - %s", cgit_root_title, "Bad request"); | 88 | title = fmt("%s - %s", cgit_root_title, "Bad request"); |
58 | cgit_print_docstart(title, item); | 89 | cgit_print_docstart(title, item); |
59 | cgit_print_pageheader(title, 0); | 90 | cgit_print_pageheader(title, 0); |
60 | cgit_print_error(fmt("Unable to scan repository: %s", | 91 | cgit_print_error(fmt("Unable to scan repository: %s", |
61 | strerror(errno))); | 92 | strerror(errno))); |
62 | cgit_print_docend(); | 93 | cgit_print_docend(); |
63 | return; | 94 | return; |
64 | } | 95 | } |
65 | 96 | ||
66 | title = fmt("%s - %s", cgit_repo->name, cgit_repo->desc); | 97 | title = fmt("%s - %s", cgit_repo->name, cgit_repo->desc); |
67 | show_search = 0; | 98 | show_search = 0; |
68 | setenv("GIT_DIR", cgit_repo->path, 1); | 99 | setenv("GIT_DIR", cgit_repo->path, 1); |
69 | 100 | ||
101 | if (!cgit_query_head) { | ||
102 | cgit_query_head = xstrdup(find_default_branch(cgit_repo)); | ||
103 | cgit_repo->defbranch = cgit_query_head; | ||
104 | } | ||
105 | |||
106 | if (!cgit_query_head) { | ||
107 | cgit_print_docstart(title, item); | ||
108 | cgit_print_pageheader(title, 0); | ||
109 | cgit_print_error("Repository seems to be empty"); | ||
110 | cgit_print_docend(); | ||
111 | return; | ||
112 | } | ||
113 | |||
114 | if (get_sha1(cgit_query_head, sha1)) { | ||
115 | tmp = xstrdup(cgit_query_head); | ||
116 | cgit_query_head = cgit_repo->defbranch; | ||
117 | cgit_print_docstart(title, item); | ||
118 | cgit_print_pageheader(title, 0); | ||
119 | cgit_print_error(fmt("Invalid branch: %s", tmp)); | ||
120 | cgit_print_docend(); | ||
121 | return; | ||
122 | } | ||
123 | |||
70 | if ((cgit_cmd == CMD_SNAPSHOT) && cgit_repo->snapshots) { | 124 | if ((cgit_cmd == CMD_SNAPSHOT) && cgit_repo->snapshots) { |
71 | cgit_print_snapshot(item, cgit_query_head, cgit_query_sha1, | 125 | cgit_print_snapshot(item, cgit_query_head, cgit_query_sha1, |
72 | cgit_repobasename(cgit_repo->url), | 126 | cgit_repobasename(cgit_repo->url), |
73 | cgit_query_path, | 127 | cgit_query_path, |
74 | cgit_repo->snapshots ); | 128 | cgit_repo->snapshots ); |
75 | return; | 129 | return; |
76 | } | 130 | } |
77 | 131 | ||
78 | if (cgit_cmd == CMD_PATCH) { | 132 | if (cgit_cmd == CMD_PATCH) { |
79 | cgit_print_patch(cgit_query_sha1, item); | 133 | cgit_print_patch(cgit_query_sha1, item); |
80 | return; | 134 | return; |
81 | } | 135 | } |
82 | 136 | ||
83 | if (cgit_cmd == CMD_BLOB) { | 137 | if (cgit_cmd == CMD_BLOB) { |
84 | cgit_print_blob(item, cgit_query_sha1, cgit_query_path); | 138 | cgit_print_blob(item, cgit_query_sha1, cgit_query_path); |
85 | return; | 139 | return; |
86 | } | 140 | } |
87 | 141 | ||
88 | show_search = (cgit_cmd == CMD_LOG); | 142 | show_search = (cgit_cmd == CMD_LOG); |
89 | cgit_print_docstart(title, item); | 143 | cgit_print_docstart(title, item); |
90 | if (!cgit_cmd) { | 144 | if (!cgit_cmd) { |
91 | cgit_print_pageheader("summary", show_search); | 145 | cgit_print_pageheader("summary", show_search); |
92 | cgit_print_summary(); | 146 | cgit_print_summary(); |
93 | cgit_print_docend(); | 147 | cgit_print_docend(); |