author | Lars Hjemli <hjemli@gmail.com> | 2008-11-29 12:33:02 (UTC) |
---|---|---|
committer | Lars Hjemli <hjemli@gmail.com> | 2008-11-29 12:33:02 (UTC) |
commit | cbac02c8b056e47b3e0092949c480c7ec64a3e0f (patch) (unidiff) | |
tree | 03b0d8a2524cef4ab57fe98d79941eb0562545de | |
parent | d71c0c725d7b5ddfc5b788d328a5fc7a27739662 (diff) | |
download | cgit-cbac02c8b056e47b3e0092949c480c7ec64a3e0f.zip cgit-cbac02c8b056e47b3e0092949c480c7ec64a3e0f.tar.gz cgit-cbac02c8b056e47b3e0092949c480c7ec64a3e0f.tar.bz2 |
ui-repolist: extract get_repo_modtime() from print_modtime()
The new function is then used by both print_modtime() and
cgit_reposort_modtime().
Signed-off-by: Lars Hjemli <hjemli@gmail.com>
-rw-r--r-- | ui-repolist.c | 46 |
1 files changed, 19 insertions, 27 deletions
diff --git a/ui-repolist.c b/ui-repolist.c index 312a7ee..436a774 100644 --- a/ui-repolist.c +++ b/ui-repolist.c | |||
@@ -7,63 +7,72 @@ | |||
7 | */ | 7 | */ |
8 | 8 | ||
9 | #include <time.h> | 9 | #include <time.h> |
10 | 10 | ||
11 | #include "cgit.h" | 11 | #include "cgit.h" |
12 | #include "html.h" | 12 | #include "html.h" |
13 | #include "ui-shared.h" | 13 | #include "ui-shared.h" |
14 | 14 | ||
15 | time_t read_agefile(char *path) | 15 | time_t read_agefile(char *path) |
16 | { | 16 | { |
17 | FILE *f; | 17 | FILE *f; |
18 | static char buf[64], buf2[64]; | 18 | static char buf[64], buf2[64]; |
19 | 19 | ||
20 | if (!(f = fopen(path, "r"))) | 20 | if (!(f = fopen(path, "r"))) |
21 | return -1; | 21 | return -1; |
22 | if (fgets(buf, sizeof(buf), f) == NULL) | 22 | if (fgets(buf, sizeof(buf), f) == NULL) |
23 | return -1; | 23 | return -1; |
24 | fclose(f); | 24 | fclose(f); |
25 | if (parse_date(buf, buf2, sizeof(buf2))) | 25 | if (parse_date(buf, buf2, sizeof(buf2))) |
26 | return strtoul(buf2, NULL, 10); | 26 | return strtoul(buf2, NULL, 10); |
27 | else | 27 | else |
28 | return 0; | 28 | return 0; |
29 | } | 29 | } |
30 | 30 | ||
31 | static void print_modtime(struct cgit_repo *repo) | 31 | static int get_repo_modtime(const struct cgit_repo *repo, time_t *mtime) |
32 | { | 32 | { |
33 | char *path; | 33 | char *path; |
34 | struct stat s; | 34 | struct stat s; |
35 | 35 | ||
36 | path = fmt("%s/%s", repo->path, ctx.cfg.agefile); | 36 | path = fmt("%s/%s", repo->path, ctx.cfg.agefile); |
37 | if (stat(path, &s) == 0) { | 37 | if (stat(path, &s) == 0) { |
38 | cgit_print_age(read_agefile(path), -1, NULL); | 38 | *mtime = read_agefile(path); |
39 | return; | 39 | return 1; |
40 | } | 40 | } |
41 | 41 | ||
42 | path = fmt("%s/refs/heads/%s", repo->path, repo->defbranch); | 42 | path = fmt("%s/refs/heads/%s", repo->path, repo->defbranch); |
43 | if (stat(path, &s) != 0) | 43 | if (stat(path, &s) == 0) { |
44 | return; | 44 | *mtime = s.st_mtime; |
45 | cgit_print_age(s.st_mtime, -1, NULL); | 45 | return 1; |
46 | } | ||
47 | return 0; | ||
48 | } | ||
49 | |||
50 | static void print_modtime(struct cgit_repo *repo) | ||
51 | { | ||
52 | time_t t; | ||
53 | if (get_repo_modtime(repo, &t)) | ||
54 | cgit_print_age(t, -1, NULL); | ||
46 | } | 55 | } |
47 | 56 | ||
48 | int is_match(struct cgit_repo *repo) | 57 | int is_match(struct cgit_repo *repo) |
49 | { | 58 | { |
50 | if (!ctx.qry.search) | 59 | if (!ctx.qry.search) |
51 | return 1; | 60 | return 1; |
52 | if (repo->url && strcasestr(repo->url, ctx.qry.search)) | 61 | if (repo->url && strcasestr(repo->url, ctx.qry.search)) |
53 | return 1; | 62 | return 1; |
54 | if (repo->name && strcasestr(repo->name, ctx.qry.search)) | 63 | if (repo->name && strcasestr(repo->name, ctx.qry.search)) |
55 | return 1; | 64 | return 1; |
56 | if (repo->desc && strcasestr(repo->desc, ctx.qry.search)) | 65 | if (repo->desc && strcasestr(repo->desc, ctx.qry.search)) |
57 | return 1; | 66 | return 1; |
58 | if (repo->owner && strcasestr(repo->owner, ctx.qry.search)) | 67 | if (repo->owner && strcasestr(repo->owner, ctx.qry.search)) |
59 | return 1; | 68 | return 1; |
60 | return 0; | 69 | return 0; |
61 | } | 70 | } |
62 | 71 | ||
63 | int is_in_url(struct cgit_repo *repo) | 72 | int is_in_url(struct cgit_repo *repo) |
64 | { | 73 | { |
65 | if (!ctx.qry.url) | 74 | if (!ctx.qry.url) |
66 | return 1; | 75 | return 1; |
67 | if (repo->url && !prefixcmp(repo->url, ctx.qry.url)) | 76 | if (repo->url && !prefixcmp(repo->url, ctx.qry.url)) |
68 | return 1; | 77 | return 1; |
69 | return 0; | 78 | return 0; |
@@ -75,71 +84,54 @@ void print_header(int columns) | |||
75 | "<th class='left'>Name</th>" | 84 | "<th class='left'>Name</th>" |
76 | "<th class='left'>Description</th>" | 85 | "<th class='left'>Description</th>" |
77 | "<th class='left'>Owner</th>" | 86 | "<th class='left'>Owner</th>" |
78 | "<th class='left'><a href=\"?s=1\">Idle</a></th>"); | 87 | "<th class='left'><a href=\"?s=1\">Idle</a></th>"); |
79 | if (ctx.cfg.enable_index_links) | 88 | if (ctx.cfg.enable_index_links) |
80 | html("<th class='left'>Links</th>"); | 89 | html("<th class='left'>Links</th>"); |
81 | html("</tr>\n"); | 90 | html("</tr>\n"); |
82 | } | 91 | } |
83 | 92 | ||
84 | 93 | ||
85 | void print_pager(int items, int pagelen, char *search) | 94 | void print_pager(int items, int pagelen, char *search) |
86 | { | 95 | { |
87 | int i; | 96 | int i; |
88 | html("<div class='pager'>"); | 97 | html("<div class='pager'>"); |
89 | for(i = 0; i * pagelen < items; i++) | 98 | for(i = 0; i * pagelen < items; i++) |
90 | cgit_index_link(fmt("[%d]", i+1), fmt("Page %d", i+1), NULL, | 99 | cgit_index_link(fmt("[%d]", i+1), fmt("Page %d", i+1), NULL, |
91 | search, i * pagelen); | 100 | search, i * pagelen); |
92 | html("</div>"); | 101 | html("</div>"); |
93 | } | 102 | } |
94 | 103 | ||
95 | static int cgit_reposort_modtime(const void *a, const void *b) | 104 | static int cgit_reposort_modtime(const void *a, const void *b) |
96 | { | 105 | { |
97 | const struct cgit_repo *r1 = a; | 106 | const struct cgit_repo *r1 = a; |
98 | const struct cgit_repo *r2 = b; | 107 | const struct cgit_repo *r2 = b; |
99 | char *path; | ||
100 | struct stat s; | ||
101 | time_t t1, t2; | 108 | time_t t1, t2; |
102 | path = fmt("%s/%s", r1->path, ctx.cfg.agefile); | ||
103 | if (stat(path, &s) == 0) { | ||
104 | t1 = read_agefile(path); | ||
105 | } else { | ||
106 | path = fmt("%s/refs/heads/%s", r1->path, r1->defbranch); | ||
107 | if (stat(path, &s) != 0) | ||
108 | return 0; | ||
109 | t1 =s.st_mtime; | ||
110 | } | ||
111 | 109 | ||
112 | path = fmt("%s/%s", r2->path, ctx.cfg.agefile); | 110 | t1 = t2 = 0; |
113 | if (stat(path, &s) == 0) { | 111 | get_repo_modtime(r1, &t1); |
114 | t2 = read_agefile(path); | 112 | get_repo_modtime(r2, &t2); |
115 | } else { | 113 | return t2 - t1; |
116 | path = fmt("%s/refs/heads/%s", r2->path, r2->defbranch); | ||
117 | if (stat(path, &s) != 0) | ||
118 | return 0; | ||
119 | t2 =s.st_mtime; | ||
120 | } | ||
121 | return t2-t1; | ||
122 | } | 114 | } |
123 | 115 | ||
124 | void cgit_print_repolist() | 116 | void cgit_print_repolist() |
125 | { | 117 | { |
126 | int i, columns = 4, hits = 0, header = 0; | 118 | int i, columns = 4, hits = 0, header = 0; |
127 | char *last_group = NULL; | 119 | char *last_group = NULL; |
128 | 120 | ||
129 | if (ctx.cfg.enable_index_links) | 121 | if (ctx.cfg.enable_index_links) |
130 | columns++; | 122 | columns++; |
131 | 123 | ||
132 | ctx.page.title = ctx.cfg.root_title; | 124 | ctx.page.title = ctx.cfg.root_title; |
133 | cgit_print_http_headers(&ctx); | 125 | cgit_print_http_headers(&ctx); |
134 | cgit_print_docstart(&ctx); | 126 | cgit_print_docstart(&ctx); |
135 | cgit_print_pageheader(&ctx); | 127 | cgit_print_pageheader(&ctx); |
136 | 128 | ||
137 | if (ctx.cfg.index_header) | 129 | if (ctx.cfg.index_header) |
138 | html_include(ctx.cfg.index_header); | 130 | html_include(ctx.cfg.index_header); |
139 | 131 | ||
140 | if(ctx.qry.sort) | 132 | if(ctx.qry.sort) |
141 | qsort(cgit_repolist.repos,cgit_repolist.count,sizeof(struct cgit_repo),cgit_reposort_modtime); | 133 | qsort(cgit_repolist.repos,cgit_repolist.count,sizeof(struct cgit_repo),cgit_reposort_modtime); |
142 | 134 | ||
143 | html("<table summary='repository list' class='list nowrap'>"); | 135 | html("<table summary='repository list' class='list nowrap'>"); |
144 | for (i=0; i<cgit_repolist.count; i++) { | 136 | for (i=0; i<cgit_repolist.count; i++) { |
145 | ctx.repo = &cgit_repolist.repos[i]; | 137 | ctx.repo = &cgit_repolist.repos[i]; |