summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRussell Belfer <rb@github.com>2012-05-11 12:16:19 -0700
committerRussell Belfer <rb@github.com>2012-05-11 12:16:19 -0700
commitdb62807215cbe26b83a354954b7433aa5d90c149 (patch)
tree99fc84122c138e06d015d50e73d35fe977cddfb9
parentdc34da6e8140c034c3673d0f82c896be9d66ef1c (diff)
downloadlibgit2-db62807215cbe26b83a354954b7433aa5d90c149.tar.gz
Fixed leaks and added tests
-rw-r--r--src/repository.c29
-rw-r--r--tests-clar/repo/init.c24
2 files changed, 37 insertions, 16 deletions
diff --git a/src/repository.c b/src/repository.c
index 9031c5956..c5eed531b 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -713,31 +713,28 @@ static int repo_write_template(
const char *git_dir, const char *file, mode_t mode, const char *content)
{
git_buf path = GIT_BUF_INIT;
- int fd;
+ int fd, error = 0;
if (git_buf_joinpath(&path, git_dir, file) < 0)
return -1;
fd = p_open(git_buf_cstr(&path), O_WRONLY | O_CREAT | O_EXCL, mode);
- if (fd < 0) {
- git_buf_free(&path);
- if (errno == EEXIST)
- return 0;
- goto fail;
- }
-
- if (p_write(fd, content, strlen(content)) < 0)
- goto fail;
- p_close(fd);
+ if (fd >= 0) {
+ error = p_write(fd, content, strlen(content));
- return 0;
+ p_close(fd);
+ }
+ else if (errno != EEXIST)
+ error = fd;
-fail:
git_buf_free(&path);
- giterr_set(GITERR_OS,
- "Failed to initialize repository with template '%s'", file);
- return -1;
+
+ if (error)
+ giterr_set(GITERR_OS,
+ "Failed to initialize repository with template '%s'", file);
+
+ return error;
}
static int repo_init_structure(const char *git_dir, int is_bare)
diff --git a/tests-clar/repo/init.c b/tests-clar/repo/init.c
index a12a2c2fb..7f16b5b7c 100644
--- a/tests-clar/repo/init.c
+++ b/tests-clar/repo/init.c
@@ -141,3 +141,27 @@ void test_repo_init__reinit_too_recent_bare_repo(void)
cl_fixture_cleanup("reinit.git");
}
+
+void test_repo_init__additional_templates(void)
+{
+ git_buf path = GIT_BUF_INIT;
+
+ cl_set_cleanup(&cleanup_repository, "tester");
+
+ ensure_repository_init("tester", 0, "tester/.git/", "tester/");
+
+ cl_git_pass(
+ git_buf_joinpath(&path, git_repository_path(_repo), "description"));
+ cl_assert(git_path_isfile(git_buf_cstr(&path)));
+
+ cl_git_pass(
+ git_buf_joinpath(&path, git_repository_path(_repo), "info/exclude"));
+ cl_assert(git_path_isfile(git_buf_cstr(&path)));
+
+ cl_git_pass(
+ git_buf_joinpath(&path, git_repository_path(_repo), "hooks"));
+ cl_assert(git_path_isdir(git_buf_cstr(&path)));
+ /* won't confirm specific contents of hooks dir since it may vary */
+
+ git_buf_free(&path);
+}