From 8fe27d51bbf9a053d7e8b2df96044719f0e59a09 Mon Sep 17 00:00:00 2001 From: Reuben D'Netto Date: Fri, 17 Jun 2016 17:29:12 +1000 Subject: [PATCH] Add support for move to parent in main view We also handle unstaged changes correctly now. [ jf: move to separate function and add test. ] Fixes #388 --- NEWS.adoc | 1 + src/main.c | 41 ++++++++++++++ test/main/move-to-parent-test | 102 ++++++++++++++++++++++++++++++++++ 3 files changed, 144 insertions(+) create mode 100755 test/main/move-to-parent-test diff --git a/NEWS.adoc b/NEWS.adoc index b21de1461..c402a857d 100644 --- a/NEWS.adoc +++ b/NEWS.adoc @@ -33,6 +33,7 @@ Improvements: - Make user tigrc location configurable. (GH #479) - Compact relative date display mode. (GH #331) - Add date column option controlling whether to show local date. + - Move to parent commit in the main view. (GH #388) - Support for custom `strftime(3)` date formats, e.g.: set main-view-date = custom diff --git a/src/main.c b/src/main.c index 94ab97b67..34d051d46 100644 --- a/src/main.c +++ b/src/main.c @@ -500,6 +500,43 @@ main_read(struct view *view, struct buffer *buf, bool force_stop) return true; } +static void +main_move_to_parent(struct view *view) +{ + struct line *line = &view->line[view->pos.lineno]; + struct commit *commit = line->data; + + /* Handle staged and unstaged commit lines. */ + if (string_rev_is_null(commit->id)) { + line++; + + } else { + const char *rev_list_parents_argv[] = { + "git", "log", "--no-color", "-n1", "--pretty=format:%P", + commit->id, NULL + }; + char parents[SIZEOF_STR] = ""; + + if (!io_run_buf(rev_list_parents_argv, parents, sizeof(parents))) { + report("Failed to read commit parent(s)"); + return; + } + + /* Find a commit line matching the first parent commit ID. */ + for (line++; view_has_line(view, line); line++) { + commit = line->data; + + if (!strncasecmp(commit->id, parents, strlen(commit->id))) + break; + } + } + + if (view_has_line(view, line)) { + select_view_line(view, line - view->line); + report_clear(); + } +} + enum request main_request(struct view *view, enum request request, struct line *line) { @@ -532,6 +569,10 @@ main_request(struct view *view, enum request request, struct line *line) refresh_view(view); break; + case REQ_PARENT: + main_move_to_parent(view); + break; + default: return request; } diff --git a/test/main/move-to-parent-test b/test/main/move-to-parent-test new file mode 100755 index 000000000..f7b9e2e72 --- /dev/null +++ b/test/main/move-to-parent-test @@ -0,0 +1,102 @@ +#!/bin/sh + +. libtest.sh +. libgit.sh +. "$source_dir/util.sh" + +export LINES=10 + +steps ' + :save-display commit-1.screen + :parent + :save-display commit-2.screen + :parent + :save-display commit-5.screen + :parent + :save-display commit-5-still.screen + :3 + :save-display unstaged-changes.screen + :parent + :save-display commit-3.screen +' + +tigrc < repo.log 2>&1 + +test_tig --merge + +assert_equals 'commit-1.screen' <