1 /* ui-refs.c: browse symbolic refs
3 * Copyright (C) 2006 Lars Hjemli
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
12 #include "ui-shared.h"
16 static int cmp_age(int age1, int age2)
18 if (age1 != 0 && age2 != 0)
21 if (age1 == 0 && age2 == 0)
30 static int cmp_ref_name(const void *a, const void *b)
32 struct refinfo *r1 = *(struct refinfo **)a;
33 struct refinfo *r2 = *(struct refinfo **)b;
35 return strcmp(r1->refname, r2->refname);
38 static int cmp_branch_age(const void *a, const void *b)
40 struct refinfo *r1 = *(struct refinfo **)a;
41 struct refinfo *r2 = *(struct refinfo **)b;
43 return cmp_age(r1->commit->committer_date, r2->commit->committer_date);
46 static int get_ref_age(struct refinfo *ref)
50 switch (ref->object->type) {
52 return ref->tag ? ref->tag->tagger_date : 0;
54 return ref->commit ? ref->commit->committer_date : 0;
59 static int cmp_tag_age(const void *a, const void *b)
61 struct refinfo *r1 = *(struct refinfo **)a;
62 struct refinfo *r2 = *(struct refinfo **)b;
64 return cmp_age(get_ref_age(r1), get_ref_age(r2));
67 static int print_branch(struct refinfo *ref)
69 struct commitinfo *info = ref->commit;
70 char *name = (char *)ref->refname;
75 cgit_log_link(name, NULL, NULL, name, NULL, NULL, 0, NULL, NULL,
79 if (ref->object->type == OBJ_COMMIT) {
80 cgit_commit_link(info->subject, NULL, NULL, name, NULL, NULL, 0);
82 html_txt(info->author);
83 html("</td><td colspan='2'>");
84 cgit_print_age(info->commit->date, -1, NULL);
86 html("</td><td></td><td>");
87 cgit_object_link(ref->object);
93 static void print_tag_header()
95 html("<tr class='nohover'><th class='left'>Tag</th>"
96 "<th class='left'>Download</th>"
97 "<th class='left'>Author</th>"
98 "<th class='left' colspan='2'>Age</th></tr>\n");
102 static void print_tag_downloads(const struct cgit_repo *repo, const char *ref)
104 const struct cgit_snapshot_format* f;
106 const char *basename;
109 if (!ref || strlen(ref) < 2)
112 basename = cgit_repobasename(repo->url);
113 if (prefixcmp(ref, basename) != 0) {
114 if ((ref[0] == 'v' || ref[0] == 'V') && isdigit(ref[1]))
116 if (isdigit(ref[0])) {
117 ref = xstrdup(fmt("%s-%s", basename, ref));
122 for (f = cgit_snapshot_formats; f->suffix; f++) {
123 if (!(repo->snapshots & f->bit))
125 filename = fmt("%s%s", ref, f->suffix);
126 cgit_snapshot_link(filename, NULL, NULL, NULL, NULL, filename);
127 html(" ");
133 static int print_tag(struct refinfo *ref)
136 struct taginfo *info;
137 char *name = (char *)ref->refname;
139 if (ref->object->type == OBJ_TAG) {
140 tag = (struct tag *)ref->object;
145 cgit_tag_link(name, NULL, NULL, ctx.qry.head, name);
147 if (ctx.repo->snapshots && (tag->tagged->type == OBJ_COMMIT))
148 print_tag_downloads(ctx.repo, name);
150 cgit_object_link(tag->tagged);
154 html("</td><td colspan='2'>");
155 if (info->tagger_date > 0)
156 cgit_print_age(info->tagger_date, -1, NULL);
157 html("</td></tr>\n");
162 cgit_tag_link(name, NULL, NULL, ctx.qry.head, name);
164 if (ctx.repo->snapshots && (ref->object->type == OBJ_COMMIT))
165 print_tag_downloads(ctx.repo, name);
167 cgit_object_link(ref->object);
169 if (ref->object->type == OBJ_COMMIT)
170 html(ref->commit->author);
171 html("</td><td colspan='2'>");
172 if (ref->object->type == OBJ_COMMIT)
173 cgit_print_age(ref->commit->commit->date, -1, NULL);
174 html("</td></tr>\n");
179 static void print_refs_link(char *path)
181 html("<tr class='nohover'><td colspan='5'>");
182 cgit_refs_link("[...]", NULL, NULL, ctx.qry.head, NULL, path);
186 void cgit_print_branches(int maxcount)
191 html("<tr class='nohover'><th class='left'>Branch</th>"
192 "<th class='left'>Commit message</th>"
193 "<th class='left'>Author</th>"
194 "<th class='left' colspan='2'>Age</th></tr>\n");
197 list.alloc = list.count = 0;
198 for_each_branch_ref(cgit_refs_cb, &list);
199 if (ctx.repo->enable_remote_branches)
200 for_each_remote_ref(cgit_refs_cb, &list);
202 if (maxcount == 0 || maxcount > list.count)
203 maxcount = list.count;
205 if (maxcount < list.count) {
206 qsort(list.refs, list.count, sizeof(*list.refs), cmp_branch_age);
207 qsort(list.refs, maxcount, sizeof(*list.refs), cmp_ref_name);
210 for (i = 0; i < maxcount; i++)
211 print_branch(list.refs[i]);
213 if (maxcount < list.count)
214 print_refs_link("heads");
216 cgit_free_reflist_inner(&list);
219 void cgit_print_tags(int maxcount)
226 list.alloc = list.count = 0;
227 for_each_tag_ref(cgit_refs_cb, &list);
230 qsort(list.refs, list.count, sizeof(*list.refs), cmp_tag_age);
232 maxcount = list.count;
233 else if (maxcount > list.count)
234 maxcount = list.count;
236 for (i = 0; i < maxcount; i++)
237 print_tag(list.refs[i]);
239 if (maxcount < list.count)
240 print_refs_link("tags");
242 cgit_free_reflist_inner(&list);
245 void cgit_print_refs()
248 html("<table class='list nowrap'>");
250 if (ctx.qry.path && !strncmp(ctx.qry.path, "heads", 5))
251 cgit_print_branches(0);
252 else if (ctx.qry.path && !strncmp(ctx.qry.path, "tags", 4))
255 cgit_print_branches(0);
256 html("<tr class='nohover'><td colspan='5'> </td></tr>");