From 93d8f77fed9407421ba1c90141d997b2dea7e0e7 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Thu, 23 May 2013 15:11:53 -0700 Subject: [PATCH 1/2] Improve test failure output --- include/git2/repository.h | 9 +++++ src/repository.c | 12 +++++++ tests-clar/repo/shallow.c | 33 ++++++++++++++++++ tests-clar/resources/shallow.git/HEAD | 1 + tests-clar/resources/shallow.git/config | 8 +++++ ...6e49b161700946489570d96153e5be4dc31ad4.idx | Bin 0 -> 1324 bytes ...e49b161700946489570d96153e5be4dc31ad4.pack | Bin 0 -> 791 bytes tests-clar/resources/shallow.git/packed-refs | 2 ++ .../resources/shallow.git/refs/.gitkeep | 0 tests-clar/resources/shallow.git/shallow | 1 + 10 files changed, 66 insertions(+) create mode 100644 tests-clar/repo/shallow.c create mode 100644 tests-clar/resources/shallow.git/HEAD create mode 100644 tests-clar/resources/shallow.git/config create mode 100644 tests-clar/resources/shallow.git/objects/pack/pack-706e49b161700946489570d96153e5be4dc31ad4.idx create mode 100644 tests-clar/resources/shallow.git/objects/pack/pack-706e49b161700946489570d96153e5be4dc31ad4.pack create mode 100644 tests-clar/resources/shallow.git/packed-refs create mode 100644 tests-clar/resources/shallow.git/refs/.gitkeep create mode 100644 tests-clar/resources/shallow.git/shallow diff --git a/include/git2/repository.h b/include/git2/repository.h index bb2b3db83..4fbd913b1 100644 --- a/include/git2/repository.h +++ b/include/git2/repository.h @@ -657,6 +657,15 @@ GIT_EXTERN(int) git_repository_set_namespace(git_repository *repo, const char *n */ GIT_EXTERN(const char *) git_repository_get_namespace(git_repository *repo); + +/** + * Determine if the repository was a shallow clone + * + * @param repo The repository + * @return 1 if shallow, zero if not + */ +GIT_EXTERN(int) git_repository_is_shallow(git_repository *repo); + /** @} */ GIT_END_DECL #endif diff --git a/src/repository.c b/src/repository.c index 9957f32b7..b0359a58f 100644 --- a/src/repository.c +++ b/src/repository.c @@ -1822,3 +1822,15 @@ int git_repository_state(git_repository *repo) git_buf_free(&repo_path); return state; } + +int git_repository_is_shallow(git_repository *repo) +{ + git_buf path = GIT_BUF_INIT; + struct stat st; + + git_buf_joinpath(&path, repo->path_repository, "shallow"); + + if (git_path_lstat(path.ptr, &st) == GIT_ENOTFOUND) + return 0; + return st.st_size == 0 ? 0 : 1; +} diff --git a/tests-clar/repo/shallow.c b/tests-clar/repo/shallow.c new file mode 100644 index 000000000..1cc66ae40 --- /dev/null +++ b/tests-clar/repo/shallow.c @@ -0,0 +1,33 @@ +#include "clar_libgit2.h" +#include "fileops.h" + +static git_repository *g_repo; + +void test_repo_shallow__initialize(void) +{ +} + +void test_repo_shallow__cleanup(void) +{ + cl_git_sandbox_cleanup(); +} + +void test_repo_shallow__no_shallow_file(void) +{ + g_repo = cl_git_sandbox_init("testrepo.git"); + cl_assert_equal_i(0, git_repository_is_shallow(g_repo)); +} + +void test_repo_shallow__empty_shallow_file(void) +{ + g_repo = cl_git_sandbox_init("testrepo.git"); + cl_git_mkfile("testrepo.git/shallow", ""); + cl_assert_equal_i(0, git_repository_is_shallow(g_repo)); +} + +void test_repo_shallow__shallow_repo(void) +{ + g_repo = cl_git_sandbox_init("shallow.git"); + cl_assert_equal_i(1, git_repository_is_shallow(g_repo)); +} + diff --git a/tests-clar/resources/shallow.git/HEAD b/tests-clar/resources/shallow.git/HEAD new file mode 100644 index 000000000..cb089cd89 --- /dev/null +++ b/tests-clar/resources/shallow.git/HEAD @@ -0,0 +1 @@ +ref: refs/heads/master diff --git a/tests-clar/resources/shallow.git/config b/tests-clar/resources/shallow.git/config new file mode 100644 index 000000000..a88b74b69 --- /dev/null +++ b/tests-clar/resources/shallow.git/config @@ -0,0 +1,8 @@ +[core] + repositoryformatversion = 0 + filemode = true + bare = true + ignorecase = true + precomposeunicode = false +[remote "origin"] + url = file://testrepo.git diff --git a/tests-clar/resources/shallow.git/objects/pack/pack-706e49b161700946489570d96153e5be4dc31ad4.idx b/tests-clar/resources/shallow.git/objects/pack/pack-706e49b161700946489570d96153e5be4dc31ad4.idx new file mode 100644 index 0000000000000000000000000000000000000000..bfc7d24ff4178cdde4ff06b36ace5dc71e6d7edf GIT binary patch literal 1324 zcmexg;-AdGz`z8=v<8eo3kCv%{6;Qj2I?KDm<8x}N-!%>KQ%BL&>VIk=AaSg1e!bI zv4p_=uf1JqtU_0s^-B0NHCl{&&YDfX=Ka!fsjb(GZ;J|_JpL87LFtU^&gOk)+ilo8 z?#$Vj<~#U=IsG@>yF%Ge;oI7R z{ubLdnMd1|`i?xlcWj?&@;dv`_DnUy^Ijt;fofp8vS~tls#F zoaLp=h1)FO31v*P;+vQv&7)at4b1w${L6S3NS6R{1CSjFjBg$w{SJsv0oilKH*)Qo rI*((~)L*l@&xOAhtS?G@u-*99hsWiQkG`n5FZS{O$3sUJO4b7af1;4_ literal 0 HcmV?d00001 diff --git a/tests-clar/resources/shallow.git/objects/pack/pack-706e49b161700946489570d96153e5be4dc31ad4.pack b/tests-clar/resources/shallow.git/objects/pack/pack-706e49b161700946489570d96153e5be4dc31ad4.pack new file mode 100644 index 0000000000000000000000000000000000000000..ccc6932fc403443b47b4bb7f4e1ea1245ce7193e GIT binary patch literal 791 zcmWG=boORoU|<4b&Z)c=bLO7D$jM~L)9~=S-$d3a+dPB0m70_-9A|m&Wj=o@@_V?< z!R_<;|81PTiY*jJm(E%f$cc{+v7Y>Avgx<|!m}v`$byd@lX{-SF$F zJinipzGF6Mwf{I>5a^&@-z;WB9=Gpyt!0r*J^oGRQ5D(rZk76n6GdtlW@`3T|8H~B zzi{{XW7~a}XBt{Ie4V$%^Q@%sPchYs#~rrf`a;eESD)>BwV~GOWpDG061|zCk+Wu* zE^Jav2tOHkCTLy#J~uI!)cM6zZksOv3j+uj2V}A=z)oy3l^v zIQd`J_wu6m*6(|)1EW!q5nH}N$0@b z3)m~>7@HUv7#jw16qS~IEKQAAn6u(TZmwf}ckb*ni{EL^nFWnZWzfV`I=F^Yx8*EFB1C~~*8-Y=WpKqu)NCIA|x{|%QE6* z(Zy}e-{Wfnf55t;MI!@vnxpqyR$FXSYuUXya!e0y47bQLb0F@qk-v9sr literal 0 HcmV?d00001 diff --git a/tests-clar/resources/shallow.git/packed-refs b/tests-clar/resources/shallow.git/packed-refs new file mode 100644 index 000000000..97eed743b --- /dev/null +++ b/tests-clar/resources/shallow.git/packed-refs @@ -0,0 +1,2 @@ +# pack-refs with: peeled +a65fedf39aefe402d3bb6e24df4d4f5fe4547750 refs/heads/master diff --git a/tests-clar/resources/shallow.git/refs/.gitkeep b/tests-clar/resources/shallow.git/refs/.gitkeep new file mode 100644 index 000000000..e69de29bb diff --git a/tests-clar/resources/shallow.git/shallow b/tests-clar/resources/shallow.git/shallow new file mode 100644 index 000000000..9536ad89c --- /dev/null +++ b/tests-clar/resources/shallow.git/shallow @@ -0,0 +1 @@ +be3563ae3f795b2b4353bcce3a527ad0a4f7f644 From 6f0b8142e65b43f2224027a7abc67116ab6ad1a7 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Thu, 23 May 2013 17:28:52 -0700 Subject: [PATCH 2/2] Stop leaking memory --- src/repository.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/repository.c b/src/repository.c index b0359a58f..28505e822 100644 --- a/src/repository.c +++ b/src/repository.c @@ -1827,10 +1827,15 @@ int git_repository_is_shallow(git_repository *repo) { git_buf path = GIT_BUF_INIT; struct stat st; + int error; git_buf_joinpath(&path, repo->path_repository, "shallow"); + error = git_path_lstat(path.ptr, &st); + git_buf_free(&path); - if (git_path_lstat(path.ptr, &st) == GIT_ENOTFOUND) + if (error == GIT_ENOTFOUND) return 0; + if (error < 0) + return -1; return st.st_size == 0 ? 0 : 1; }