All of lore.kernel.org
 help / color / mirror / Atom feed
From: Atharva Raykar <raykar.ath@gmail.com>
To: raykar.ath@gmail.com
Cc: avarab@gmail.com, christian.couder@gmail.com,
	congdanhqx@gmail.com, emilyshaffer@google.com,
	git@vger.kernel.org, gitster@pobox.com, jrnieder@gmail.com,
	kaartic.sivaraam@gmail.com, pc44800@gmail.com,
	periperidip@gmail.com, rafaeloliveira.cs@gmail.com,
	sunshine@sunshineco.com
Subject: [GSoC] [PATCH v3 4/8] dir: libify and export helper functions from clone.c
Date: Fri,  6 Aug 2021 17:31:43 +0530	[thread overview]
Message-ID: <20210806120147.73349-5-raykar.ath@gmail.com> (raw)
In-Reply-To: <20210806120147.73349-1-raykar.ath@gmail.com>

These functions can be useful to other parts of Git. Let's move them to
dir.c, while renaming them to be make their functionality more explicit.

Signed-off-by: Atharva Raykar <raykar.ath@gmail.com>
Mentored-by: Christian Couder <christian.couder@gmail.com>
Mentored-by: Shourya Shukla <periperidip@gmail.com>
---
 builtin/clone.c | 118 +-----------------------------------------------
 dir.c           | 114 ++++++++++++++++++++++++++++++++++++++++++++++
 dir.h           |  11 +++++
 3 files changed, 127 insertions(+), 116 deletions(-)

diff --git a/builtin/clone.c b/builtin/clone.c
index 66fe66679c..fab0a3cef6 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -217,120 +217,6 @@ static char *get_repo_path(const char *repo, int *is_bundle)
 	return canon;
 }
 
-static char *guess_dir_name(const char *repo, int is_bundle, int is_bare)
-{
-	const char *end = repo + strlen(repo), *start, *ptr;
-	size_t len;
-	char *dir;
-
-	/*
-	 * Skip scheme.
-	 */
-	start = strstr(repo, "://");
-	if (start == NULL)
-		start = repo;
-	else
-		start += 3;
-
-	/*
-	 * Skip authentication data. The stripping does happen
-	 * greedily, such that we strip up to the last '@' inside
-	 * the host part.
-	 */
-	for (ptr = start; ptr < end && !is_dir_sep(*ptr); ptr++) {
-		if (*ptr == '@')
-			start = ptr + 1;
-	}
-
-	/*
-	 * Strip trailing spaces, slashes and /.git
-	 */
-	while (start < end && (is_dir_sep(end[-1]) || isspace(end[-1])))
-		end--;
-	if (end - start > 5 && is_dir_sep(end[-5]) &&
-	    !strncmp(end - 4, ".git", 4)) {
-		end -= 5;
-		while (start < end && is_dir_sep(end[-1]))
-			end--;
-	}
-
-	/*
-	 * Strip trailing port number if we've got only a
-	 * hostname (that is, there is no dir separator but a
-	 * colon). This check is required such that we do not
-	 * strip URI's like '/foo/bar:2222.git', which should
-	 * result in a dir '2222' being guessed due to backwards
-	 * compatibility.
-	 */
-	if (memchr(start, '/', end - start) == NULL
-	    && memchr(start, ':', end - start) != NULL) {
-		ptr = end;
-		while (start < ptr && isdigit(ptr[-1]) && ptr[-1] != ':')
-			ptr--;
-		if (start < ptr && ptr[-1] == ':')
-			end = ptr - 1;
-	}
-
-	/*
-	 * Find last component. To remain backwards compatible we
-	 * also regard colons as path separators, such that
-	 * cloning a repository 'foo:bar.git' would result in a
-	 * directory 'bar' being guessed.
-	 */
-	ptr = end;
-	while (start < ptr && !is_dir_sep(ptr[-1]) && ptr[-1] != ':')
-		ptr--;
-	start = ptr;
-
-	/*
-	 * Strip .{bundle,git}.
-	 */
-	len = end - start;
-	strip_suffix_mem(start, &len, is_bundle ? ".bundle" : ".git");
-
-	if (!len || (len == 1 && *start == '/'))
-		die(_("No directory name could be guessed.\n"
-		      "Please specify a directory on the command line"));
-
-	if (is_bare)
-		dir = xstrfmt("%.*s.git", (int)len, start);
-	else
-		dir = xstrndup(start, len);
-	/*
-	 * Replace sequences of 'control' characters and whitespace
-	 * with one ascii space, remove leading and trailing spaces.
-	 */
-	if (*dir) {
-		char *out = dir;
-		int prev_space = 1 /* strip leading whitespace */;
-		for (end = dir; *end; ++end) {
-			char ch = *end;
-			if ((unsigned char)ch < '\x20')
-				ch = '\x20';
-			if (isspace(ch)) {
-				if (prev_space)
-					continue;
-				prev_space = 1;
-			} else
-				prev_space = 0;
-			*out++ = ch;
-		}
-		*out = '\0';
-		if (out > dir && prev_space)
-			out[-1] = '\0';
-	}
-	return dir;
-}
-
-static void strip_trailing_slashes(char *dir)
-{
-	char *end = dir + strlen(dir);
-
-	while (dir < end - 1 && is_dir_sep(end[-1]))
-		end--;
-	*end = '\0';
-}
-
 static int add_one_reference(struct string_list_item *item, void *cb_data)
 {
 	struct strbuf err = STRBUF_INIT;
@@ -1041,8 +927,8 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 	if (argc == 2)
 		dir = xstrdup(argv[1]);
 	else
-		dir = guess_dir_name(repo_name, is_bundle, option_bare);
-	strip_trailing_slashes(dir);
+		dir = guess_target_dir_from_git_url(repo_name, is_bundle, option_bare);
+	strip_dir_trailing_slashes(dir);
 
 	dest_exists = path_exists(dir);
 	if (dest_exists && !is_empty_dir(dir))
diff --git a/dir.c b/dir.c
index 03c4d21267..9a9ab135c4 100644
--- a/dir.c
+++ b/dir.c
@@ -2970,6 +2970,120 @@ int is_empty_dir(const char *path)
 	return ret;
 }
 
