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
7fc00435
Commit
7fc00435
authored
Jan 03, 2013
by
Russell Belfer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add index API to remove all files in a directory
This adds the git_index_remove_directory API plus tests.
parent
1b88faf7
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
132 additions
and
0 deletions
+132
-0
include/git2/index.h
+11
-0
src/index.c
+38
-0
tests-clar/index/tests.c
+83
-0
No files found.
include/git2/index.h
View file @
7fc00435
...
...
@@ -313,6 +313,17 @@ GIT_EXTERN(const git_index_entry *) git_index_get_bypath(
GIT_EXTERN
(
int
)
git_index_remove
(
git_index
*
index
,
const
char
*
path
,
int
stage
);
/**
* Remove all entries from the index under a given directory
*
* @param index an existing index object
* @param dir container directory path
* @param stage stage to search
* @return 0 or an error code
*/
GIT_EXTERN
(
int
)
git_index_remove_directory
(
git_index
*
index
,
const
char
*
dir
,
int
stage
);
/**
* Add or update an index entry from an in-memory struct
*
* If a previous index entry exists that has the same path and stage
...
...
src/index.c
View file @
7fc00435
...
...
@@ -794,6 +794,44 @@ int git_index_remove(git_index *index, const char *path, int stage)
return
error
;
}
int
git_index_remove_directory
(
git_index
*
index
,
const
char
*
dir
,
int
stage
)
{
git_buf
pfx
=
GIT_BUF_INIT
;
int
error
=
0
;
size_t
pos
;
git_index_entry
*
entry
;
if
(
git_buf_sets
(
&
pfx
,
dir
)
<
0
||
git_path_to_dir
(
&
pfx
)
<
0
)
return
-
1
;
git_vector_sort
(
&
index
->
entries
);
pos
=
git_index__prefix_position
(
index
,
pfx
.
ptr
);
while
(
1
)
{
entry
=
git_vector_get
(
&
index
->
entries
,
pos
);
if
(
!
entry
||
git__prefixcmp
(
entry
->
path
,
pfx
.
ptr
)
!=
0
)
break
;
if
(
index_entry_stage
(
entry
)
!=
stage
)
{
++
pos
;
continue
;
}
git_tree_cache_invalidate_path
(
index
->
tree
,
entry
->
path
);
if
((
error
=
git_vector_remove
(
&
index
->
entries
,
pos
))
<
0
)
break
;
index_entry_free
(
entry
);
/* removed entry at 'pos' so we don't need to increment it */
}
git_buf_free
(
&
pfx
);
return
error
;
}
static
int
index_find
(
git_index
*
index
,
const
char
*
path
,
int
stage
)
{
struct
entry_srch_key
srch_key
;
...
...
tests-clar/index/tests.c
View file @
7fc00435
...
...
@@ -290,3 +290,86 @@ void test_index_tests__write_invalid_filename(void)
cl_fixture_cleanup
(
"read_tree"
);
}
void
test_index_tests__remove_entry
(
void
)
{
git_repository
*
repo
;
git_index
*
index
;
p_mkdir
(
"index_test"
,
0770
);
cl_git_pass
(
git_repository_init
(
&
repo
,
"index_test"
,
0
));
cl_git_pass
(
git_repository_index
(
&
index
,
repo
));
cl_assert
(
git_index_entrycount
(
index
)
==
0
);
cl_git_mkfile
(
"index_test/hello"
,
NULL
);
cl_git_pass
(
git_index_add_from_workdir
(
index
,
"hello"
));
cl_git_pass
(
git_index_write
(
index
));
cl_git_pass
(
git_index_read
(
index
));
/* reload */
cl_assert
(
git_index_entrycount
(
index
)
==
1
);
cl_assert
(
git_index_get_bypath
(
index
,
"hello"
,
0
)
!=
NULL
);
cl_git_pass
(
git_index_remove
(
index
,
"hello"
,
0
));
cl_git_pass
(
git_index_write
(
index
));
cl_git_pass
(
git_index_read
(
index
));
/* reload */
cl_assert
(
git_index_entrycount
(
index
)
==
0
);
cl_assert
(
git_index_get_bypath
(
index
,
"hello"
,
0
)
==
NULL
);
git_index_free
(
index
);
git_repository_free
(
repo
);
cl_fixture_cleanup
(
"index_test"
);
}
void
test_index_tests__remove_directory
(
void
)
{
git_repository
*
repo
;
git_index
*
index
;
p_mkdir
(
"index_test"
,
0770
);
cl_git_pass
(
git_repository_init
(
&
repo
,
"index_test"
,
0
));
cl_git_pass
(
git_repository_index
(
&
index
,
repo
));
cl_assert_equal_i
(
0
,
(
int
)
git_index_entrycount
(
index
));
p_mkdir
(
"index_test/a"
,
0770
);
cl_git_mkfile
(
"index_test/a/1.txt"
,
NULL
);
cl_git_mkfile
(
"index_test/a/2.txt"
,
NULL
);
cl_git_mkfile
(
"index_test/a/3.txt"
,
NULL
);
cl_git_mkfile
(
"index_test/b.txt"
,
NULL
);
cl_git_pass
(
git_index_add_from_workdir
(
index
,
"a/1.txt"
));
cl_git_pass
(
git_index_add_from_workdir
(
index
,
"a/2.txt"
));
cl_git_pass
(
git_index_add_from_workdir
(
index
,
"a/3.txt"
));
cl_git_pass
(
git_index_add_from_workdir
(
index
,
"b.txt"
));
cl_git_pass
(
git_index_write
(
index
));
cl_git_pass
(
git_index_read
(
index
));
/* reload */
cl_assert_equal_i
(
4
,
(
int
)
git_index_entrycount
(
index
));
cl_assert
(
git_index_get_bypath
(
index
,
"a/1.txt"
,
0
)
!=
NULL
);
cl_assert
(
git_index_get_bypath
(
index
,
"a/2.txt"
,
0
)
!=
NULL
);
cl_assert
(
git_index_get_bypath
(
index
,
"b.txt"
,
0
)
!=
NULL
);
cl_git_pass
(
git_index_remove
(
index
,
"a/1.txt"
,
0
));
cl_git_pass
(
git_index_write
(
index
));
cl_git_pass
(
git_index_read
(
index
));
/* reload */
cl_assert_equal_i
(
3
,
(
int
)
git_index_entrycount
(
index
));
cl_assert
(
git_index_get_bypath
(
index
,
"a/1.txt"
,
0
)
==
NULL
);
cl_assert
(
git_index_get_bypath
(
index
,
"a/2.txt"
,
0
)
!=
NULL
);
cl_assert
(
git_index_get_bypath
(
index
,
"b.txt"
,
0
)
!=
NULL
);
cl_git_pass
(
git_index_remove_directory
(
index
,
"a"
,
0
));
cl_git_pass
(
git_index_write
(
index
));
cl_git_pass
(
git_index_read
(
index
));
/* reload */
cl_assert_equal_i
(
1
,
(
int
)
git_index_entrycount
(
index
));
cl_assert
(
git_index_get_bypath
(
index
,
"a/1.txt"
,
0
)
==
NULL
);
cl_assert
(
git_index_get_bypath
(
index
,
"a/2.txt"
,
0
)
==
NULL
);
cl_assert
(
git_index_get_bypath
(
index
,
"b.txt"
,
0
)
!=
NULL
);
git_index_free
(
index
);
git_repository_free
(
repo
);
cl_fixture_cleanup
(
"index_test"
);
}
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