Skip to content

Commit ceddeed

Browse files
authored
Merge pull request libgit2#6104 from libgit2/ethomson/path
path: refactor utility path functions
2 parents 9ab351c + 1a8b292 commit ceddeed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

123 files changed

+4042
-3769
lines changed

fuzzers/standalone_driver.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ int main(int argc, char **argv)
5252
fprintf(stderr, "Running %s against %s\n", argv[0], argv[1]);
5353
LLVMFuzzerInitialize(&argc, &argv);
5454

55-
if (git_path_dirload(&corpus_files, argv[1], 0, 0x0) < 0) {
55+
if (git_fs_path_dirload(&corpus_files, argv[1], 0, 0x0) < 0) {
5656
fprintf(stderr, "Failed to scan corpus directory '%s': %s\n",
5757
argv[1], git_error_last()->message);
5858
error = -1;

src/attr.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -629,18 +629,18 @@ static int collect_attr_files(
629629
const char *workdir = git_repository_workdir(repo);
630630
attr_walk_up_info info = { NULL };
631631

632-
GIT_ASSERT(!git_path_is_absolute(path));
632+
GIT_ASSERT(!git_fs_path_is_absolute(path));
633633

634634
if ((error = attr_setup(repo, attr_session, opts)) < 0)
635635
return error;
636636

637637
/* Resolve path in a non-bare repo */
638638
if (workdir != NULL) {
639639
if (!(error = git_repository_workdir_path(&dir, repo, path)))
640-
error = git_path_find_dir(&dir);
640+
error = git_fs_path_find_dir(&dir);
641641
}
642642
else {
643-
error = git_path_dirname_r(&dir, path);
643+
error = git_fs_path_dirname_r(&dir, path);
644644
}
645645

646646
if (error < 0)
@@ -670,7 +670,7 @@ static int collect_attr_files(
670670
if (!strcmp(dir.ptr, "."))
671671
error = push_one_attr(&info, "");
672672
else
673-
error = git_path_walk_up(&dir, workdir, push_one_attr, &info);
673+
error = git_fs_path_walk_up(&dir, workdir, push_one_attr, &info);
674674

675675
if (error < 0)
676676
goto cleanup;

src/attr_file.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ int git_attr_file__parse_buffer(
345345
int error = 0;
346346

347347
/* If subdir file path, convert context for file paths */
348-
if (attrs->entry && git_path_root(attrs->entry->path) < 0 &&
348+
if (attrs->entry && git_fs_path_root(attrs->entry->path) < 0 &&
349349
!git__suffixcmp(attrs->entry->path, "/" GIT_ATTR_FILE))
350350
context = attrs->entry->path;
351351

@@ -560,7 +560,7 @@ int git_attr_path__init(
560560
/* build full path as best we can */
561561
git_str_init(&info->full, 0);
562562

563-
if (git_path_join_unrooted(&info->full, path, base, &root) < 0)
563+
if (git_fs_path_join_unrooted(&info->full, path, base, &root) < 0)
564564
return -1;
565565

566566
info->path = info->full.ptr + root;
@@ -596,7 +596,7 @@ int git_attr_path__init(
596596

597597
case GIT_DIR_FLAG_UNKNOWN:
598598
default:
599-
info->is_dir = (int)git_path_isdir(info->full.ptr);
599+
info->is_dir = (int)git_fs_path_isdir(info->full.ptr);
600600
break;
601601
}
602602

src/attrcache.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "config.h"
1313
#include "sysdir.h"
1414
#include "ignore.h"
15+
#include "path.h"
1516

1617
GIT_INLINE(int) attr_cache_lock(git_attr_cache *cache)
1718
{
@@ -43,11 +44,12 @@ int git_attr_cache__alloc_file_entry(
4344
const char *path,
4445
git_pool *pool)
4546
{
47+
git_str fullpath_str = GIT_STR_INIT;
4648
size_t baselen = 0, pathlen = strlen(path);
4749
size_t cachesize = sizeof(git_attr_file_entry) + pathlen + 1;
4850
git_attr_file_entry *ce;
4951

50-
if (base != NULL && git_path_root(path) < 0) {
52+
if (base != NULL && git_fs_path_root(path) < 0) {
5153
baselen = strlen(base);
5254
cachesize += baselen;
5355

@@ -66,7 +68,10 @@ int git_attr_cache__alloc_file_entry(
6668
}
6769
memcpy(&ce->fullpath[baselen], path, pathlen);
6870

69-
if (git_path_validate_workdir_with_len(repo, ce->fullpath, pathlen + baselen) < 0)
71+
fullpath_str.ptr = ce->fullpath;
72+
fullpath_str.size = pathlen + baselen;
73+
74+
if (git_path_validate_str_length(repo, &fullpath_str) < 0)
7075
return -1;
7176

7277
ce->path = &ce->fullpath[baselen];
@@ -169,11 +174,11 @@ static int attr_cache_lookup(
169174
git_attr_file *file = NULL;
170175

171176
/* join base and path as needed */
172-
if (source->base != NULL && git_path_root(source->filename) < 0) {
177+
if (source->base != NULL && git_fs_path_root(source->filename) < 0) {
173178
git_str *p = attr_session ? &attr_session->tmp : &path;
174179

175180
if (git_str_joinpath(p, source->base, source->filename) < 0 ||
176-
git_path_validate_workdir_buf(repo, p) < 0)
181+
git_path_validate_str_length(repo, p) < 0)
177182
return -1;
178183

179184
filename = p->ptr;

src/blob.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ int git_blob__create_from_paths(
205205
content_path = path.ptr;
206206
}
207207

208-
if ((error = git_path_lstat(content_path, &st)) < 0 ||
208+
if ((error = git_fs_path_lstat(content_path, &st)) < 0 ||
209209
(error = git_repository_odb(&odb, repo)) < 0)
210210
goto done;
211211

@@ -280,7 +280,7 @@ int git_blob_create_from_disk(
280280
git_str full_path = GIT_STR_INIT;
281281
const char *workdir, *hintpath = NULL;
282282

283-
if ((error = git_path_prettify(&full_path, path, NULL)) < 0) {
283+
if ((error = git_fs_path_prettify(&full_path, path, NULL)) < 0) {
284284
git_str_dispose(&full_path);
285285
return error;
286286
}

src/checkout.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@
2727
#include "diff_generate.h"
2828
#include "pathspec.h"
2929
#include "diff_xdiff.h"
30-
#include "path.h"
30+
#include "fs_path.h"
3131
#include "attr.h"
3232
#include "pool.h"
3333
#include "strmap.h"
34+
#include "path.h"
3435

3536
/* See docs/checkout-internals.md for more information */
3637

@@ -328,7 +329,7 @@ static int checkout_target_fullpath(
328329
if (path && git_str_puts(&data->target_path, path) < 0)
329330
return -1;
330331

331-
if (git_path_validate_workdir_buf(data->repo, &data->target_path) < 0)
332+
if (git_path_validate_str_length(data->repo, &data->target_path) < 0)
332333
return -1;
333334

334335
*out = &data->target_path;
@@ -347,7 +348,7 @@ static bool wd_item_is_removable(
347348
if (checkout_target_fullpath(&full, data, wd->path) < 0)
348349
return false;
349350

350-
return !full || !git_path_contains(full, DOT_GIT);
351+
return !full || !git_fs_path_contains(full, DOT_GIT);
351352
}
352353

353354
static int checkout_queue_remove(checkout_data *data, const char *path)
@@ -481,7 +482,7 @@ static bool checkout_is_empty_dir(checkout_data *data, const char *path)
481482
if (checkout_target_fullpath(&fullpath, data, path) < 0)
482483
return false;
483484

484-
return git_path_is_empty_dir(fullpath->ptr);
485+
return git_fs_path_is_empty_dir(fullpath->ptr);
485486
}
486487

487488
static int checkout_action_with_wd(
@@ -1201,12 +1202,12 @@ static int checkout_conflicts_mark_directoryfile(
12011202
goto done;
12021203
}
12031204

1204-
prefixed = git_path_equal_or_prefixed(path, entry->path, NULL);
1205+
prefixed = git_fs_path_equal_or_prefixed(path, entry->path, NULL);
12051206

1206-
if (prefixed == GIT_PATH_EQUAL)
1207+
if (prefixed == GIT_FS_PATH_EQUAL)
12071208
continue;
12081209

1209-
if (prefixed == GIT_PATH_PREFIX)
1210+
if (prefixed == GIT_FS_PATH_PREFIX)
12101211
conflict->directoryfile = 1;
12111212

12121213
break;
@@ -1280,14 +1281,14 @@ static int checkout_verify_paths(
12801281
unsigned int flags = GIT_PATH_REJECT_WORKDIR_DEFAULTS;
12811282

12821283
if (action & CHECKOUT_ACTION__REMOVE) {
1283-
if (!git_path_validate(repo, delta->old_file.path, delta->old_file.mode, flags)) {
1284+
if (!git_path_is_valid(repo, delta->old_file.path, delta->old_file.mode, flags)) {
12841285
git_error_set(GIT_ERROR_CHECKOUT, "cannot remove invalid path '%s'", delta->old_file.path);
12851286
return -1;
12861287
}
12871288
}
12881289

12891290
if (action & ~CHECKOUT_ACTION__REMOVE) {
1290-
if (!git_path_validate(repo, delta->new_file.path, delta->new_file.mode, flags)) {
1291+
if (!git_path_is_valid(repo, delta->new_file.path, delta->new_file.mode, flags)) {
12911292
git_error_set(GIT_ERROR_CHECKOUT, "cannot checkout to invalid path '%s'", delta->new_file.path);
12921293
return -1;
12931294
}
@@ -1949,7 +1950,7 @@ static int checkout_path_suffixed(git_str *path, const char *suffix)
19491950

19501951
path_len = git_str_len(path);
19511952

1952-
while (git_path_exists(git_str_cstr(path)) && i < INT_MAX) {
1953+
while (git_fs_path_exists(git_str_cstr(path)) && i < INT_MAX) {
19531954
git_str_truncate(path, path_len);
19541955

19551956
if ((error = git_str_putc(path, '_')) < 0 ||
@@ -2034,7 +2035,7 @@ static int checkout_merge_path(
20342035
int error = 0;
20352036

20362037
if ((error = git_str_joinpath(out, data->opts.target_directory, result->path)) < 0 ||
2037-
(error = git_path_validate_workdir_buf(data->repo, out)) < 0)
2038+
(error = git_path_validate_str_length(data->repo, out)) < 0)
20382039
return error;
20392040

20402041
/* Most conflicts simply use the filename in the index */
@@ -2337,10 +2338,10 @@ static int validate_target_directory(checkout_data *data)
23372338
{
23382339
int error;
23392340

2340-
if ((error = git_path_validate_workdir(data->repo, data->opts.target_directory)) < 0)
2341+
if ((error = git_path_validate_length(data->repo, data->opts.target_directory)) < 0)
23412342
return error;
23422343

2343-
if (git_path_isdir(data->opts.target_directory))
2344+
if (git_fs_path_isdir(data->opts.target_directory))
23442345
return 0;
23452346

23462347
error = checkout_mkdir(data, data->opts.target_directory, NULL,
@@ -2507,7 +2508,7 @@ static int checkout_data_init(
25072508
(error = git_vector_init(&data->remove_conflicts, 0, NULL)) < 0 ||
25082509
(error = git_vector_init(&data->update_conflicts, 0, NULL)) < 0 ||
25092510
(error = git_str_puts(&data->target_path, data->opts.target_directory)) < 0 ||
2510-
(error = git_path_to_dir(&data->target_path)) < 0 ||
2511+
(error = git_fs_path_to_dir(&data->target_path)) < 0 ||
25112512
(error = git_strmap_new(&data->mkdir_map)) < 0)
25122513
goto cleanup;
25132514

src/clone.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include "remote.h"
2020
#include "futils.h"
2121
#include "refs.h"
22-
#include "path.h"
22+
#include "fs_path.h"
2323
#include "repository.h"
2424
#include "odb.h"
2525

@@ -333,7 +333,7 @@ static int create_and_configure_origin(
333333
void *payload = options->remote_cb_payload;
334334

335335
/* If the path exists and is a dir, the url should be the absolute path */
336-
if (git_path_root(url) < 0 && git_path_exists(url) && git_path_isdir(url)) {
336+
if (git_fs_path_root(url) < 0 && git_fs_path_exists(url) && git_fs_path_isdir(url)) {
337337
if (p_realpath(url, buf) == NULL)
338338
return -1;
339339

@@ -433,8 +433,8 @@ int git_clone__should_clone_local(const char *url_or_path, git_clone_local_t loc
433433
if (local == GIT_CLONE_NO_LOCAL)
434434
return 0;
435435

436-
if ((is_url = git_path_is_local_file_url(url_or_path)) != 0) {
437-
if (git_path_fromurl(&fromurl, url_or_path) < 0) {
436+
if ((is_url = git_fs_path_is_local_file_url(url_or_path)) != 0) {
437+
if (git_fs_path_fromurl(&fromurl, url_or_path) < 0) {
438438
is_local = -1;
439439
goto done;
440440
}
@@ -443,7 +443,7 @@ int git_clone__should_clone_local(const char *url_or_path, git_clone_local_t loc
443443
}
444444

445445
is_local = (!is_url || local != GIT_CLONE_LOCAL_AUTO) &&
446-
git_path_isdir(path);
446+
git_fs_path_isdir(path);
447447

448448
done:
449449
git_str_dispose(&fromurl);
@@ -474,14 +474,14 @@ static int git__clone(
474474
GIT_ERROR_CHECK_VERSION(&options, GIT_CLONE_OPTIONS_VERSION, "git_clone_options");
475475

476476
/* Only clone to a new directory or an empty directory */
477-
if (git_path_exists(local_path) && !use_existing && !git_path_is_empty_dir(local_path)) {
477+
if (git_fs_path_exists(local_path) && !use_existing && !git_fs_path_is_empty_dir(local_path)) {
478478
git_error_set(GIT_ERROR_INVALID,
479479
"'%s' exists and is not an empty directory", local_path);
480480
return GIT_EEXISTS;
481481
}
482482

483483
/* Only remove the root directory on failure if we create it */
484-
if (git_path_exists(local_path))
484+
if (git_fs_path_exists(local_path))
485485
rmdir_flags |= GIT_RMDIR_SKIP_ROOT;
486486

487487
if (options.repository_cb)
@@ -602,7 +602,7 @@ static int clone_local_into(git_repository *repo, git_remote *remote, const git_
602602
* repo, if it's not rooted, the path should be relative to
603603
* the repository's worktree/gitdir.
604604
*/
605-
if ((error = git_path_from_url_or_path(&src_path, git_remote_url(remote))) < 0)
605+
if ((error = git_fs_path_from_url_or_path(&src_path, git_remote_url(remote))) < 0)
606606
return error;
607607

608608
/* Copy .git/objects/ from the source to the target */

src/config.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1177,7 +1177,7 @@ int git_config__find_programdata(git_str *path)
11771177
if (ret != GIT_OK)
11781178
return ret;
11791179

1180-
return git_path_validate_system_file_ownership(path->ptr);
1180+
return git_fs_path_validate_system_file_ownership(path->ptr);
11811181
}
11821182

11831183
int git_config__global_location(git_str *buf)

src/config_file.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ static int config_file_open(git_config_backend *cfg, git_config_level_t level, c
108108
if ((res = git_config_entries_new(&b->entries)) < 0)
109109
return res;
110110

111-
if (!git_path_exists(b->file.path))
111+
if (!git_fs_path_exists(b->file.path))
112112
return 0;
113113

114114
/*
@@ -529,7 +529,7 @@ static int included_path(git_str *out, const char *dir, const char *path)
529529
if (path[0] == '~' && path[1] == '/')
530530
return git_sysdir_expand_global_file(out, &path[1]);
531531

532-
return git_path_join_unrooted(out, path, dir, NULL);
532+
return git_fs_path_join_unrooted(out, path, dir, NULL);
533533
}
534534

535535
/* Escape the values to write them to the file */
@@ -574,7 +574,7 @@ static int parse_include(config_file_parse_data *parse_data, const char *file)
574574
if (!file)
575575
return 0;
576576

577-
if ((result = git_path_dirname_r(&path, parse_data->file->path)) < 0)
577+
if ((result = git_fs_path_dirname_r(&path, parse_data->file->path)) < 0)
578578
return result;
579579

580580
dir = git_str_detach(&path);
@@ -611,17 +611,17 @@ static int do_match_gitdir(
611611
git_str pattern = GIT_STR_INIT, gitdir = GIT_STR_INIT;
612612
int error;
613613

614-
if (condition[0] == '.' && git_path_is_dirsep(condition[1])) {
615-
git_path_dirname_r(&pattern, cfg_file);
614+
if (condition[0] == '.' && git_fs_path_is_dirsep(condition[1])) {
615+
git_fs_path_dirname_r(&pattern, cfg_file);
616616
git_str_joinpath(&pattern, pattern.ptr, condition + 2);
617-
} else if (condition[0] == '~' && git_path_is_dirsep(condition[1]))
617+
} else if (condition[0] == '~' && git_fs_path_is_dirsep(condition[1]))
618618
git_sysdir_expand_global_file(&pattern, condition + 1);
619-
else if (!git_path_is_absolute(condition))
619+
else if (!git_fs_path_is_absolute(condition))
620620
git_str_joinpath(&pattern, "**", condition);
621621
else
622622
git_str_sets(&pattern, condition);
623623

624-
if (git_path_is_dirsep(condition[strlen(condition) - 1]))
624+
if (git_fs_path_is_dirsep(condition[strlen(condition) - 1]))
625625
git_str_puts(&pattern, "**");
626626

627627
if (git_str_oom(&pattern)) {
@@ -632,7 +632,7 @@ static int do_match_gitdir(
632632
if ((error = git_repository__item_path(&gitdir, repo, GIT_REPOSITORY_ITEM_GITDIR)) < 0)
633633
goto out;
634634

635-
if (git_path_is_dirsep(gitdir.ptr[gitdir.size - 1]))
635+
if (git_fs_path_is_dirsep(gitdir.ptr[gitdir.size - 1]))
636636
git_str_truncate(&gitdir, gitdir.size - 1);
637637

638638
*matches = wildmatch(pattern.ptr, gitdir.ptr,
@@ -699,7 +699,7 @@ static int conditional_match_onbranch(
699699
*/
700700
if ((error = git_str_sets(&buf, condition)) < 0)
701701
goto out;
702-
if (git_path_is_dirsep(condition[strlen(condition) - 1]) &&
702+
if (git_fs_path_is_dirsep(condition[strlen(condition) - 1]) &&
703703
(error = git_str_puts(&buf, "**")) < 0)
704704
goto out;
705705

@@ -861,7 +861,7 @@ static int config_file_read(
861861
int error;
862862

863863
if (p_stat(file->path, &st) < 0) {
864-
error = git_path_set_error(errno, file->path, "stat");
864+
error = git_fs_path_set_error(errno, file->path, "stat");
865865
goto out;
866866
}
867867

0 commit comments

Comments
 (0)