Skip to content

Commit

Permalink
Improve the blob view experience
Browse files Browse the repository at this point in the history
* Fix opening the blob view for the same file from a different commit.

* Set %(file) rather than %(file_old) in the blame view to allow the
  blob view to open properly.

* Allow faster access to the blob view when using `tig <file>`.
  • Loading branch information
koutcher committed Mar 26, 2024
1 parent 3c2cdb4 commit 683fdc8
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 7 deletions.
1 change: 1 addition & 0 deletions NEWS.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Improvements:
- Add new "prefetch" reference type for refs created by `git maintenance`
(hidden in default config). (#1318)
- Show the selected commit in the blame view title window.
- Improve the blob view experience.

Bug fixes:

Expand Down
6 changes: 2 additions & 4 deletions src/blame.c
Original file line number Diff line number Diff line change
Expand Up @@ -482,13 +482,11 @@ blame_select(struct view *view, struct line *line)
string_format(view->ref, "%s changed %s", commit->id, commit->filename);
}

if (strcmp(commit->filename, view->env->file))
string_format(view->env->file_old, "%s", commit->filename);
else
view->env->file_old[0] = '\0';
string_ncopy(view->env->file, commit->filename, strlen(commit->filename));

view->env->lineno = view->pos.lineno + 1;
string_ncopy(view->env->text, text, strlen(text));
view->env->blob[0] = 0;
}

static struct view_ops blame_ops = {
Expand Down
14 changes: 12 additions & 2 deletions src/blob.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,19 @@ open_blob_view(struct view *prev, enum open_flags flags)
{
struct view *view = &blob_view;
bool in_blob_view = prev == view;
bool has_blob_selection = view->env->blob[0] || view->env->file[0];

if (!in_blob_view && (view->lines || has_blob_selection)) {
if (!view->env->file[0] && opt_file_args && !opt_file_args[1]) {
const char *ls_tree_argv[] = {
"git", "ls-tree", "-d", "-z", view->env->commit, opt_file_args[0], NULL
};
char buf[SIZEOF_STR] = "";

/* Check that opt_file_args[0] is not a directory */
if (!io_run_buf(ls_tree_argv, buf, sizeof(buf), NULL, false))
string_concat_path(view->env->file, repo.prefix, opt_file_args[0]);
}

if (!in_blob_view && (view->lines || view->env->blob[0] || view->env->file[0])) {
if (view->env->goto_lineno > 0)
flags |= OPEN_RELOAD;
open_view(prev, view, flags);
Expand Down
1 change: 1 addition & 0 deletions src/grep.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ grep_select(struct view *view, struct line *line)
string_ncopy(view->ref, grep->file, strlen(grep->file));
view->env->lineno = grep->lineno + 1;
string_ncopy(view->env->text, text, strlen(text));
view->env->blob[0] = 0;
}

static const char *grep_args[] = {
Expand Down
1 change: 1 addition & 0 deletions src/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ log_copy_rev(struct view *view, struct line *line)
size_t offset = get_graph_indent(text);

string_copy_rev_from_commit_line(view->ref, text + offset);
view->env->blob[0] = 0;
}

static void
Expand Down
1 change: 1 addition & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,7 @@ main_select(struct view *view, struct line *line)
view->env->tag[0] = view->env->remote[0] = view->env->branch[0] = view->env->refname[0] = 0;
}
string_copy_rev(view->env->commit, commit->id);
view->env->blob[0] = 0;
}

static struct view_ops main_ops = {
Expand Down
1 change: 1 addition & 0 deletions src/refs.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ refs_select(struct view *view, struct line *line)
string_copy_rev(view->env->head, reference->ref->id);
string_ncopy(view->env->ref, reference->ref->name, strlen(reference->ref->name));
ref_update_env(view->env, reference->ref, false);
view->env->blob[0] = 0;
}

static struct view_ops refs_ops = {
Expand Down
1 change: 1 addition & 0 deletions src/stash.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ stash_select(struct view *view, struct line *line)
string_ncopy(view->env->stash, state->reflog[line->lineno - 1] + STRING_SIZE("refs/"),
strlen(state->reflog[line->lineno - 1]) - STRING_SIZE("refs/"));
string_copy(view->ref, view->env->stash);
view->env->blob[0] = 0;
}

static enum request
Expand Down
4 changes: 3 additions & 1 deletion src/status.c
Original file line number Diff line number Diff line change
Expand Up @@ -856,8 +856,10 @@ status_select(struct view *view, struct line *line)

string_format(view->ref, text, key, file);
status_stage_info(view->env->status, line->type, status);
if (status)
if (status) {
string_copy(view->env->file, status->new.name);
view->env->blob[0] = 0;
}
}

static struct view_ops status_ops = {
Expand Down

0 comments on commit 683fdc8

Please sign in to comment.