net: introduce url formatting function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
diff --git a/src/net.c b/src/net.c
index c4a351a..c707743 100644
--- a/src/net.c
+++ b/src/net.c
@@ -348,6 +348,39 @@ void git_net_url_swap(git_net_url *a, git_net_url *b)
memcpy(b, &tmp, sizeof(git_net_url));
}
+int git_net_url_fmt(git_buf *buf, git_net_url *url)
+{
+ git_buf_puts(buf, url->scheme);
+ git_buf_puts(buf, "://");
+
+ if (url->username) {
+ git_buf_puts(buf, url->username);
+
+ if (url->password) {
+ git_buf_puts(buf, ":");
+ git_buf_puts(buf, url->password);
+ }
+
+ git_buf_putc(buf, '@');
+ }
+
+ git_buf_puts(buf, url->host);
+
+ if (url->port && !git_net_url_is_default_port(url)) {
+ git_buf_putc(buf, ':');
+ git_buf_puts(buf, url->port);
+ }
+
+ git_buf_puts(buf, url->path ? url->path : "/");
+
+ if (url->query) {
+ git_buf_putc(buf, '?');
+ git_buf_puts(buf, url->query);
+ }
+
+ return git_buf_oom(buf) ? -1 : 0;
+}
+
void git_net_url_dispose(git_net_url *url)
{
if (url->username)
diff --git a/src/net.h b/src/net.h
index 089d9ae..c140f95 100644
--- a/src/net.h
+++ b/src/net.h
@@ -45,6 +45,9 @@ extern int git_net_url_apply_redirect(
/** Swaps the contents of one URL for another. */
extern void git_net_url_swap(git_net_url *a, git_net_url *b);
+/** Places the URL into the given buffer. */
+extern int git_net_url_fmt(git_buf *out, git_net_url *url);
+
/** Disposes the contents of the structure. */
extern void git_net_url_dispose(git_net_url *url);