diff options
author | Edward Thomson <ethomson@edwardthomson.com> | 2022-02-26 23:11:53 -0500 |
---|---|---|
committer | Edward Thomson <ethomson@edwardthomson.com> | 2022-02-26 23:11:53 -0500 |
commit | cf7def1477822d6555bd50e72d22ce9581e41a30 (patch) | |
tree | 512ce81421d2c323a41a5cfb8ec18466a9c952ff | |
parent | b862be9b09176b3e292694678abf6d5bb29ee14e (diff) | |
download | libgit2-cf7def1477822d6555bd50e72d22ce9581e41a30.tar.gz |
cli: support bare clones
-rw-r--r-- | src/cli/cmd_clone.c | 21 | ||||
-rw-r--r-- | src/util/str.c | 11 | ||||
-rw-r--r-- | src/util/str.h | 1 |
3 files changed, 28 insertions, 5 deletions
diff --git a/src/cli/cmd_clone.c b/src/cli/cmd_clone.c index 196c95bda..34eedfe87 100644 --- a/src/cli/cmd_clone.c +++ b/src/cli/cmd_clone.c @@ -19,6 +19,8 @@ static int show_help; static int quiet; +static git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT; + static char *remote_path, *local_path; static bool local_path_exists; static cli_progress progress = CLI_PROGRESS_INIT; @@ -30,6 +32,8 @@ static const cli_opt_spec opts[] = { { CLI_OPT_TYPE_BOOL, "quiet", 'q', &quiet, 0, CLI_OPT_USAGE_DEFAULT, NULL, "do not display progress output" }, + { CLI_OPT_TYPE_BOOL, "bare", 0, &clone_opts.bare, 1, + CLI_OPT_USAGE_DEFAULT, NULL, "don't create a working directory" }, { CLI_OPT_TYPE_LITERAL }, { CLI_OPT_TYPE_ARG, "repository", 0, &remote_path, 0, @@ -54,16 +58,24 @@ static void print_help(void) static char *compute_local_path(const char *orig_path) { + git_str local_path = GIT_STR_INIT; const char *slash; - char *local_path; if ((slash = strrchr(orig_path, '/')) == NULL && (slash = strrchr(orig_path, '\\')) == NULL) - local_path = git__strdup(orig_path); + git_str_puts(&local_path, orig_path); else - local_path = git__strdup(slash + 1); + git_str_puts(&local_path, slash + 1); + + if (clone_opts.bare) { + if (!git_str_endswith(&local_path, ".git")) + git_str_puts(&local_path, ".git"); + } else { + if (git_str_endswith(&local_path, ".git")) + git_str_shorten(&local_path, 4); + } - return local_path; + return git_str_detach(&local_path); } static bool validate_local_path(const char *path) @@ -101,7 +113,6 @@ static void interrupt_cleanup(void) int cmd_clone(int argc, char **argv) { - git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT; git_repository *repo = NULL; cli_opt invalid_opt; char *computed_path = NULL; diff --git a/src/util/str.c b/src/util/str.c index 0d405bfda..61887f780 100644 --- a/src/util/str.c +++ b/src/util/str.c @@ -619,6 +619,17 @@ void git_str_rtruncate_at_char(git_str *buf, char separator) git_str_truncate(buf, idx < 0 ? 0 : (size_t)idx); } +bool git_str_endswith(git_str *str, const char *end) +{ + size_t end_len = strlen(end); + + if (end_len > str->size) + return false; + + return (end_len <= str->size && + strcmp(str->ptr + (str->size - end_len), end) == 0); +} + void git_str_swap(git_str *str_a, git_str *str_b) { git_str t = *str_a; diff --git a/src/util/str.h b/src/util/str.h index 588e6fc22..797125373 100644 --- a/src/util/str.h +++ b/src/util/str.h @@ -154,6 +154,7 @@ void git_str_truncate(git_str *str, size_t len); void git_str_shorten(git_str *str, size_t amount); void git_str_truncate_at_char(git_str *path, char separator); void git_str_rtruncate_at_char(git_str *path, char separator); +bool git_str_endswith(git_str *path, const char *end); /** General join with separator */ int git_str_join_n(git_str *str, char separator, int len, ...); |