Skip to content

Commit

Permalink
check_filename(): make verify_filename() callable without dying
Browse files Browse the repository at this point in the history
Make it possible to invole the logic of verify_filename() to make sure the
pathname arguments are unambiguous without actually dying.  The caller may
want to do something different.
  • Loading branch information
Junio C Hamano committed Oct 18, 2009
1 parent ad12b81 commit c6e8c80
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
1 change: 1 addition & 0 deletions cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ extern const char *setup_git_directory_gently(int *);
extern const char *setup_git_directory(void);
extern const char *prefix_path(const char *prefix, int len, const char *path);
extern const char *prefix_filename(const char *prefix, int len, const char *path);
extern int check_filename(const char *prefix, const char *name);
extern void verify_filename(const char *prefix, const char *name);
extern void verify_non_filename(const char *prefix, const char *name);

Expand Down
38 changes: 20 additions & 18 deletions setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ const char *prefix_filename(const char *pfx, int pfx_len, const char *arg)
return path;
}

int check_filename(const char *prefix, const char *arg)
{
const char *name;
struct stat st;

name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg;
if (!lstat(name, &st))
return 1; /* file exists */
if (errno == ENOENT || errno == ENOTDIR)
return 0; /* file does not exist */
die_errno("failed to stat '%s'", arg);
}

/*
* Verify a filename that we got as an argument for a pathspec
* entry. Note that a filename that begins with "-" never verifies
Expand All @@ -70,18 +83,12 @@ const char *prefix_filename(const char *pfx, int pfx_len, const char *arg)
*/
void verify_filename(const char *prefix, const char *arg)
{
const char *name;
struct stat st;

if (*arg == '-')
die("bad flag '%s' used after filename", arg);
name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg;
if (!lstat(name, &st))
if (check_filename(prefix, arg))
return;
if (errno == ENOENT)
die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
"Use '--' to separate paths from revisions", arg);
die_errno("failed to stat '%s'", arg);
die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
"Use '--' to separate paths from revisions", arg);
}

/*
Expand All @@ -91,19 +98,14 @@ void verify_filename(const char *prefix, const char *arg)
*/
void verify_non_filename(const char *prefix, const char *arg)
{
const char *name;
struct stat st;

if (!is_inside_work_tree() || is_inside_git_dir())
return;
if (*arg == '-')
return; /* flag */
name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg;
if (!lstat(name, &st))
die("ambiguous argument '%s': both revision and filename\n"
"Use '--' to separate filenames from revisions", arg);
if (errno != ENOENT && errno != ENOTDIR)
die_errno("failed to stat '%s'", arg);
if (!check_filename(prefix, arg))
return;
die("ambiguous argument '%s': both revision and filename\n"
"Use '--' to separate filenames from revisions", arg);
}

const char **get_pathspec(const char *prefix, const char **pathspec)
Expand Down

0 comments on commit c6e8c80

Please sign in to comment.