Merge branch 'snapshot-fixes'

This commit is contained in:
Lars Hjemli 2008-12-06 11:34:48 +01:00
commit e550440233
5 changed files with 94 additions and 60 deletions

3
cmd.c
View File

@ -104,8 +104,7 @@ static void refs_fn(struct cgit_context *ctx)
static void snapshot_fn(struct cgit_context *ctx) static void snapshot_fn(struct cgit_context *ctx)
{ {
cgit_print_snapshot(ctx->qry.head, ctx->qry.sha1, cgit_print_snapshot(ctx->qry.head, ctx->qry.sha1, ctx->qry.path,
cgit_repobasename(ctx->repo->url), ctx->qry.path,
ctx->repo->snapshots, ctx->qry.nohead); ctx->repo->snapshots, ctx->qry.nohead);
} }

View File

@ -78,12 +78,37 @@ static int print_branch(struct refinfo *ref)
static void print_tag_header() static void print_tag_header()
{ {
html("<tr class='nohover'><th class='left'>Tag</th>" html("<tr class='nohover'><th class='left'>Tag</th>"
"<th class='left'>Reference</th>" "<th class='left'>Download</th>"
"<th class='left'>Author</th>" "<th class='left'>Author</th>"
"<th class='left' colspan='2'>Age</th></tr>\n"); "<th class='left' colspan='2'>Age</th></tr>\n");
header = 1; header = 1;
} }
static void print_tag_downloads(const struct cgit_repo *repo, const char *ref)
{
const struct cgit_snapshot_format* f;
char *filename;
const char *basename;
if (!ref || strlen(ref) < 2)
return;
basename = cgit_repobasename(repo->url);
if (prefixcmp(ref, basename) != 0) {
if ((ref[0] == 'v' || ref[0] == 'V') && isdigit(ref[1]))
ref++;
if (isdigit(ref[0]))
ref = xstrdup(fmt("%s-%s", basename, ref));
}
for (f = cgit_snapshot_formats; f->suffix; f++) {
if (!(repo->snapshots & f->bit))
continue;
filename = fmt("%s%s", ref, f->suffix);
cgit_snapshot_link(filename, NULL, NULL, NULL, NULL, filename);
html("&nbsp;&nbsp;");
}
}
static int print_tag(struct refinfo *ref) static int print_tag(struct refinfo *ref)
{ {
struct tag *tag; struct tag *tag;
@ -98,6 +123,9 @@ static int print_tag(struct refinfo *ref)
html("<tr><td>"); html("<tr><td>");
cgit_tag_link(name, NULL, NULL, ctx.qry.head, name); cgit_tag_link(name, NULL, NULL, ctx.qry.head, name);
html("</td><td>"); html("</td><td>");
if (ctx.repo->snapshots && (tag->tagged->type == OBJ_COMMIT))
print_tag_downloads(ctx.repo, name);
else
cgit_object_link(tag->tagged); cgit_object_link(tag->tagged);
html("</td><td>"); html("</td><td>");
if (info->tagger) if (info->tagger)
@ -112,6 +140,9 @@ static int print_tag(struct refinfo *ref)
html("<tr><td>"); html("<tr><td>");
html_txt(name); html_txt(name);
html("</td><td>"); html("</td><td>");
if (ctx.repo->snapshots && (tag->tagged->type == OBJ_COMMIT))
print_tag_downloads(ctx.repo, name);
else
cgit_object_link(ref->object); cgit_object_link(ref->object);
html("</td></tr>\n"); html("</td></tr>\n");
} }

View File

@ -365,11 +365,14 @@ void cgit_patch_link(char *name, char *title, char *class, char *head,
void cgit_object_link(struct object *obj) void cgit_object_link(struct object *obj)
{ {
char *page, *rev, *name; char *page, *shortrev, *fullrev, *name;
fullrev = sha1_to_hex(obj->sha1);
shortrev = xstrdup(fullrev);
shortrev[10] = '\0';
if (obj->type == OBJ_COMMIT) { if (obj->type == OBJ_COMMIT) {
cgit_commit_link(fmt("commit %s", sha1_to_hex(obj->sha1)), NULL, NULL, cgit_commit_link(fmt("commit %s...", shortrev), NULL, NULL,
ctx.qry.head, sha1_to_hex(obj->sha1)); ctx.qry.head, fullrev);
return; return;
} else if (obj->type == OBJ_TREE) } else if (obj->type == OBJ_TREE)
page = "tree"; page = "tree";
@ -377,9 +380,8 @@ void cgit_object_link(struct object *obj)
page = "tag"; page = "tag";
else else
page = "blob"; page = "blob";
rev = sha1_to_hex(obj->sha1); name = fmt("%s %s...", typename(obj->type), shortrev);
name = fmt("%s %s", typename(obj->type), rev); reporevlink(page, name, NULL, NULL, ctx.qry.head, fullrev, NULL);
reporevlink(page, name, NULL, NULL, ctx.qry.head, rev, NULL);
} }
void cgit_print_date(time_t secs, char *format, int local_time) void cgit_print_date(time_t secs, char *format, int local_time)
@ -707,8 +709,7 @@ void cgit_print_snapshot_links(const char *repo, const char *head,
continue; continue;
filename = fmt("%s-%s%s", cgit_repobasename(repo), hex, filename = fmt("%s-%s%s", cgit_repobasename(repo), hex,
f->suffix); f->suffix);
cgit_snapshot_link(filename, NULL, NULL, (char *)head, cgit_snapshot_link(filename, NULL, NULL, NULL, NULL, filename);
(char *)hex, filename);
html("<br/>"); html("<br/>");
} }
} }

