Skip to content

Commit a591f36

Browse files
committed
net: introduce url formatting function
1 parent d68f2b1 commit a591f36

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/net.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,39 @@ void git_net_url_swap(git_net_url *a, git_net_url *b)
348348
memcpy(b, &tmp, sizeof(git_net_url));
349349
}
350350

351+
int git_net_url_fmt(git_buf *buf, git_net_url *url)
352+
{
353+
git_buf_puts(buf, url->scheme);
354+
git_buf_puts(buf, "://");
355+
356+
if (url->username) {
357+
git_buf_puts(buf, url->username);
358+
359+
if (url->password) {
360+
git_buf_puts(buf, ":");
361+
git_buf_puts(buf, url->password);
362+
}
363+
364+
git_buf_putc(buf, '@');
365+
}
366+
367+
git_buf_puts(buf, url->host);
368+
369+
if (url->port && !git_net_url_is_default_port(url)) {
370+
git_buf_putc(buf, ':');
371+
git_buf_puts(buf, url->port);
372+
}
373+
374+
git_buf_puts(buf, url->path ? url->path : "/");
375+
376+
if (url->query) {
377+
git_buf_putc(buf, '?');
378+
git_buf_puts(buf, url->query);
379+
}
380+
381+
return git_buf_oom(buf) ? -1 : 0;
382+
}
383+
351384
void git_net_url_dispose(git_net_url *url)
352385
{
353386
if (url->username)

src/net.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ extern int git_net_url_apply_redirect(
4545
/** Swaps the contents of one URL for another. */
4646
extern void git_net_url_swap(git_net_url *a, git_net_url *b);
4747

48+
/** Places the URL into the given buffer. */
49+
extern int git_net_url_fmt(git_buf *out, git_net_url *url);
50+
4851
/** Disposes the contents of the structure. */
4952
extern void git_net_url_dispose(git_net_url *url);
5053

0 commit comments

Comments
 (0)