Skip to content

Commit

Permalink
Added support for move to parent in main view.
Browse files Browse the repository at this point in the history
  • Loading branch information
rdnetto4 committed Jun 10, 2016
1 parent 68b7868 commit d140e25
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
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);
} 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

0 comments on commit d140e25

Please sign in to comment.