mirror of
https://git.proxmox.com/git/libgit2
synced 2025-05-05 17:12:51 +00:00

Add a new command `git_repository_open_ext` with extended options that control how searching for a repository will be done. The existing `git_repository_open` and `git_repository_discover` are reimplemented on top of it. We may want to change the default behavior of `git_repository_open` but this commit does not do that. Improve support for "gitdir" files where the work dir is separate from the repo and support for the "separate-git-dir" config. Also, add support for opening repos created with `git-new-workdir` script (although I have only confirmed that they can be opened, not that all functions work correctly). There are also a few minor changes that came up: - Fix `git_path_prettify` to allow in-place prettifying. - Fix `git_path_root` to support backslashes on Win32. This fix should help many repo open/discover scenarios - it is the one function called when opening before prettifying the path. - Tweak `git_config_get_string` to set the "out" pointer to NULL if the config value is not found. Allows some other cleanup. - Fix a couple places that should have been calling `git_repository_config__weakptr` and were not. - Fix `cl_git_sandbox_init` clar helper to support bare repos.
102 lines
2.5 KiB
C
102 lines
2.5 KiB
C
#include "clar_libgit2.h"
|
|
#include "posix.h"
|
|
|
|
void clar_on_init(void)
|
|
{
|
|
git_threads_init();
|
|
}
|
|
|
|
void clar_on_shutdown(void)
|
|
{
|
|
git_threads_shutdown();
|
|
}
|
|
|
|
void cl_git_mkfile(const char *filename, const char *content)
|
|
{
|
|
int fd;
|
|
|
|
fd = p_creat(filename, 0666);
|
|
cl_assert(fd != 0);
|
|
|
|
if (content) {
|
|
cl_must_pass(p_write(fd, content, strlen(content)));
|
|
} else {
|
|
cl_must_pass(p_write(fd, filename, strlen(filename)));
|
|
cl_must_pass(p_write(fd, "\n", 1));
|
|
}
|
|
|
|
cl_must_pass(p_close(fd));
|
|
}
|
|
|
|
void cl_git_write2file(const char *filename, const char *new_content, int flags)
|
|
{
|
|
int fd = p_open(filename, flags);
|
|
cl_assert(fd != 0);
|
|
if (!new_content)
|
|
new_content = "\n";
|
|
cl_must_pass(p_write(fd, new_content, strlen(new_content)));
|
|
cl_must_pass(p_close(fd));
|
|
cl_must_pass(p_chmod(filename, 0644));
|
|
}
|
|
|
|
void cl_git_append2file(const char *filename, const char *new_content)
|
|
{
|
|
cl_git_write2file(filename, new_content, O_WRONLY | O_APPEND | O_CREAT);
|
|
}
|
|
|
|
void cl_git_rewritefile(const char *filename, const char *new_content)
|
|
{
|
|
cl_git_write2file(filename, new_content, O_WRONLY | O_CREAT | O_TRUNC);
|
|
}
|
|
|
|
static const char *_cl_sandbox = NULL;
|
|
static git_repository *_cl_repo = NULL;
|
|
|
|
git_repository *cl_git_sandbox_init(const char *sandbox)
|
|
{
|
|
/* Copy the whole sandbox folder from our fixtures to our test sandbox
|
|
* area. After this it can be accessed with `./sandbox`
|
|
*/
|
|
cl_fixture_sandbox(sandbox);
|
|
_cl_sandbox = sandbox;
|
|
|
|
p_chdir(sandbox);
|
|
|
|
/* If this is not a bare repo, then rename `sandbox/.gitted` to
|
|
* `sandbox/.git` which must be done since we cannot store a folder
|
|
* named `.git` inside the fixtures folder of our libgit2 repo.
|
|
*/
|
|
if (p_access(".gitted", F_OK) == 0)
|
|
cl_git_pass(p_rename(".gitted", ".git"));
|
|
|
|
/* If we have `gitattributes`, rename to `.gitattributes`. This may
|
|
* be necessary if we don't want the attributes to be applied in the
|
|
* libgit2 repo, but just during testing.
|
|
*/
|
|
if (p_access("gitattributes", F_OK) == 0)
|
|
cl_git_pass(p_rename("gitattributes", ".gitattributes"));
|
|
|
|
/* As with `gitattributes`, we may need `gitignore` just for testing. */
|
|
if (p_access("gitignore", F_OK) == 0)
|
|
cl_git_pass(p_rename("gitignore", ".gitignore"));
|
|
|
|
p_chdir("..");
|
|
|
|
/* Now open the sandbox repository and make it available for tests */
|
|
cl_git_pass(git_repository_open(&_cl_repo, sandbox));
|
|
|
|
return _cl_repo;
|
|
}
|
|
|
|
void cl_git_sandbox_cleanup(void)
|
|
{
|
|
if (_cl_repo) {
|
|
git_repository_free(_cl_repo);
|
|
_cl_repo = NULL;
|
|
}
|
|
if (_cl_sandbox) {
|
|
cl_fixture_cleanup(_cl_sandbox);
|
|
_cl_sandbox = NULL;
|
|
}
|
|
}
|