+char *guess_target_dir_from_git_url(const char *repo, int is_bundle, int is_bare)
+{
+	const char *end = repo + strlen(repo), *start, *ptr;
+	size_t len;
+	char *dir;
+
+	/*
+	 * Skip scheme.
+	 */
+	start = strstr(repo, "://");
+	if (start == NULL)
+		start = repo;
+	else
+		start += 3;
+
+	/*
+	 * Skip authentication data. The stripping does happen
+	 * greedily, such that we strip up to the last '@' inside
+	 * the host part.
+	 */
+	for (ptr = start; ptr < end && !is_dir_sep(*ptr); ptr++) {
+		if (*ptr == '@')
+			start = ptr + 1;
+	}
+
+	/*
+	 * Strip trailing spaces, slashes and /.git
+	 */
+	while (start < end && (is_dir_sep(end[-1]) || isspace(end[-1])))
+		end--;
+	if (end - start > 5 && is_dir_sep(end[-5]) &&
+	    !strncmp(end - 4, ".git", 4)) {
+		end -= 5;
+		while (start < end && is_dir_sep(end[-1]))
+			end--;
+	}
+
+	/*
+	 * Strip trailing port number if we've got only a
+	 * hostname (that is, there is no dir separator but a
+	 * colon). This check is required such that we do not
+	 * strip URI's like '/foo/bar:2222.git', which should
+	 * result in a dir '2222' being guessed due to backwards
+	 * compatibility.
+	 */
+	if (memchr(start, '/', end - start) == NULL
+	    && memchr(start, ':', end - start) != NULL) {
+		ptr = end;
+		while (start < ptr && isdigit(ptr[-1]) && ptr[-1] != ':')
+			ptr--;
+		if (start < ptr && ptr[-1] == ':')
+			end = ptr - 1;
+	}
+
+	/*
+	 * Find last component. To remain backwards compatible we
+	 * also regard colons as path separators, such that
+	 * cloning a repository 'foo:bar.git' would result in a
+	 * directory 'bar' being guessed.
+	 */
+	ptr = end;
+	while (start < ptr && !is_dir_sep(ptr[-1]) && ptr[-1] != ':')
+		ptr--;
+	start = ptr;
+
+	/*
+	 * Strip .{bundle,git}.
+	 */
+	len = end - start;
+	strip_suffix_mem(start, &len, is_bundle ? ".bundle" : ".git");
+
+	if (!len || (len == 1 && *start == '/'))
+		die(_("No directory name could be guessed.\n"
+		      "Please specify a directory on the command line"));
+
+	if (is_bare)
+		dir = xstrfmt("%.*s.git", (int)len, start);
+	else
+		dir = xstrndup(start, len);
+	/*
+	 * Replace sequences of 'control' characters and whitespace
+	 * with one ascii space, remove leading and trailing spaces.
+	 */
+	if (*dir) {
+		char *out = dir;
+		int prev_space = 1 /* strip leading whitespace */;
+		for (end = dir; *end; ++end) {
+			char ch = *end;
+			if ((unsigned char)ch < '\x20')
+				ch = '\x20';
+			if (isspace(ch)) {
+				if (prev_space)
+					continue;
+				prev_space = 1;
+			} else
+				prev_space = 0;
+			*out++ = ch;
+		}
+		*out = '\0';
+		if (out > dir && prev_space)
+			out[-1] = '\0';
+	}
+	return dir;
+}
+
+void strip_dir_trailing_slashes(char *dir)
+{
+	char *end = dir + strlen(dir);
+
+	while (dir < end - 1 && is_dir_sep(end[-1]))
+		end--;
+	*end = '\0';
+}
+
 static int remove_dir_recurse(struct strbuf *path, int flag, int *kept_up)
 {
 	DIR *dir;
diff --git a/dir.h b/dir.h
index b3e1a54a97..6f0abdfe04 100644
--- a/dir.h
+++ b/dir.h
@@ -453,6 +453,17 @@ static inline int is_dot_or_dotdot(const char *name)
 
 int is_empty_dir(const char *dir);
 
+/*
+ * Retrieve a target directory name by reading "humanish" part of the
+ * given Git URL.
+ *
+ * For example:
+ * 	/path/to/repo.git => "repo"
+ * 	host.xz.foo/.git => "foo"
+ */
+char *guess_target_dir_from_git_url(const char *repo, int is_bundle, int is_bare);
+void strip_dir_trailing_slashes(char *dir);
+
 void setup_standard_excludes(struct dir_struct *dir);
 
 char *get_sparse_checkout_filename(void);
-- 
2.32.0


  parent reply	other threads:[~2021-08-06 12:02 UTC|newest]

Thread overview: 78+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-05  7:19 [GSoC] [PATCH 0/8] submodule: convert the rest of 'add' to C Atharva Raykar
2021-08-05  7:19 ` [GSoC] [PATCH 1/8] submodule--helper: refactor resolve_relative_url() helper Atharva Raykar
2021-08-05  7:19 ` [GSoC] [PATCH 2/8] submodule--helper: remove repeated code in sync_submodule() Atharva Raykar
2021-08-06  0:53   ` Đoàn Trần Công Danh
2021-08-06  9:06     ` Christian Couder
2021-08-06 10:06       ` Atharva Raykar
2021-08-06 16:21       ` Junio C Hamano
2021-08-05  7:19 ` [GSoC] [PATCH 3/8] dir: libify and export helper functions from clone.c Atharva Raykar
2021-08-05  7:19 ` [GSoC] [PATCH 4/8] submodule--helper: remove constness of sm_path Atharva Raykar
2021-08-05  7:19 ` [GSoC] [PATCH 5/8] submodule--helper: convert the bulk of cmd_add() to C Atharva Raykar
2021-08-05  7:19 ` [GSoC] [PATCH 6/8] submodule--helper: remove add-clone subcommand Atharva Raykar
2021-08-05  7:19 ` [GSoC] [PATCH 7/8] submodule--helper: remove add-config subcommand Atharva Raykar
2021-08-05  7:19 ` [GSoC] [PATCH 8/8] submodule--helper: remove resolve-relative-url subcommand Atharva Raykar
2021-08-05  7:40 ` [GSoC] [PATCH v2 0/9] submodule: convert the rest of 'add' to C Atharva Raykar
2021-08-05  7:40   ` [GSoC] [PATCH v2 1/9] submodule--helper: add options for compute_submodule_clone_url() Atharva Raykar
2021-08-05 20:05     ` Junio C Hamano
2021-08-05  7:40   ` [GSoC] [PATCH v2 2/9] submodule--helper: refactor resolve_relative_url() helper Atharva Raykar
2021-08-05 20:13     ` Junio C Hamano
2021-08-05  7:40   ` [GSoC] [PATCH v2 3/9] submodule--helper: remove repeated code in sync_submodule() Atharva Raykar
2021-08-05 20:20     ` Junio C Hamano
2021-08-05  7:40   ` [GSoC] [PATCH v2 4/9] dir: libify and export helper functions from clone.c Atharva Raykar
2021-08-05 20:37     ` Junio C Hamano
2021-08-06 11:12       ` Atharva Raykar
2021-08-06 16:36         ` Junio C Hamano
2021-08-07  7:15           ` Atharva Raykar
2021-08-05  7:40   ` [GSoC] [PATCH v2 5/9] submodule--helper: remove constness of sm_path Atharva Raykar
2021-08-05 20:40     ` Junio C Hamano
2021-08-06 11:16       ` Atharva Raykar
2021-08-05  7:40   ` [GSoC] [PATCH v2 6/9] submodule--helper: convert the bulk of cmd_add() to C Atharva Raykar
2021-08-06  1:14     ` Đoàn Trần Công Danh
2021-08-06 11:33       ` Atharva Raykar
2021-08-05  7:40   ` [GSoC] [PATCH v2 7/9] submodule--helper: remove add-clone subcommand Atharva Raykar
2021-08-05  7:40   ` [GSoC] [PATCH v2 8/9] submodule--helper: remove add-config subcommand Atharva Raykar
2021-08-05  7:40   ` [GSoC] [PATCH v2 9/9] submodule--helper: remove resolve-relative-url subcommand Atharva Raykar
2021-08-06 12:01   ` [GSoC] [PATCH v3 0/8] submodule: convert the rest of 'add' to C Atharva Raykar
2021-08-06 12:01     ` [GSoC] [PATCH v3 1/8] submodule--helper: add options for compute_submodule_clone_url() Atharva Raykar
2021-08-06 12:01     ` [GSoC] [PATCH v3 2/8] submodule--helper: refactor resolve_relative_url() helper Atharva Raykar
2021-08-06 12:01     ` [GSoC] [PATCH v3 3/8] submodule--helper: remove repeated code in sync_submodule() Atharva Raykar
2021-08-06 12:01     ` Atharva Raykar [this message]
2021-08-06 12:01     ` [GSoC] [PATCH v3 5/8] submodule--helper: convert the bulk of cmd_add() to C Atharva Raykar
2021-08-06 12:01     ` [GSoC] [PATCH v3 6/8] submodule--helper: remove add-clone subcommand Atharva Raykar
2021-08-06 12:01     ` [GSoC] [PATCH v3 7/8] submodule--helper: remove add-config subcommand Atharva Raykar
2021-08-06 12:01     ` [GSoC] [PATCH v3 8/8] submodule--helper: remove resolve-relative-url subcommand Atharva Raykar
2021-08-07  7:16     ` [GSoC] [PATCH v4 0/8] submodule: convert the rest of 'add' to C Atharva Raykar
2021-08-07  7:16       ` [GSoC] [PATCH v4 1/8] submodule--helper: add options for compute_submodule_clone_url() Atharva Raykar
2021-08-08 17:41         ` Kaartic Sivaraam
2021-08-08 18:26           ` Kaartic Sivaraam
2021-08-09  7:29             ` Atharva Raykar
2021-08-09  8:47               ` Atharva Raykar
2021-08-10 17:36                 ` Kaartic Sivaraam
2021-08-07  7:16       ` [GSoC] [PATCH v4 2/8] submodule--helper: refactor resolve_relative_url() helper Atharva Raykar
2021-08-07  7:16       ` [GSoC] [PATCH v4 3/8] submodule--helper: remove repeated code in sync_submodule() Atharva Raykar
2021-08-08 19:00         ` Kaartic Sivaraam
2021-08-09  7:36           ` Atharva Raykar
2021-08-07  7:16       ` [GSoC] [PATCH v4 4/8] dir: libify and export helper functions from clone.c Atharva Raykar
2021-08-08 19:23         ` Kaartic Sivaraam
2021-08-09  8:02           ` Atharva Raykar
2021-08-10 17:53             ` Kaartic Sivaraam
2021-08-10 21:27               ` Junio C Hamano
2021-08-11 10:25               ` Atharva Raykar
2021-08-07  7:16       ` [GSoC] [PATCH v4 5/8] submodule--helper: convert the bulk of cmd_add() to C Atharva Raykar
2021-08-07  7:16       ` [GSoC] [PATCH v4 6/8] submodule--helper: remove add-clone subcommand Atharva Raykar
2021-08-07  7:16       ` [GSoC] [PATCH v4 7/8] submodule--helper: remove add-config subcommand Atharva Raykar
2021-08-07  7:16       ` [GSoC] [PATCH v4 8/8] submodule--helper: remove resolve-relative-url subcommand Atharva Raykar
2021-08-08 18:01       ` [GSoC] [PATCH v4 0/8] submodule: convert the rest of 'add' to C Kaartic Sivaraam
2021-08-10 11:46       ` [GSoC] [PATCH v5 0/9] " Atharva Raykar
2021-08-10 11:46         ` [GSoC] [PATCH v5 1/9] submodule--helper: add options for compute_submodule_clone_url() Atharva Raykar
2021-08-11  6:44           ` Bagas Sanjaya
2021-08-11 10:30             ` Atharva Raykar
2021-08-10 11:46         ` [GSoC] [PATCH v5 2/9] submodule--helper: refactor resolve_relative_url() helper Atharva Raykar
2021-08-10 11:46         ` [GSoC] [PATCH v5 3/9] submodule--helper: remove repeated code in sync_submodule() Atharva Raykar
2021-08-10 11:46         ` [GSoC] [PATCH v5 4/9] dir: libify and export helper functions from clone.c Atharva Raykar
2021-08-10 11:46         ` [GSoC] [PATCH v5 5/9] submodule--helper: convert the bulk of cmd_add() to C Atharva Raykar
2021-08-10 11:46         ` [GSoC] [PATCH v5 6/9] submodule--helper: remove add-clone subcommand Atharva Raykar
2021-08-10 11:46         ` [GSoC] [PATCH v5 7/9] submodule--helper: remove add-config subcommand Atharva Raykar
2021-08-10 11:46         ` [GSoC] [PATCH v5 8/9] submodule--helper: remove resolve-relative-url subcommand Atharva Raykar
2021-08-10 11:46         ` [GSoC] [PATCH v5 9/9] submodule--helper: rename compute_submodule_clone_url() Atharva Raykar
2021-09-08  0:31         ` [GSoC] [PATCH v5 0/9] submodule: convert the rest of 'add' to C Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210806120147.73349-5-raykar.ath@gmail.com \
    --to=raykar.ath@gmail.com \
    --cc=avarab@gmail.com \
    --cc=christian.couder@gmail.com \
    --cc=congdanhqx@gmail.com \
    --cc=emilyshaffer@google.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=jrnieder@gmail.com \
    --cc=kaartic.sivaraam@gmail.com \
    --cc=pc44800@gmail.com \
    --cc=periperidip@gmail.com \
    --cc=rafaeloliveira.cs@gmail.com \
    --cc=sunshine@sunshineco.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.