Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for move to parent in main view. #506

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/tig/view.h
Original file line number Diff line number Diff line change
Expand Up @@ -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: */
6 changes: 5 additions & 1 deletion src/tig.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes to support REQ_PARENT should go in src/main.c which is all the main view specific code. There's a main_request method where the case REQ_PARENT and related move_view changes in this patch can exist.

} else {
report("Moving to parent is not supported by the %s view", view->name);
}
break;

case REQ_BACK:
Expand Down
42 changes: 42 additions & 0 deletions src/view.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/

#include "tig/tig.h"
#include "tig/main.h"
#include "tig/argv.h"
#include "tig/repo.h"
#include "tig/watch.h"
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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.
*/
Expand Down