aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Makefile23
-rw-r--r--cgit.c2
-rw-r--r--cgit.h8
-rw-r--r--cmd.c3
-rw-r--r--parsing.c4
-rw-r--r--shared.c1
-rw-r--r--ui-log.c17
-rw-r--r--ui-refs.c37
-rw-r--r--ui-repolist.c139
-rw-r--r--ui-shared.c17
-rw-r--r--ui-snapshot.c94
-rw-r--r--ui-snapshot.h3
12 files changed, 266 insertions, 82 deletions
diff --git a/Makefile b/Makefile
index 2107610..2e51c31 100644
--- a/Makefile
+++ b/Makefile
@@ -7,6 +7,26 @@ SHA1_HEADER = <openssl/sha.h>
7GIT_VER = 1.6.0.3 7GIT_VER = 1.6.0.3
8GIT_URL = http://www.kernel.org/pub/software/scm/git/git-$(GIT_VER).tar.bz2 8GIT_URL = http://www.kernel.org/pub/software/scm/git/git-$(GIT_VER).tar.bz2
9 9
10# Define NO_STRCASESTR if you don't have strcasestr.
11#
12# Define NEEDS_LIBICONV if linking with libc is not enough (eg. Darwin).
13#
14
15#-include config.mak
16
17#
18# Platform specific tweaks
19#
20
21uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not')
22uname_O := $(shell sh -c 'uname -o 2>/dev/null || echo not')
23uname_R := $(shell sh -c 'uname -r 2>/dev/null || echo not')
24
25ifeq ($(uname_O),Cygwin)
26 NO_STRCASESTR = YesPlease
27 NEEDS_LIBICONV = YesPlease
28endif
29
10# 30#
11# Let the user override the above settings. 31# Let the user override the above settings.
12# 32#
@@ -96,6 +116,9 @@ CFLAGS += -DCGIT_CACHE_ROOT='"$(CACHE_ROOT)"'
96ifdef NO_ICONV 116ifdef NO_ICONV
97 CFLAGS += -DNO_ICONV 117 CFLAGS += -DNO_ICONV
98endif 118endif
119ifdef NO_STRCASESTR
120 CFLAGS += -DNO_STRCASESTR
121endif
99 122
100cgit: $(OBJECTS) libgit 123cgit: $(OBJECTS) libgit
101 $(QUIET_CC)$(CC) $(CFLAGS) $(LDFLAGS) -o cgit $(OBJECTS) $(EXTLIBS) 124 $(QUIET_CC)$(CC) $(CFLAGS) $(LDFLAGS) -o cgit $(OBJECTS) $(EXTLIBS)
diff --git a/cgit.c b/cgit.c
index db5d342..166fbc6 100644
--- a/cgit.c
+++ b/cgit.c
@@ -154,6 +154,8 @@ static void querystring_cb(const char *name, const char *value)
154 ctx.qry.name = xstrdup(value); 154 ctx.qry.name = xstrdup(value);
155 } else if (!strcmp(name, "mimetype")) { 155 } else if (!strcmp(name, "mimetype")) {
156 ctx.qry.mimetype = xstrdup(value); 156 ctx.qry.mimetype = xstrdup(value);
157 } else if (!strcmp(name, "s")){
158 ctx.qry.sort = xstrdup(value);
157 } else if (!strcmp(name, "showmsg")) { 159 } else if (!strcmp(name, "showmsg")) {
158 ctx.qry.showmsg = atoi(value); 160 ctx.qry.showmsg = atoi(value);
159 } 161 }
diff --git a/cgit.h b/cgit.h
index aab898b..cb2f176 100644
--- a/cgit.h
+++ b/cgit.h
@@ -61,6 +61,7 @@ struct cgit_repo {
61 int snapshots; 61 int snapshots;
62 int enable_log_filecount; 62 int enable_log_filecount;
63 int enable_log_linecount; 63 int enable_log_linecount;
64 time_t mtime;
64}; 65};
65 66
66struct cgit_repolist { 67struct cgit_repolist {
@@ -121,6 +122,7 @@ struct cgit_query {
121 char *url; 122 char *url;
122 int ofs; 123 int ofs;
123 int nohead; 124 int nohead;
125 char *sort;
124 int showmsg; 126 int showmsg;
125}; 127};
126 128
@@ -234,11 +236,5 @@ extern const char *cgit_repobasename(const char *reponame);
234 236
235extern int cgit_parse_snapshots_mask(const char *str); 237extern int cgit_parse_snapshots_mask(const char *str);
236 238
237/* libgit.a either links against or compiles its own implementation of
238 * strcasestr(), and we'd like to reuse it. Simply re-declaring it
239 * seems to do the trick.
240 */
241extern char *strcasestr(const char *haystack, const char *needle);
242
243 239
244#endif /* CGIT_H */ 240#endif /* CGIT_H */
diff --git a/cmd.c b/cmd.c
index 5b3c14c..8914fa5 100644
--- a/cmd.c
+++ b/cmd.c
@@ -104,8 +104,7 @@ static void refs_fn(struct cgit_context *ctx)
104 104
105static void snapshot_fn(struct cgit_context *ctx) 105static void snapshot_fn(struct cgit_context *ctx)
106{ 106{
107 cgit_print_snapshot(ctx->qry.head, ctx->qry.sha1, 107 cgit_print_snapshot(ctx->qry.head, ctx->qry.sha1, ctx->qry.path,
108 cgit_repobasename(ctx->repo->url), ctx->qry.path,
109 ctx->repo->snapshots, ctx->qry.nohead); 108 ctx->repo->snapshots, ctx->qry.nohead);
110} 109}
111 110
diff --git a/parsing.c b/parsing.c
index c8f3048..f3f3b15 100644
--- a/parsing.c
+++ b/parsing.c
@@ -96,6 +96,9 @@ char *parse_user(char *t, char **name, char **email, unsigned long *date)
96 return p; 96 return p;
97} 97}
98 98
99#ifdef NO_ICONV
100#define reencode(a, b, c)
101#else
99const char *reencode(char **txt, const char *src_enc, const char *dst_enc) 102const char *reencode(char **txt, const char *src_enc, const char *dst_enc)
100{ 103{
101 char *tmp; 104 char *tmp;
@@ -110,6 +113,7 @@ const char *reencode(char **txt, const char *src_enc, const char *dst_enc)
110 } 113 }
111 return *txt; 114 return *txt;
112} 115}
116#endif
113 117
114struct commitinfo *cgit_parse_commit(struct commit *commit) 118struct commitinfo *cgit_parse_commit(struct commit *commit)
115{ 119{
diff --git a/shared.c b/shared.c
index f5875e4..89d1bab 100644
--- a/shared.c
+++ b/shared.c
@@ -60,6 +60,7 @@ struct cgit_repo *cgit_add_repo(const char *url)
60 ret->enable_log_linecount = ctx.cfg.enable_log_linecount; 60 ret->enable_log_linecount = ctx.cfg.enable_log_linecount;
61 ret->module_link = ctx.cfg.module_link; 61 ret->module_link = ctx.cfg.module_link;
62 ret->readme = NULL; 62 ret->readme = NULL;
63 ret->mtime = -1;
63 return ret; 64 return ret;
64} 65}
65 66
diff --git a/ui-log.c b/ui-log.c
index 00ecd4e..2f90778 100644
--- a/ui-log.c
+++ b/ui-log.c
@@ -78,18 +78,31 @@ void print_commit(struct commit *commit)
78 cgit_free_commitinfo(info); 78 cgit_free_commitinfo(info);
79} 79}
80 80
81static const char *disambiguate_ref(const char *ref)
82{
83 unsigned char sha1[20];
84 const char *longref;
85
86 longref = fmt("refs/heads/%s", ref);
87 if (get_sha1(longref, sha1) == 0)
88 return longref;
89
90 return ref;
91}
81 92
82void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, char *pattern, 93void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, char *pattern,
83 char *path, int pager) 94 char *path, int pager)
84{ 95{
85 struct rev_info rev; 96 struct rev_info rev;
86 struct commit *commit; 97 struct commit *commit;
87 const char *argv[] = {NULL, tip, NULL, NULL, NULL}; 98 const char *argv[] = {NULL, NULL, NULL, NULL, NULL};
88 int argc = 2; 99 int argc = 2;
89 int i, columns = 3; 100 int i, columns = 3;
90 101
91 if (!tip) 102 if (!tip)
92 argv[1] = ctx.qry.head; 103 tip = ctx.qry.head;
104
105 argv[1] = disambiguate_ref(tip);
93 106
94 if (grep && pattern && (!strcmp(grep, "grep") || 107 if (grep && pattern && (!strcmp(grep, "grep") ||
95 !strcmp(grep, "author") || 108 !strcmp(grep, "author") ||
diff --git a/ui-refs.c b/ui-refs.c
index 7eb16d5..d61ee7c 100644
--- a/ui-refs.c
+++ b/ui-refs.c
@@ -79,12 +79,37 @@ static int print_branch(struct refinfo *ref)
79static void print_tag_header() 79static void print_tag_header()
80{ 80{
81 html("<tr class='nohover'><th class='left'>Tag</th>" 81 html("<tr class='nohover'><th class='left'>Tag</th>"
82 "<th class='left'>Reference</th>" 82 "<th class='left'>Download</th>"
83 "<th class='left'>Author</th>" 83 "<th class='left'>Author</th>"
84 "<th class='left' colspan='2'>Age</th></tr>\n"); 84 "<th class='left' colspan='2'>Age</th></tr>\n");
85 header = 1; 85 header = 1;
86} 86}
87 87
88static void print_tag_downloads(const struct cgit_repo *repo, const char *ref)
89{
90 const struct cgit_snapshot_format* f;
91 char *filename;
92 const char *basename;
93
94 if (!ref || strlen(ref) < 2)
95 return;
96
97 basename = cgit_repobasename(repo->url);
98 if (prefixcmp(ref, basename) != 0) {
99 if ((ref[0] == 'v' || ref[0] == 'V') && isdigit(ref[1]))
100 ref++;
101 if (isdigit(ref[0]))
102 ref = xstrdup(fmt("%s-%s", basename, ref));
103 }
104
105 for (f = cgit_snapshot_formats; f->suffix; f++) {
106 if (!(repo->snapshots & f->bit))
107 continue;
108 filename = fmt("%s%s", ref, f->suffix);
109 cgit_snapshot_link(filename, NULL, NULL, NULL, NULL, filename);
110 html("&nbsp;&nbsp;");
111 }
112}
88static int print_tag(struct refinfo *ref) 113static int print_tag(struct refinfo *ref)
89{ 114{
90 struct tag *tag; 115 struct tag *tag;
@@ -99,7 +124,10 @@ static int print_tag(struct refinfo *ref)
99 html("<tr><td>"); 124 html("<tr><td>");
100 cgit_tag_link(name, NULL, NULL, ctx.qry.head, name); 125 cgit_tag_link(name, NULL, NULL, ctx.qry.head, name);
101 html("</td><td>"); 126 html("</td><td>");
102 cgit_object_link(tag->tagged); 127 if (ctx.repo->snapshots && (tag->tagged->type == OBJ_COMMIT))
128 print_tag_downloads(ctx.repo, name);
129 else
130 cgit_object_link(tag->tagged);
103 html("</td><td>"); 131 html("</td><td>");
104 if (info->tagger) 132 if (info->tagger)
105 html(info->tagger); 133 html(info->tagger);
@@ -113,7 +141,10 @@ static int print_tag(struct refinfo *ref)
113 html("<tr><td>"); 141 html("<tr><td>");
114 html_txt(name); 142 html_txt(name);
115 html("</td><td>"); 143 html("</td><td>");
116 cgit_object_link(ref->object); 144 if (ctx.repo->snapshots && (tag->tagged->type == OBJ_COMMIT))
145 print_tag_downloads(ctx.repo, name);
146 else
147 cgit_object_link(ref->object);
117 html("</td></tr>\n"); 148 html("</td></tr>\n");
118 } 149 }
119 return 0; 150 return 0;
diff --git a/ui-repolist.c b/ui-repolist.c
index 5833140..2c13d50 100644
--- a/ui-repolist.c
+++ b/ui-repolist.c
@@ -6,6 +6,10 @@
6 * (see COPYING for full license text) 6 * (see COPYING for full license text)
7 */ 7 */
8 8
9/* This is needed for strcasestr to be defined by <string.h> */
10#define _GNU_SOURCE 1
11#include <string.h>
12
9#include <time.h> 13#include <time.h>
10 14
11#include "cgit.h" 15#include "cgit.h"
@@ -28,21 +32,38 @@ time_t read_agefile(char *path)
28 return 0; 32 return 0;
29} 33}
30 34
31static void print_modtime(struct cgit_repo *repo) 35static int get_repo_modtime(const struct cgit_repo *repo, time_t *mtime)
32{ 36{
33 char *path; 37 char *path;
34 struct stat s; 38 struct stat s;
39 struct cgit_repo *r = (struct cgit_repo *)repo;
35 40
41 if (repo->mtime != -1) {
42 *mtime = repo->mtime;
43 return 1;
44 }
36 path = fmt("%s/%s", repo->path, ctx.cfg.agefile); 45 path = fmt("%s/%s", repo->path, ctx.cfg.agefile);
37 if (stat(path, &s) == 0) { 46 if (stat(path, &s) == 0) {
38 cgit_print_age(read_agefile(path), -1, NULL); 47 *mtime = read_agefile(path);
39 return; 48 r->mtime = *mtime;
49 return 1;
40 } 50 }
41 51
42 path = fmt("%s/refs/heads/%s", repo->path, repo->defbranch); 52 path = fmt("%s/refs/heads/%s", repo->path, repo->defbranch);
43 if (stat(path, &s) != 0) 53 if (stat(path, &s) == 0)
44 return; 54 *mtime = s.st_mtime;
45 cgit_print_age(s.st_mtime, -1, NULL); 55 else
56 *mtime = 0;
57
58 r->mtime = *mtime;
59 return (r->mtime != 0);
60}
61
62static void print_modtime(struct cgit_repo *repo)
63{
64 time_t t;
65 if (get_repo_modtime(repo, &t))
66 cgit_print_age(t, -1, NULL);
46} 67}
47 68
48int is_match(struct cgit_repo *repo) 69int is_match(struct cgit_repo *repo)
@@ -69,13 +90,23 @@ int is_in_url(struct cgit_repo *repo)
69 return 0; 90 return 0;
70} 91}
71 92
93void print_sort_header(const char *title, const char *sort)
94{
95 htmlf("<th class='left'><a href='./?s=%s", sort);
96 if (ctx.qry.search) {
97 html("&q=");
98 html_url_arg(ctx.qry.search);
99 }
100 htmlf("'>%s</a></th>", title);
101}
102
72void print_header(int columns) 103void print_header(int columns)
73{ 104{
74 html("<tr class='nohover'>" 105 html("<tr class='nohover'>");
75 "<th class='left'>Name</th>" 106 print_sort_header("Name", "name");
76 "<th class='left'>Description</th>" 107 print_sort_header("Description", "desc");
77 "<th class='left'>Owner</th>" 108 print_sort_header("Owner", "owner");
78 "<th class='left'>Idle</th>"); 109 print_sort_header("Idle", "idle");
79 if (ctx.cfg.enable_index_links) 110 if (ctx.cfg.enable_index_links)
80 html("<th class='left'>Links</th>"); 111 html("<th class='left'>Links</th>");
81 html("</tr>\n"); 112 html("</tr>\n");
@@ -92,10 +123,86 @@ void print_pager(int items, int pagelen, char *search)
92 html("</div>"); 123 html("</div>");
93} 124}
94 125
126static int cmp(const char *s1, const char *s2)
127{
128 if (s1 && s2)
129 return strcmp(s1, s2);
130 if (s1 && !s2)
131 return -1;
132 if (s2 && !s1)
133 return 1;
134 return 0;
135}
136
137static int sort_name(const void *a, const void *b)
138{
139 const struct cgit_repo *r1 = a;
140 const struct cgit_repo *r2 = b;
141
142 return cmp(r1->name, r2->name);
143}
144
145static int sort_desc(const void *a, const void *b)
146{
147 const struct cgit_repo *r1 = a;
148 const struct cgit_repo *r2 = b;
149
150 return cmp(r1->desc, r2->desc);
151}
152
153static int sort_owner(const void *a, const void *b)
154{
155 const struct cgit_repo *r1 = a;
156 const struct cgit_repo *r2 = b;
157
158 return cmp(r1->owner, r2->owner);
159}
160
161static int sort_idle(const void *a, const void *b)
162{
163 const struct cgit_repo *r1 = a;
164 const struct cgit_repo *r2 = b;
165 time_t t1, t2;
166
167 t1 = t2 = 0;
168 get_repo_modtime(r1, &t1);
169 get_repo_modtime(r2, &t2);
170 return t2 - t1;
171}
172
173struct sortcolumn {
174 const char *name;
175 int (*fn)(const void *a, const void *b);
176};
177
178struct sortcolumn sortcolumn[] = {
179 {"name", sort_name},
180 {"desc", sort_desc},
181 {"owner", sort_owner},
182 {"idle", sort_idle},
183 {NULL, NULL}
184};
185
186int sort_repolist(char *field)
187{
188 struct sortcolumn *column;
189
190 for (column = &sortcolumn[0]; column->name; column++) {
191 if (strcmp(field, column->name))
192 continue;
193 qsort(cgit_repolist.repos, cgit_repolist.count,
194 sizeof(struct cgit_repo), column->fn);
195 return 1;
196 }
197 return 0;
198}
199
200
95void cgit_print_repolist() 201void cgit_print_repolist()
96{ 202{
97 int i, columns = 4, hits = 0, header = 0; 203 int i, columns = 4, hits = 0, header = 0;
98 char *last_group = NULL; 204 char *last_group = NULL;
205 int sorted = 0;
99 206
100 if (ctx.cfg.enable_index_links) 207 if (ctx.cfg.enable_index_links)
101 columns++; 208 columns++;
@@ -108,6 +215,9 @@ void cgit_print_repolist()
108 if (ctx.cfg.index_header) 215 if (ctx.cfg.index_header)
109 html_include(ctx.cfg.index_header); 216 html_include(ctx.cfg.index_header);
110 217
218 if(ctx.qry.sort)
219 sorted = sort_repolist(ctx.qry.sort);
220
111 html("<table summary='repository list' class='list nowrap'>"); 221 html("<table summary='repository list' class='list nowrap'>");
112 for (i=0; i<cgit_repolist.count; i++) { 222 for (i=0; i<cgit_repolist.count; i++) {
113 ctx.repo = &cgit_repolist.repos[i]; 223 ctx.repo = &cgit_repolist.repos[i];
@@ -120,10 +230,11 @@ void cgit_print_repolist()
120 continue; 230 continue;
121 if (!header++) 231 if (!header++)
122 print_header(columns); 232 print_header(columns);
123 if ((last_group == NULL && ctx.repo->group != NULL) || 233 if (!sorted &&
234 ((last_group == NULL && ctx.repo->group != NULL) ||
124 (last_group != NULL && ctx.repo->group == NULL) || 235 (last_group != NULL && ctx.repo->group == NULL) ||
125 (last_group != NULL && ctx.repo->group != NULL && 236 (last_group != NULL && ctx.repo->group != NULL &&
126 strcmp(ctx.repo->group, last_group))) { 237 strcmp(ctx.repo->group, last_group)))) {
127 htmlf("<tr class='nohover'><td colspan='%d' class='repogroup'>", 238 htmlf("<tr class='nohover'><td colspan='%d' class='repogroup'>",
128 columns); 239 columns);
129 html_txt(ctx.repo->group); 240 html_txt(ctx.repo->group);
@@ -131,7 +242,7 @@ void cgit_print_repolist()
131 last_group = ctx.repo->group; 242 last_group = ctx.repo->group;
132 } 243 }
133 htmlf("<tr><td class='%s'>", 244 htmlf("<tr><td class='%s'>",
134 ctx.repo->group ? "sublevel-repo" : "toplevel-repo"); 245 !sorted && ctx.repo->group ? "sublevel-repo" : "toplevel-repo");
135 cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL); 246 cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
136 html("</td><td>"); 247 html("</td><td>");
137 html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL); 248 html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL);
diff --git a/ui-shared.c b/ui-shared.c
index dc39e64..95dfeb4 100644
--- a/ui-shared.c
+++ b/ui-shared.c
@@ -371,11 +371,14 @@ void cgit_patch_link(char *name, char *title, char *class, char *head,
371 371
372void cgit_object_link(struct object *obj) 372void cgit_object_link(struct object *obj)
373{ 373{
374 char *page, *rev, *name; 374 char *page, *shortrev, *fullrev, *name;
375 375
376 fullrev = sha1_to_hex(obj->sha1);
377 shortrev = xstrdup(fullrev);
378 shortrev[10] = '\0';
376 if (obj->type == OBJ_COMMIT) { 379 if (obj->type == OBJ_COMMIT) {
377 cgit_commit_link(fmt("commit %s", sha1_to_hex(obj->sha1)), NULL, NULL, 380 cgit_commit_link(fmt("commit %s...", shortrev), NULL, NULL,
378 ctx.qry.head, sha1_to_hex(obj->sha1)); 381 ctx.qry.head, fullrev);
379 return; 382 return;
380 } else if (obj->type == OBJ_TREE) 383 } else if (obj->type == OBJ_TREE)
381 page = "tree"; 384 page = "tree";
@@ -383,9 +386,8 @@ void cgit_object_link(struct object *obj)
383 page = "tag"; 386 page = "tag";
384 else 387 else
385 page = "blob"; 388 page = "blob";
386 rev = sha1_to_hex(obj->sha1); 389 name = fmt("%s %s...", typename(obj->type), shortrev);
387 name = fmt("%s %s", typename(obj->type), rev); 390 reporevlink(page, name, NULL, NULL, ctx.qry.head, fullrev, NULL);
388 reporevlink(page, name, NULL, NULL, ctx.qry.head, rev, NULL);
389} 391}
390 392
391void cgit_print_date(time_t secs, char *format, int local_time) 393void cgit_print_date(time_t secs, char *format, int local_time)
@@ -715,8 +717,7 @@ void cgit_print_snapshot_links(const char *repo, const char *head,
715 continue; 717 continue;
716 filename = fmt("%s-%s%s", cgit_repobasename(repo), hex, 718 filename = fmt("%s-%s%s", cgit_repobasename(repo), hex,
717 f->suffix); 719 f->suffix);
718 cgit_snapshot_link(filename, NULL, NULL, (char *)head, 720 cgit_snapshot_link(filename, NULL, NULL, NULL, NULL, filename);
719 (char *)hex, filename);
720 html("<br/>"); 721 html("<br/>");
721 } 722 }
722} 723}
diff --git a/ui-snapshot.c b/ui-snapshot.c
index 9c4d086..6f09151 100644
--- a/ui-snapshot.c
+++ b/ui-snapshot.c
@@ -114,58 +114,53 @@ static int make_snapshot(const struct cgit_snapshot_format *format,
114 return 0; 114 return 0;
115} 115}
116 116
117char *dwim_filename = NULL; 117/* Try to guess the requested revision from the requested snapshot name.
118const char *dwim_refname = NULL; 118 * First the format extension is stripped, e.g. "cgit-0.7.2.tar.gz" become
119 119 * "cgit-0.7.2". If this is a valid commit object name we've got a winner.
120static int ref_cb(const char *refname, const unsigned char *sha1, int flags, 120 * Otherwise, if the snapshot name has a prefix matching the result from
121 void *cb_data) 121 * repo_basename(), we strip the basename and any following '-' and '_'
122{ 122 * characters ("cgit-0.7.2" -> "0.7.2") and check the resulting name once
123 const char *r = refname; 123 * more. If this still isn't a valid commit object name, we check if pre-
124 while (r && *r) { 124 * pending a 'v' to the remaining snapshot name ("0.7.2" -> "v0.7.2") gives
125 fprintf(stderr, " cmp %s with %s:", dwim_filename, r); 125 * us something valid.
126 if (!strcmp(dwim_filename, r)) {
127 fprintf(stderr, "MATCH!\n");
128 dwim_refname = refname;
129 return 1;
130 }
131 fprintf(stderr, "no match\n");
132 if (isdigit(*r))
133 break;
134 r++;
135 }
136 return 0;
137}
138
139/* Try to guess the requested revision by combining repo name and tag name
140 * and comparing this to the requested snapshot name. E.g. the requested
141 * snapshot is "cgit-0.7.2.tar.gz" while repo name is "cgit" and tag name
142 * is "v0.7.2". First, the reponame is stripped off, leaving "-0.7.2.tar.gz".
143 * Next, any '-' and '_' characters are stripped, leaving "0.7.2.tar.gz".
144 * Finally, the requested format suffix is removed and we end up with "0.7.2".
145 * Then we test each tag against this dwimmed filename, and for each tag
146 * we even try to remove any leading characters which are non-digits. I.e.
147 * we first compare with "v0.7.2", then with "0.7.2" and we've got a match.
148 */ 126 */
149static const char *get_ref_from_filename(const char *url, const char *filename, 127static const char *get_ref_from_filename(const char *url, const char *filename,
150 const struct cgit_snapshot_format *fmt) 128 const struct cgit_snapshot_format *format)
151{ 129{
152 const char *reponame = cgit_repobasename(url); 130 const char *reponame;
153 fprintf(stderr, "reponame=%s, filename=%s\n", reponame, filename); 131 unsigned char sha1[20];
154 if (prefixcmp(filename, reponame)) 132 char *snapshot;
155 return NULL; 133
156 filename += strlen(reponame); 134 snapshot = xstrdup(filename);
157 while (filename && (*filename == '-' || *filename == '_')) 135 snapshot[strlen(snapshot) - strlen(format->suffix)] = '\0';
158 filename++; 136 fprintf(stderr, "snapshot=%s\n", snapshot);
159 dwim_filename = xstrdup(filename); 137
160 dwim_filename[strlen(filename) - strlen(fmt->suffix)] = '\0'; 138 if (get_sha1(snapshot, sha1) == 0)
161 for_each_tag_ref(ref_cb, NULL); 139 return snapshot;
162 return dwim_refname; 140
141 reponame = cgit_repobasename(url);
142 fprintf(stderr, "reponame=%s\n", reponame);
143 if (prefixcmp(snapshot, reponame) == 0) {
144 snapshot += strlen(reponame);
145 while (snapshot && (*snapshot == '-' || *snapshot == '_'))
146 snapshot++;
147 }
148
149 if (get_sha1(snapshot, sha1) == 0)
150 return snapshot;
151
152 snapshot = fmt("v%s", snapshot);
153 if (get_sha1(snapshot, sha1) == 0)
154 return snapshot;
155
156 return NULL;
163} 157}
164 158
165void cgit_print_snapshot(const char *head, const char *hex, const char *prefix, 159void cgit_print_snapshot(const char *head, const char *hex,
166 const char *filename, int snapshots, int dwim) 160 const char *filename, int snapshots, int dwim)
167{ 161{
168 const struct cgit_snapshot_format* f; 162 const struct cgit_snapshot_format* f;
163 char *prefix = NULL;
169 164
170 f = get_format(filename); 165 f = get_format(filename);
171 if (!f) { 166 if (!f) {
@@ -178,11 +173,20 @@ void cgit_print_snapshot(const char *head, const char *hex, const char *prefix,
178 return; 173 return;
179 } 174 }
180 175
181 if (!hex && dwim) 176 if (!hex && dwim) {
182 hex = get_ref_from_filename(ctx.repo->url, filename, f); 177 hex = get_ref_from_filename(ctx.repo->url, filename, f);
178 if (hex != NULL) {
179 prefix = xstrdup(filename);
180 prefix[strlen(filename) - strlen(f->suffix)] = '\0';
181 }
182 }
183 183
184 if (!hex) 184 if (!hex)
185 hex = head; 185 hex = head;
186 186
187 if (!prefix)
188 prefix = xstrdup(cgit_repobasename(ctx.repo->url));
189
187 make_snapshot(f, hex, prefix, filename); 190 make_snapshot(f, hex, prefix, filename);
191 free(prefix);
188} 192}
diff --git a/ui-snapshot.h b/ui-snapshot.h
index 3540303..b6ede52 100644
--- a/ui-snapshot.h
+++ b/ui-snapshot.h
@@ -2,7 +2,6 @@
2#define UI_SNAPSHOT_H 2#define UI_SNAPSHOT_H
3 3
4extern void cgit_print_snapshot(const char *head, const char *hex, 4extern void cgit_print_snapshot(const char *head, const char *hex,
5 const char *prefix, const char *filename, 5 const char *filename, int snapshot, int dwim);
6 int snapshot, int dwim);
7 6
8#endif /* UI_SNAPSHOT_H */ 7#endif /* UI_SNAPSHOT_H */