Lars Hjemli | 7937d06 | 2007-10-27 10:36:53 +0200 | [diff] [blame] | 1 | /* ui-refs.c: browse symbolic refs |
| 2 | * |
| 3 | * Copyright (C) 2006 Lars Hjemli |
| 4 | * |
| 5 | * Licensed under GNU General Public License v2 |
| 6 | * (see COPYING for full license text) |
| 7 | */ |
| 8 | |
| 9 | #include "cgit.h" |
John Keeping | 8f20879 | 2013-04-06 11:37:59 +0100 | [diff] [blame] | 10 | #include "ui-refs.h" |
Lars Hjemli | b1f9b9c | 2008-02-23 22:45:33 +0100 | [diff] [blame] | 11 | #include "html.h" |
Lars Hjemli | a4d1ca1 | 2008-03-24 16:50:57 +0100 | [diff] [blame] | 12 | #include "ui-shared.h" |
Lars Hjemli | 7937d06 | 2007-10-27 10:36:53 +0200 | [diff] [blame] | 13 | |
Lars Hjemli | c5984a9 | 2008-03-24 16:38:47 +0100 | [diff] [blame] | 14 | static int cmp_age(int age1, int age2) |
| 15 | { |
| 16 | if (age1 != 0 && age2 != 0) |
| 17 | return age2 - age1; |
| 18 | |
| 19 | if (age1 == 0 && age2 == 0) |
| 20 | return 0; |
| 21 | |
| 22 | if (age1 == 0) |
| 23 | return +1; |
| 24 | |
| 25 | return -1; |
| 26 | } |
| 27 | |
| 28 | static int cmp_ref_name(const void *a, const void *b) |
| 29 | { |
| 30 | struct refinfo *r1 = *(struct refinfo **)a; |
| 31 | struct refinfo *r2 = *(struct refinfo **)b; |
| 32 | |
| 33 | return strcmp(r1->refname, r2->refname); |
| 34 | } |
| 35 | |
| 36 | static int cmp_branch_age(const void *a, const void *b) |
| 37 | { |
| 38 | struct refinfo *r1 = *(struct refinfo **)a; |
| 39 | struct refinfo *r2 = *(struct refinfo **)b; |
| 40 | |
| 41 | return cmp_age(r1->commit->committer_date, r2->commit->committer_date); |
| 42 | } |
| 43 | |
Lars Hjemli | 3687be2 | 2010-08-03 22:06:21 +0200 | [diff] [blame] | 44 | static int get_ref_age(struct refinfo *ref) |
| 45 | { |
| 46 | if (!ref->object) |
| 47 | return 0; |
| 48 | switch (ref->object->type) { |
| 49 | case OBJ_TAG: |
| 50 | return ref->tag ? ref->tag->tagger_date : 0; |
| 51 | case OBJ_COMMIT: |
| 52 | return ref->commit ? ref->commit->committer_date : 0; |
| 53 | } |
| 54 | return 0; |
| 55 | } |
| 56 | |
Lars Hjemli | c5984a9 | 2008-03-24 16:38:47 +0100 | [diff] [blame] | 57 | static int cmp_tag_age(const void *a, const void *b) |
| 58 | { |
| 59 | struct refinfo *r1 = *(struct refinfo **)a; |
| 60 | struct refinfo *r2 = *(struct refinfo **)b; |
| 61 | |
Lars Hjemli | 3687be2 | 2010-08-03 22:06:21 +0200 | [diff] [blame] | 62 | return cmp_age(get_ref_age(r1), get_ref_age(r2)); |
Lars Hjemli | c5984a9 | 2008-03-24 16:38:47 +0100 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | static int print_branch(struct refinfo *ref) |
| 66 | { |
| 67 | struct commitinfo *info = ref->commit; |
| 68 | char *name = (char *)ref->refname; |
| 69 | |
| 70 | if (!info) |
| 71 | return 1; |
| 72 | html("<tr><td>"); |
Lars Hjemli | 0274b57 | 2008-11-29 18:39:41 +0100 | [diff] [blame] | 73 | cgit_log_link(name, NULL, NULL, name, NULL, NULL, 0, NULL, NULL, |
| 74 | ctx.qry.showmsg); |
Lars Hjemli | c5984a9 | 2008-03-24 16:38:47 +0100 | [diff] [blame] | 75 | html("</td><td>"); |
| 76 | |
| 77 | if (ref->object->type == OBJ_COMMIT) { |
Johan Herland | 685872b | 2010-06-10 01:09:35 +0200 | [diff] [blame] | 78 | cgit_commit_link(info->subject, NULL, NULL, name, NULL, NULL, 0); |
Lars Hjemli | c5984a9 | 2008-03-24 16:38:47 +0100 | [diff] [blame] | 79 | html("</td><td>"); |
| 80 | html_txt(info->author); |
Lars Hjemli | 5764fe9 | 2008-04-14 22:13:38 +0200 | [diff] [blame] | 81 | html("</td><td colspan='2'>"); |
| 82 | cgit_print_age(info->commit->date, -1, NULL); |
Lars Hjemli | c5984a9 | 2008-03-24 16:38:47 +0100 | [diff] [blame] | 83 | } else { |
| 84 | html("</td><td></td><td>"); |
| 85 | cgit_object_link(ref->object); |
| 86 | } |
| 87 | html("</td></tr>\n"); |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | static void print_tag_header() |
| 92 | { |
| 93 | html("<tr class='nohover'><th class='left'>Tag</th>" |
Lars Hjemli | 6596268 | 2008-12-01 21:56:07 +0100 | [diff] [blame] | 94 | "<th class='left'>Download</th>" |
Lars Hjemli | c5984a9 | 2008-03-24 16:38:47 +0100 | [diff] [blame] | 95 | "<th class='left'>Author</th>" |
Lars Hjemli | 5764fe9 | 2008-04-14 22:13:38 +0200 | [diff] [blame] | 96 | "<th class='left' colspan='2'>Age</th></tr>\n"); |
Lars Hjemli | c5984a9 | 2008-03-24 16:38:47 +0100 | [diff] [blame] | 97 | } |
| 98 | |
Lars Hjemli | 6596268 | 2008-12-01 21:56:07 +0100 | [diff] [blame] | 99 | static void print_tag_downloads(const struct cgit_repo *repo, const char *ref) |
| 100 | { |
| 101 | const struct cgit_snapshot_format* f; |
| 102 | char *filename; |
| 103 | const char *basename; |
Lukas Fleischer | fab385e | 2013-03-04 13:25:34 +0100 | [diff] [blame] | 104 | int free_ref = 0; |
Lars Hjemli | 6596268 | 2008-12-01 21:56:07 +0100 | [diff] [blame] | 105 | |
| 106 | if (!ref || strlen(ref) < 2) |
| 107 | return; |
| 108 | |
| 109 | basename = cgit_repobasename(repo->url); |
| 110 | if (prefixcmp(ref, basename) != 0) { |
| 111 | if ((ref[0] == 'v' || ref[0] == 'V') && isdigit(ref[1])) |
| 112 | ref++; |
Lukas Fleischer | fab385e | 2013-03-04 13:25:34 +0100 | [diff] [blame] | 113 | if (isdigit(ref[0])) { |
Lars Hjemli | 6596268 | 2008-12-01 21:56:07 +0100 | [diff] [blame] | 114 | ref = xstrdup(fmt("%s-%s", basename, ref)); |
Lukas Fleischer | fab385e | 2013-03-04 13:25:34 +0100 | [diff] [blame] | 115 | free_ref = 1; |
| 116 | } |
Lars Hjemli | 6596268 | 2008-12-01 21:56:07 +0100 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | for (f = cgit_snapshot_formats; f->suffix; f++) { |
| 120 | if (!(repo->snapshots & f->bit)) |
| 121 | continue; |
| 122 | filename = fmt("%s%s", ref, f->suffix); |
| 123 | cgit_snapshot_link(filename, NULL, NULL, NULL, NULL, filename); |
| 124 | html(" "); |
| 125 | } |
Lukas Fleischer | fab385e | 2013-03-04 13:25:34 +0100 | [diff] [blame] | 126 | |
| 127 | if (free_ref) |
| 128 | free((char *)ref); |
Lars Hjemli | 6596268 | 2008-12-01 21:56:07 +0100 | [diff] [blame] | 129 | } |
Lukas Fleischer | 4b4a62d | 2013-04-06 23:39:08 +0200 | [diff] [blame^] | 130 | |
Lars Hjemli | c5984a9 | 2008-03-24 16:38:47 +0100 | [diff] [blame] | 131 | static int print_tag(struct refinfo *ref) |
| 132 | { |
Lukas Fleischer | 4b4a62d | 2013-04-06 23:39:08 +0200 | [diff] [blame^] | 133 | struct tag *tag = NULL; |
| 134 | struct taginfo *info = NULL; |
Lars Hjemli | 24d4bb3 | 2008-10-05 21:19:05 +0200 | [diff] [blame] | 135 | char *name = (char *)ref->refname; |
Lukas Fleischer | 4b4a62d | 2013-04-06 23:39:08 +0200 | [diff] [blame^] | 136 | struct object *obj = ref->object; |
Lars Hjemli | c5984a9 | 2008-03-24 16:38:47 +0100 | [diff] [blame] | 137 | |
Lukas Fleischer | 4b4a62d | 2013-04-06 23:39:08 +0200 | [diff] [blame^] | 138 | if (obj->type == OBJ_TAG) { |
| 139 | tag = (struct tag *)obj; |
| 140 | obj = tag->tagged; |
Lars Hjemli | c5984a9 | 2008-03-24 16:38:47 +0100 | [diff] [blame] | 141 | info = ref->tag; |
| 142 | if (!tag || !info) |
| 143 | return 1; |
Lukas Fleischer | 4b4a62d | 2013-04-06 23:39:08 +0200 | [diff] [blame^] | 144 | } |
| 145 | |
| 146 | html("<tr><td>"); |
| 147 | cgit_tag_link(name, NULL, NULL, ctx.qry.head, name); |
| 148 | html("</td><td>"); |
| 149 | if (ctx.repo->snapshots && (obj->type == OBJ_COMMIT)) |
| 150 | print_tag_downloads(ctx.repo, name); |
| 151 | else |
| 152 | cgit_object_link(obj); |
| 153 | html("</td><td>"); |
| 154 | if (info) { |
Lars Hjemli | c5984a9 | 2008-03-24 16:38:47 +0100 | [diff] [blame] | 155 | if (info->tagger) |
| 156 | html(info->tagger); |
Lukas Fleischer | 4b4a62d | 2013-04-06 23:39:08 +0200 | [diff] [blame^] | 157 | } else if (ref->object->type == OBJ_COMMIT) { |
| 158 | html(ref->commit->author); |
| 159 | } |
| 160 | html("</td><td colspan='2'>"); |
| 161 | if (info) { |
Lars Hjemli | 5764fe9 | 2008-04-14 22:13:38 +0200 | [diff] [blame] | 162 | if (info->tagger_date > 0) |
| 163 | cgit_print_age(info->tagger_date, -1, NULL); |
Lukas Fleischer | 4b4a62d | 2013-04-06 23:39:08 +0200 | [diff] [blame^] | 164 | } else if (ref->object->type == OBJ_COMMIT) { |
| 165 | cgit_print_age(ref->commit->commit->date, -1, NULL); |
Lars Hjemli | c5984a9 | 2008-03-24 16:38:47 +0100 | [diff] [blame] | 166 | } |
Lukas Fleischer | 4b4a62d | 2013-04-06 23:39:08 +0200 | [diff] [blame^] | 167 | html("</td></tr>\n"); |
| 168 | |
Lars Hjemli | c5984a9 | 2008-03-24 16:38:47 +0100 | [diff] [blame] | 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | static void print_refs_link(char *path) |
| 173 | { |
Lukas Fleischer | ef8a97d | 2013-03-05 15:42:14 +0100 | [diff] [blame] | 174 | html("<tr class='nohover'><td colspan='5'>"); |
Lars Hjemli | c5984a9 | 2008-03-24 16:38:47 +0100 | [diff] [blame] | 175 | cgit_refs_link("[...]", NULL, NULL, ctx.qry.head, NULL, path); |
| 176 | html("</td></tr>"); |
| 177 | } |
| 178 | |
| 179 | void cgit_print_branches(int maxcount) |
| 180 | { |
| 181 | struct reflist list; |
| 182 | int i; |
| 183 | |
| 184 | html("<tr class='nohover'><th class='left'>Branch</th>" |
Lars Hjemli | 5764fe9 | 2008-04-14 22:13:38 +0200 | [diff] [blame] | 185 | "<th class='left'>Commit message</th>" |
Lars Hjemli | c5984a9 | 2008-03-24 16:38:47 +0100 | [diff] [blame] | 186 | "<th class='left'>Author</th>" |
Lars Hjemli | 5764fe9 | 2008-04-14 22:13:38 +0200 | [diff] [blame] | 187 | "<th class='left' colspan='2'>Age</th></tr>\n"); |
Lars Hjemli | c5984a9 | 2008-03-24 16:38:47 +0100 | [diff] [blame] | 188 | |
| 189 | list.refs = NULL; |
| 190 | list.alloc = list.count = 0; |
| 191 | for_each_branch_ref(cgit_refs_cb, &list); |
Lars Hjemli | 41934a3 | 2009-11-07 19:10:58 +0100 | [diff] [blame] | 192 | if (ctx.repo->enable_remote_branches) |
| 193 | for_each_remote_ref(cgit_refs_cb, &list); |
Lars Hjemli | c5984a9 | 2008-03-24 16:38:47 +0100 | [diff] [blame] | 194 | |
| 195 | if (maxcount == 0 || maxcount > list.count) |
| 196 | maxcount = list.count; |
| 197 | |
| 198 | if (maxcount < list.count) { |
| 199 | qsort(list.refs, list.count, sizeof(*list.refs), cmp_branch_age); |
| 200 | qsort(list.refs, maxcount, sizeof(*list.refs), cmp_ref_name); |
| 201 | } |
| 202 | |
Jason A. Donenfeld | bdae1d8 | 2013-03-03 23:21:33 -0500 | [diff] [blame] | 203 | for (i = 0; i < maxcount; i++) |
Lars Hjemli | c5984a9 | 2008-03-24 16:38:47 +0100 | [diff] [blame] | 204 | print_branch(list.refs[i]); |
| 205 | |
| 206 | if (maxcount < list.count) |
| 207 | print_refs_link("heads"); |
Lukas Fleischer | 1268afe | 2013-03-04 13:25:33 +0100 | [diff] [blame] | 208 | |
| 209 | cgit_free_reflist_inner(&list); |
Lars Hjemli | c5984a9 | 2008-03-24 16:38:47 +0100 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | void cgit_print_tags(int maxcount) |
| 213 | { |
| 214 | struct reflist list; |
| 215 | int i; |
| 216 | |
Lars Hjemli | c5984a9 | 2008-03-24 16:38:47 +0100 | [diff] [blame] | 217 | list.refs = NULL; |
| 218 | list.alloc = list.count = 0; |
| 219 | for_each_tag_ref(cgit_refs_cb, &list); |
| 220 | if (list.count == 0) |
| 221 | return; |
| 222 | qsort(list.refs, list.count, sizeof(*list.refs), cmp_tag_age); |
| 223 | if (!maxcount) |
| 224 | maxcount = list.count; |
| 225 | else if (maxcount > list.count) |
| 226 | maxcount = list.count; |
| 227 | print_tag_header(); |
Jason A. Donenfeld | bdae1d8 | 2013-03-03 23:21:33 -0500 | [diff] [blame] | 228 | for (i = 0; i < maxcount; i++) |
Lars Hjemli | c5984a9 | 2008-03-24 16:38:47 +0100 | [diff] [blame] | 229 | print_tag(list.refs[i]); |
| 230 | |
| 231 | if (maxcount < list.count) |
| 232 | print_refs_link("tags"); |
Lukas Fleischer | 1268afe | 2013-03-04 13:25:33 +0100 | [diff] [blame] | 233 | |
| 234 | cgit_free_reflist_inner(&list); |
Lars Hjemli | c5984a9 | 2008-03-24 16:38:47 +0100 | [diff] [blame] | 235 | } |
| 236 | |
Lars Hjemli | 7937d06 | 2007-10-27 10:36:53 +0200 | [diff] [blame] | 237 | void cgit_print_refs() |
| 238 | { |
| 239 | |
| 240 | html("<table class='list nowrap'>"); |
| 241 | |
Lars Hjemli | d14d77f | 2008-02-16 11:53:40 +0100 | [diff] [blame] | 242 | if (ctx.qry.path && !strncmp(ctx.qry.path, "heads", 5)) |
Lars Hjemli | 7937d06 | 2007-10-27 10:36:53 +0200 | [diff] [blame] | 243 | cgit_print_branches(0); |
Lars Hjemli | d14d77f | 2008-02-16 11:53:40 +0100 | [diff] [blame] | 244 | else if (ctx.qry.path && !strncmp(ctx.qry.path, "tags", 4)) |
Lars Hjemli | 7937d06 | 2007-10-27 10:36:53 +0200 | [diff] [blame] | 245 | cgit_print_tags(0); |
| 246 | else { |
| 247 | cgit_print_branches(0); |
Lukas Fleischer | ef8a97d | 2013-03-05 15:42:14 +0100 | [diff] [blame] | 248 | html("<tr class='nohover'><td colspan='5'> </td></tr>"); |
Lars Hjemli | 7937d06 | 2007-10-27 10:36:53 +0200 | [diff] [blame] | 249 | cgit_print_tags(0); |
| 250 | } |
Lars Hjemli | 7937d06 | 2007-10-27 10:36:53 +0200 | [diff] [blame] | 251 | html("</table>"); |
| 252 | } |