Skip to content

Commit 31171d9

Browse files
mhaggerpeff
authored andcommitted
longest_ancestor_length(): take a string_list argument for prefixes
Change longest_ancestor_length() to take the prefixes argument as a string_list rather than as a colon-separated string. This will make it easier for the caller to alter the entries before calling longest_ancestor_length(). Signed-off-by: Michael Haggerty <[email protected]> Signed-off-by: Jeff King <[email protected]>
1 parent a5ccdbe commit 31171d9

File tree

4 files changed

+26
-17
lines changed

4 files changed

+26
-17
lines changed

cache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ const char *real_path_if_valid(const char *path);
718718
const char *absolute_path(const char *path);
719719
const char *relative_path(const char *abs, const char *base);
720720
int normalize_path_copy(char *dst, const char *src);
721-
int longest_ancestor_length(const char *path, const char *prefix_list);
721+
int longest_ancestor_length(const char *path, struct string_list *prefixes);
722722
char *strip_path_suffix(const char *path, const char *suffix);
723723
int daemon_avoid_alias(const char *path);
724724
int offset_1st_component(const char *path);

path.c

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -570,30 +570,27 @@ int normalize_path_copy(char *dst, const char *src)
570570

571571
/*
572572
* path = Canonical absolute path
573-
* prefix_list = Colon-separated list of absolute paths
573+
* prefixes = string_list containing absolute paths
574574
*
575-
* Determines, for each path in prefix_list, whether the "prefix" really
575+
* Determines, for each path in prefixes, whether the "prefix" really
576576
* is an ancestor directory of path. Returns the length of the longest
577577
* ancestor directory, excluding any trailing slashes, or -1 if no prefix
578-
* is an ancestor. (Note that this means 0 is returned if prefix_list is
579-
* "/".) "/foo" is not considered an ancestor of "/foobar". Directories
578+
* is an ancestor. (Note that this means 0 is returned if prefixes is
579+
* ["/"].) "/foo" is not considered an ancestor of "/foobar". Directories
580580
* are not considered to be their own ancestors. path must be in a
581581
* canonical form: empty components, or "." or ".." components are not
582-
* allowed. prefix_list may be null, which is like "".
582+
* allowed. Empty strings in prefixes are ignored.
583583
*/
584-
int longest_ancestor_length(const char *path, const char *prefix_list)
584+
int longest_ancestor_length(const char *path, struct string_list *prefixes)
585585
{
586-
struct string_list prefixes = STRING_LIST_INIT_DUP;
587586
char buf[PATH_MAX+1];
588587
int i, max_len = -1;
589588

590-
if (prefix_list == NULL || !strcmp(path, "/"))
589+
if (!strcmp(path, "/"))
591590
return -1;
592591

593-
string_list_split(&prefixes, prefix_list, PATH_SEP, -1);
594-
595-
for (i = 0; i < prefixes.nr; i++) {
596-
const char *ceil = prefixes.items[i].string;
592+
for (i = 0; i < prefixes->nr; i++) {
593+
const char *ceil = prefixes->items[i].string;
597594
int len = strlen(ceil);
598595

599596
if (len == 0 || len > PATH_MAX || !is_absolute_path(ceil))
@@ -611,7 +608,6 @@ int longest_ancestor_length(const char *path, const char *prefix_list)
611608
}
612609
}
613610

614-
string_list_clear(&prefixes, 0);
615611
return max_len;
616612
}
617613

setup.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "cache.h"
22
#include "dir.h"
3+
#include "string-list.h"
34

45
static int inside_git_dir = -1;
56
static int inside_work_tree = -1;
@@ -627,10 +628,11 @@ static dev_t get_device_or_die(const char *path, const char *prefix, int prefix_
627628
static const char *setup_git_directory_gently_1(int *nongit_ok)
628629
{
629630
const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT);
631+
struct string_list ceiling_dirs = STRING_LIST_INIT_DUP;
630632
static char cwd[PATH_MAX+1];
631633
const char *gitdirenv, *ret;
632634
char *gitfile;
633-
int len, offset, offset_parent, ceil_offset;
635+
int len, offset, offset_parent, ceil_offset = -1;
634636
dev_t current_device = 0;
635637
int one_filesystem = 1;
636638

@@ -655,7 +657,12 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)
655657
if (gitdirenv)
656658
return setup_explicit_git_dir(gitdirenv, cwd, len, nongit_ok);
657659

658-
ceil_offset = longest_ancestor_length(cwd, env_ceiling_dirs);
660+
if (env_ceiling_dirs) {
661+
string_list_split(&ceiling_dirs, env_ceiling_dirs, PATH_SEP, -1);
662+
ceil_offset = longest_ancestor_length(cwd, &ceiling_dirs);
663+
string_list_clear(&ceiling_dirs, 0);
664+
}
665+
659666
if (ceil_offset < 0 && has_dos_drive_prefix(cwd))
660667
ceil_offset = 1;
661668

test-path-utils.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "cache.h"
2+
#include "string-list.h"
23

34
int main(int argc, char **argv)
45
{
@@ -30,7 +31,12 @@ int main(int argc, char **argv)
3031
}
3132

3233
if (argc == 4 && !strcmp(argv[1], "longest_ancestor_length")) {
33-
int len = longest_ancestor_length(argv[2], argv[3]);
34+
int len;
35+
struct string_list ceiling_dirs = STRING_LIST_INIT_DUP;
36+
37+
string_list_split(&ceiling_dirs, argv[3], PATH_SEP, -1);
38+
len = longest_ancestor_length(argv[2], &ceiling_dirs);
39+
string_list_clear(&ceiling_dirs, 0);
3440
printf("%d\n", len);
3541
return 0;
3642
}

0 commit comments

Comments
 (0)