This is a prerequisite for implementing a gentle version of read_gitfile. Signed-off-by: Erik Elfström <erik.elfstrom@xxxxxxxxx> --- cache.h | 4 ++++ setup.c | 44 +++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/cache.h b/cache.h index b34447f..dd67695 100644 --- a/cache.h +++ b/cache.h @@ -431,7 +431,11 @@ extern int is_inside_git_dir(void); extern char *git_work_tree_cfg; extern int is_inside_work_tree(void); extern const char *get_git_dir(void); + +#define IS_GIT_DIRECTORY_ERR_PATH_TOO_LONG 1 +extern int is_git_directory_gently(const char *path, int *return_err, struct strbuf *err_msg); extern int is_git_directory(const char *path); + extern char *get_object_directory(void); extern char *get_index_file(void); extern char *get_graft_file(void); diff --git a/setup.c b/setup.c index 979b13f..62ee88c 100644 --- a/setup.c +++ b/setup.c @@ -224,6 +224,18 @@ void verify_non_filename(const char *prefix, const char *arg) "'git <command> [<revision>...] -- [<file>...]'", arg); } +__attribute((format (printf,4,5))) +static void set_error(int *return_err, struct strbuf *err_msg, int err, + const char *msg, ...) +{ + va_list params; + va_start(params, msg); + if (err_msg) + strbuf_vaddf(err_msg, msg, params); + va_end(params); + if (return_err) + *return_err = err; +} /* * Test if it looks like we're at a git directory. @@ -235,14 +247,28 @@ void verify_non_filename(const char *prefix, const char *arg) * - either a HEAD symlink or a HEAD file that is formatted as * a proper "ref:", or a regular file HEAD that has a properly * formatted sha1 object name. + * + * In the event of an error, return_err will be set to an error code + * and err_msg will be set to an error message describing the error + * and 0 will be returned. If no error reporting is required, pass + * NULL for return_err and/or err_msg. */ -int is_git_directory(const char *suspect) +int is_git_directory_gently(const char *suspect, int *return_err, + struct strbuf *err_msg) { char path[PATH_MAX]; size_t len = strlen(suspect); - if (PATH_MAX <= len + strlen("/objects")) - die("Too long path: %.*s", 60, suspect); + if (return_err) + *return_err = 0; + + if (PATH_MAX <= len + strlen("/objects")) { + set_error(return_err, err_msg, + IS_GIT_DIRECTORY_ERR_PATH_TOO_LONG, + "Too long path: %.*s", 60, suspect); + return 0; + } + strcpy(path, suspect); if (getenv(DB_ENVIRONMENT)) { if (access(getenv(DB_ENVIRONMENT), X_OK)) @@ -265,6 +291,18 @@ int is_git_directory(const char *suspect) return 1; } +int is_git_directory(const char *suspect) +{ + int err; + int ret; + struct strbuf err_msg = STRBUF_INIT; + ret = is_git_directory_gently(suspect, &err, &err_msg); + if (err) + die("%s", err_msg.buf); + /* No need to free err_msg, will only be touched in case of error */ + return ret; +} + int is_inside_git_dir(void) { if (inside_git_dir < 0) -- 2.4.0.60.gf7143f7 -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html