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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
|
/* foundry-git-uri.c
*
* Copyright 2015-2025 Christian Hergert <chergert@redhat.com>
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of the
* License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include "foundry-git-uri.h"
#include "foundry-util.h"
G_DEFINE_BOXED_TYPE (FoundryGitUri, foundry_git_uri, foundry_git_uri_ref, foundry_git_uri_unref)
struct _FoundryGitUri
{
int ref_count;
/*
* If the URI string was created and has not been changed, we try extra
* hard to provide the same URI back from foundry_git_uri_to_string(). This
* field is cleared any time any of the other fields are changed.
*/
char *non_destructive_uri;
char *scheme;
char *user;
char *host;
char *path;
guint port;
};
static inline void
foundry_git_uri_set_dirty (FoundryGitUri *self)
{
g_clear_pointer (&self->non_destructive_uri, g_free);
}
static gboolean
foundry_git_uri_validate (const FoundryGitUri *self)
{
g_assert (self != NULL);
if (g_strcmp0 (self->scheme, "file") == 0)
return ((self->path != NULL) &&
(self->port == 0) &&
(self->host == NULL) &&
(self->user == NULL));
if ((g_strcmp0 (self->scheme, "http") == 0) ||
(g_strcmp0 (self->scheme, "ssh") == 0) ||
(g_strcmp0 (self->scheme, "git") == 0) ||
(g_strcmp0 (self->scheme, "https") == 0) ||
(g_strcmp0 (self->scheme, "rsync") == 0))
return ((self->path != NULL) && (self->host != NULL));
return TRUE;
}
static gboolean
foundry_git_uri_parse (FoundryGitUri *self,
const char *str)
{
static GRegex *regex1;
static GRegex *regex2;
static GRegex *regex3;
static gsize initialized;
GMatchInfo *match_info = NULL;
gboolean ret = FALSE;
if (g_once_init_enter (&initialized))
{
/* http://stackoverflow.com/questions/2514859/regular-expression-for-git-repository */
regex1 = g_regex_new ("file://(.*)", 0, 0, NULL);
g_assert (regex1);
regex2 = g_regex_new ("(\\w+://)(.+@)*([\\w\\d\\.]+)(:[\\d]+){0,1}/*(.*)", 0, 0, NULL);
g_assert (regex2);
regex3 = g_regex_new ("(.+@)*([\\w\\d\\.]+):(.*)", 0, 0, NULL);
g_assert (regex3);
g_once_init_leave (&initialized, TRUE);
}
if (str == NULL)
return FALSE;
/* check for local file:// style uris */
g_regex_match (regex1, str, 0, &match_info);
if (g_match_info_matches (match_info))
{
g_autofree char *path = NULL;
path = g_match_info_fetch (match_info, 1);
foundry_git_uri_set_scheme (self, "file://");
foundry_git_uri_set_user (self, NULL);
foundry_git_uri_set_host (self, NULL);
foundry_git_uri_set_port (self, 0);
foundry_git_uri_set_path (self, path);
ret = TRUE;
}
g_clear_pointer (&match_info, g_match_info_free);
if (ret)
return ret;
/* check for ssh:// style network uris */
g_regex_match (regex2, str, 0, &match_info);
if (g_match_info_matches (match_info))
{
g_autofree char *scheme = NULL;
g_autofree char *user = NULL;
g_autofree char *host = NULL;
g_autofree char *path = NULL;
g_autofree char *portstr = NULL;
int start_pos;
int end_pos;
guint port = 0;
scheme = g_match_info_fetch (match_info, 1);
user = g_match_info_fetch (match_info, 2);
host = g_match_info_fetch (match_info, 3);
portstr = g_match_info_fetch (match_info, 4);
path = g_match_info_fetch (match_info, 5);
g_match_info_fetch_pos (match_info, 5, &start_pos, &end_pos);
if (*path != '~' && (start_pos > 0) && str [start_pos-1] == '/')
{
char *tmp;
tmp = path;
path = g_strdup_printf ("/%s", path);
g_free (tmp);
}
if (!foundry_str_empty0 (portstr) && g_ascii_isdigit (portstr [1]))
port = CLAMP (atoi (&portstr [1]), 1, G_MAXINT16);
foundry_git_uri_set_scheme (self, scheme);
foundry_git_uri_set_user (self, user);
foundry_git_uri_set_host (self, host);
foundry_git_uri_set_port (self, port);
foundry_git_uri_set_path (self, path);
ret = TRUE;
}
g_clear_pointer (&match_info, g_match_info_free);
if (ret)
return ret;
/* check for user@host style uris */
g_regex_match (regex3, str, 0, &match_info);
if (g_match_info_matches (match_info))
{
g_autofree char *user = NULL;
g_autofree char *host = NULL;
g_autofree char *path = NULL;
user = g_match_info_fetch (match_info, 1);
host = g_match_info_fetch (match_info, 2);
path = g_match_info_fetch (match_info, 3);
if (path && path[0] != '~' && path[0] != '/')
{
g_autofree char *tmp = path;
path = g_strdup_printf ("~/%s", tmp);
}
foundry_git_uri_set_user (self, user);
foundry_git_uri_set_host (self, host);
foundry_git_uri_set_path (self, path);
foundry_git_uri_set_scheme (self, "ssh://");
ret = TRUE;
}
g_clear_pointer (&match_info, g_match_info_free);
if (ret)
return ret;
/* try to avoid some in-progress schemes */
if (strstr (str, "://"))
return FALSE;
foundry_git_uri_set_scheme (self, "file://");
foundry_git_uri_set_user (self, NULL);
foundry_git_uri_set_host (self, NULL);
foundry_git_uri_set_port (self, 0);
foundry_git_uri_set_path (self, str);
return TRUE;
}
FoundryGitUri *
foundry_git_uri_new (const char *uri)
{
FoundryGitUri *self;
self = g_slice_new0 (FoundryGitUri);
self->ref_count = 1;
if (foundry_git_uri_parse (self, uri) && foundry_git_uri_validate (self))
{
self->non_destructive_uri = g_strdup (uri);
return self;
}
foundry_git_uri_unref (self);
return NULL;
}
static void
foundry_git_uri_finalize (FoundryGitUri *self)
{
g_free (self->non_destructive_uri);
g_free (self->scheme);
g_free (self->user);
g_free (self->host);
g_free (self->path);
g_slice_free (FoundryGitUri, self);
}
FoundryGitUri *
foundry_git_uri_ref (FoundryGitUri *self)
{
g_return_val_if_fail (self, NULL);
g_return_val_if_fail (self->ref_count > 0, NULL);
g_atomic_int_inc (&self->ref_count);
return self;
}
void
foundry_git_uri_unref (FoundryGitUri *self)
{
g_return_if_fail (self);
g_return_if_fail (self->ref_count > 0);
if (g_atomic_int_dec_and_test (&self->ref_count))
foundry_git_uri_finalize (self);
}
const char *
foundry_git_uri_get_scheme (const FoundryGitUri *self)
{
g_return_val_if_fail (self, NULL);
return self->scheme;
}
const char *
foundry_git_uri_get_user (const FoundryGitUri *self)
{
g_return_val_if_fail (self, NULL);
return self->user;
}
const char *
foundry_git_uri_get_host (const FoundryGitUri *self)
{
g_return_val_if_fail (self, NULL);
return self->host;
}
guint
foundry_git_uri_get_port (const FoundryGitUri *self)
{
g_return_val_if_fail (self, 0);
return self->port;
}
const char *
foundry_git_uri_get_path (const FoundryGitUri *self)
{
g_return_val_if_fail (self, NULL);
return self->path;
}
void
foundry_git_uri_set_scheme (FoundryGitUri *self,
const char *scheme)
{
g_return_if_fail (self);
if (foundry_str_empty0 (scheme))
scheme = NULL;
if (scheme != self->scheme)
{
const char *tmp;
g_clear_pointer (&self->scheme, g_free);
if (scheme != NULL && (tmp = strchr (scheme, ':')))
self->scheme = g_strndup (scheme, tmp - scheme);
else
self->scheme = g_strdup (scheme);
}
foundry_git_uri_set_dirty (self);
}
void
foundry_git_uri_set_user (FoundryGitUri *self,
const char *user)
{
g_return_if_fail (self);
if (foundry_str_empty0 (user))
user = NULL;
if (user != self->user)
{
const char *tmp;
g_clear_pointer (&self->user, g_free);
if (user != NULL && (tmp = strchr (user, '@')))
self->user = g_strndup (user, tmp - user);
else
self->user = g_strdup (user);
}
foundry_git_uri_set_dirty (self);
}
void
foundry_git_uri_set_host (FoundryGitUri *self,
const char *host)
{
g_return_if_fail (self);
if (foundry_str_empty0 (host))
host = NULL;
if (host != self->host)
{
g_free (self->host);
self->host = g_strdup (host);
}
foundry_git_uri_set_dirty (self);
}
void
foundry_git_uri_set_port (FoundryGitUri *self,
guint port)
{
g_return_if_fail (self);
g_return_if_fail (port <= G_MAXINT16);
self->port = port;
foundry_git_uri_set_dirty (self);
}
void
foundry_git_uri_set_path (FoundryGitUri *self,
const char *path)
{
g_return_if_fail (self);
if (foundry_str_empty0 (path))
path = NULL;
if (path != self->path)
{
if (path != NULL && (*path == ':'))
path++;
g_free (self->path);
self->path = g_strdup (path);
}
foundry_git_uri_set_dirty (self);
}
char *
foundry_git_uri_to_string (const FoundryGitUri *self)
{
GString *str;
g_return_val_if_fail (self, NULL);
if (self->non_destructive_uri != NULL)
return g_strdup (self->non_destructive_uri);
str = g_string_new (NULL);
g_string_append_printf (str, "%s://", self->scheme);
if (0 == g_strcmp0 (self->scheme, "file"))
{
g_string_append (str, self->path);
return g_string_free (str, FALSE);
}
if (self->user != NULL)
g_string_append_printf (str, "%s@", self->user);
g_string_append (str, self->host);
if (self->port != 0)
g_string_append_printf (str, ":%u", self->port);
if (self->path == NULL)
g_string_append (str, "/");
else if (self->path [0] == '~')
g_string_append_printf (str, "/%s", self->path);
else if (self->path [0] != '/')
g_string_append_printf (str, "/%s", self->path);
else
g_string_append (str, self->path);
return g_string_free (str, FALSE);
}
gboolean
foundry_git_uri_is_valid (const char *uri_string)
{
gboolean ret = FALSE;
if (uri_string != NULL)
{
FoundryGitUri *uri;
uri = foundry_git_uri_new (uri_string);
ret = !!uri;
g_clear_pointer (&uri, foundry_git_uri_unref);
}
return ret;
}
/**
* foundry_git_uri_get_clone_name:
* @self: an #ideVcsUri
*
* Determines a suggested name for the checkout directory. Some special
* handling of suffixes such as ".git" are performed to improve the the
* quality of results.
*
* Returns: (transfer full) (nullable): a string containing the suggested
* clone directory name, or %NULL.
*/
char *
foundry_git_uri_get_clone_name (const FoundryGitUri *self)
{
g_autofree char *name = NULL;
const char *path;
g_return_val_if_fail (self != NULL, NULL);
if (!(path = foundry_git_uri_get_path (self)))
return NULL;
if (foundry_str_empty0 (path))
return NULL;
if (!(name = g_path_get_basename (path)))
return NULL;
/* Trim trailing ".git" */
if (g_str_has_suffix (name, ".git"))
*(strrchr (name, '.')) = '\0';
if (!g_str_equal (name, "/") && !g_str_equal (name, "~"))
return g_steal_pointer (&name);
return NULL;
}
|