From d140e257779a48ff6d5bb8386774a0c6462b7137 Mon Sep 17 00:00:00 2001 From: Reuben D'Netto Date: Fri, 10 Jun 2016 11:43:00 +1000 Subject: [PATCH 1/3] Added support for move to parent in main view. --- include/tig/view.h | 1 + src/tig.c | 6 +++++- src/view.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/include/tig/view.h b/include/tig/view.h index 2ef4eaa5b..071669c86 100644 --- a/include/tig/view.h +++ b/include/tig/view.h @@ -396,6 +396,7 @@ struct line *add_line_text(struct view *view, const char *text, enum line_type t struct line *add_line_text_at(struct view *view, unsigned long pos, const char *text, enum line_type type, size_t cells); struct line * PRINTF_LIKE(3, 4) add_line_format(struct view *view, enum line_type type, const char *fmt, ...); bool append_line_format(struct view *view, struct line *line, const char *fmt, ...); +enum status_code read_hash(char *id, size_t idlen, char *s2, size_t n2, void *data); #endif /* vim: set ts=8 sw=8 noexpandtab: */ diff --git a/src/tig.c b/src/tig.c index 2f4dee254..f2a47a4e0 100644 --- a/src/tig.c +++ b/src/tig.c @@ -266,7 +266,11 @@ view_driver(struct view *view, enum request request) break; case REQ_PARENT: - report("Moving to parent is not supported by the %s view", view->name); + if (! strcmp(view->name, "main")) { + move_view(view, request); + } else { + report("Moving to parent is not supported by the %s view", view->name); + } break; case REQ_BACK: diff --git a/src/view.c b/src/view.c index 1cc9e6a70..ffab0ad13 100644 --- a/src/view.c +++ b/src/view.c @@ -12,6 +12,7 @@ */ #include "tig/tig.h" +#include "tig/main.h" #include "tig/argv.h" #include "tig/repo.h" #include "tig/watch.h" @@ -171,6 +172,12 @@ move_view(struct view *view, enum request request) { int scroll_steps = 0; int steps; + struct commit *commit = view->line[view->pos.lineno].data; + + char current_hash[SIZEOF_REV + 1] = ""; + char parent_hash[SIZEOF_REV] = ""; + static const char *rev_list_parents_argv[] = { "git", "rev-list", "-n1", NULL, NULL }; + rev_list_parents_argv[3] = current_hash; switch (request) { case REQ_MOVE_FIRST_LINE: @@ -211,6 +218,31 @@ move_view(struct view *view, enum request request) steps = 1; break; + case REQ_PARENT: + string_copy_rev(current_hash, commit->id); + + //can't assume the hash takes the full 41 bytes + size_t n = strlen(commit->id); + current_hash[n] = '~'; + current_hash[n + 1] = 0; + int i; + + if(io_run_load(rev_list_parents_argv, " ", read_hash, parent_hash) != SUCCESS || !strlen(parent_hash)) { + report("Unable to locate parent."); + return; + } + + for (i = 0; i < view->lines; i++) { + struct commit *commit = view->line[i].data; + + if (!strncasecmp(commit->id, parent_hash, strlen(parent_hash))) { + steps = i - view->pos.lineno; + break; + } + } + + break; + default: die("request %d not handled in switch", request); } @@ -1610,6 +1642,16 @@ append_line_format(struct view *view, struct line *line, const char *fmt, ...) return true; } +enum status_code read_hash(char *id, size_t idlen, char *s2, size_t s2len, void *data) { + char *result = data; + + if (! result[0] && idlen > 0) { + string_copy_rev(result, id); + } + + return SUCCESS; +} + /* * Global view state. */ From 0164f5136cc8a1ff47ddf925e6e3d3b2f0039cdf Mon Sep 17 00:00:00 2001 From: Reuben D'Netto Date: Fri, 17 Jun 2016 17:26:21 +1000 Subject: [PATCH 2/3] Revert "Added support for move to parent in main view." This reverts commit d140e257779a48ff6d5bb8386774a0c6462b7137. --- include/tig/view.h | 1 - src/tig.c | 6 +----- src/view.c | 42 ------------------------------------------ 3 files changed, 1 insertion(+), 48 deletions(-) diff --git a/include/tig/view.h b/include/tig/view.h index 071669c86..2ef4eaa5b 100644 --- a/include/tig/view.h +++ b/include/tig/view.h @@ -396,7 +396,6 @@ struct line *add_line_text(struct view *view, const char *text, enum line_type t struct line *add_line_text_at(struct view *view, unsigned long pos, const char *text, enum line_type type, size_t cells); struct line * PRINTF_LIKE(3, 4) add_line_format(struct view *view, enum line_type type, const char *fmt, ...); bool append_line_format(struct view *view, struct line *line, const char *fmt, ...); -enum status_code read_hash(char *id, size_t idlen, char *s2, size_t n2, void *data); #endif /* vim: set ts=8 sw=8 noexpandtab: */ diff --git a/src/tig.c b/src/tig.c index f2a47a4e0..2f4dee254 100644 --- a/src/tig.c +++ b/src/tig.c @@ -266,11 +266,7 @@ view_driver(struct view *view, enum request request) break; case REQ_PARENT: - if (! strcmp(view->name, "main")) { - move_view(view, request); - } else { - report("Moving to parent is not supported by the %s view", view->name); - } + report("Moving to parent is not supported by the %s view", view->name); break; case REQ_BACK: diff --git a/src/view.c b/src/view.c index ffab0ad13..1cc9e6a70 100644 --- a/src/view.c +++ b/src/view.c @@ -12,7 +12,6 @@ */ #include "tig/tig.h" -#include "tig/main.h" #include "tig/argv.h" #include "tig/repo.h" #include "tig/watch.h" @@ -172,12 +171,6 @@ move_view(struct view *view, enum request request) { int scroll_steps = 0; int steps; - struct commit *commit = view->line[view->pos.lineno].data; - - char current_hash[SIZEOF_REV + 1] = ""; - char parent_hash[SIZEOF_REV] = ""; - static const char *rev_list_parents_argv[] = { "git", "rev-list", "-n1", NULL, NULL }; - rev_list_parents_argv[3] = current_hash; switch (request) { case REQ_MOVE_FIRST_LINE: @@ -218,31 +211,6 @@ move_view(struct view *view, enum request request) steps = 1; break; - case REQ_PARENT: - string_copy_rev(current_hash, commit->id); - - //can't assume the hash takes the full 41 bytes - size_t n = strlen(commit->id); - current_hash[n] = '~'; - current_hash[n + 1] = 0; - int i; - - if(io_run_load(rev_list_parents_argv, " ", read_hash, parent_hash) != SUCCESS || !strlen(parent_hash)) { - report("Unable to locate parent."); - return; - } - - for (i = 0; i < view->lines; i++) { - struct commit *commit = view->line[i].data; - - if (!strncasecmp(commit->id, parent_hash, strlen(parent_hash))) { - steps = i - view->pos.lineno; - break; - } - } - - break; - default: die("request %d not handled in switch", request); } @@ -1642,16 +1610,6 @@ append_line_format(struct view *view, struct line *line, const char *fmt, ...) return true; } -enum status_code read_hash(char *id, size_t idlen, char *s2, size_t s2len, void *data) { - char *result = data; - - if (! result[0] && idlen > 0) { - string_copy_rev(result, id); - } - - return SUCCESS; -} - /* * Global view state. */ From 034d61f6f69f670731adec6aef1b02cfefddbfbd Mon Sep 17 00:00:00 2001 From: Reuben D'Netto Date: Fri, 17 Jun 2016 17:29:12 +1000 Subject: [PATCH 3/3] Reimplemented "Added support for move to parent in main view.", with logic now in main.c We also handle unstaged changes correctly now. --- include/tig/main.h | 1 + src/main.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/include/tig/main.h b/include/tig/main.h index 8c51663a9..acd108d28 100644 --- a/include/tig/main.h +++ b/include/tig/main.h @@ -47,6 +47,7 @@ enum request main_request(struct view *view, enum request request, struct line * void main_select(struct view *view, struct line *line); void main_done(struct view *view); bool main_status_exists(struct view *view, enum line_type type); +enum status_code read_hash(char *id, size_t idlen, char *s2, size_t n2, void *data); extern struct view main_view; diff --git a/src/main.c b/src/main.c index 94ab97b67..6ffa6ea30 100644 --- a/src/main.c +++ b/src/main.c @@ -505,6 +505,12 @@ main_request(struct view *view, enum request request, struct line *line) { enum open_flags flags = (view_is_displayed(view) && request != REQ_VIEW_DIFF) ? OPEN_SPLIT : OPEN_DEFAULT; + struct commit *commit = view->line[view->pos.lineno].data; + + char current_hash[SIZEOF_REV + 1] = ""; + char parent_hash[SIZEOF_REV] = ""; + static const char *rev_list_parents_argv[] = { "git", "rev-list", "-n1", NULL, NULL }; + rev_list_parents_argv[3] = current_hash; switch (request) { case REQ_NEXT: @@ -532,6 +538,38 @@ main_request(struct view *view, enum request request, struct line *line) refresh_view(view); break; + case REQ_PARENT: + string_copy_rev(current_hash, commit->id); + + if(!strcasecmp(commit->id, NULL_ID)) { + // Unstaged changes - parent is HEAD + strcpy(current_hash, "HEAD"); + } else { + // Can't assume the hash takes the full 41 bytes + size_t n = strlen(commit->id); + current_hash[n] = '~'; + current_hash[n + 1] = 0; + } + + + if(io_run_load(rev_list_parents_argv, " ", read_hash, parent_hash) != SUCCESS || !strlen(parent_hash)) { + report("Unable to locate parent."); + break; + } + + int i; + for (i = 0; i < view->lines; i++) { + struct commit *commit = view->line[i].data; + + if (!strncasecmp(commit->id, parent_hash, strlen(parent_hash))) { + select_view_line(view, i); + report_clear(); + break; + } + } + + break; + default: return request; } @@ -575,6 +613,16 @@ static struct view_ops main_ops = { main_get_column_data, }; +enum status_code read_hash(char *id, size_t idlen, char *s2, size_t s2len, void *data) { + char *result = data; + + if (! result[0] && idlen > 0) { + string_copy_rev(result, id); + } + + return SUCCESS; +} + DEFINE_VIEW(main); /* vim: set ts=8 sw=8 noexpandtab: */