View File

@ -114,58 +114,53 @@ static int make_snapshot(const struct cgit_snapshot_format *format,
return 0; return 0;
} }
char *dwim_filename = NULL; /* Try to guess the requested revision from the requested snapshot name.
const char *dwim_refname = NULL; * First the format extension is stripped, e.g. "cgit-0.7.2.tar.gz" become
* "cgit-0.7.2". If this is a valid commit object name we've got a winner.
static int ref_cb(const char *refname, const unsigned char *sha1, int flags, * Otherwise, if the snapshot name has a prefix matching the result from
void *cb_data) * repo_basename(), we strip the basename and any following '-' and '_'
{ * characters ("cgit-0.7.2" -> "0.7.2") and check the resulting name once
const char *r = refname; * more. If this still isn't a valid commit object name, we check if pre-
while (r && *r) { * pending a 'v' to the remaining snapshot name ("0.7.2" -> "v0.7.2") gives
fprintf(stderr, " cmp %s with %s:", dwim_filename, r); * us something valid.
if (!strcmp(dwim_filename, r)) {
fprintf(stderr, "MATCH!\n");
dwim_refname = refname;
return 1;
}
fprintf(stderr, "no match\n");
if (isdigit(*r))
break;
r++;
}
return 0;
}
/* Try to guess the requested revision by combining repo name and tag name
* and comparing this to the requested snapshot name. E.g. the requested
* snapshot is "cgit-0.7.2.tar.gz" while repo name is "cgit" and tag name
* is "v0.7.2". First, the reponame is stripped off, leaving "-0.7.2.tar.gz".
* Next, any '-' and '_' characters are stripped, leaving "0.7.2.tar.gz".
* Finally, the requested format suffix is removed and we end up with "0.7.2".
* Then we test each tag against this dwimmed filename, and for each tag
* we even try to remove any leading characters which are non-digits. I.e.
* we first compare with "v0.7.2", then with "0.7.2" and we've got a match.
*/ */
static const char *get_ref_from_filename(const char *url, const char *filename, static const char *get_ref_from_filename(const char *url, const char *filename,
const struct cgit_snapshot_format *fmt) const struct cgit_snapshot_format *format)
{ {
const char *reponame = cgit_repobasename(url); const char *reponame;
fprintf(stderr, "reponame=%s, filename=%s\n", reponame, filename); unsigned char sha1[20];
if (prefixcmp(filename, reponame)) char *snapshot;
return NULL;
filename += strlen(reponame); snapshot = xstrdup(filename);
while (filename && (*filename == '-' || *filename == '_')) snapshot[strlen(snapshot) - strlen(format->suffix)] = '\0';
filename++; fprintf(stderr, "snapshot=%s\n", snapshot);
dwim_filename = xstrdup(filename);
dwim_filename[strlen(filename) - strlen(fmt->suffix)] = '\0'; if (get_sha1(snapshot, sha1) == 0)
for_each_tag_ref(ref_cb, NULL); return snapshot;
return dwim_refname;
reponame = cgit_repobasename(url);
fprintf(stderr, "reponame=%s\n", reponame);
if (prefixcmp(snapshot, reponame) == 0) {
snapshot += strlen(reponame);
while (snapshot && (*snapshot == '-' || *snapshot == '_'))
snapshot++;
} }
void cgit_print_snapshot(const char *head, const char *hex, const char *prefix, if (get_sha1(snapshot, sha1) == 0)
return snapshot;
snapshot = fmt("v%s", snapshot);
if (get_sha1(snapshot, sha1) == 0)
return snapshot;
return NULL;
}
void cgit_print_snapshot(const char *head, const char *hex,
const char *filename, int snapshots, int dwim) const char *filename, int snapshots, int dwim)
{ {
const struct cgit_snapshot_format* f; const struct cgit_snapshot_format* f;
char *prefix = NULL;
f = get_format(filename); f = get_format(filename);
if (!f) { if (!f) {
@ -178,11 +173,20 @@ void cgit_print_snapshot(const char *head, const char *hex, const char *prefix,
return; return;
} }
if (!hex && dwim) if (!hex && dwim) {
hex = get_ref_from_filename(ctx.repo->url, filename, f); hex = get_ref_from_filename(ctx.repo->url, filename, f);
if (hex != NULL) {
prefix = xstrdup(filename);
prefix[strlen(filename) - strlen(f->suffix)] = '\0';
}
}
if (!hex) if (!hex)
hex = head; hex = head;
if (!prefix)
prefix = xstrdup(cgit_repobasename(ctx.repo->url));
make_snapshot(f, hex, prefix, filename); make_snapshot(f, hex, prefix, filename);
free(prefix);
} }

View File

@ -2,7 +2,6 @@
#define UI_SNAPSHOT_H #define UI_SNAPSHOT_H
extern void cgit_print_snapshot(const char *head, const char *hex, extern void cgit_print_snapshot(const char *head, const char *hex,
const char *prefix, const char *filename, const char *filename, int snapshot, int dwim);
int snapshot, int dwim);
#endif /* UI_SNAPSHOT_H */ #endif /* UI_SNAPSHOT_H */