Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
git2
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
lvzhengyang
git2
Commits
ba6f86eb
Commit
ba6f86eb
authored
Mar 18, 2016
by
Edward Thomson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Introduce `git_path_common_dirlen`
parent
82a1aab6
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
46 additions
and
0 deletions
+46
-0
src/path.c
+14
-0
src/path.h
+12
-0
tests/core/path.c
+20
-0
No files found.
src/path.c
View file @
ba6f86eb
...
...
@@ -810,6 +810,20 @@ int git_path_cmp(
return
(
c1
<
c2
)
?
-
1
:
(
c1
>
c2
)
?
1
:
0
;
}
size_t
git_path_common_dirlen
(
const
char
*
one
,
const
char
*
two
)
{
const
char
*
p
,
*
q
,
*
dirsep
=
NULL
;
for
(
p
=
one
,
q
=
two
;
*
p
&&
*
q
;
p
++
,
q
++
)
{
if
(
*
p
==
'/'
&&
*
q
==
'/'
)
dirsep
=
p
;
else
if
(
*
p
!=
*
q
)
break
;
}
return
dirsep
?
(
dirsep
-
one
)
+
1
:
0
;
}
int
git_path_make_relative
(
git_buf
*
path
,
const
char
*
parent
)
{
const
char
*
p
,
*
q
,
*
p_dirsep
,
*
q_dirsep
;
...
...
src/path.h
View file @
ba6f86eb
...
...
@@ -203,6 +203,18 @@ extern bool git_path_contains(git_buf *dir, const char *item);
extern
bool
git_path_contains_dir
(
git_buf
*
parent
,
const
char
*
subdir
);
/**
* Determine the common directory length between two paths, including
* the final path separator. For example, given paths 'a/b/c/1.txt
* and 'a/b/c/d/2.txt', the common directory is 'a/b/c/', and this
* will return the length of the string 'a/b/c/', which is 6.
*
* @param one The first path
* @param two The second path
* @return The length of the common directory
*/
extern
size_t
git_path_common_dirlen
(
const
char
*
one
,
const
char
*
two
);
/**
* Make the path relative to the given parent path.
*
* @param path The path to make relative
...
...
tests/core/path.c
View file @
ba6f86eb
...
...
@@ -652,3 +652,23 @@ void test_core_path__15_resolve_relative(void)
git_buf_free
(
&
buf
);
}
#define assert_common_dirlen(i, p, q) \
cl_assert_equal_i((i), git_path_common_dirlen((p), (q)));
void
test_core_path__16_resolve_relative
(
void
)
{
assert_common_dirlen
(
0
,
""
,
""
);
assert_common_dirlen
(
0
,
""
,
"bar.txt"
);
assert_common_dirlen
(
0
,
"foo.txt"
,
"bar.txt"
);
assert_common_dirlen
(
0
,
"foo.txt"
,
""
);
assert_common_dirlen
(
0
,
"foo/bar.txt"
,
"bar/foo.txt"
);
assert_common_dirlen
(
0
,
"foo/bar.txt"
,
"../foo.txt"
);
assert_common_dirlen
(
1
,
"/one.txt"
,
"/two.txt"
);
assert_common_dirlen
(
4
,
"foo/one.txt"
,
"foo/two.txt"
);
assert_common_dirlen
(
5
,
"/foo/one.txt"
,
"/foo/two.txt"
);
assert_common_dirlen
(
6
,
"a/b/c/foo.txt"
,
"a/b/c/d/e/bar.txt"
);
assert_common_dirlen
(
7
,
"/a/b/c/foo.txt"
,
"/a/b/c/d/e/bar.txt"
);
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment