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
0ee9f31c
Commit
0ee9f31c
authored
Aug 20, 2014
by
Edward Thomson
Committed by
Jameson Miller
Sep 02, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Introduce git_path_make_relative
parent
6f3082d9
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
121 additions
and
0 deletions
+121
-0
src/path.c
+55
-0
src/path.h
+11
-0
tests/path/core.c
+55
-0
No files found.
src/path.c
View file @
0ee9f31c
...
...
@@ -750,6 +750,61 @@ int git_path_cmp(
return
(
c1
<
c2
)
?
-
1
:
(
c1
>
c2
)
?
1
:
0
;
}
int
git_path_make_relative
(
git_buf
*
path
,
const
char
*
parent
)
{
const
char
*
p
,
*
q
,
*
p_dirsep
,
*
q_dirsep
;
size_t
plen
=
path
->
size
,
newlen
,
depth
=
1
,
i
;
for
(
p_dirsep
=
p
=
path
->
ptr
,
q_dirsep
=
q
=
parent
;
*
p
&&
*
q
;
p
++
,
q
++
)
{
if
(
*
p
==
'/'
&&
*
q
==
'/'
)
{
p_dirsep
=
p
;
q_dirsep
=
q
;
}
else
if
(
*
p
!=
*
q
)
break
;
}
/* need at least 1 common path segment */
if
((
p_dirsep
==
path
->
ptr
||
q_dirsep
==
parent
)
&&
(
*
p_dirsep
!=
'/'
||
*
q_dirsep
!=
'/'
))
{
giterr_set
(
GITERR_INVALID
,
"%s is not a parent of %s"
,
parent
,
path
->
ptr
);
return
GIT_ENOTFOUND
;
}
if
(
*
p
==
'/'
&&
!*
q
)
p
++
;
else
if
(
!*
p
&&
*
q
==
'/'
)
q
++
;
else
if
(
!*
p
&&
!*
q
)
return
git_buf_clear
(
path
),
0
;
else
{
p
=
p_dirsep
+
1
;
q
=
q_dirsep
+
1
;
}
plen
-=
(
p
-
path
->
ptr
);
if
(
!*
q
)
return
git_buf_set
(
path
,
p
,
plen
);
for
(;
(
q
=
strchr
(
q
,
'/'
))
&&
*
(
q
+
1
);
q
++
)
depth
++
;
newlen
=
(
depth
*
3
)
+
plen
;
if
(
git_buf_try_grow
(
path
,
newlen
+
1
,
1
,
0
)
<
0
)
return
-
1
;
memmove
(
path
->
ptr
+
(
depth
*
3
),
p
,
plen
+
1
);
for
(
i
=
0
;
i
<
depth
;
i
++
)
memcpy
(
path
->
ptr
+
(
i
*
3
),
"../"
,
3
);
path
->
size
=
newlen
;
return
0
;
}
bool
git_path_has_non_ascii
(
const
char
*
path
,
size_t
pathlen
)
{
const
uint8_t
*
scan
=
(
const
uint8_t
*
)
path
,
*
end
;
...
...
src/path.h
View file @
0ee9f31c
...
...
@@ -197,6 +197,17 @@ extern bool git_path_contains(git_buf *dir, const char *item);
extern
bool
git_path_contains_dir
(
git_buf
*
parent
,
const
char
*
subdir
);
/**
* Make the path relative to the given parent path.
*
* @param path The path to make relative
* @param parent The parent path to make path relative to
* @return 0 if path was made relative, GIT_ENOTFOUND
* if there was not common root between the paths,
* or <0.
*/
extern
int
git_path_make_relative
(
git_buf
*
path
,
const
char
*
parent
);
/**
* Check if the given path contains the given file.
*
* @param dir Directory path that might contain file
...
...
tests/path/core.c
0 → 100644
View file @
0ee9f31c
#include "clar_libgit2.h"
#include "path.h"
static
void
test_make_relative
(
const
char
*
expected_path
,
const
char
*
path
,
const
char
*
parent
,
int
expected_status
)
{
git_buf
buf
=
GIT_BUF_INIT
;
git_buf_puts
(
&
buf
,
path
);
cl_assert_equal_i
(
expected_status
,
git_path_make_relative
(
&
buf
,
parent
));
cl_assert_equal_s
(
expected_path
,
buf
.
ptr
);
git_buf_free
(
&
buf
);
}
void
test_path_core__make_relative
(
void
)
{
git_buf
buf
=
GIT_BUF_INIT
;
test_make_relative
(
"foo.c"
,
"/path/to/foo.c"
,
"/path/to"
,
0
);
test_make_relative
(
"bar/foo.c"
,
"/path/to/bar/foo.c"
,
"/path/to"
,
0
);
test_make_relative
(
"foo.c"
,
"/path/to/foo.c"
,
"/path/to/"
,
0
);
test_make_relative
(
""
,
"/path/to"
,
"/path/to"
,
0
);
test_make_relative
(
""
,
"/path/to"
,
"/path/to/"
,
0
);
test_make_relative
(
"../"
,
"/path/to"
,
"/path/to/foo"
,
0
);
test_make_relative
(
"../foo.c"
,
"/path/to/foo.c"
,
"/path/to/bar"
,
0
);
test_make_relative
(
"../bar/foo.c"
,
"/path/to/bar/foo.c"
,
"/path/to/baz"
,
0
);
test_make_relative
(
"../../foo.c"
,
"/path/to/foo.c"
,
"/path/to/foo/bar"
,
0
);
test_make_relative
(
"../../foo/bar.c"
,
"/path/to/foo/bar.c"
,
"/path/to/bar/foo"
,
0
);
test_make_relative
(
"../../foo.c"
,
"/foo.c"
,
"/bar/foo"
,
0
);
test_make_relative
(
"foo.c"
,
"/path/to/foo.c"
,
"/path/to/"
,
0
);
test_make_relative
(
"../foo.c"
,
"/path/to/foo.c"
,
"/path/to/bar/"
,
0
);
test_make_relative
(
"foo.c"
,
"d:/path/to/foo.c"
,
"d:/path/to"
,
0
);
test_make_relative
(
"../foo"
,
"/foo"
,
"/bar"
,
0
);
test_make_relative
(
"path/to/foo.c"
,
"/path/to/foo.c"
,
"/"
,
0
);
test_make_relative
(
"../foo"
,
"path/to/foo"
,
"path/to/bar"
,
0
);
test_make_relative
(
"/path/to/foo.c"
,
"/path/to/foo.c"
,
"d:/path/to"
,
GIT_ENOTFOUND
);
test_make_relative
(
"d:/path/to/foo.c"
,
"d:/path/to/foo.c"
,
"/path/to"
,
GIT_ENOTFOUND
);
test_make_relative
(
"/path/to/foo.c"
,
"/path/to/foo.c"
,
"not-a-rooted-path"
,
GIT_ENOTFOUND
);
test_make_relative
(
"not-a-rooted-path"
,
"not-a-rooted-path"
,
"/path/to"
,
GIT_ENOTFOUND
);
test_make_relative
(
"/path"
,
"/path"
,
"pathtofoo"
,
GIT_ENOTFOUND
);
test_make_relative
(
"path"
,
"path"
,
"pathtofoo"
,
GIT_ENOTFOUND
);
}
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