summaryrefslogtreecommitdiffabout
authorMichael Krelin <hacker@klever.net>2007-07-21 13:24:07 (UTC)
committer Michael Krelin <hacker@klever.net>2007-07-21 13:24:07 (UTC)
commit1cb8bedf1e0a4aa73bb8ad3f96bfa7eda50919b3 (patch) (unidiff)
tree471330d2f6ead56437bca3698329792a23ce1334
parent0df096f6e146187e55e2203ea1c017442cc2c8c6 (diff)
downloadcgit-1cb8bedf1e0a4aa73bb8ad3f96bfa7eda50919b3.zip
cgit-1cb8bedf1e0a4aa73bb8ad3f96bfa7eda50919b3.tar.gz
cgit-1cb8bedf1e0a4aa73bb8ad3f96bfa7eda50919b3.tar.bz2
introduce cgit_repobasename
that shortens reponame stripping any directories and .git suffixes, that is turning 'dir/repo.git/' or 'dir/repo/.git/' or alikes into mere 'repo'. Signed-off-by: Michael Krelin <hacker@klever.net>
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--cgit.h2
-rw-r--r--ui-shared.c24
2 files changed, 26 insertions, 0 deletions
diff --git a/cgit.h b/cgit.h
index aabf725..1dbf901 100644
--- a/cgit.h
+++ b/cgit.h
@@ -161,79 +161,81 @@ extern int chk_non_negative(int result, char *msg);
161 161
162extern int hextoint(char c); 162extern int hextoint(char c);
163extern char *trim_end(const char *str, char c); 163extern char *trim_end(const char *str, char c);
164 164
165extern void *cgit_free_commitinfo(struct commitinfo *info); 165extern void *cgit_free_commitinfo(struct commitinfo *info);
166 166
167extern int cgit_diff_files(const unsigned char *old_sha1, 167extern int cgit_diff_files(const unsigned char *old_sha1,
168 const unsigned char *new_sha1, 168 const unsigned char *new_sha1,
169 linediff_fn fn); 169 linediff_fn fn);
170 170
171extern void cgit_diff_tree(const unsigned char *old_sha1, 171extern void cgit_diff_tree(const unsigned char *old_sha1,
172 const unsigned char *new_sha1, 172 const unsigned char *new_sha1,
173 filepair_fn fn); 173 filepair_fn fn);
174 174
175extern void cgit_diff_commit(struct commit *commit, filepair_fn fn); 175extern void cgit_diff_commit(struct commit *commit, filepair_fn fn);
176 176
177extern char *fmt(const char *format,...); 177extern char *fmt(const char *format,...);
178 178
179extern void html(const char *txt); 179extern void html(const char *txt);
180extern void htmlf(const char *format,...); 180extern void htmlf(const char *format,...);
181extern void html_txt(char *txt); 181extern void html_txt(char *txt);
182extern void html_ntxt(int len, char *txt); 182extern void html_ntxt(int len, char *txt);
183extern void html_attr(char *txt); 183extern void html_attr(char *txt);
184extern void html_hidden(char *name, char *value); 184extern void html_hidden(char *name, char *value);
185extern void html_link_open(char *url, char *title, char *class); 185extern void html_link_open(char *url, char *title, char *class);
186extern void html_link_close(void); 186extern void html_link_close(void);
187extern void html_filemode(unsigned short mode); 187extern void html_filemode(unsigned short mode);
188extern int html_include(const char *filename); 188extern int html_include(const char *filename);
189 189
190extern int cgit_read_config(const char *filename, configfn fn); 190extern int cgit_read_config(const char *filename, configfn fn);
191extern int cgit_parse_query(char *txt, configfn fn); 191extern int cgit_parse_query(char *txt, configfn fn);
192extern struct commitinfo *cgit_parse_commit(struct commit *commit); 192extern struct commitinfo *cgit_parse_commit(struct commit *commit);
193extern struct taginfo *cgit_parse_tag(struct tag *tag); 193extern struct taginfo *cgit_parse_tag(struct tag *tag);
194extern void cgit_parse_url(const char *url); 194extern void cgit_parse_url(const char *url);
195 195
196extern char *cache_safe_filename(const char *unsafe); 196extern char *cache_safe_filename(const char *unsafe);
197extern int cache_lock(struct cacheitem *item); 197extern int cache_lock(struct cacheitem *item);
198extern int cache_unlock(struct cacheitem *item); 198extern int cache_unlock(struct cacheitem *item);
199extern int cache_cancel_lock(struct cacheitem *item); 199extern int cache_cancel_lock(struct cacheitem *item);
200extern int cache_exist(struct cacheitem *item); 200extern int cache_exist(struct cacheitem *item);
201extern int cache_expired(struct cacheitem *item); 201extern int cache_expired(struct cacheitem *item);
202 202
203extern char *cgit_repourl(const char *reponame); 203extern char *cgit_repourl(const char *reponame);
204extern char *cgit_fileurl(const char *reponame, const char *pagename, 204extern char *cgit_fileurl(const char *reponame, const char *pagename,
205 const char *filename, const char *query); 205 const char *filename, const char *query);
206extern char *cgit_pageurl(const char *reponame, const char *pagename, 206extern char *cgit_pageurl(const char *reponame, const char *pagename,
207 const char *query); 207 const char *query);
208 208
209extern const char *cgit_repobasename(const char *reponame);
210
209extern void cgit_tree_link(char *name, char *title, char *class, char *head, 211extern void cgit_tree_link(char *name, char *title, char *class, char *head,
210 char *rev, char *path); 212 char *rev, char *path);
211extern void cgit_log_link(char *name, char *title, char *class, char *head, 213extern void cgit_log_link(char *name, char *title, char *class, char *head,
212 char *rev, char *path, int ofs); 214 char *rev, char *path, int ofs);
213extern void cgit_commit_link(char *name, char *title, char *class, char *head, 215extern void cgit_commit_link(char *name, char *title, char *class, char *head,
214 char *rev); 216 char *rev);
215extern void cgit_diff_link(char *name, char *title, char *class, char *head, 217extern void cgit_diff_link(char *name, char *title, char *class, char *head,
216 char *new_rev, char *old_rev, char *path); 218 char *new_rev, char *old_rev, char *path);
217 219
218extern void cgit_print_error(char *msg); 220extern void cgit_print_error(char *msg);
219extern void cgit_print_date(time_t secs, char *format); 221extern void cgit_print_date(time_t secs, char *format);
220extern void cgit_print_age(time_t t, time_t max_relative, char *format); 222extern void cgit_print_age(time_t t, time_t max_relative, char *format);
221extern void cgit_print_docstart(char *title, struct cacheitem *item); 223extern void cgit_print_docstart(char *title, struct cacheitem *item);
222extern void cgit_print_docend(); 224extern void cgit_print_docend();
223extern void cgit_print_pageheader(char *title, int show_search); 225extern void cgit_print_pageheader(char *title, int show_search);
224extern void cgit_print_snapshot_start(const char *mimetype, 226extern void cgit_print_snapshot_start(const char *mimetype,
225 const char *filename, 227 const char *filename,
226 struct cacheitem *item); 228 struct cacheitem *item);
227 229
228extern void cgit_print_repolist(struct cacheitem *item); 230extern void cgit_print_repolist(struct cacheitem *item);
229extern void cgit_print_summary(); 231extern void cgit_print_summary();
230extern void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, char *path, int pager); 232extern void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, char *path, int pager);
231extern void cgit_print_blob(struct cacheitem *item, const char *hex, char *path); 233extern void cgit_print_blob(struct cacheitem *item, const char *hex, char *path);
232extern void cgit_print_tree(const char *rev, char *path); 234extern void cgit_print_tree(const char *rev, char *path);
233extern void cgit_print_commit(char *hex); 235extern void cgit_print_commit(char *hex);
234extern void cgit_print_diff(const char *new_hex, const char *old_hex); 236extern void cgit_print_diff(const char *new_hex, const char *old_hex);
235extern void cgit_print_snapshot(struct cacheitem *item, const char *hex, 237extern void cgit_print_snapshot(struct cacheitem *item, const char *hex,
236 const char *prefix, const char *filename); 238 const char *prefix, const char *filename);
237extern void cgit_print_snapshot_links(const char *repo, const char *hex); 239extern void cgit_print_snapshot_links(const char *repo, const char *hex);
238 240
239#endif /* CGIT_H */ 241#endif /* CGIT_H */
diff --git a/ui-shared.c b/ui-shared.c
index 1c1415e..3e378a4 100644
--- a/ui-shared.c
+++ b/ui-shared.c
@@ -36,96 +36,120 @@ static long ttl_seconds(long ttl)
36void cgit_print_error(char *msg) 36void cgit_print_error(char *msg)
37{ 37{
38 html("<div class='error'>"); 38 html("<div class='error'>");
39 html_txt(msg); 39 html_txt(msg);
40 html("</div>\n"); 40 html("</div>\n");
41} 41}
42 42
43char *cgit_rooturl() 43char *cgit_rooturl()
44{ 44{
45 if (cgit_virtual_root) 45 if (cgit_virtual_root)
46 return fmt("%s/", cgit_virtual_root); 46 return fmt("%s/", cgit_virtual_root);
47 else 47 else
48 return cgit_script_name; 48 return cgit_script_name;
49} 49}
50 50
51char *cgit_repourl(const char *reponame) 51char *cgit_repourl(const char *reponame)
52{ 52{
53 if (cgit_virtual_root) { 53 if (cgit_virtual_root) {
54 return fmt("%s/%s/", cgit_virtual_root, reponame); 54 return fmt("%s/%s/", cgit_virtual_root, reponame);
55 } else { 55 } else {
56 return fmt("?r=%s", reponame); 56 return fmt("?r=%s", reponame);
57 } 57 }
58} 58}
59 59
60char *cgit_fileurl(const char *reponame, const char *pagename, 60char *cgit_fileurl(const char *reponame, const char *pagename,
61 const char *filename, const char *query) 61 const char *filename, const char *query)
62{ 62{
63 if (cgit_virtual_root) { 63 if (cgit_virtual_root) {
64 if (query) 64 if (query)
65 return fmt("%s/%s/%s/%s?%s", cgit_virtual_root, reponame, 65 return fmt("%s/%s/%s/%s?%s", cgit_virtual_root, reponame,
66 pagename, filename?filename:"", query); 66 pagename, filename?filename:"", query);
67 else 67 else
68 return fmt("%s/%s/%s/", cgit_virtual_root, reponame, 68 return fmt("%s/%s/%s/", cgit_virtual_root, reponame,
69 pagename); 69 pagename);
70 } else { 70 } else {
71 if (query) 71 if (query)
72 return fmt("?r=%s&amp;p=%s&amp;%s", reponame, pagename, query); 72 return fmt("?r=%s&amp;p=%s&amp;%s", reponame, pagename, query);
73 else 73 else
74 return fmt("?r=%s&amp;p=%s", reponame, pagename); 74 return fmt("?r=%s&amp;p=%s", reponame, pagename);
75 } 75 }
76} 76}
77 77
78char *cgit_pageurl(const char *reponame, const char *pagename, 78char *cgit_pageurl(const char *reponame, const char *pagename,
79 const char *query) 79 const char *query)
80{ 80{
81 return cgit_fileurl(reponame,pagename,0,query); 81 return cgit_fileurl(reponame,pagename,0,query);
82} 82}
83 83
84const char *cgit_repobasename(const char *reponame)
85{
86 /* I assume we don't need to store more than one repo basename */
87 static char rvbuf[1024];
88 int p;
89 const char *rv;
90 strncpy(rvbuf,reponame,sizeof(rvbuf));
91 if(rvbuf[sizeof(rvbuf)-1])
92 die("cgit_repobasename: truncated repository name '%s'", reponame);
93 p = strlen(rvbuf)-1;
94 /* strip trailing slashes */
95 while(p && rvbuf[p]=='/') rvbuf[p--]=0;
96 /* strip trailing .git */
97 if(p>=3 && !strncmp(&rvbuf[p-3],".git",4)) {
98 p -= 3; rvbuf[p--] = 0;
99 }
100 /* strip more trailing slashes if any */
101 while( p && rvbuf[p]=='/') rvbuf[p--]=0;
102 /* find last slash in the remaining string */
103 rv = strrchr(rvbuf,'/');
104 if(rv)
105 return ++rv;
106 return rvbuf;
107}
84 108
85char *cgit_currurl() 109char *cgit_currurl()
86{ 110{
87 if (!cgit_virtual_root) 111 if (!cgit_virtual_root)
88 return cgit_script_name; 112 return cgit_script_name;
89 else if (cgit_query_page) 113 else if (cgit_query_page)
90 return fmt("%s/%s/%s/", cgit_virtual_root, cgit_query_repo, cgit_query_page); 114 return fmt("%s/%s/%s/", cgit_virtual_root, cgit_query_repo, cgit_query_page);
91 else if (cgit_query_repo) 115 else if (cgit_query_repo)
92 return fmt("%s/%s/", cgit_virtual_root, cgit_query_repo); 116 return fmt("%s/%s/", cgit_virtual_root, cgit_query_repo);
93 else 117 else
94 return fmt("%s/", cgit_virtual_root); 118 return fmt("%s/", cgit_virtual_root);
95} 119}
96 120
97static char *repolink(char *title, char *class, char *page, char *head, 121static char *repolink(char *title, char *class, char *page, char *head,
98 char *path) 122 char *path)
99{ 123{
100 char *delim = "?"; 124 char *delim = "?";
101 125
102 html("<a"); 126 html("<a");
103 if (title) { 127 if (title) {
104 html(" title='"); 128 html(" title='");
105 html_attr(title); 129 html_attr(title);
106 html("'"); 130 html("'");
107 } 131 }
108 if (class) { 132 if (class) {
109 html(" class='"); 133 html(" class='");
110 html_attr(class); 134 html_attr(class);
111 html("'"); 135 html("'");
112 } 136 }
113 html(" href='"); 137 html(" href='");
114 if (cgit_virtual_root) { 138 if (cgit_virtual_root) {
115 html_attr(cgit_virtual_root); 139 html_attr(cgit_virtual_root);
116 if (cgit_virtual_root[strlen(cgit_virtual_root) - 1] != '/') 140 if (cgit_virtual_root[strlen(cgit_virtual_root) - 1] != '/')
117 html("/"); 141 html("/");
118 html_attr(cgit_repo->url); 142 html_attr(cgit_repo->url);
119 if (cgit_repo->url[strlen(cgit_repo->url) - 1] != '/') 143 if (cgit_repo->url[strlen(cgit_repo->url) - 1] != '/')
120 html("/"); 144 html("/");
121 if (page) { 145 if (page) {
122 html(page); 146 html(page);
123 html("/"); 147 html("/");
124 if (path) 148 if (path)
125 html_attr(path); 149 html_attr(path);
126 } 150 }
127 } else { 151 } else {
128 html(cgit_script_name); 152 html(cgit_script_name);
129 html("?url="); 153 html("?url=");
130 html_attr(cgit_repo->url); 154 html_attr(cgit_repo->url);
131 if (cgit_repo->url[strlen(cgit_repo->url) - 1] != '/') 155 if (cgit_repo->url[strlen(cgit_repo->url) - 1] != '/')