|
diff --git a/cgit.c b/cgit.c index 9ca93a7..e8acc03 100644 --- a/ cgit.c+++ b/ cgit.c |
|
@@ -42,19 +42,50 @@ static int cgit_prepare_cache(struct cacheitem *item) |
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", |
@@ -64,12 +95,35 @@ static void cgit_print_repo_page(struct cacheitem *item) |
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; |
|