summaryrefslogtreecommitdiffabout
path: root/cgit.c
Unidiff
Diffstat (limited to 'cgit.c') (more/less context) (ignore whitespace changes)
-rw-r--r--cgit.c62
1 files changed, 58 insertions, 4 deletions
diff --git a/cgit.c b/cgit.c
index 9ca93a7..e8acc03 100644
--- a/cgit.c
+++ b/cgit.c
@@ -36,46 +36,100 @@ static int cgit_prepare_cache(struct cacheitem *item)
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
48struct refmatch {
49 char *req_ref;
50 char *first_ref;
51 int match;
52};
53
54int 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
67char *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
48static void cgit_print_repo_page(struct cacheitem *item) 81static 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 }