author | Lars Hjemli <hjemli@gmail.com> | 2006-12-15 17:17:36 (UTC) |
---|---|---|
committer | Lars Hjemli <hjemli@gmail.com> | 2006-12-15 17:17:36 (UTC) |
commit | 2101e26fd68f816e77de62b93df4c32fd1110d0c (patch) (unidiff) | |
tree | d70d28734c4fbfd0a4e4a40bcfd445eb50dc1666 | |
parent | 420712ac2531f65a2b94d5ec6d8e03de6942331e (diff) | |
download | cgit-2101e26fd68f816e77de62b93df4c32fd1110d0c.zip cgit-2101e26fd68f816e77de62b93df4c32fd1110d0c.tar.gz cgit-2101e26fd68f816e77de62b93df4c32fd1110d0c.tar.bz2 |
Add a common commit parser
Make a better commit parser, replacing the ugly one in ui-log.c
Signed-off-by: Lars Hjemli <hjemli@gmail.com>
-rw-r--r-- | cgit.h | 9 | ||||
-rw-r--r-- | parsing.c | 53 | ||||
-rw-r--r-- | ui-log.c | 76 |
3 files changed, 75 insertions, 63 deletions
@@ -1,89 +1,98 @@ | |||
1 | #ifndef CGIT_H | 1 | #ifndef CGIT_H |
2 | #define CGIT_H | 2 | #define CGIT_H |
3 | 3 | ||
4 | #include "git.h" | 4 | #include "git.h" |
5 | #include <openssl/sha.h> | 5 | #include <openssl/sha.h> |
6 | #include <ctype.h> | 6 | #include <ctype.h> |
7 | #include <sched.h> | 7 | #include <sched.h> |
8 | 8 | ||
9 | typedef void (*configfn)(const char *name, const char *value); | 9 | typedef void (*configfn)(const char *name, const char *value); |
10 | 10 | ||
11 | struct cacheitem { | 11 | struct cacheitem { |
12 | char *name; | 12 | char *name; |
13 | struct stat st; | 13 | struct stat st; |
14 | int ttl; | 14 | int ttl; |
15 | int fd; | 15 | int fd; |
16 | }; | 16 | }; |
17 | 17 | ||
18 | struct commitinfo { | ||
19 | struct commit *commit; | ||
20 | char *author; | ||
21 | char *committer; | ||
22 | char *subject; | ||
23 | char *msg; | ||
24 | }; | ||
25 | |||
18 | extern const char cgit_version[]; | 26 | extern const char cgit_version[]; |
19 | 27 | ||
20 | extern char *cgit_root; | 28 | extern char *cgit_root; |
21 | extern char *cgit_root_title; | 29 | extern char *cgit_root_title; |
22 | extern char *cgit_css; | 30 | extern char *cgit_css; |
23 | extern char *cgit_logo; | 31 | extern char *cgit_logo; |
24 | extern char *cgit_logo_link; | 32 | extern char *cgit_logo_link; |
25 | extern char *cgit_virtual_root; | 33 | extern char *cgit_virtual_root; |
26 | extern char *cgit_cache_root; | 34 | extern char *cgit_cache_root; |
27 | 35 | ||
28 | extern int cgit_max_lock_attempts; | 36 | extern int cgit_max_lock_attempts; |
29 | extern int cgit_cache_root_ttl; | 37 | extern int cgit_cache_root_ttl; |
30 | extern int cgit_cache_repo_ttl; | 38 | extern int cgit_cache_repo_ttl; |
31 | extern int cgit_cache_dynamic_ttl; | 39 | extern int cgit_cache_dynamic_ttl; |
32 | extern int cgit_cache_static_ttl; | 40 | extern int cgit_cache_static_ttl; |
33 | extern int cgit_cache_max_create_time; | 41 | extern int cgit_cache_max_create_time; |
34 | 42 | ||
35 | extern char *cgit_repo_name; | 43 | extern char *cgit_repo_name; |
36 | extern char *cgit_repo_desc; | 44 | extern char *cgit_repo_desc; |
37 | extern char *cgit_repo_owner; | 45 | extern char *cgit_repo_owner; |
38 | 46 | ||
39 | extern int cgit_query_has_symref; | 47 | extern int cgit_query_has_symref; |
40 | extern int cgit_query_has_sha1; | 48 | extern int cgit_query_has_sha1; |
41 | 49 | ||
42 | extern char *cgit_querystring; | 50 | extern char *cgit_querystring; |
43 | extern char *cgit_query_repo; | 51 | extern char *cgit_query_repo; |
44 | extern char *cgit_query_page; | 52 | extern char *cgit_query_page; |
45 | extern char *cgit_query_head; | 53 | extern char *cgit_query_head; |
46 | extern char *cgit_query_sha1; | 54 | extern char *cgit_query_sha1; |
47 | extern int cgit_query_ofs; | 55 | extern int cgit_query_ofs; |
48 | 56 | ||
49 | extern int htmlfd; | 57 | extern int htmlfd; |
50 | 58 | ||
51 | extern void cgit_global_config_cb(const char *name, const char *value); | 59 | extern void cgit_global_config_cb(const char *name, const char *value); |
52 | extern void cgit_repo_config_cb(const char *name, const char *value); | 60 | extern void cgit_repo_config_cb(const char *name, const char *value); |
53 | extern void cgit_querystring_cb(const char *name, const char *value); | 61 | extern void cgit_querystring_cb(const char *name, const char *value); |
54 | 62 | ||
55 | extern char *fmt(const char *format,...); | 63 | extern char *fmt(const char *format,...); |
56 | 64 | ||
57 | extern void html(const char *txt); | 65 | extern void html(const char *txt); |
58 | extern void htmlf(const char *format,...); | 66 | extern void htmlf(const char *format,...); |
59 | extern void html_txt(char *txt); | 67 | extern void html_txt(char *txt); |
60 | extern void html_attr(char *txt); | 68 | extern void html_attr(char *txt); |
61 | extern void html_link_open(char *url, char *title, char *class); | 69 | extern void html_link_open(char *url, char *title, char *class); |
62 | extern void html_link_close(void); | 70 | extern void html_link_close(void); |
63 | 71 | ||
64 | extern int cgit_read_config(const char *filename, configfn fn); | 72 | extern int cgit_read_config(const char *filename, configfn fn); |
65 | extern int cgit_parse_query(char *txt, configfn fn); | 73 | extern int cgit_parse_query(char *txt, configfn fn); |
74 | extern struct commitinfo *cgit_parse_commit(struct commit *commit); | ||
66 | 75 | ||
67 | extern void cache_prepare(struct cacheitem *item); | 76 | extern void cache_prepare(struct cacheitem *item); |
68 | extern int cache_lock(struct cacheitem *item); | 77 | extern int cache_lock(struct cacheitem *item); |
69 | extern int cache_unlock(struct cacheitem *item); | 78 | extern int cache_unlock(struct cacheitem *item); |
70 | extern int cache_cancel_lock(struct cacheitem *item); | 79 | extern int cache_cancel_lock(struct cacheitem *item); |
71 | extern int cache_exist(struct cacheitem *item); | 80 | extern int cache_exist(struct cacheitem *item); |
72 | extern int cache_expired(struct cacheitem *item); | 81 | extern int cache_expired(struct cacheitem *item); |
73 | 82 | ||
74 | extern char *cgit_repourl(const char *reponame); | 83 | extern char *cgit_repourl(const char *reponame); |
75 | extern char *cgit_pageurl(const char *reponame, const char *pagename, | 84 | extern char *cgit_pageurl(const char *reponame, const char *pagename, |
76 | const char *query); | 85 | const char *query); |
77 | 86 | ||
78 | extern void cgit_print_error(char *msg); | 87 | extern void cgit_print_error(char *msg); |
79 | extern void cgit_print_docstart(char *title, struct cacheitem *item); | 88 | extern void cgit_print_docstart(char *title, struct cacheitem *item); |
80 | extern void cgit_print_docend(); | 89 | extern void cgit_print_docend(); |
81 | extern void cgit_print_pageheader(char *title); | 90 | extern void cgit_print_pageheader(char *title); |
82 | 91 | ||
83 | extern void cgit_print_repolist(struct cacheitem *item); | 92 | extern void cgit_print_repolist(struct cacheitem *item); |
84 | extern void cgit_print_summary(); | 93 | extern void cgit_print_summary(); |
85 | extern void cgit_print_log(const char *tip, int ofs, int cnt); | 94 | extern void cgit_print_log(const char *tip, int ofs, int cnt); |
86 | extern void cgit_print_view(char *hex); | 95 | extern void cgit_print_view(char *hex); |
87 | extern void cgit_print_tree(const char *sha1); | 96 | extern void cgit_print_tree(const char *sha1); |
88 | 97 | ||
89 | #endif /* CGIT_H */ | 98 | #endif /* CGIT_H */ |
@@ -59,48 +59,101 @@ int read_config_line(FILE *f, char *line, const char **value, int bufsize) | |||
59 | i++; | 59 | i++; |
60 | } | 60 | } |
61 | line[i+1] = 0; | 61 | line[i+1] = 0; |
62 | return i; | 62 | return i; |
63 | } | 63 | } |
64 | 64 | ||
65 | int cgit_read_config(const char *filename, configfn fn) | 65 | int cgit_read_config(const char *filename, configfn fn) |
66 | { | 66 | { |
67 | int ret = 0, len; | 67 | int ret = 0, len; |
68 | char line[256]; | 68 | char line[256]; |
69 | const char *value; | 69 | const char *value; |
70 | FILE *f = fopen(filename, "r"); | 70 | FILE *f = fopen(filename, "r"); |
71 | 71 | ||
72 | if (!f) | 72 | if (!f) |
73 | return -1; | 73 | return -1; |
74 | 74 | ||
75 | while((len = read_config_line(f, line, &value, sizeof(line))) > 0) | 75 | while((len = read_config_line(f, line, &value, sizeof(line))) > 0) |
76 | (*fn)(line, value); | 76 | (*fn)(line, value); |
77 | 77 | ||
78 | fclose(f); | 78 | fclose(f); |
79 | return ret; | 79 | return ret; |
80 | } | 80 | } |
81 | 81 | ||
82 | int cgit_parse_query(char *txt, configfn fn) | 82 | int cgit_parse_query(char *txt, configfn fn) |
83 | { | 83 | { |
84 | char *t, *value = NULL, c; | 84 | char *t, *value = NULL, c; |
85 | 85 | ||
86 | if (!txt) | 86 | if (!txt) |
87 | return 0; | 87 | return 0; |
88 | 88 | ||
89 | t = txt = xstrdup(txt); | 89 | t = txt = xstrdup(txt); |
90 | 90 | ||
91 | while((c=*t) != '\0') { | 91 | while((c=*t) != '\0') { |
92 | if (c=='=') { | 92 | if (c=='=') { |
93 | *t = '\0'; | 93 | *t = '\0'; |
94 | value = t+1; | 94 | value = t+1; |
95 | } else if (c=='&') { | 95 | } else if (c=='&') { |
96 | *t = '\0'; | 96 | *t = '\0'; |
97 | (*fn)(txt, value); | 97 | (*fn)(txt, value); |
98 | txt = t+1; | 98 | txt = t+1; |
99 | value = NULL; | 99 | value = NULL; |
100 | } | 100 | } |
101 | t++; | 101 | t++; |
102 | } | 102 | } |
103 | if (t!=txt) | 103 | if (t!=txt) |
104 | (*fn)(txt, value); | 104 | (*fn)(txt, value); |
105 | return 0; | 105 | return 0; |
106 | } | 106 | } |
107 | |||
108 | char *substr(const char *head, const char *tail) | ||
109 | { | ||
110 | char *buf; | ||
111 | |||
112 | buf = xmalloc(tail - head + 1); | ||
113 | strncpy(buf, head, tail - head); | ||
114 | buf[tail - head] = '\0'; | ||
115 | return buf; | ||
116 | } | ||
117 | |||
118 | struct commitinfo *cgit_parse_commit(struct commit *commit) | ||
119 | { | ||
120 | struct commitinfo *ret; | ||
121 | char *p = commit->buffer, *t = commit->buffer; | ||
122 | |||
123 | ret = xmalloc(sizeof(*ret)); | ||
124 | ret->commit = commit; | ||
125 | |||
126 | if (strncmp(p, "tree ", 5)) | ||
127 | die("Bad commit: %s", sha1_to_hex(commit->object.sha1)); | ||
128 | else | ||
129 | p += 46; // "tree " + hex[40] + "\n" | ||
130 | |||
131 | while (!strncmp(p, "parent ", 7)) | ||
132 | p += 48; // "parent " + hex[40] + "\n" | ||
133 | |||
134 | if (!strncmp(p, "author ", 7)) { | ||
135 | p += 7; | ||
136 | t = strchr(p, '<') - 1; | ||
137 | ret->author = substr(p, t); | ||
138 | p = strchr(p, '\n') + 1; | ||
139 | } | ||
140 | |||
141 | if (!strncmp(p, "committer ", 9)) { | ||
142 | p += 9; | ||
143 | t = strchr(p, '<') - 1; | ||
144 | ret->committer = substr(p, t); | ||
145 | p = strchr(p, '\n') + 1; | ||
146 | } | ||
147 | |||
148 | while (*p == '\n') | ||
149 | p = strchr(p, '\n') + 1; | ||
150 | |||
151 | t = strchr(p, '\n'); | ||
152 | ret->subject = substr(p, t); | ||
153 | |||
154 | while (*p == '\n') | ||
155 | p = strchr(p, '\n') + 1; | ||
156 | ret->msg = p; | ||
157 | |||
158 | return ret; | ||
159 | } | ||
@@ -1,147 +1,97 @@ | |||
1 | /* ui-log.c: functions for log output | 1 | /* ui-log.c: functions for log output |
2 | * | 2 | * |
3 | * Copyright (C) 2006 Lars Hjemli | 3 | * Copyright (C) 2006 Lars Hjemli |
4 | * | 4 | * |
5 | * Licensed under GNU General Public License v2 | 5 | * Licensed under GNU General Public License v2 |
6 | * (see COPYING for full license text) | 6 | * (see COPYING for full license text) |
7 | */ | 7 | */ |
8 | 8 | ||
9 | #include "cgit.h" | 9 | #include "cgit.h" |
10 | 10 | ||
11 | static int get_one_line(char *txt) | 11 | void print_commit(struct commit *commit) |
12 | { | 12 | { |
13 | char *t; | ||
14 | |||
15 | for(t=txt; *t != '\n' && t != '\0'; t++) | ||
16 | ; | ||
17 | *t = '\0'; | ||
18 | return t-txt-1; | ||
19 | } | ||
20 | |||
21 | static void cgit_print_commit_shortlog(struct commit *commit) | ||
22 | { | ||
23 | char *h, *t, *p; | ||
24 | char *tree = NULL, *author = NULL, *subject = NULL; | ||
25 | int len; | ||
26 | time_t sec; | ||
27 | struct tm *time; | ||
28 | char buf[32]; | 13 | char buf[32]; |
14 | struct commitinfo *info; | ||
15 | struct tm *time; | ||
29 | 16 | ||
30 | h = t = commit->buffer; | 17 | info = cgit_parse_commit(commit); |
31 | 18 | time = gmtime(&commit->date); | |
32 | if (strncmp(h, "tree ", 5)) | ||
33 | die("Bad commit format: %s", | ||
34 | sha1_to_hex(commit->object.sha1)); | ||
35 | |||
36 | len = get_one_line(h); | ||
37 | tree = h+5; | ||
38 | h += len + 2; | ||
39 | |||
40 | while (!strncmp(h, "parent ", 7)) | ||
41 | h += get_one_line(h) + 2; | ||
42 | |||
43 | if (!strncmp(h, "author ", 7)) { | ||
44 | author = h+7; | ||
45 | h += get_one_line(h) + 2; | ||
46 | t = author; | ||
47 | while(t!=h && *t!='<') | ||
48 | t++; | ||
49 | *t='\0'; | ||
50 | p = t; | ||
51 | while(--t!=author && *t==' ') | ||
52 | *t='\0'; | ||
53 | while(++p!=h && *p!='>') | ||
54 | ; | ||
55 | while(++p!=h && !isdigit(*p)) | ||
56 | ; | ||
57 | |||
58 | t = p; | ||
59 | while(++p && isdigit(*p)) | ||
60 | ; | ||
61 | *p = '\0'; | ||
62 | sec = atoi(t); | ||
63 | time = gmtime(&sec); | ||
64 | } | ||
65 | |||
66 | while((len = get_one_line(h)) > 0) | ||
67 | h += len+2; | ||
68 | |||
69 | h++; | ||
70 | len = get_one_line(h); | ||
71 | |||
72 | subject = h; | ||
73 | |||
74 | html("<tr><td>"); | 19 | html("<tr><td>"); |
75 | strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", time); | 20 | strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", time); |
76 | html_txt(buf); | 21 | html_txt(buf); |
77 | html("</td><td>"); | 22 | html("</td><td>"); |
78 | char *qry = fmt("id=%s", sha1_to_hex(commit->object.sha1)); | 23 | char *qry = fmt("id=%s", sha1_to_hex(commit->object.sha1)); |
79 | char *url = cgit_pageurl(cgit_query_repo, "view", qry); | 24 | char *url = cgit_pageurl(cgit_query_repo, "view", qry); |
80 | html_link_open(url, NULL, NULL); | 25 | html_link_open(url, NULL, NULL); |
81 | html_txt(subject); | 26 | html_txt(info->subject); |
82 | html_link_close(); | 27 | html_link_close(); |
83 | html("</td><td>"); | 28 | html("</td><td>"); |
84 | html_txt(author); | 29 | html_txt(info->author); |
85 | html("</td><td><a href='"); | 30 | html("</td><td><a href='"); |
86 | html_attr(cgit_pageurl(cgit_query_repo, "tree", | 31 | html_attr(cgit_pageurl(cgit_query_repo, "tree", |
87 | fmt("id=%s", | 32 | fmt("id=%s", |
88 | sha1_to_hex(commit->tree->object.sha1)))); | 33 | sha1_to_hex(commit->tree->object.sha1)))); |
89 | html("'>tree</a>"); | 34 | html("'>tree</a>"); |
90 | html("</td></tr>\n"); | 35 | html("</td></tr>\n"); |
36 | free(info->author); | ||
37 | free(info->committer); | ||
38 | free(info->subject); | ||
39 | free(info); | ||
91 | } | 40 | } |
92 | 41 | ||
42 | |||
93 | void cgit_print_log(const char *tip, int ofs, int cnt) | 43 | void cgit_print_log(const char *tip, int ofs, int cnt) |
94 | { | 44 | { |
95 | struct rev_info rev; | 45 | struct rev_info rev; |
96 | struct commit *commit; | 46 | struct commit *commit; |
97 | const char *argv[2] = {NULL, tip}; | 47 | const char *argv[2] = {NULL, tip}; |
98 | int i; | 48 | int i; |
99 | 49 | ||
100 | init_revisions(&rev, NULL); | 50 | init_revisions(&rev, NULL); |
101 | rev.abbrev = DEFAULT_ABBREV; | 51 | rev.abbrev = DEFAULT_ABBREV; |
102 | rev.commit_format = CMIT_FMT_DEFAULT; | 52 | rev.commit_format = CMIT_FMT_DEFAULT; |
103 | rev.verbose_header = 1; | 53 | rev.verbose_header = 1; |
104 | rev.show_root_diff = 0; | 54 | rev.show_root_diff = 0; |
105 | setup_revisions(2, argv, &rev, NULL); | 55 | setup_revisions(2, argv, &rev, NULL); |
106 | prepare_revision_walk(&rev); | 56 | prepare_revision_walk(&rev); |
107 | 57 | ||
108 | html("<h2>Log</h2>"); | 58 | html("<h2>Log</h2>"); |
109 | html("<table class='list'>"); | 59 | html("<table class='list'>"); |
110 | html("<tr><th>Date</th><th>Message</th><th>Author</th><th>Link</th></tr>\n"); | 60 | html("<tr><th>Date</th><th>Message</th><th>Author</th><th>Link</th></tr>\n"); |
111 | 61 | ||
112 | if (ofs<0) | 62 | if (ofs<0) |
113 | ofs = 0; | 63 | ofs = 0; |
114 | 64 | ||
115 | for (i = 0; i < ofs && (commit = get_revision(&rev)) != NULL; i++) { | 65 | for (i = 0; i < ofs && (commit = get_revision(&rev)) != NULL; i++) { |
116 | free(commit->buffer); | 66 | free(commit->buffer); |
117 | commit->buffer = NULL; | 67 | commit->buffer = NULL; |
118 | free_commit_list(commit->parents); | 68 | free_commit_list(commit->parents); |
119 | commit->parents = NULL; | 69 | commit->parents = NULL; |
120 | } | 70 | } |
121 | 71 | ||
122 | for (i = 0; i < cnt && (commit = get_revision(&rev)) != NULL; i++) { | 72 | for (i = 0; i < cnt && (commit = get_revision(&rev)) != NULL; i++) { |
123 | cgit_print_commit_shortlog(commit); | 73 | print_commit(commit); |
124 | free(commit->buffer); | 74 | free(commit->buffer); |
125 | commit->buffer = NULL; | 75 | commit->buffer = NULL; |
126 | free_commit_list(commit->parents); | 76 | free_commit_list(commit->parents); |
127 | commit->parents = NULL; | 77 | commit->parents = NULL; |
128 | } | 78 | } |
129 | html("</table>\n"); | 79 | html("</table>\n"); |
130 | 80 | ||
131 | html("<div class='pager'>"); | 81 | html("<div class='pager'>"); |
132 | if (ofs > 0) { | 82 | if (ofs > 0) { |
133 | html(" <a href='"); | 83 | html(" <a href='"); |
134 | html(cgit_pageurl(cgit_query_repo, cgit_query_page, | 84 | html(cgit_pageurl(cgit_query_repo, cgit_query_page, |
135 | fmt("h=%s&ofs=%d", tip, ofs-cnt))); | 85 | fmt("h=%s&ofs=%d", tip, ofs-cnt))); |
136 | html("'>[prev]</a> "); | 86 | html("'>[prev]</a> "); |
137 | } | 87 | } |
138 | 88 | ||
139 | if ((commit = get_revision(&rev)) != NULL) { | 89 | if ((commit = get_revision(&rev)) != NULL) { |
140 | html(" <a href='"); | 90 | html(" <a href='"); |
141 | html(cgit_pageurl(cgit_query_repo, "log", | 91 | html(cgit_pageurl(cgit_query_repo, "log", |
142 | fmt("h=%s&ofs=%d", tip, ofs+cnt))); | 92 | fmt("h=%s&ofs=%d", tip, ofs+cnt))); |
143 | html("'>[next]</a> "); | 93 | html("'>[next]</a> "); |
144 | } | 94 | } |
145 | html("</div>"); | 95 | html("</div>"); |
146 | } | 96 | } |
147 | 97 | ||