Skip to content

Commit

Permalink
Fail if tig is given an invalid or ambiguous ref
Browse files Browse the repository at this point in the history
Fixes #980
  • Loading branch information
koutcher committed Feb 25, 2020
1 parent 5bb948f commit b824232
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 18 deletions.
2 changes: 1 addition & 1 deletion include/tig/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ enum status_code io_load(struct io *io, const char *separators,
io_read_fn read_property, void *data);
enum status_code io_load_span(struct io *io, const char *separators,
size_t *lineno, io_read_fn read_property, void *data);
enum status_code io_run_load(const char **argv, const char *separators,
enum status_code io_run_load(struct io *io, const char **argv, const char *separators,
io_read_fn read_property, void *data);
char *io_memchr(struct buffer *buf, char *data, int c);

Expand Down
8 changes: 3 additions & 5 deletions src/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -702,14 +702,12 @@ io_load(struct io *io, const char *separators,
}

enum status_code
io_run_load(const char **argv, const char *separators,
io_run_load(struct io *io, const char **argv, const char *separators,
io_read_fn read_property, void *data)
{
struct io io;

if (!io_run(&io, IO_RD, NULL, NULL, argv))
if (!io_run(io, IO_RD, NULL, NULL, argv))
return error("Failed to open IO");
return io_load(&io, separators, read_property, data);
return io_load(io, separators, read_property, data);
}

bool
Expand Down
3 changes: 2 additions & 1 deletion src/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -1475,9 +1475,10 @@ read_repo_config_option(char *name, size_t namelen, char *value, size_t valuelen
enum status_code
load_git_config(void)
{
struct io io;
const char *config_list_argv[] = { "git", "config", "--list", NULL };

return io_run_load(config_list_argv, "=", read_repo_config_option, NULL);
return io_run_load(&io, config_list_argv, "=", read_repo_config_option, NULL);
}

/* vim: set ts=8 sw=8 noexpandtab: */
3 changes: 2 additions & 1 deletion src/refdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ cleanup_refs(void *data, void *ref_)
static enum status_code
reload_refs(bool force)
{
struct io io;
const char *ls_remote_argv[SIZEOF_ARG] = {
"git", "show-ref", "--head", "--dereference", NULL
};
Expand Down Expand Up @@ -342,7 +343,7 @@ reload_refs(bool force)
string_map_clear(&refs_by_id);
string_map_foreach(&refs_by_name, invalidate_refs, NULL);

code = io_run_load(ls_remote_argv, " \t", read_ref, &opt);
code = io_run_load(&io, ls_remote_argv, " \t", read_ref, &opt);
if (code != SUCCESS)
return code;

Expand Down
3 changes: 2 additions & 1 deletion src/repo.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ read_repo_info(char *name, size_t namelen, char *value, size_t valuelen, void *d
static enum status_code
reload_repo_info(const char **rev_parse_argv)
{
struct io io;
struct repo_info_state state = { rev_parse_argv + 2 };

return io_run_load(rev_parse_argv, "\n", read_repo_info, &state);
return io_run_load(&io, rev_parse_argv, "\n", read_repo_info, &state);
}

enum status_code
Expand Down
20 changes: 11 additions & 9 deletions src/tig.c
Original file line number Diff line number Diff line change
Expand Up @@ -402,29 +402,32 @@ read_filter_args(char *name, size_t namelen, char *value, size_t valuelen, void
return argv_append(filter_args, name) ? SUCCESS : ERROR_OUT_OF_MEMORY;
}

static void
static bool
filter_rev_parse(const char ***args, const char *arg1, const char *arg2, const char *argv[])
{
const char *rev_parse_argv[SIZEOF_ARG] = { "git", "rev-parse", arg1, arg2 };
const char **all_argv = NULL;
struct io io;

if (!argv_append_array(&all_argv, rev_parse_argv) ||
!argv_append_array(&all_argv, argv) ||
io_run_load(all_argv, "\n", read_filter_args, args) != SUCCESS)
io_run_load(&io, all_argv, "\n", read_filter_args, args) != SUCCESS)
die("Failed to split arguments");
argv_free(all_argv);
free(all_argv);

return !io.status;
}

static void
filter_options(const char *argv[], bool rev_parse)
filter_options(const char *argv[], enum request request)
{
const char **flags = NULL;
int next, flags_pos;

update_options_from_argv(argv);

if (!rev_parse) {
if (request == REQ_VIEW_GREP || request == REQ_VIEW_REFS) {
opt_cmdline_args = argv;
return;
}
Expand All @@ -446,7 +449,9 @@ filter_options(const char *argv[], bool rev_parse)

argv[flags_pos] = NULL;

filter_rev_parse(&opt_file_args, "--no-revs", "--no-flags", argv);
if (!filter_rev_parse(&opt_file_args, "--no-revs", "--no-flags", argv) &&
request != REQ_VIEW_BLAME)
die("No revisions match the given arguments.");
filter_rev_parse(&flags, "--flags", "--no-revs", argv);

if (flags) {
Expand All @@ -473,7 +478,6 @@ parse_options(int argc, const char *argv[], bool pager_mode)
enum request request;
const char *subcommand;
bool seen_dashdash = false;
bool rev_parse = true;
const char **filter_argv = NULL;
int i;

Expand Down Expand Up @@ -503,7 +507,6 @@ parse_options(int argc, const char *argv[], bool pager_mode)

} else if (!strcmp(subcommand, "grep")) {
request = REQ_VIEW_GREP;
rev_parse = false;

} else if (!strcmp(subcommand, "show")) {
request = REQ_VIEW_DIFF;
Expand All @@ -519,7 +522,6 @@ parse_options(int argc, const char *argv[], bool pager_mode)

} else if (!strcmp(subcommand, "refs")) {
request = REQ_VIEW_REFS;
rev_parse = false;

} else {
subcommand = NULL;
Expand Down Expand Up @@ -568,7 +570,7 @@ parse_options(int argc, const char *argv[], bool pager_mode)
}

if (filter_argv)
filter_options(filter_argv, rev_parse);
filter_options(filter_argv, request);

return request;
}
Expand Down

0 comments on commit b824232

Please sign in to comment.