diff options
author | Edward Thomson <ethomson@edwardthomson.com> | 2019-12-09 19:48:10 +1000 |
---|---|---|
committer | Edward Thomson <ethomson@edwardthomson.com> | 2020-01-24 10:16:36 -0600 |
commit | a591f3623c755328c12afe08dce9acfb19d0252d (patch) | |
tree | a265fd971b180badf1c4a4a6e35df49e6aa4423b | |
parent | d68f2b1ab7aa7358f8ff0b75b72054bb9eeced3d (diff) | |
download | libgit2-a591f3623c755328c12afe08dce9acfb19d0252d.tar.gz |
net: introduce url formatting function
-rw-r--r-- | src/net.c | 33 | ||||
-rw-r--r-- | src/net.h | 3 |
2 files changed, 36 insertions, 0 deletions
@@ -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) @@ -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); |