]>
git.cameronkatri.com Git - cgit.git/blob - ui-refs.c
75f2789fc1653b76509f2ff3f8a9120051dd30e6
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 inline int cmp_age(int age1
, int age2
)
16 /* age1 and age2 are assumed to be non-negative */
20 static int cmp_ref_name(const void *a
, const void *b
)
22 struct refinfo
*r1
= *(struct refinfo
**)a
;
23 struct refinfo
*r2
= *(struct refinfo
**)b
;
25 return strcmp(r1
->refname
, r2
->refname
);
28 static int cmp_branch_age(const void *a
, const void *b
)
30 struct refinfo
*r1
= *(struct refinfo
**)a
;
31 struct refinfo
*r2
= *(struct refinfo
**)b
;
33 return cmp_age(r1
->commit
->committer_date
, r2
->commit
->committer_date
);
36 static int get_ref_age(struct refinfo
*ref
)
40 switch (ref
->object
->type
) {
42 return ref
->tag
? ref
->tag
->tagger_date
: 0;
44 return ref
->commit
? ref
->commit
->committer_date
: 0;
49 static int cmp_tag_age(const void *a
, const void *b
)
51 struct refinfo
*r1
= *(struct refinfo
**)a
;
52 struct refinfo
*r2
= *(struct refinfo
**)b
;
54 return cmp_age(get_ref_age(r1
), get_ref_age(r2
));
57 static int print_branch(struct refinfo
*ref
)
59 struct commitinfo
*info
= ref
->commit
;
60 char *name
= (char *)ref
->refname
;
65 cgit_log_link(name
, NULL
, NULL
, name
, NULL
, NULL
, 0, NULL
, NULL
,
69 if (ref
->object
->type
== OBJ_COMMIT
) {
70 cgit_commit_link(info
->subject
, NULL
, NULL
, name
, NULL
, NULL
);
72 cgit_open_filter(ctx
.repo
->email_filter
, info
->author_email
, "refs");
73 html_txt(info
->author
);
74 cgit_close_filter(ctx
.repo
->email_filter
);
75 html("</td><td colspan='2'>");
76 cgit_print_age(info
->committer_date
, info
->committer_tz
, -1);
78 html("</td><td></td><td>");
79 cgit_object_link(ref
->object
);
85 static void print_tag_header(void)
87 html("<tr class='nohover'><th class='left'>Tag</th>"
88 "<th class='left'>Download</th>"
89 "<th class='left'>Author</th>"
90 "<th class='left' colspan='2'>Age</th></tr>\n");
93 static void print_tag_downloads(const struct cgit_repo
*repo
, const char *ref
)
95 const struct cgit_snapshot_format
* f
;
97 struct strbuf filename
= STRBUF_INIT
;
100 if (!ref
|| strlen(ref
) < 1)
103 basename
= cgit_repobasename(repo
->url
);
104 if (starts_with(ref
, basename
))
105 strbuf_addstr(&filename
, ref
);
107 cgit_compose_snapshot_prefix(&filename
, basename
, ref
);
108 prefixlen
= filename
.len
;
109 for (f
= cgit_snapshot_formats
; f
->suffix
; f
++) {
110 if (!(repo
->snapshots
& f
->bit
))
112 strbuf_setlen(&filename
, prefixlen
);
113 strbuf_addstr(&filename
, f
->suffix
);
114 cgit_snapshot_link(filename
.buf
, NULL
, NULL
, NULL
, NULL
, filename
.buf
);
115 html(" ");
118 strbuf_release(&filename
);
121 static int print_tag(struct refinfo
*ref
)
123 struct tag
*tag
= NULL
;
124 struct taginfo
*info
= NULL
;
125 char *name
= (char *)ref
->refname
;
126 struct object
*obj
= ref
->object
;
128 if (obj
->type
== OBJ_TAG
) {
129 tag
= (struct tag
*)obj
;
137 cgit_tag_link(name
, NULL
, NULL
, name
);
139 if (ctx
.repo
->snapshots
&& (obj
->type
== OBJ_COMMIT
))
140 print_tag_downloads(ctx
.repo
, name
);
142 cgit_object_link(obj
);
146 cgit_open_filter(ctx
.repo
->email_filter
, info
->tagger_email
, "refs");
147 html_txt(info
->tagger
);
148 cgit_close_filter(ctx
.repo
->email_filter
);
150 } else if (ref
->object
->type
== OBJ_COMMIT
) {
151 cgit_open_filter(ctx
.repo
->email_filter
, ref
->commit
->author_email
, "refs");
152 html_txt(ref
->commit
->author
);
153 cgit_close_filter(ctx
.repo
->email_filter
);
155 html("</td><td colspan='2'>");
157 if (info
->tagger_date
> 0)
158 cgit_print_age(info
->tagger_date
, info
->tagger_tz
, -1);
159 } else if (ref
->object
->type
== OBJ_COMMIT
) {
160 cgit_print_age(ref
->commit
->commit
->date
, 0, -1);
162 html("</td></tr>\n");
167 static void print_refs_link(char *path
)
169 html("<tr class='nohover'><td colspan='5'>");
170 cgit_refs_link("[...]", NULL
, NULL
, ctx
.qry
.head
, NULL
, path
);
174 void cgit_print_branches(int maxcount
)
179 html("<tr class='nohover'><th class='left'>Branch</th>"
180 "<th class='left'>Commit message</th>"
181 "<th class='left'>Author</th>"
182 "<th class='left' colspan='2'>Age</th></tr>\n");
185 list
.alloc
= list
.count
= 0;
186 for_each_branch_ref(cgit_refs_cb
, &list
);
187 if (ctx
.repo
->enable_remote_branches
)
188 for_each_remote_ref(cgit_refs_cb
, &list
);
190 if (maxcount
== 0 || maxcount
> list
.count
)
191 maxcount
= list
.count
;
193 qsort(list
.refs
, list
.count
, sizeof(*list
.refs
), cmp_branch_age
);
194 if (ctx
.repo
->branch_sort
== 0)
195 qsort(list
.refs
, maxcount
, sizeof(*list
.refs
), cmp_ref_name
);
197 for (i
= 0; i
< maxcount
; i
++)
198 print_branch(list
.refs
[i
]);
200 if (maxcount
< list
.count
)
201 print_refs_link("heads");
203 cgit_free_reflist_inner(&list
);
206 void cgit_print_tags(int maxcount
)
212 list
.alloc
= list
.count
= 0;
213 for_each_tag_ref(cgit_refs_cb
, &list
);
216 qsort(list
.refs
, list
.count
, sizeof(*list
.refs
), cmp_tag_age
);
218 maxcount
= list
.count
;
219 else if (maxcount
> list
.count
)
220 maxcount
= list
.count
;
222 for (i
= 0; i
< maxcount
; i
++)
223 print_tag(list
.refs
[i
]);
225 if (maxcount
< list
.count
)
226 print_refs_link("tags");
228 cgit_free_reflist_inner(&list
);
231 void cgit_print_refs(void)
233 cgit_print_layout_start();
234 html("<table class='list nowrap'>");
236 if (ctx
.qry
.path
&& starts_with(ctx
.qry
.path
, "heads"))
237 cgit_print_branches(0);
238 else if (ctx
.qry
.path
&& starts_with(ctx
.qry
.path
, "tags"))
241 cgit_print_branches(0);
242 html("<tr class='nohover'><td colspan='5'> </td></tr>");
246 cgit_print_layout_end();