summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 4ed6751)
raw | patch | inline | side by side (parent: 4ed6751)
author | Jonas Fonseca <fonseca@diku.dk> | |
Sat, 22 Mar 2008 02:05:00 +0000 (03:05 +0100) | ||
committer | Jonas Fonseca <fonseca@diku.dk> | |
Sat, 22 Mar 2008 22:56:28 +0000 (23:56 +0100) |
It may both be entered from the command line using:
tig blame [rev] path
or from either the status and stage, as well as by using the tree view
to navigte.
tig blame [rev] path
or from either the status and stage, as well as by using the tree view
to navigte.
manual.txt | patch | blob | history | |
tig.1.txt | patch | blob | history | |
tig.c | patch | blob | history | |
tigrc.5.txt | patch | blob | history |
diff --git a/manual.txt b/manual.txt
index fd466dbef64394189cae4fe2ef717d9bc43d377f..4e8b402b549d65e906bf90e403938243d7a5473d 100644 (file)
--- a/manual.txt
+++ b/manual.txt
Displays the file content or "blob" of data associated with a file
name.
+The blame view::
+ Displays the file content annotated or blamed by commits.
+
The status view::
Displays status of files in the working tree and allows changes to be
staged/unstaged as well as adding of untracked files.
p Switch to pager view.
t Switch to (directory) tree view.
f Switch to (file) blob view.
+B Switch to blame view.
h Switch to help view
S Switch to status view
c Switch to stage view
diff --git a/tig.1.txt b/tig.1.txt
index 8730c06c52abaa1b7f577c77baea4c7d4ddcd8b0..b3f89058c6047ac91d3006c1990219c5ebe3d122 100644 (file)
--- a/tig.1.txt
+++ b/tig.1.txt
[verse]
tig [options] [revisions] [--] [paths]
tig show [options] [revisions] [--] [paths]
+tig blame [rev] path
tig status
tig < [git command output]
show::
Open diff view using the given git show options.
+blame::
+ Show given file annotated or blamed by commits.
+ Optionally limited from given revision.
+
status::
Start up in status view.
COPYRIGHT
---------
-Copyright (c) 2006-2007 Jonas Fonseca <fonseca@diku.dk>
+Copyright (c) 2006-2008 Jonas Fonseca <fonseca@diku.dk>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
index 2551ba313bf6d77556df908f96f0028cc4762fb1..8db304e872cc47f6a2051aff619b9cca0c76bcd1 100644 (file)
--- a/tig.c
+++ b/tig.c
-/* Copyright (c) 2006-2007 Jonas Fonseca <fonseca@diku.dk>
+/* Copyright (c) 2006-2008 Jonas Fonseca <fonseca@diku.dk>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
#define DATE_COLS STRING_SIZE("2006-04-29 14:21 ")
#define AUTHOR_COLS 20
+#define ID_COLS 8
/* The default interval between line numbers. */
-#define NUMBER_INTERVAL 1
+#define NUMBER_INTERVAL 5
#define TABSIZE 8
@@ -129,6 +130,7 @@ static size_t utf8_length(const char *string, size_t max_width, int *trimmed, bo
#define TIG_PAGER_CMD ""
#define TIG_STATUS_CMD ""
#define TIG_STAGE_CMD ""
+#define TIG_BLAME_CMD ""
/* Some ascii-shorthands fitted into the ncurses namespace. */
#define KEY_TAB '\t'
REQ_(VIEW_LOG, "Show log view"), \
REQ_(VIEW_TREE, "Show tree view"), \
REQ_(VIEW_BLOB, "Show blob view"), \
+ REQ_(VIEW_BLAME, "Show blame view"), \
REQ_(VIEW_HELP, "Show help page"), \
REQ_(VIEW_PAGER, "Show pager view"), \
REQ_(VIEW_STATUS, "Show status view"), \
"\n"
"Usage: tig [options] [revs] [--] [paths]\n"
" or: tig show [options] [revs] [--] [paths]\n"
+" or: tig blame [rev] path\n"
" or: tig status\n"
" or: tig < [git command output]\n"
"\n"
"Options:\n"
" -v, --version Show version and exit\n"
-" -h, --help Show help message and exit\n";
+" -h, --help Show help message and exit";
/* Option and state variables. */
static bool opt_date = TRUE;
static enum request opt_request = REQ_VIEW_MAIN;
static char opt_cmd[SIZEOF_STR] = "";
static char opt_path[SIZEOF_STR] = "";
+static char opt_ref[SIZEOF_REF] = "";
static FILE *opt_pipe = NULL;
static char opt_encoding[20] = "UTF-8";
static bool opt_utf8 = TRUE;
warn("ignoring arguments after `%s'", subcommand);
return TRUE;
+ } else if (!strcmp(subcommand, "blame")) {
+ opt_request = REQ_VIEW_BLAME;
+ if (argc <= 2 || argc > 4)
+ die("invalid number of options to blame\n\n%s", usage);
+
+ i = 2;
+ if (argc == 4) {
+ string_ncopy(opt_ref, argv[i], strlen(argv[i]));
+ i++;
+ }
+
+ string_ncopy(opt_path, argv[i], strlen(argv[i]));
+ return TRUE;
+
} else if (!strcmp(subcommand, "show")) {
opt_request = REQ_VIEW_DIFF;
LINE(STAT_NONE, "", COLOR_DEFAULT, COLOR_DEFAULT, 0), \
LINE(STAT_STAGED, "", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
LINE(STAT_UNSTAGED,"", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
-LINE(STAT_UNTRACKED,"", COLOR_MAGENTA, COLOR_DEFAULT, 0)
+LINE(STAT_UNTRACKED,"", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
+LINE(BLAME_DATE, "", COLOR_BLUE, COLOR_DEFAULT, 0), \
+LINE(BLAME_AUTHOR, "", COLOR_GREEN, COLOR_DEFAULT, 0), \
+LINE(BLAME_COMMIT, "", COLOR_DEFAULT, COLOR_DEFAULT, 0), \
+LINE(BLAME_ID, "", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
+LINE(BLAME_LINENO, "", COLOR_CYAN, COLOR_DEFAULT, 0)
enum line_type {
#define LINE(type, line, fg, bg, attr) \
/* State flags */
unsigned int selected:1;
+ unsigned int dirty:1;
void *data; /* User data */
};
{ 'l', REQ_VIEW_LOG },
{ 't', REQ_VIEW_TREE },
{ 'f', REQ_VIEW_BLOB },
+ { 'B', REQ_VIEW_BLAME },
{ 'p', REQ_VIEW_PAGER },
{ 'h', REQ_VIEW_HELP },
{ 'S', REQ_VIEW_STATUS },
KEYMAP_(LOG), \
KEYMAP_(TREE), \
KEYMAP_(BLOB), \
+ KEYMAP_(BLAME), \
KEYMAP_(PAGER), \
KEYMAP_(HELP), \
KEYMAP_(STATUS), \
static struct view_ops main_ops;
static struct view_ops tree_ops;
static struct view_ops blob_ops;
+static struct view_ops blame_ops;
static struct view_ops help_ops;
static struct view_ops status_ops;
static struct view_ops stage_ops;
VIEW_(LOG, "log", &pager_ops, ref_head),
VIEW_(TREE, "tree", &tree_ops, ref_commit),
VIEW_(BLOB, "blob", &blob_ops, ref_blob),
+ VIEW_(BLAME, "blame", &blame_ops, ref_commit),
VIEW_(HELP, "help", &help_ops, ""),
VIEW_(PAGER, "pager", &pager_ops, "stdin"),
VIEW_(STATUS, "status", &status_ops, ""),
return draw_ok;
}
+static void
+redraw_view_dirty(struct view *view)
+{
+ bool dirty = FALSE;
+ int lineno;
+
+ for (lineno = 0; lineno < view->height; lineno++) {
+ struct line *line = &view->line[view->offset + lineno];
+
+ if (!line->dirty)
+ continue;
+ line->dirty = 0;
+ dirty = TRUE;
+ if (!draw_view_line(view, lineno))
+ break;
+ }
+
+ if (!dirty)
+ return;
+ redrawwin(view->win);
+ if (input_mode)
+ wnoutrefresh(view->win);
+ else
+ wrefresh(view->win);
+}
+
static void
redraw_view_from(struct view *view, int lineno)
{
* might have rearranged things. */
redraw_view(view);
+
} else if (redraw_from >= 0) {
/* If this is an incremental update, redraw the previous line
* since for commits some members could have changed when
redraw_view_from(view, redraw_from);
}
+ if (view == VIEW(REQ_VIEW_BLAME))
+ redraw_view_dirty(view);
+
/* Update the title _after_ the redraw so that if the redraw picks up a
* commit reference in view->ref it'll be available here. */
update_view_title(view);
scroll_view(view, request);
break;
+ case REQ_VIEW_BLAME:
+ if (!opt_path[0]) {
+ report("No file chosen, press %s to open tree view",
+ get_key(REQ_VIEW_TREE));
+ break;
+ }
+ if (opt_path[strlen(opt_path) - 1] == '/') {
+ report("Cannot show blame for directory %s", opt_path);
+ break;
+ }
+ string_copy(opt_ref, ref_commit);
+ open_view(view, request, OPEN_DEFAULT);
+ break;
+
case REQ_VIEW_BLOB:
if (!ref_blob[0]) {
report("No file chosen, press %s to open tree view",
if ((view == VIEW(REQ_VIEW_DIFF) &&
view->parent == VIEW(REQ_VIEW_MAIN)) ||
+ (view == VIEW(REQ_VIEW_DIFF) &&
+ view->parent == VIEW(REQ_VIEW_BLAME)) ||
(view == VIEW(REQ_VIEW_STAGE) &&
view->parent == VIEW(REQ_VIEW_STATUS)) ||
(view == VIEW(REQ_VIEW_BLOB) &&
pager_select,
};
+/*
+ * Blame backend
+ *
+ * Loading the blame view is a two phase job:
+ *
+ * 1. File content is read either using opt_path from the
+ * filesystem or using git-cat-file.
+ * 2. Then blame information is incrementally added by
+ * reading output from git-blame.
+ */
+
+struct blame_commit {
+ char id[SIZEOF_REV]; /* SHA1 ID. */
+ char title[128]; /* First line of the commit message. */
+ char author[75]; /* Author of the commit. */
+ struct tm time; /* Date from the author ident. */
+ char filename[128]; /* Name of file. */
+};
+
+struct blame {
+ struct blame_commit *commit;
+ unsigned int header:1;
+ char text[1];
+};
+
+#define BLAME_CAT_FILE_CMD "git cat-file blob %s:%s"
+#define BLAME_INCREMENTAL_CMD "git blame --incremental %s %s"
+
+static bool
+blame_open(struct view *view)
+{
+ char path[SIZEOF_STR];
+ char ref[SIZEOF_STR] = "";
+
+ if (sq_quote(path, 0, opt_path) >= sizeof(path))
+ return FALSE;
+
+ if (*opt_ref && sq_quote(ref, 0, opt_ref) >= sizeof(ref))
+ return FALSE;
+
+ if (*opt_ref) {
+ if (!string_format(view->cmd, BLAME_CAT_FILE_CMD, ref, path))
+ return FALSE;
+ } else {
+ view->pipe = fopen(opt_path, "r");
+ if (!view->pipe &&
+ !string_format(view->cmd, BLAME_CAT_FILE_CMD, "HEAD", path))
+ return FALSE;
+ }
+
+ if (!view->pipe)
+ view->pipe = popen(view->cmd, "r");
+ if (!view->pipe)
+ return FALSE;
+
+ if (!string_format(view->cmd, BLAME_INCREMENTAL_CMD, ref, path))
+ return FALSE;
+
+ string_format(view->ref, "%s ...", opt_path);
+ string_copy_rev(view->vid, opt_path);
+ set_nonblocking_input(TRUE);
+
+ if (view->line) {
+ int i;
+
+ for (i = 0; i < view->lines; i++)
+ free(view->line[i].data);
+ free(view->line);
+ }
+
+ view->lines = view->line_alloc = view->line_size = view->lineno = 0;
+ view->offset = view->lines = view->lineno = 0;
+ view->line = NULL;
+ view->start_time = time(NULL);
+}
+
+static struct blame_commit *
+get_blame_commit(struct view *view, const char *id)
+{
+ size_t i;
+
+ for (i = 0; i < view->lines; i++) {
+ struct blame *blame = view->line[i].data;
+
+ if (!blame->commit)
+ continue;
+
+ if (!strncmp(blame->commit->id, id, SIZEOF_REV - 1))
+ return blame->commit;
+ }
+
+ {
+ struct blame_commit *commit = calloc(1, sizeof(*commit));
+
+ if (commit)
+ string_ncopy(commit->id, id, SIZEOF_REV);
+ return commit;
+ }
+}
+
+static bool
+parse_number(char **posref, size_t *number, size_t min, size_t max)
+{
+ char *pos = *posref;
+
+ *posref = NULL;
+ pos = strchr(pos + 1, ' ');
+ if (!pos || !isdigit(pos[1]))
+ return FALSE;
+ *number = atoi(pos + 1);
+ if (*number < min || *number > max)
+ return FALSE;
+
+ *posref = pos;
+ return TRUE;
+}
+
+static struct blame_commit *
+parse_blame_commit(struct view *view, char *text, int *blamed)
+{
+ struct blame_commit *commit;
+ struct blame *blame;
+ char *pos = text + SIZEOF_REV - 1;
+ size_t lineno;
+ size_t group;
+ struct line *line;
+
+ if (strlen(text) <= SIZEOF_REV || *pos != ' ')
+ return NULL;
+
+ if (!parse_number(&pos, &lineno, 1, view->lines) ||
+ !parse_number(&pos, &group, 1, view->lines - lineno + 1))
+ return NULL;
+
+ commit = get_blame_commit(view, text);
+ if (!commit)
+ return NULL;
+
+ *blamed += group;
+ while (group--) {
+ struct line *line = &view->line[lineno + group - 1];
+
+ blame = line->data;
+ blame->commit = commit;
+ line->dirty = 1;
+ }
+ blame->header = 1;
+
+ return commit;
+}
+
+static bool
+blame_read_file(struct view *view, char *line)
+{
+ if (!line) {
+ FILE *pipe = NULL;
+
+ if (view->lines > 0)
+ pipe = popen(view->cmd, "r");
+ view->cmd[0] = 0;
+ if (!pipe) {
+ report("Failed to load blame data");
+ return TRUE;
+ }
+
+ fclose(view->pipe);
+ view->pipe = pipe;
+ return FALSE;
+
+ } else {
+ size_t linelen = strlen(line);
+ struct blame *blame = malloc(sizeof(*blame) + linelen);
+
+ if (!line)
+ return FALSE;
+
+ blame->commit = NULL;
+ strncpy(blame->text, line, linelen);
+ blame->text[linelen] = 0;
+ return add_line_data(view, blame, LINE_BLAME_COMMIT) != NULL;
+ }
+}
+
+static bool
+match_blame_header(const char *name, char **line)
+{
+ size_t namelen = strlen(name);
+ bool matched = !strncmp(name, *line, namelen);
+
+ if (matched)
+ *line += namelen;
+
+ return matched;
+}
+
+static bool
+blame_read(struct view *view, char *line)
+{
+ static struct blame_commit *commit = NULL;
+ static int blamed = 0;
+ static time_t author_time;
+
+ if (*view->cmd)
+ return blame_read_file(view, line);
+
+ if (!line) {
+ /* Reset all! */
+ commit = NULL;
+ blamed = 0;
+ string_format(view->ref, "%s", view->vid);
+ if (view_is_displayed(view)) {
+ update_view_title(view);
+ redraw_view_from(view, 0);
+ }
+ return TRUE;
+ }
+
+ if (!commit) {
+ commit = parse_blame_commit(view, line, &blamed);
+ string_format(view->ref, "%s %2d%%", view->vid,
+ blamed * 100 / view->lines);
+
+ } else if (match_blame_header("author ", &line)) {
+ string_ncopy(commit->author, line, strlen(line));
+
+ } else if (match_blame_header("author-time ", &line)) {
+ author_time = (time_t) atol(line);
+
+ } else if (match_blame_header("author-tz ", &line)) {
+ long tz;
+
+ tz = ('0' - line[1]) * 60 * 60 * 10;
+ tz += ('0' - line[2]) * 60 * 60;
+ tz += ('0' - line[3]) * 60;
+ tz += ('0' - line[4]) * 60;
+
+ if (line[0] == '-')
+ tz = -tz;
+
+ author_time -= tz;
+ gmtime_r(&author_time, &commit->time);
+
+ } else if (match_blame_header("summary ", &line)) {
+ string_ncopy(commit->title, line, strlen(line));
+
+ } else if (match_blame_header("filename ", &line)) {
+ string_ncopy(commit->filename, line, strlen(line));
+ commit = NULL;
+ }
+
+ return TRUE;
+}
+
+static bool
+blame_draw(struct view *view, struct line *line, unsigned int lineno, bool selected)
+{
+ int tilde_attr = -1;
+ struct blame *blame = line->data;
+ int col = 0;
+
+ wmove(view->win, lineno, 0);
+
+ if (selected) {
+ wattrset(view->win, get_line_attr(LINE_CURSOR));
+ wchgat(view->win, -1, 0, LINE_CURSOR, NULL);
+ } else {
+ wattrset(view->win, A_NORMAL);
+ tilde_attr = get_line_attr(LINE_MAIN_DELIM);
+ }
+
+ if (opt_date) {
+ int n;
+
+ if (!selected)
+ wattrset(view->win, get_line_attr(LINE_MAIN_DATE));
+ if (blame->commit) {
+ char buf[DATE_COLS + 1];
+ int timelen;
+
+ timelen = strftime(buf, sizeof(buf), DATE_FORMAT, &blame->commit->time);
+ n = draw_text(view, buf, view->width - col, FALSE, tilde_attr);
+ draw_text(view, " ", view->width - col - n, FALSE, tilde_attr);
+ }
+
+ col += DATE_COLS;
+ wmove(view->win, lineno, col);
+ if (col >= view->width)
+ return TRUE;
+ }
+
+ if (opt_author) {
+ int max = MIN(AUTHOR_COLS - 1, view->width - col);
+
+ if (!selected)
+ wattrset(view->win, get_line_attr(LINE_MAIN_AUTHOR));
+ if (blame->commit)
+ draw_text(view, blame->commit->author, max, TRUE, tilde_attr);
+ col += AUTHOR_COLS;
+ if (col >= view->width)
+ return TRUE;
+ wmove(view->win, lineno, col);
+ }
+
+ {
+ int max = MIN(ID_COLS - 1, view->width - col);
+
+ if (!selected)
+ wattrset(view->win, get_line_attr(LINE_BLAME_ID));
+ if (blame->commit)
+ draw_text(view, blame->commit->id, max, FALSE, -1);
+ col += ID_COLS;
+ if (col >= view->width)
+ return TRUE;
+ wmove(view->win, lineno, col);
+ }
+
+ {
+ unsigned long real_lineno = view->offset + lineno + 1;
+ char number[10] = " ";
+ int max = MIN(view->digits, STRING_SIZE(number));
+ bool showtrimmed = FALSE;
+
+ if (real_lineno == 1 ||
+ (real_lineno % opt_num_interval) == 0) {
+ char fmt[] = "%1ld";
+
+ if (view->digits <= 9)
+ fmt[1] = '0' + view->digits;
+
+ if (!string_format(number, fmt, real_lineno))
+ number[0] = 0;
+ showtrimmed = TRUE;
+ }
+
+ if (max > view->width - col)
+ max = view->width - col;
+ if (!selected)
+ wattrset(view->win, get_line_attr(LINE_BLAME_LINENO));
+ col += draw_text(view, number, max, showtrimmed, tilde_attr);
+ if (col >= view->width)
+ return TRUE;
+ }
+
+ if (!selected)
+ wattrset(view->win, A_NORMAL);
+
+ if (col >= view->width)
+ return TRUE;
+ waddch(view->win, ACS_VLINE);
+ col++;
+ if (col >= view->width)
+ return TRUE;
+ waddch(view->win, ' ');
+ col++;
+ col += draw_text(view, blame->text, view->width - col, TRUE, tilde_attr);
+
+ return TRUE;
+}
+
+static enum request
+blame_request(struct view *view, enum request request, struct line *line)
+{
+ enum open_flags flags = display[0] == view ? OPEN_SPLIT : OPEN_DEFAULT;
+ struct blame *blame = line->data;
+
+ switch (request) {
+ case REQ_ENTER:
+ if (!blame->commit) {
+ report("No commit loaded yet");
+ break;
+ }
+
+ if (!strcmp(blame->commit->id, "0000000000000000000000000000000000000000")) {
+ char path[SIZEOF_STR];
+
+ if (sq_quote(path, 0, view->vid) >= sizeof(path))
+ break;
+ string_format(opt_cmd, "git diff-index --root --patch-with-stat -C -M --cached HEAD -- %s 2>/dev/null", path);
+ }
+
+ open_view(view, REQ_VIEW_DIFF, flags);
+ break;
+
+ default:
+ return request;
+ }
+
+ return REQ_NONE;
+}
+
+static bool
+blame_grep(struct view *view, struct line *line)
+{
+ struct blame *blame = line->data;
+ struct blame_commit *commit = blame->commit;
+ regmatch_t pmatch;
+
+#define MATCH(text) \
+ (*text && regexec(view->regex, text, 1, &pmatch, 0) != REG_NOMATCH)
+
+ if (commit) {
+ char buf[DATE_COLS + 1];
+
+ if (MATCH(commit->title) ||
+ MATCH(commit->author) ||
+ MATCH(commit->id))
+ return TRUE;
+
+ if (strftime(buf, sizeof(buf), DATE_FORMAT, &commit->time) &&
+ MATCH(buf))
+ return TRUE;
+ }
+
+ return MATCH(blame->text);
+
+#undef MATCH
+}
+
+static void
+blame_select(struct view *view, struct line *line)
+{
+ struct blame *blame = line->data;
+ struct blame_commit *commit = blame->commit;
+
+ if (!commit)
+ return;
+
+ if (!strcmp(commit->id, "0000000000000000000000000000000000000000"))
+ string_ncopy(ref_commit, "HEAD", 4);
+ else
+ string_copy_rev(ref_commit, commit->id);
+}
+
+static struct view_ops blame_ops = {
+ "line",
+ blame_open,
+ blame_read,
+ blame_draw,
+ blame_request,
+ blame_grep,
+ blame_select,
+};
/*
* Status backend
open_editor(status->status != '?', status->new.name);
break;
+ case REQ_VIEW_BLAME:
+ if (status) {
+ string_copy(opt_path, status->new.name);
+ opt_ref[0] = 0;
+ }
+ return request;
+
case REQ_ENTER:
/* After returning the status view has been split to
* show the stage view. No further reloading is
open_editor(stage_status.status != '?', stage_status.new.name);
break;
+ case REQ_VIEW_BLAME:
+ if (stage_status.new.name[0]) {
+ string_copy(opt_path, stage_status.new.name);
+ opt_ref[0] = 0;
+ }
+ return request;
+
case REQ_ENTER:
pager_request(view, request, line);
break;
diff --git a/tigrc.5.txt b/tigrc.5.txt
index bed3116c202f3881e254762a04cca9e56dd59ef4..86d94c00e52879cd9a1ae0a1bbdd57f03f920e17 100644 (file)
--- a/tigrc.5.txt
+++ b/tigrc.5.txt
view-log Show log view
view-tree Show tree view
view-blob Show blob view
+view-blame Show blame view
view-status Show status view
view-stage Show stage view
view-pager Show pager view
*main-date*, *main-author*, *main-commit*, *main-delim*, *main-tag*,
*main-local-tag*, *main-ref*, *main-remote*, *main-revgraph*
+Blame view colors::
+
+The colors used for the blame view are similar to those in the main view.
+The commit ID color can be colored using *blame-id*.
+
+*blame-date*, *blame-author*, *blame-commit*, *blame-id*, *blame-lineno*
+
--
Highlighting