Author: Lars Hjemli <hjemli@gmail.com>
Add cgit_commit_link() + support for id=sha1 to commit view This adds a function to generate links to the commit page and extends said page to use id from querystring as primary revision specified (fallback to h). Signed-off-by: Lars Hjemli <hjemli@gmail.com>
cgit.c | 2 +- cgit.h | 2 ++ ui-commit.c | 3 +++ ui-log.c | 7 ++----- ui-shared.c | 16 ++++++++++++++-- ui-summary.c | 18 +++++++++--------- ui-tree.c | 1 -
diff --git a/cgit.c b/cgit.c index 1c213c7bbe83f34251a9526e685fe5cb23a56d36..f5135d9242dd07175909e999e5ec69d648c48907 100644 --- a/cgit.c +++ b/cgit.c @@ -100,7 +100,7 @@ case CMD_TREE: cgit_print_tree(cgit_query_sha1, cgit_query_path); break; case CMD_COMMIT: - cgit_print_commit(cgit_query_head); + cgit_print_commit(cgit_query_sha1); break; case CMD_DIFF: cgit_print_diff(cgit_query_head, cgit_query_sha1, cgit_query_sha2, diff --git a/cgit.h b/cgit.h index b2f6361d05850ffab5625968cb437c072425eb41..c276a24268047646dfd20c8339ab702091869f42 100644 --- a/cgit.h +++ b/cgit.h @@ -205,6 +205,8 @@ extern void cgit_tree_link(char *name, char *title, char *class, char *head, char *rev, char *path); extern void cgit_log_link(char *name, char *title, char *class, char *head, char *rev, char *path); +extern void cgit_commit_link(char *name, char *title, char *class, char *head, + char *rev); extern void cgit_print_error(char *msg); extern void cgit_print_date(time_t secs, char *format); diff --git a/ui-commit.c b/ui-commit.c index 885f8707cee24b9c2a0c4e2e5f04dfa10e63f3d5..8864fc65a0d62281cfd2839d3b5bc672fe6ee9d4 100644 --- a/ui-commit.c +++ b/ui-commit.c @@ -156,6 +156,9 @@ char *filename; char *tmp; int i; + if (!hex) + hex = cgit_query_head; + if (get_sha1(hex, sha1)) { cgit_print_error(fmt("Bad object id: %s", hex)); return; diff --git a/ui-log.c b/ui-log.c index bb17e1dd98dff3c193fd3afdfd14737ef61cca2c..8d774b05669f36a820c892e24c21c71275ba4deb 100644 --- a/ui-log.c +++ b/ui-log.c @@ -31,11 +31,8 @@ info = cgit_parse_commit(commit); html("<tr><td>"); cgit_print_age(commit->date, TM_WEEK * 2, FMT_SHORTDATE); html("</td><td>"); - char *qry = fmt("h=%s", sha1_to_hex(commit->object.sha1)); - char *url = cgit_pageurl(cgit_query_repo, "commit", qry); - html_link_open(url, NULL, NULL); - html_ntxt(cgit_max_msg_len, info->subject); - html_link_close(); + cgit_commit_link(info->subject, NULL, NULL, cgit_query_head, + sha1_to_hex(commit->object.sha1)); if (cgit_repo->enable_log_filecount) { files = 0; lines = 0; diff --git a/ui-shared.c b/ui-shared.c index 64ee79c16a87b5feea6b5b9ed3e2a9c96872e762..71c899a12bd72ab068a84358931505fd35780cd7 100644 --- a/ui-shared.c +++ b/ui-shared.c @@ -136,8 +136,8 @@ } return fmt("%s", delim); } -static char *reporevlink(char *page, char *name, char *title, char *class, - char *head, char *rev, char *path) +static void reporevlink(char *page, char *name, char *title, char *class, + char *head, char *rev, char *path) { char *delim; @@ -162,6 +162,18 @@ void cgit_log_link(char *name, char *title, char *class, char *head, char *rev, char *path) { reporevlink("log", name, title, class, head, rev, path); +} + +void cgit_commit_link(char *name, char *title, char *class, char *head, + char *rev) +{ + if (strlen(name) > cgit_max_msg_len && cgit_max_msg_len >= 15) { + name[cgit_max_msg_len] = '\0'; + name[cgit_max_msg_len - 1] = '.'; + name[cgit_max_msg_len - 2] = '.'; + name[cgit_max_msg_len - 3] = '.'; + } + reporevlink("commit", name, title, class, head, rev, NULL); } void cgit_print_date(time_t secs, char *format) diff --git a/ui-summary.c b/ui-summary.c index 29b76e368366469353b311a7a8b2a3cc5cfd333c..03dd078d992e1968be6ff389969f8897589af478 100644 --- a/ui-summary.c +++ b/ui-summary.c @@ -15,8 +15,10 @@ int flags, void *cb_data) { struct commit *commit; struct commitinfo *info; - char buf[256], *url; + char buf[256]; + char *ref; + ref = xstrdup(refname); strncpy(buf, refname, sizeof(buf)); commit = lookup_commit(sha1); // object is not really parsed at this point, because of some fallout @@ -25,17 +27,13 @@ commit->object.parsed = 0; if (commit && !parse_commit(commit)){ info = cgit_parse_commit(commit); html("<tr><td>"); - cgit_log_link(refname, NULL, NULL, refname, NULL, NULL); + cgit_log_link(ref, NULL, NULL, ref, NULL, NULL); html("</td><td>"); cgit_print_age(commit->date, -1, NULL); html("</td><td>"); html_txt(info->author); html("</td><td>"); - url = cgit_pageurl(cgit_query_repo, "commit", - fmt("h=%s", sha1_to_hex(sha1))); - html_link_open(url, NULL, NULL); - html_ntxt(cgit_max_msg_len, info->subject); - html_link_close(); + cgit_commit_link(info->subject, NULL, NULL, ref, NULL); html("</td></tr>\n"); cgit_free_commitinfo(info); } else { @@ -45,6 +43,7 @@ html(""); htmlf("*** bad ref %s ***", sha1_to_hex(sha1)); html("</td></tr>\n"); } + free(ref); return 0; } @@ -54,8 +53,9 @@ { char *page, *arg, *url; if (obj->type == OBJ_COMMIT) { - page = "commit"; - arg = "h"; + cgit_commit_link(fmt("commit %s", sha1_to_hex(obj->sha1)), NULL, NULL, + cgit_query_head, sha1_to_hex(obj->sha1)); + return; } else if (obj->type == OBJ_TREE) { page = "tree"; arg = "id"; diff --git a/ui-tree.c b/ui-tree.c index e16b63828dcf7b29c32da71454276b809174d404..1037c823a3fc0f3034066603f79307d6a87aa594 100644 --- a/ui-tree.c +++ b/ui-tree.c @@ -58,7 +58,6 @@ char *name; char *fullpath; enum object_type type; unsigned long size = 0; - char *url, *qry; name = xstrdup(pathname); fullpath = fmt("%s%s%s", cgit_query_path ? cgit_query_path : "",