diff options
Diffstat (limited to 'cgit.c')
-rw-r--r-- | cgit.c | 67 |
1 files changed, 63 insertions, 4 deletions
@@ -45,13 +45,44 @@ static int cgit_prepare_cache(struct cacheitem *item) | |||
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"); |
@@ -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), |
@@ -75,6 +129,11 @@ static void cgit_print_repo_page(struct cacheitem *item) | |||
75 | return; | 129 | return; |
76 | } | 130 | } |
77 | 131 | ||
132 | if (cgit_cmd == CMD_PATCH) { | ||
133 | cgit_print_patch(cgit_query_sha1, item); | ||
134 | return; | ||
135 | } | ||
136 | |||
78 | if (cgit_cmd == CMD_BLOB) { | 137 | if (cgit_cmd == CMD_BLOB) { |
79 | cgit_print_blob(item, cgit_query_sha1, cgit_query_path); | 138 | cgit_print_blob(item, cgit_query_sha1, cgit_query_path); |
80 | return; | 139 | return; |