aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Lars Hjemli <hjemli@gmail.com>2008-01-04 21:43:40 (JST)
committerGravatar Lars Hjemli <hjemli@gmail.com>2008-01-04 21:43:40 (JST)
commitf80ff37a1706e6774ca21a3ce1fceeb17f89a37a (patch)
tree49558c7d594c054a9523f332de33112853e2c125
parent620bb3e5e4ff87da740fe7232ba74330b5f862d4 (diff)
downloadcgit-f80ff37a1706e6774ca21a3ce1fceeb17f89a37a.zip
cgit-f80ff37a1706e6774ca21a3ce1fceeb17f89a37a.tar.gz
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.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
@@ -45,13 +45,44 @@ static int cgit_prepare_cache(struct cacheitem *item)
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");
@@ -67,6 +98,29 @@ static void cgit_print_repo_page(struct cacheitem *item)
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),