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 <mhagger@alum.mit.edu> Signed-off-by: Jeff King <peff@peff.net>
Michael Haggerty committed
Oct 28, 2012 at 17:16 UTC
31171d9e454d71144685866cfd6476b8ac69d314
4 files changed
+26
-17
cache.h
+1
-1
@@ -718,7 +718,7 @@ const char *real_path_if_valid(const char *path);
718
const char *absolute_path(const char *path);
719
const char *relative_path(const char *abs, const char *base);
720
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);
722
char *strip_path_suffix(const char *path, const char *suffix);
723
int daemon_avoid_alias(const char *path);
724
int offset_1st_component(const char *path);
path.c
+9
-13
@@ -570,30 +570,27 @@ int normalize_path_copy(char *dst, const char *src)
570
571
/*
572
* path = Canonical absolute path
573
- * prefix_list = Colon-separated list of absolute paths
573
+ * prefixes = string_list containing absolute paths
574
*
575
- * Determines, for each path in prefix_list, whether the "prefix" really
575
+ * Determines, for each path in prefixes, whether the "prefix" really
576
* is an ancestor directory of path. Returns the length of the longest
577
* 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
580
* are not considered to be their own ancestors. path must be in a
581
* 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.
583
*/
584
-int longest_ancestor_length(const char *path, const char *prefix_list)
584
+int longest_ancestor_length(const char *path, struct string_list *prefixes)
585
{
586
- struct string_list prefixes = STRING_LIST_INIT_DUP;
586
char buf[PATH_MAX+1];
587
int i, max_len = -1;
588
590
- if (prefix_list == NULL || !strcmp(path, "/"))
589
+ if (!strcmp(path, "/"))
590
return -1;
591
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;
594
int len = strlen(ceil);
595
596
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)
608
}
609
}
610
614
- string_list_clear(&prefixes, 0);
611
return max_len;
612
}
613
setup.c
+9
-2
@@ -1,5 +1,6 @@
1
#include "cache.h"
2
#include "dir.h"
3
+#include "string-list.h"
4
5
static int inside_git_dir = -1;
6
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_
628
static const char *setup_git_directory_gently_1(int *nongit_ok)
629
{
630
const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT);
631
+ struct string_list ceiling_dirs = STRING_LIST_INIT_DUP;
632
static char cwd[PATH_MAX+1];
633
const char *gitdirenv, *ret;
634
char *gitfile;
633
- int len, offset, offset_parent, ceil_offset;
635
+ int len, offset, offset_parent, ceil_offset = -1;
636
dev_t current_device = 0;
637
int one_filesystem = 1;
638
@@ -655,7 +657,12 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)
657
if (gitdirenv)
658
return setup_explicit_git_dir(gitdirenv, cwd, len, nongit_ok);
659
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
+
666
if (ceil_offset < 0 && has_dos_drive_prefix(cwd))
667
ceil_offset = 1;
668
test-path-utils.c
+7
-1
@@ -1,4 +1,5 @@
1
#include "cache.h"
2
+#include "string-list.h"
3
4
int main(int argc, char **argv)
5
{
@@ -30,7 +31,12 @@ int main(int argc, char **argv)
31
}
32
33
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);
40
printf("%d\n", len);
41
return 0;
42
}