From 034d61f6f69f670731adec6aef1b02cfefddbfbd Mon Sep 17 00:00:00 2001 From: Reuben D'Netto Date: Fri, 17 Jun 2016 17:29:12 +1000 Subject: [PATCH] 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: */