1 /* ui-refs.c: browse symbolic refs
3 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
12 #include "ui-shared.h"
14 static int cmp_age(int age1
, int age2
)
16 if (age1
!= 0 && age2
!= 0)
19 if (age1
== 0 && age2
== 0)
28 static int cmp_ref_name(const void *a
, const void *b
)
30 struct refinfo
*r1
= *(struct refinfo
**)a
;
31 struct refinfo
*r2
= *(struct refinfo
**)b
;
33 return strcmp(r1
->refname
, r2
->refname
);
36 static int cmp_branch_age(const void *a
, const void *b
)
38 struct refinfo
*r1
= *(struct refinfo
**)a
;
39 struct refinfo
*r2
= *(struct refinfo
**)b
;
41 return cmp_age(r1
->commit
->committer_date
, r2
->commit
->committer_date
);
44 static int get_ref_age(struct refinfo
*ref
)
48 switch (ref
->object
->type
) {
50 return ref
->tag
? ref
->tag
->tagger_date
: 0;
52 return ref
->commit
? ref
->commit
->committer_date
: 0;
57 static int cmp_tag_age(const void *a
, const void *b
)
59 struct refinfo
*r1
= *(struct refinfo
**)a
;
60 struct refinfo
*r2
= *(struct refinfo
**)b
;
62 return cmp_age(get_ref_age(r1
), get_ref_age(r2
));
65 static int print_branch(struct refinfo
*ref
)
67 struct commitinfo
*info
= ref
->commit
;
68 char *name
= (char *)ref
->refname
;
73 cgit_log_link(name
, NULL
, NULL
, name
, NULL
, NULL
, 0, NULL
, NULL
,
77 if (ref
->object
->type
== OBJ_COMMIT
) {
78 cgit_commit_link(info
->subject
, NULL
, NULL
, name
, NULL
, NULL
, 0);
80 cgit_open_filter(ctx
.repo
->email_filter
, info
->author_email
);
81 html_txt(info
->author
);
82 cgit_close_filter(ctx
.repo
->email_filter
);
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");
101 static void print_tag_downloads(const struct cgit_repo
*repo
, const char *ref
)
103 const struct cgit_snapshot_format
* f
;
104 struct strbuf filename
= STRBUF_INIT
;
105 const char *basename
;
108 if (!ref
|| strlen(ref
) < 2)
111 basename
= cgit_repobasename(repo
->url
);
112 if (prefixcmp(ref
, basename
) != 0) {
113 if ((ref
[0] == 'v' || ref
[0] == 'V') && isdigit(ref
[1]))
115 if (isdigit(ref
[0])) {
116 ref
= fmtalloc("%s-%s", basename
, ref
);
121 for (f
= cgit_snapshot_formats
; f
->suffix
; f
++) {
122 if (!(repo
->snapshots
& f
->bit
))
124 strbuf_reset(&filename
);
125 strbuf_addf(&filename
, "%s%s", ref
, f
->suffix
);
126 cgit_snapshot_link(filename
.buf
, NULL
, NULL
, NULL
, NULL
, filename
.buf
);
127 html(" ");
132 strbuf_release(&filename
);
135 static int print_tag(struct refinfo
*ref
)
137 struct tag
*tag
= NULL
;
138 struct taginfo
*info
= NULL
;
139 char *name
= (char *)ref
->refname
;
140 struct object
*obj
= ref
->object
;
142 if (obj
->type
== OBJ_TAG
) {
143 tag
= (struct tag
*)obj
;
151 cgit_tag_link(name
, NULL
, NULL
, ctx
.qry
.head
, name
);
153 if (ctx
.repo
->snapshots
&& (obj
->type
== OBJ_COMMIT
))
154 print_tag_downloads(ctx
.repo
, name
);
156 cgit_object_link(obj
);
160 cgit_open_filter(ctx
.repo
->email_filter
, info
->tagger_email
);
161 html_txt(info
->tagger
);
162 cgit_close_filter(ctx
.repo
->email_filter
);
164 } else if (ref
->object
->type
== OBJ_COMMIT
) {
165 cgit_open_filter(ctx
.repo
->email_filter
, ref
->commit
->author_email
);
166 html_txt(ref
->commit
->author
);
167 cgit_close_filter(ctx
.repo
->email_filter
);
169 html("</td><td colspan='2'>");
171 if (info
->tagger_date
> 0)
172 cgit_print_age(info
->tagger_date
, -1, NULL
);
173 } else if (ref
->object
->type
== OBJ_COMMIT
) {
174 cgit_print_age(ref
->commit
->commit
->date
, -1, NULL
);
176 html("</td></tr>\n");
181 static void print_refs_link(char *path
)
183 html("<tr class='nohover'><td colspan='5'>");
184 cgit_refs_link("[...]", NULL
, NULL
, ctx
.qry
.head
, NULL
, path
);
188 void cgit_print_branches(int maxcount
)
193 html("<tr class='nohover'><th class='left'>Branch</th>"
194 "<th class='left'>Commit message</th>"
195 "<th class='left'>Author</th>"
196 "<th class='left' colspan='2'>Age</th></tr>\n");
199 list
.alloc
= list
.count
= 0;
200 for_each_branch_ref(cgit_refs_cb
, &list
);
201 if (ctx
.repo
->enable_remote_branches
)
202 for_each_remote_ref(cgit_refs_cb
, &list
);
204 if (maxcount
== 0 || maxcount
> list
.count
)
205 maxcount
= list
.count
;
207 qsort(list
.refs
, list
.count
, sizeof(*list
.refs
), cmp_branch_age
);
208 if (ctx
.repo
->branch_sort
== 0)
209 qsort(list
.refs
, maxcount
, sizeof(*list
.refs
), cmp_ref_name
);
211 for (i
= 0; i
< maxcount
; i
++)
212 print_branch(list
.refs
[i
]);
214 if (maxcount
< list
.count
)
215 print_refs_link("heads");
217 cgit_free_reflist_inner(&list
);
220 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
&& !prefixcmp(ctx
.qry
.path
, "heads"))
251 cgit_print_branches(0);
252 else if (ctx
.qry
.path
&& !prefixcmp(ctx
.qry
.path
, "tags"))
255 cgit_print_branches(0);
256 html("<tr class='nohover'><td colspan='5'> </td></tr>");