Skip to content
This repository

Update git_repository_open #623

Merged
merged 1 commit into from about 2 years ago

5 participants

Russell Belfer Vicent Marti nulltoken Keith Dahlby Sean M. Collins
Russell Belfer
Owner

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.

These changes should help with some old issues:

  • For issue #479, this should allow you to open a repository using a Win32 path. This is not a global fix to allow non-posix paths, but a local one that will allow open and discover to work. (The win32 version of realpath() was already posix-ifying the path, so I just had to fix git_path_root() as mentioned above.)
  • For issue #529, separate-git-dir should now be fully supported because workdir guessing will respect the config entry.
  • For issue #620, I have written tests to confirm that a workdir built with git-new-workdir can definitely be opened, so reading will work. I'm guessing that writing is still broken which is more what that issue is about.
Russell Belfer arrbee Refactor git_repository_open with new options
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.
7784bcb
Vicent Marti
Owner
vmg commented

Looks nice. I suggest killing _discover -- anybody is against this?

nulltoken
Collaborator

I suggest killing _discover -- anybody is against this?

From the LibGit2Sharp standpoint, at least @davidebbo and @yysun were in favor of this feature (cf. libgit2/libgit2sharp#25).

I don't know about PoshGit (@dahlbyk?) or Git-Tfs (@sc68cal?) requirements regarding this. Maybe would they be interested in the possibility of retrieving the repository if the current directory points within the .git folder (cf. this test).

/cc @tclem @xpaulbettsx

Vicent Marti
Owner
vmg commented

Fair enough. We won't kill it.

Russell Belfer
Owner

One main open question I had was whether the "search" behavior of discover should become the default for open. The extended open allows you to turn search on or off (i.e. walk up the tree or not), but I preserved the old non-searching behavior of open.

Another minor question: I've been using _ext as an extension for "extended" versions of commands (e.g. git_repository_open_ext() and git_status_foreach_ext()) when there is a simpler pre-existing API with sane defaults. But I don't think any of the _ext versions have been exposed through any wrapper libraries yet. We could also name these with a 2 or an _ex -- e.g. git_repository_open2() or something like that. I'm not a huge fan of appending a 2, but I could be convinced. I'm neutral with regards to other extensions -- obviously _ext was the first thing that came to mind. Thoughts or opinions?

Vicent Marti
Owner
vmg commented

The 2 prefix is certainly more POSIX-like... Or rather, 0. git_repository_open0 has a familiar feel to it. I'm not entirely sure.

Keith Dahlby

Moving posh-git to libgit2sharp will require discover or equivalent. I think discovering a repo on open is a reasonable default, but I expect most wrappers will end up using the _ext version for flexibility and picking their own default behavior.

Russell Belfer
Owner

So, unless there are protests, I'm tempted to merge this as it is. If we want to change the default behavior for git_repository_open to scan a la git_repository_discover, then we can do that trivially at a future date. And it doesn't sound like anyone is passionate about the naming, so I'm tempted to just least it as is for now.

Vicent Marti vmg merged commit fcb2164 into from
Vicent Marti vmg closed this
Sean M. Collins

Sorry for the late reply - Git-Tfs currently passes a path - we do not use _discover

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Apr 11, 2012
Russell Belfer arrbee Refactor git_repository_open with new options
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.
7784bcb
This page is out of date. Refresh to see the latest.
14 include/git2/repository.h
@@ -70,6 +70,20 @@ GIT_EXTERN(int) git_repository_discover(
70 70 int across_fs,
71 71 const char *ceiling_dirs);
72 72
  73 +enum {
  74 + GIT_REPOSITORY_OPEN_NO_SEARCH = (1 << 0),
  75 + GIT_REPOSITORY_OPEN_CROSS_FS = (1 << 1),
  76 +};
  77 +
  78 +/**
  79 + * Find and open a repository with extended controls.
  80 + */
  81 +GIT_EXTERN(int) git_repository_open_ext(
  82 + git_repository **repo,
  83 + const char *start_path,
  84 + uint32_t flags,
  85 + const char *ceiling_dirs);
  86 +
73 87 /**
74 88 * Free a previously allocated repository
75 89 *
22 src/attr.c
@@ -347,6 +347,7 @@ static int collect_attr_files(
347 347
348 348 int git_attr_cache__init(git_repository *repo)
349 349 {
  350 + int ret;
350 351 git_attr_cache *cache = git_repository_attr_cache(repo);
351 352 git_config *cfg;
352 353
@@ -354,17 +355,18 @@ int git_attr_cache__init(git_repository *repo)
354 355 return 0;
355 356
356 357 /* cache config settings for attributes and ignores */
357   - if (git_repository_config(&cfg, repo) < 0)
  358 + if (git_repository_config__weakptr(&cfg, repo) < 0)
358 359 return -1;
359   - if (git_config_get_string(cfg, GIT_ATTR_CONFIG, &cache->cfg_attr_file)) {
360   - giterr_clear();
361   - cache->cfg_attr_file = NULL;
362   - }
363   - if (git_config_get_string(cfg, GIT_IGNORE_CONFIG, &cache->cfg_excl_file)) {
364   - giterr_clear();
365   - cache->cfg_excl_file = NULL;
366   - }
367   - git_config_free(cfg);
  360 +
  361 + ret = git_config_get_string(cfg, GIT_ATTR_CONFIG, &cache->cfg_attr_file);
  362 + if (ret < 0 && ret != GIT_ENOTFOUND)
  363 + return ret;
  364 +
  365 + ret = git_config_get_string(cfg, GIT_IGNORE_CONFIG, &cache->cfg_excl_file);
  366 + if (ret < 0 && ret != GIT_ENOTFOUND)
  367 + return ret;
  368 +
  369 + giterr_clear();
368 370
369 371 /* allocate hashtable for attribute and ignore file contents */
370 372 if (cache->files == NULL) {
31 src/config.c
@@ -327,11 +327,11 @@ int git_config_lookup_map_value(
327 327 int git_config_get_mapped(git_config *cfg, const char *name, git_cvar_map *maps, size_t map_n, int *out)
328 328 {
329 329 const char *value;
330   - int error;
  330 + int ret;
331 331
332   - error = git_config_get_string(cfg, name, &value);
333   - if (error < 0)
334   - return error;
  332 + ret = git_config_get_string(cfg, name, &value);
  333 + if (ret < 0)
  334 + return ret;
335 335
336 336 if (!git_config_lookup_map_value(maps, map_n, value, out))
337 337 return 0;
@@ -371,7 +371,7 @@ int git_config_get_int32(git_config *cfg, const char *name, int32_t *out)
371 371 giterr_set(GITERR_CONFIG, "Failed to parse '%s' as a 32-bit integer", value);
372 372 return -1;
373 373 }
374   -
  374 +
375 375 return 0;
376 376 }
377 377
@@ -399,26 +399,17 @@ int git_config_get_bool(git_config *cfg, const char *name, int *out)
399 399 int git_config_get_string(git_config *cfg, const char *name, const char **out)
400 400 {
401 401 file_internal *internal;
402   - git_config_file *file;
403   - int ret = GIT_ENOTFOUND;
404 402 unsigned int i;
405 403
406 404 assert(cfg->files.length);
407 405
408   - for (i = 0; i < cfg->files.length; ++i) {
409   - internal = git_vector_get(&cfg->files, i);
410   - file = internal->file;
411   -
412   - ret = file->get(file, name, out);
413   - if (ret == 0)
414   - return 0;
  406 + *out = NULL;
415 407
416   - /* File backend doesn't set error message on variable
417   - * not found */
418   - if (ret == GIT_ENOTFOUND)
419   - continue;
420   -
421   - return ret;
  408 + git_vector_foreach(&cfg->files, i, internal) {
  409 + git_config_file *file = internal->file;
  410 + int ret = file->get(file, name, out);
  411 + if (ret != GIT_ENOTFOUND)
  412 + return ret;
422 413 }
423 414
424 415 giterr_set(GITERR_CONFIG, "Config variable '%s' not found", name);
3  src/diff.c
@@ -253,7 +253,7 @@ static git_diff_list *git_diff_list_alloc(
253 253 diff->repo = repo;
254 254
255 255 /* load config values that affect diff behavior */
256   - if (git_repository_config(&cfg, repo) < 0)
  256 + if (git_repository_config__weakptr(&cfg, repo) < 0)
257 257 goto fail;
258 258 if (config_bool(cfg, "core.symlinks", 1))
259 259 diff->diffcaps = diff->diffcaps | GIT_DIFFCAPS_HAS_SYMLINKS;
@@ -264,7 +264,6 @@ static git_diff_list *git_diff_list_alloc(
264 264 if (config_bool(cfg, "core.trustctime", 1))
265 265 diff->diffcaps = diff->diffcaps | GIT_DIFFCAPS_TRUST_CTIME;
266 266 /* Don't set GIT_DIFFCAPS_USE_DEV - compile time option in core git */
267   - git_config_free(cfg);
268 267
269 268 if (opts == NULL)
270 269 return diff;
6 src/errors.c
@@ -92,8 +92,12 @@ const char *git_lasterror(void)
92 92 {
93 93 char *last_error = GIT_GLOBAL->error.last;
94 94
95   - if (!last_error[0])
  95 + if (!last_error[0]) {
  96 + const git_error *err = git_error_last();
  97 + if (err != NULL)
  98 + return err->message;
96 99 return NULL;
  100 + }
97 101
98 102 return last_error;
99 103 }
25 src/path.c
@@ -172,20 +172,22 @@ int git_path_root(const char *path)
172 172 if (isalpha(path[0]) && (path[1] == ':'))
173 173 offset += 2;
174 174
175   - /* Are we dealing with a network path? */
176   - else if (path[0] == '/' && path[1] == '/') {
  175 + /* Are we dealing with a windows network path? */
  176 + else if ((path[0] == '/' && path[1] == '/') ||
  177 + (path[0] == '\\' && path[1] == '\\'))
  178 + {
177 179 offset += 2;
178   -
  180 +
179 181 /* Skip the computer name segment */
180   - while (*(path + offset) && *(path + offset) != '/')
  182 + while (path[offset] && path[offset] != '/' && path[offset] != '\\')
181 183 offset++;
182 184 }
183 185 #endif
184 186
185   - if (*(path + offset) == '/')
  187 + if (path[offset] == '/' || path[offset] == '\\')
186 188 return offset;
187 189
188   - return -1; /* Not a real error. Rather a signal than the path is not rooted */
  190 + return -1; /* Not a real error - signals that path is not rooted */
189 191 }
190 192
191 193 int git_path_prettify(git_buf *path_out, const char *path, const char *base)
@@ -193,26 +195,21 @@ int git_path_prettify(git_buf *path_out, const char *path, const char *base)
193 195 char buf[GIT_PATH_MAX];
194 196
195 197 assert(path && path_out);
196   - git_buf_clear(path_out);
197 198
198 199 /* construct path if needed */
199 200 if (base != NULL && git_path_root(path) < 0) {
200 201 if (git_buf_joinpath(path_out, base, path) < 0)
201 202 return -1;
202   -
203 203 path = path_out->ptr;
204 204 }
205 205
206 206 if (p_realpath(path, buf) == NULL) {
207   - giterr_set(GITERR_OS, "Failed to resolve path '%s': %s",
208   - path, strerror(errno));
  207 + giterr_set(GITERR_OS, "Failed to resolve path '%s'", path);
  208 + git_buf_clear(path_out);
209 209 return (errno == ENOENT) ? GIT_ENOTFOUND : -1;
210 210 }
211 211
212   - if (git_buf_sets(path_out, buf) < 0)
213   - return -1;
214   -
215   - return 0;
  212 + return git_buf_sets(path_out, buf);
216 213 }
217 214
218 215 int git_path_prettify_dir(git_buf *path_out, const char *path, const char *base)
537 src/repository.c
@@ -5,6 +5,7 @@
5 5 * a Linking Exception. For full terms see the included COPYING file.
6 6 */
7 7 #include <stdarg.h>
  8 +#include <ctype.h>
8 9
9 10 #include "git2/object.h"
10 11
@@ -20,7 +21,7 @@
20 21 #define GIT_OBJECTS_INFO_DIR GIT_OBJECTS_DIR "info/"
21 22 #define GIT_OBJECTS_PACK_DIR GIT_OBJECTS_DIR "pack/"
22 23
23   -#define GIT_FILE_CONTENT_PREFIX "gitdir: "
  24 +#define GIT_FILE_CONTENT_PREFIX "gitdir:"
24 25
25 26 #define GIT_BRANCH_MASTER "master"
26 27
@@ -132,76 +133,294 @@ static int load_config_data(git_repository *repo)
132 133 return 0;
133 134 }
134 135
135   -static int load_workdir(git_repository *repo)
  136 +static int load_workdir(git_repository *repo, git_buf *parent_path)
136 137 {
137   - git_buf workdir_buf = GIT_BUF_INIT;
  138 + int error;
  139 + git_config *config;
  140 + const char *worktree;
  141 + git_buf worktree_buf = GIT_BUF_INIT;
138 142
139 143 if (repo->is_bare)
140 144 return 0;
141 145
142   - git_path_dirname_r(&workdir_buf, repo->path_repository);
143   - git_path_to_dir(&workdir_buf);
144   -
145   - if (git_buf_oom(&workdir_buf))
  146 + if (git_repository_config__weakptr(&config, repo) < 0)
146 147 return -1;
147 148
148   - repo->workdir = git_buf_detach(&workdir_buf);
149   - git_buf_free(&workdir_buf);
  149 + error = git_config_get_string(config, "core.worktree", &worktree);
  150 + if (!error && worktree != NULL)
  151 + repo->workdir = git__strdup(worktree);
  152 + else if (error != GIT_ENOTFOUND)
  153 + return error;
  154 + else {
  155 + giterr_clear();
  156 +
  157 + if (parent_path && git_path_isdir(parent_path->ptr))
  158 + repo->workdir = git_buf_detach(parent_path);
  159 + else {
  160 + git_path_dirname_r(&worktree_buf, repo->path_repository);
  161 + git_path_to_dir(&worktree_buf);
  162 + repo->workdir = git_buf_detach(&worktree_buf);
  163 + }
  164 + }
  165 +
  166 + GITERR_CHECK_ALLOC(repo->workdir);
150 167
151 168 return 0;
152 169 }
153 170
154   -int git_repository_open(git_repository **repo_out, const char *path)
  171 +/*
  172 + * This function returns furthest offset into path where a ceiling dir
  173 + * is found, so we can stop processing the path at that point.
  174 + *
  175 + * Note: converting this to use git_bufs instead of GIT_PATH_MAX buffers on
  176 + * the stack could remove directories name limits, but at the cost of doing
  177 + * repeated malloc/frees inside the loop below, so let's not do it now.
  178 + */
  179 +static int find_ceiling_dir_offset(
  180 + const char *path,
  181 + const char *ceiling_directories)
155 182 {
156   - git_buf path_buf = GIT_BUF_INIT;
157   - git_repository *repo = NULL;
158   - int res;
  183 + char buf[GIT_PATH_MAX + 1];
  184 + char buf2[GIT_PATH_MAX + 1];
  185 + const char *ceil, *sep;
  186 + int len, max_len = -1;
  187 + int min_len;
159 188
160   - res = git_path_prettify_dir(&path_buf, path, NULL);
161   - if (res < 0)
162   - return res;
  189 + assert(path);
  190 +
  191 + min_len = git_path_root(path) + 1;
163 192
164   - /**
165   - * Check if the path we've been given is actually the path
166   - * of the working dir, by testing if it contains a `.git`
167   - * folder inside of it.
168   - */
169   - if (git_path_contains_dir(&path_buf, GIT_DIR) == true)
170   - git_buf_joinpath(&path_buf, path_buf.ptr, GIT_DIR);
  193 + if (ceiling_directories == NULL || min_len == 0)
  194 + return min_len;
  195 +
  196 + for (sep = ceil = ceiling_directories; *sep; ceil = sep + 1) {
  197 + for (sep = ceil; *sep && *sep != GIT_PATH_LIST_SEPARATOR; sep++);
  198 + len = sep - ceil;
  199 +
  200 + if (len == 0 || len >= (int)sizeof(buf) || git_path_root(ceil) == -1)
  201 + continue;
  202 +
  203 + strncpy(buf, ceil, len);
  204 + buf[len] = '\0';
  205 +
  206 + if (p_realpath(buf, buf2) == NULL)
  207 + continue;
  208 +
  209 + len = strlen(buf2);
  210 + if (len > 0 && buf2[len-1] == '/')
  211 + buf[--len] = '\0';
  212 +
  213 + if (!strncmp(path, buf2, len) &&
  214 + path[len] == '/' &&
  215 + len > max_len)
  216 + {
  217 + max_len = len;
  218 + }
  219 + }
  220 +
  221 + return max_len <= min_len ? min_len : max_len;
  222 +}
  223 +
  224 +/*
  225 + * Read the contents of `file_path` and set `path_out` to the repo dir that
  226 + * it points to. Before calling, set `path_out` to the base directory that
  227 + * should be used if the contents of `file_path` are a relative path.
  228 + */
  229 +static int read_gitfile(git_buf *path_out, const char *file_path)
  230 +{
  231 + int error = 0;
  232 + git_buf file = GIT_BUF_INIT;
  233 + size_t prefix_len = strlen(GIT_FILE_CONTENT_PREFIX);
  234 +
  235 + assert(path_out && file_path);
  236 +
  237 + if (git_futils_readbuffer(&file, file_path) < 0)
  238 + return -1;
  239 +
  240 + git_buf_rtrim(&file);
  241 +
  242 + if (file.size <= prefix_len ||
  243 + memcmp(file.ptr, GIT_FILE_CONTENT_PREFIX, prefix_len) != 0)
  244 + {
  245 + giterr_set(GITERR_REPOSITORY, "The `.git` file at '%s' is malformed", file_path);
  246 + error = -1;
  247 + }
  248 + else if ((error = git_path_dirname_r(path_out, file_path)) >= 0) {
  249 + const char *gitlink = ((const char *)file.ptr) + prefix_len;
  250 + while (*gitlink && isspace(*gitlink)) gitlink++;
  251 + error = git_path_prettify_dir(path_out, gitlink, path_out->ptr);
  252 + }
171 253
172   - if (valid_repository_path(&path_buf) == false) {
  254 + git_buf_free(&file);
  255 + return error;
  256 +}
  257 +
  258 +static int find_repo(
  259 + git_buf *repo_path,
  260 + git_buf *parent_path,
  261 + const char *start_path,
  262 + uint32_t flags,
  263 + const char *ceiling_dirs)
  264 +{
  265 + int error;
  266 + git_buf path = GIT_BUF_INIT;
  267 + struct stat st;
  268 + dev_t initial_device = 0;
  269 + bool try_with_dot_git = false;
  270 + int ceiling_offset;
  271 +
  272 + git_buf_free(repo_path);
  273 +
  274 + if ((error = git_path_prettify_dir(&path, start_path, NULL)) < 0)
  275 + return error;
  276 +
  277 + ceiling_offset = find_ceiling_dir_offset(path.ptr, ceiling_dirs);
  278 +
  279 + if ((error = git_buf_joinpath(&path, path.ptr, DOT_GIT)) < 0)
  280 + return error;
  281 +
  282 + while (!error && !repo_path->size) {
  283 + if (p_stat(path.ptr, &st) == 0) {
  284 + /* check that we have not crossed device boundaries */
  285 + if (initial_device == 0)
  286 + initial_device = st.st_dev;
  287 + else if (st.st_dev != initial_device &&
  288 + (flags & GIT_REPOSITORY_OPEN_CROSS_FS) == 0)
  289 + break;
  290 +
  291 + if (S_ISDIR(st.st_mode)) {
  292 + if (valid_repository_path(&path)) {
  293 + git_path_to_dir(&path);
  294 + git_buf_set(repo_path, path.ptr, path.size);
  295 + break;
  296 + }
  297 + }
  298 + else if (S_ISREG(st.st_mode)) {
  299 + git_buf repo_link = GIT_BUF_INIT;
  300 +
  301 + if (!(error = read_gitfile(&repo_link, path.ptr))) {
  302 + if (valid_repository_path(&repo_link))
  303 + git_buf_swap(repo_path, &repo_link);
  304 + break;
  305 + }
  306 + git_buf_free(&repo_link);
  307 + }
  308 + }
  309 +
  310 + /* move up one directory level */
  311 + if (git_path_dirname_r(&path, path.ptr) < 0) {
  312 + error = -1;
  313 + break;
  314 + }
  315 +
  316 + if (try_with_dot_git) {
  317 + /* if we tried original dir with and without .git AND either hit
  318 + * directory ceiling or NO_SEARCH was requested, then be done.
  319 + */
  320 + if (path.ptr[ceiling_offset] == '\0' ||
  321 + (flags & GIT_REPOSITORY_OPEN_NO_SEARCH) != 0)
  322 + break;
  323 + /* otherwise look first for .git item */
  324 + error = git_buf_joinpath(&path, path.ptr, DOT_GIT);
  325 + }
  326 + try_with_dot_git = !try_with_dot_git;
  327 + }
  328 +
  329 + if (!error && parent_path != NULL) {
  330 + if (!repo_path->size)
  331 + git_buf_clear(parent_path);
  332 + else {
  333 + git_path_dirname_r(parent_path, path.ptr);
  334 + git_path_to_dir(parent_path);
  335 + }
  336 + if (git_buf_oom(parent_path))
  337 + return -1;
  338 + }
  339 +
  340 + git_buf_free(&path);
  341 +
  342 + if (!repo_path->size && !error) {
173 343 giterr_set(GITERR_REPOSITORY,
174   - "The given path (%s) is not a valid Git repository", git_buf_cstr(&path_buf));
175   - git_buf_free(&path_buf);
176   - return GIT_ENOTFOUND;
  344 + "Could not find repository from '%s'", start_path);
  345 + error = GIT_ENOTFOUND;
177 346 }
178 347
  348 + return error;
  349 +}
  350 +
  351 +int git_repository_open_ext(
  352 + git_repository **repo_ptr,
  353 + const char *start_path,
  354 + uint32_t flags,
  355 + const char *ceiling_dirs)
  356 +{
  357 + int error;
  358 + git_buf path = GIT_BUF_INIT, parent = GIT_BUF_INIT;
  359 + git_repository *repo;
  360 +
  361 + *repo_ptr = NULL;
  362 +
  363 + if ((error = find_repo(&path, &parent, start_path, flags, ceiling_dirs)) < 0)
  364 + return error;
  365 +
179 366 repo = repository_alloc();
180 367 GITERR_CHECK_ALLOC(repo);
181 368
182   - repo->path_repository = git_buf_detach(&path_buf);
  369 + repo->path_repository = git_buf_detach(&path);
183 370 GITERR_CHECK_ALLOC(repo->path_repository);
184 371
185   - if (load_config_data(repo) < 0)
186   - goto cleanup;
187   -
188   - if (load_workdir(repo) < 0)
189   - goto cleanup;
  372 + if ((error = load_config_data(repo)) < 0 ||
  373 + (error = load_workdir(repo, &parent)) < 0)
  374 + {
  375 + git_repository_free(repo);
  376 + return error;
  377 + }
190 378
191   - *repo_out = repo;
  379 + *repo_ptr = repo;
192 380 return 0;
  381 +}
193 382
194   - cleanup:
195   - git_repository_free(repo);
196   - git_buf_free(&path_buf);
197   - return -1;
  383 +int git_repository_open(git_repository **repo_out, const char *path)
  384 +{
  385 + return git_repository_open_ext(
  386 + repo_out, path, GIT_REPOSITORY_OPEN_NO_SEARCH, NULL);
  387 +}
  388 +
  389 +int git_repository_discover(
  390 + char *repository_path,
  391 + size_t size,
  392 + const char *start_path,
  393 + int across_fs,
  394 + const char *ceiling_dirs)
  395 +{
  396 + git_buf path = GIT_BUF_INIT;
  397 + uint32_t flags = across_fs ? GIT_REPOSITORY_OPEN_CROSS_FS : 0;
  398 +
  399 + assert(start_path && repository_path && size > 0);
  400 +
  401 + *repository_path = '\0';
  402 +
  403 + if (find_repo(&path, NULL, start_path, flags, ceiling_dirs) < 0)
  404 + return -1;
  405 +
  406 + if (size < (size_t)(path.size + 1)) {
  407 + giterr_set(GITERR_REPOSITORY,
  408 + "The given buffer is too long to store the discovered path");
  409 + git_buf_free(&path);
  410 + return -1;
  411 + }
  412 +
  413 + /* success: we discovered a repository */
  414 + git_buf_copy_cstr(repository_path, size, &path);
  415 + git_buf_free(&path);
  416 + return 0;
198 417 }
199 418
200 419 static int load_config(
201   - git_config **out,
202   - git_repository *repo,
203   - const char *global_config_path,
204   - const char *system_config_path)
  420 + git_config **out,
  421 + git_repository *repo,
  422 + const char *global_config_path,
  423 + const char *system_config_path)
205 424 {
206 425 git_buf config_path = GIT_BUF_INIT;
207 426 git_config *cfg = NULL;
@@ -375,240 +594,6 @@ void git_repository_set_index(git_repository *repo, git_index *index)
375 594 GIT_REFCOUNT_OWN(repo->_index, repo);
376 595 }
377 596
378   -
379   -static int retrieve_device(dev_t *device_out, const char *path)
380   -{
381   - int error;
382   - struct stat path_info;
383   -
384   - assert(device_out);
385   -
386   - if ((error = git_path_lstat(path, &path_info)) == 0)
387   - *device_out = path_info.st_dev;
388   -
389   - return error;
390   -}
391   -
392   -/*
393   - * This function returns furthest offset into path where a ceiling dir
394   - * is found, so we can stop processing the path at that point.
395   - *
396   - * Note: converting this to use git_bufs instead of GIT_PATH_MAX buffers on
397   - * the stack could remove directories name limits, but at the cost of doing
398   - * repeated malloc/frees inside the loop below, so let's not do it now.
399   - */
400   -static int retrieve_ceiling_directories_offset(
401   - const char *path,
402   - const char *ceiling_directories)
403   -{
404   - char buf[GIT_PATH_MAX + 1];
405   - char buf2[GIT_PATH_MAX + 1];
406   - const char *ceil, *sep;
407   - int len, max_len = -1;
408   - int min_len;
409   -
410   - assert(path);
411   -
412   - min_len = git_path_root(path) + 1;
413   -
414   - if (ceiling_directories == NULL || min_len == 0)
415   - return min_len;
416   -
417   - for (sep = ceil = ceiling_directories; *sep; ceil = sep + 1) {
418   - for (sep = ceil; *sep && *sep != GIT_PATH_LIST_SEPARATOR; sep++);
419   - len = sep - ceil;
420   -
421   - if (len == 0 || len >= (int)sizeof(buf) || git_path_root(ceil) == -1)
422   - continue;
423   -
424   - strncpy(buf, ceil, len);
425   - buf[len] = '\0';
426   -
427   - if (p_realpath(buf, buf2) == NULL)
428   - continue;
429   -
430   - len = strlen(buf2);
431   - if (len > 0 && buf2[len-1] == '/')
432   - buf[--len] = '\0';
433   -
434   - if (!strncmp(path, buf2, len) &&
435   - path[len] == '/' &&
436   - len > max_len)
437   - {
438   - max_len = len;
439   - }
440   - }
441   -
442   - return max_len <= min_len ? min_len : max_len;
443   -}
444   -
445   -/*
446   - * Read the contents of `file_path` and set `path_out` to the repo dir that
447   - * it points to. Before calling, set `path_out` to the base directory that
448   - * should be used if the contents of `file_path` are a relative path.
449   - */
450   -static int read_gitfile(git_buf *path_out, const char *file_path, const char *base_path)
451   -{
452   - git_buf file = GIT_BUF_INIT;
453   -
454   - assert(path_out && file_path);
455   -
456   - if (git_futils_readbuffer(&file, file_path) < 0)
457   - return -1;
458   -
459   - git_buf_rtrim(&file);
460   -
461   - if (git__prefixcmp((char *)file.ptr, GIT_FILE_CONTENT_PREFIX) != 0 ||
462   - strlen(GIT_FILE_CONTENT_PREFIX) == file.size) {
463   - git_buf_free(&file);
464   - giterr_set(GITERR_REPOSITORY, "The `.git` file at '%s' is corrupted", file_path);
465   - return -1;
466   - }
467   -
468   - if (git_path_prettify_dir(path_out,
469   - ((char *)file.ptr) + strlen(GIT_FILE_CONTENT_PREFIX), base_path) < 0) {
470   - git_buf_free(&file);
471   - giterr_set(GITERR_REPOSITORY,
472   - "The `.git` file at '%s' points to an invalid path.", file_path);
473   - return -1;
474   - }
475   -
476   - git_buf_free(&file);
477   - return 0;
478   -}
479   -
480   -int git_repository_discover(
481   - char *repository_path,
482   - size_t size,
483   - const char *start_path,
484   - int across_fs,
485   - const char *ceiling_dirs)
486   -{
487   - int res, ceiling_offset;
488   - git_buf bare_path = GIT_BUF_INIT;
489   - git_buf normal_path = GIT_BUF_INIT;
490   - git_buf *found_path = NULL;
491   - dev_t current_device = 0;
492   -
493   - assert(start_path && repository_path);
494   -
495   - *repository_path = '\0';
496   -
497   - res = git_path_prettify_dir(&bare_path, start_path, NULL);
498   - if (res < 0)
499   - return res;
500   -
501   - if (!across_fs) {
502   - if (retrieve_device(&current_device, bare_path.ptr) < 0)
503   - goto on_error;
504   - }
505   -
506   - ceiling_offset = retrieve_ceiling_directories_offset(bare_path.ptr, ceiling_dirs);
507   -
508   - while(1) {
509   - if (git_buf_joinpath(&normal_path, bare_path.ptr, DOT_GIT) < 0)
510   - goto on_error;
511   -
512   - /**
513   - * If the `.git` file is regular instead of
514   - * a directory, it should contain the path of the actual git repository
515   - */
516   - if (git_path_isfile(normal_path.ptr) == true) {
517   - git_buf gitfile_path = GIT_BUF_INIT;
518   -
519   - if (read_gitfile(&gitfile_path, normal_path.ptr, bare_path.ptr) < 0) {
520   - git_buf_free(&gitfile_path);
521   - goto on_error;
522   - }
523   -
524   - if (valid_repository_path(&gitfile_path) == false) {
525   - git_buf_free(&gitfile_path);
526   - giterr_set(GITERR_REPOSITORY,
527   - "The `.git` file found at '%s' points to an invalid git folder", normal_path.ptr);
528   - goto on_error;
529   - }
530   -
531   - git_buf_swap(&normal_path, &gitfile_path);
532   - found_path = &normal_path;
533   -
534   - git_buf_free(&gitfile_path);
535   - break;
536   - }
537   -
538   - /**
539   - * If the `.git` file is a folder, we check inside of it
540   - */
541   - if (git_path_isdir(normal_path.ptr) == true) {
542   - if (valid_repository_path(&normal_path) == true) {
543   - found_path = &normal_path;
544   - break;
545   - }
546   - }
547   -
548   - /**
549   - * Otherwise, the repository may be bare, let's check
550   - * the root anyway
551   - */
552   - if (valid_repository_path(&bare_path) == true) {
553   - found_path = &bare_path;
554   - break;
555   - }
556   -
557   - /**
558   - * If we didn't find it, walk up the tree
559   - */
560   - if (git_path_dirname_r(&normal_path, bare_path.ptr) < 0)
561   - goto on_error;
562   -
563   - git_buf_swap(&bare_path, &normal_path);
564   -
565   - if (!across_fs) {
566   - dev_t new_device;
567   - if (retrieve_device(&new_device, bare_path.ptr) < 0)
568   - goto on_error;
569   -
570   - if (current_device != new_device)
571   - goto on_not_found;
572   -
573   - current_device = new_device;
574   - }
575   -
576   - /* nothing has been found, lets try the parent directory
577   - * but stop if we hit one of the ceiling directories
578   - */
579   - if (bare_path.ptr[ceiling_offset] == '\0')
580   - goto on_not_found;
581   - }
582   -
583   - assert(found_path);
584   -
585   - if (git_path_to_dir(found_path) < 0)
586   - goto on_error;
587   -
588   - if (size < (size_t)(found_path->size + 1)) {
589   - giterr_set(GITERR_REPOSITORY,
590   - "The given buffer is too long to store the discovered path");
591   - goto on_error;
592   - }
593   -
594   - /* success: we discovered a repository */
595   - git_buf_copy_cstr(repository_path, size, found_path);
596   -
597   - git_buf_free(&bare_path);
598   - git_buf_free(&normal_path);
599   - return 0;
600   -
601   -on_error: /* unrecoverable error */
602   - git_buf_free(&bare_path);
603   - git_buf_free(&normal_path);
604   - return -1;
605   -
606   -on_not_found: /* failed to discover the repository */
607   - git_buf_free(&bare_path);
608   - git_buf_free(&normal_path);
609   - return GIT_ENOTFOUND;
610   -}
611   -
612 597 static int check_repositoryformatversion(git_repository *repo)
613 598 {
614 599 git_config *config;
23 tests-clar/clar_helpers.c
@@ -28,9 +28,9 @@ void cl_git_mkfile(const char *filename, const char *content)
28 28 cl_must_pass(p_close(fd));
29 29 }
30 30
31   -void cl_git_append2file(const char *filename, const char *new_content)
  31 +void cl_git_write2file(const char *filename, const char *new_content, int flags)
32 32 {
33   - int fd = p_open(filename, O_WRONLY | O_APPEND | O_CREAT);
  33 + int fd = p_open(filename, flags);
34 34 cl_assert(fd != 0);
35 35 if (!new_content)
36 36 new_content = "\n";
@@ -39,6 +39,16 @@ void cl_git_append2file(const char *filename, const char *new_content)
39 39 cl_must_pass(p_chmod(filename, 0644));
40 40 }
41 41
  42 +void cl_git_append2file(const char *filename, const char *new_content)
  43 +{
  44 + cl_git_write2file(filename, new_content, O_WRONLY | O_APPEND | O_CREAT);
  45 +}
  46 +
  47 +void cl_git_rewritefile(const char *filename, const char *new_content)
  48 +{
  49 + cl_git_write2file(filename, new_content, O_WRONLY | O_CREAT | O_TRUNC);
  50 +}
  51 +
42 52 static const char *_cl_sandbox = NULL;
43 53 static git_repository *_cl_repo = NULL;
44 54
@@ -52,11 +62,12 @@ git_repository *cl_git_sandbox_init(const char *sandbox)
52 62
53 63 p_chdir(sandbox);
54 64
55   - /* Rename `sandbox/.gitted` to `sandbox/.git` which must be done since
56   - * we cannot store a folder named `.git` inside the fixtures folder of
57   - * our libgit2 repo.
  65 + /* If this is not a bare repo, then rename `sandbox/.gitted` to
  66 + * `sandbox/.git` which must be done since we cannot store a folder
  67 + * named `.git` inside the fixtures folder of our libgit2 repo.
58 68 */
59   - cl_git_pass(p_rename(".gitted", ".git"));
  69 + if (p_access(".gitted", F_OK) == 0)
  70 + cl_git_pass(p_rename(".gitted", ".git"));
60 71
61 72 /* If we have `gitattributes`, rename to `.gitattributes`. This may
62 73 * be necessary if we don't want the attributes to be applied in the
2  tests-clar/clar_libgit2.h
@@ -57,6 +57,8 @@ GIT_INLINE(void) cl_assert_strequal_internal(
57 57 /* Write the contents of a buffer to disk */
58 58 void cl_git_mkfile(const char *filename, const char *content);
59 59 void cl_git_append2file(const char *filename, const char *new_content);
  60 +void cl_git_rewritefile(const char *filename, const char *new_content);
  61 +void cl_git_write2file(const char *filename, const char *new_content, int mode);
60 62
61 63 /* Git sandbox setup helpers */
62 64
250 tests-clar/repo/open.c
... ... @@ -1,25 +1,27 @@
1 1 #include "clar_libgit2.h"
2   -#include "posix.h"
3   -
4   -static git_repository *repo;
  2 +#include "fileops.h"
5 3
6 4 void test_repo_open__cleanup(void)
7 5 {
8   - git_repository_free(repo);
  6 + cl_git_sandbox_cleanup();
  7 +
  8 + if (git_path_isdir("alternate"))
  9 + git_futils_rmdir_r("alternate", 1);
9 10 }
10 11
11 12 void test_repo_open__bare_empty_repo(void)
12 13 {
13   - cl_git_pass(git_repository_open(&repo, cl_fixture("empty_bare.git")));
  14 + git_repository *repo = cl_git_sandbox_init("empty_bare.git");
14 15
15 16 cl_assert(git_repository_path(repo) != NULL);
16 17 cl_assert(git__suffixcmp(git_repository_path(repo), "/") == 0);
17   -
18 18 cl_assert(git_repository_workdir(repo) == NULL);
19 19 }
20 20
21 21 void test_repo_open__standard_empty_repo_through_gitdir(void)
22 22 {
  23 + git_repository *repo;
  24 +
23 25 cl_git_pass(git_repository_open(&repo, cl_fixture("empty_standard_repo/.gitted")));
24 26
25 27 cl_assert(git_repository_path(repo) != NULL);
@@ -27,20 +29,246 @@ void test_repo_open__standard_empty_repo_through_gitdir(void)
27 29
28 30 cl_assert(git_repository_workdir(repo) != NULL);
29 31 cl_assert(git__suffixcmp(git_repository_workdir(repo), "/") == 0);
  32 +
  33 + git_repository_free(repo);
30 34 }
31 35
32 36 void test_repo_open__standard_empty_repo_through_workdir(void)
33 37 {
34   - cl_fixture_sandbox("empty_standard_repo");
35   - cl_git_pass(p_rename("empty_standard_repo/.gitted", "empty_standard_repo/.git"));
36   -
37   - cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
  38 + git_repository *repo = cl_git_sandbox_init("empty_standard_repo");
38 39
39 40 cl_assert(git_repository_path(repo) != NULL);
40 41 cl_assert(git__suffixcmp(git_repository_path(repo), "/") == 0);
41 42
42 43 cl_assert(git_repository_workdir(repo) != NULL);
43 44 cl_assert(git__suffixcmp(git_repository_workdir(repo), "/") == 0);
  45 +}
  46 +
  47 +
  48 +void test_repo_open__open_with_discover(void)
  49 +{
  50 + static const char *variants[] = {
  51 + "attr", "attr/", "attr/.git", "attr/.git/",
  52 + "attr/sub", "attr/sub/", "attr/sub/sub", "attr/sub/sub/",
  53 + NULL
  54 + };
  55 + git_repository *repo;
  56 + const char **scan;
  57 +
  58 + cl_fixture_sandbox("attr");
  59 + cl_git_pass(p_rename("attr/.gitted", "attr/.git"));
  60 +
  61 + for (scan = variants; *scan != NULL; scan++) {
  62 + cl_git_pass(git_repository_open_ext(&repo, *scan, 0, NULL));
  63 + cl_assert(git__suffixcmp(git_repository_path(repo), "attr/.git/") == 0);
  64 + cl_assert(git__suffixcmp(git_repository_workdir(repo), "attr/") == 0);
  65 + git_repository_free(repo);
  66 + }
  67 +
  68 + cl_fixture_cleanup("attr");
  69 +}
  70 +
  71 +void test_repo_open__gitlinked(void)
  72 +{
  73 + /* need to have both repo dir and workdir set up correctly */
  74 + git_repository *repo = cl_git_sandbox_init("empty_standard_repo");
  75 + git_repository *repo2;
  76 +
  77 + cl_must_pass(p_mkdir("alternate", 0777));
  78 + cl_git_mkfile("alternate/.git", "gitdir: ../empty_standard_repo/.git");
  79 +
  80 + cl_git_pass(git_repository_open(&repo2, "alternate"));
  81 +
  82 + cl_assert(git_repository_path(repo2) != NULL);
  83 + cl_assert_(git__suffixcmp(git_repository_path(repo2), "empty_standard_repo/.git/") == 0, git_repository_path(repo2));
  84 + cl_assert_equal_s(git_repository_path(repo), git_repository_path(repo2));
  85 +
  86 + cl_assert(git_repository_workdir(repo2) != NULL);
  87 + cl_assert_(git__suffixcmp(git_repository_workdir(repo2), "alternate/") == 0, git_repository_workdir(repo2));
  88 +
  89 + git_repository_free(repo2);
  90 +}
  91 +
  92 +void test_repo_open__from_git_new_workdir(void)
  93 +{
  94 + /* The git-new-workdir script that ships with git sets up a bunch of
  95 + * symlinks to create a second workdir that shares the object db with
  96 + * another checkout. Libgit2 can open a repo that has been configured
  97 + * this way.
  98 + */
  99 + cl_git_sandbox_init("empty_standard_repo");
  100 +
  101 +#ifndef GIT_WIN32
  102 + git_repository *repo2;
  103 + git_buf link_tgt = GIT_BUF_INIT, link = GIT_BUF_INIT, body = GIT_BUF_INIT;
  104 + const char **scan;
  105 + int link_fd;
  106 + static const char *links[] = {
  107 + "config", "refs", "logs/refs", "objects", "info", "hooks",
  108 + "packed-refs", "remotes", "rr-cache", "svn", NULL
  109 + };
  110 + static const char *copies[] = {
  111 + "HEAD", NULL
  112 + };
  113 +
  114 + cl_git_pass(p_mkdir("alternate", 0777));
  115 + cl_git_pass(p_mkdir("alternate/.git", 0777));
  116 +
  117 + for (scan = links; *scan != NULL; scan++) {
  118 + git_buf_joinpath(&link_tgt, "empty_standard_repo/.git", *scan);
  119 + if (git_path_exists(link_tgt.ptr)) {
  120 + git_buf_joinpath(&link_tgt, "../../empty_standard_repo/.git", *scan);
  121 + git_buf_joinpath(&link, "alternate/.git", *scan);
  122 + if (strchr(*scan, '/'))
  123 + git_futils_mkpath2file(link.ptr, 0777);
  124 + cl_assert_(symlink(link_tgt.ptr, link.ptr) == 0, strerror(errno));
  125 + }
  126 + }
  127 + for (scan = copies; *scan != NULL; scan++) {
  128 + git_buf_joinpath(&link_tgt, "empty_standard_repo/.git", *scan);
  129 + if (git_path_exists(link_tgt.ptr)) {
  130 + git_buf_joinpath(&link, "alternate/.git", *scan);
  131 + cl_git_pass(git_futils_readbuffer(&body, link_tgt.ptr));
  132 +
  133 + cl_assert((link_fd = git_futils_creat_withpath(link.ptr, 0777, 0666)) >= 0);
  134 + cl_must_pass(p_write(link_fd, body.ptr, body.size));
  135 + p_close(link_fd);
  136 + }
  137 + }
  138 +
  139 + git_buf_free(&link_tgt);
  140 + git_buf_free(&link);
  141 + git_buf_free(&body);
  142 +
  143 +
  144 + cl_git_pass(git_repository_open(&repo2, "alternate"));
  145 +
  146 + cl_assert(git_repository_path(repo2) != NULL);
  147 + cl_assert_(git__suffixcmp(git_repository_path(repo2), "alternate/.git/") == 0, git_repository_path(repo2));
  148 +
  149 + cl_assert(git_repository_workdir(repo2) != NULL);
  150 + cl_assert_(git__suffixcmp(git_repository_workdir(repo2), "alternate/") == 0, git_repository_workdir(repo2));
  151 +
  152 + git_repository_free(repo2);
  153 +#endif
  154 +}
  155 +
  156 +void test_repo_open__failures(void)
  157 +{
  158 + git_repository *base, *repo;
  159 + git_buf ceiling = GIT_BUF_INIT;
  160 +
  161 + base = cl_git_sandbox_init("attr");
  162 + cl_git_pass(git_buf_sets(&ceiling, git_repository_workdir(base)));
  163 +
  164 + /* fail with no searching */
  165 + cl_git_fail(git_repository_open(&repo, "attr/sub"));
  166 + cl_git_fail(git_repository_open_ext(
  167 + &repo, "attr/sub", GIT_REPOSITORY_OPEN_NO_SEARCH, NULL));
  168 +
  169 + /* fail with ceiling too low */
  170 + cl_git_pass(git_buf_joinpath(&ceiling, ceiling.ptr, "sub"));
  171 + cl_git_fail(git_repository_open_ext(&repo, "attr/sub", 0, ceiling.ptr));
  172 +
  173 + /* fail with no repo */
  174 + cl_git_pass(p_mkdir("alternate", 0777));
  175 + cl_git_pass(p_mkdir("alternate/.git", 0777));
  176 + cl_git_fail(git_repository_open_ext(&repo, "alternate", 0, NULL));
  177 + cl_git_fail(git_repository_open_ext(&repo, "alternate/.git", 0, NULL));
  178 +
  179 + git_buf_free(&ceiling);
  180 +}
  181 +
  182 +void test_repo_open__bad_gitlinks(void)
  183 +{
  184 + git_repository *repo;
  185 + static const char *bad_links[] = {
  186 + "garbage\n", "gitdir", "gitdir:\n", "gitdir: foobar",
  187 + "gitdir: ../invalid", "gitdir: ../invalid2",
  188 + "gitdir: ../attr/.git with extra stuff",
  189 + NULL
  190 + };
  191 + const char **scan;
  192 +
  193 + cl_git_sandbox_init("attr");
  194 +
  195 + cl_git_pass(p_mkdir("alternate", 0777));
  196 + cl_git_pass(p_mkdir("invalid", 0777));
  197 + cl_git_pass(git_futils_mkdir_r("invalid2/.git", NULL, 0777));
  198 +
  199 + for (scan = bad_links; *scan != NULL; scan++) {
  200 + cl_git_rewritefile("alternate/.git", *scan);
  201 + cl_git_fail(git_repository_open_ext(&repo, "alternate", 0, NULL));
  202 + }
  203 +
  204 + git_futils_rmdir_r("invalid", 1);
  205 + git_futils_rmdir_r("invalid2", 1);
  206 +}
  207 +
  208 +static void unposix_path(git_buf *path)
  209 +{
  210 + char *src, *tgt;
  211 +
  212 + src = tgt = path->ptr;
  213 +
  214 + /* convert "/d/..." to "d:\..." */
  215 + if (src[0] == '/' && isalpha(src[1]) && src[2] == '/') {
  216 + *tgt++ = src[1];
  217 + *tgt++ = ':';
  218 + *tgt++ = '\\';
  219 + src += 3;
  220 + }
  221 +
  222 + while (*src) {
  223 + *tgt++ = (*src == '/') ? '\\' : *src;
  224 + src++;
  225 + }
  226 +
  227 + *tgt = '\0';
  228 +}
  229 +
  230 +void test_repo_open__win32_path(void)
  231 +{
  232 +#ifdef GIT_WIN32
  233 + git_repository *repo = cl_git_sandbox_init("empty_standard_repo"), *repo2;
  234 + git_buf winpath = GIT_BUF_INIT;
  235 + char *src, *tgt;
  236 + static const char *repo_path = "empty_standard_repo/.git/";
  237 + static const char *repo_wd = "empty_standard_repo/";
  238 +
  239 + cl_assert(git__suffixcmp(git_repository_path(repo), repo_path) == 0);
  240 + cl_assert(git__suffixcmp(git_repository_workdir(repo), repo_wd) == 0);
  241 +
  242 + cl_git_pass(git_buf_sets(&winpath, git_repository_path(repo)));
  243 + unposix_path(&winpath);
  244 + cl_git_pass(git_repository_open(&repo2, winpath.ptr));
  245 + cl_assert(git__suffixcmp(git_repository_path(repo2), repo_path) == 0);
  246 + cl_assert(git__suffixcmp(git_repository_workdir(repo2), repo_wd) == 0);
  247 + git_repository_free(repo2);
  248 +
  249 + cl_git_pass(git_buf_sets(&winpath, git_repository_path(repo)));
  250 + git_buf_truncate(&winpath, winpath.size - 1); /* remove trailing '/' */
  251 + unposix_path(&winpath);
  252 + cl_git_pass(git_repository_open(&repo2, winpath.ptr));
  253 + cl_assert(git__suffixcmp(git_repository_path(repo2), repo_path) == 0);
  254 + cl_assert(git__suffixcmp(git_repository_workdir(repo2), repo_wd) == 0);
  255 + git_repository_free(repo2);
  256 +
  257 + cl_git_pass(git_buf_sets(&winpath, git_repository_workdir(repo)));
  258 + unposix_path(&winpath);
  259 + cl_git_pass(git_repository_open(&repo2, winpath.ptr));
  260 + cl_assert(git__suffixcmp(git_repository_path(repo2), repo_path) == 0);
  261 + cl_assert(git__suffixcmp(git_repository_workdir(repo2), repo_wd) == 0);
  262 + git_repository_free(repo2);
  263 +
  264 + cl_git_pass(git_buf_sets(&winpath, git_repository_workdir(repo)));
  265 + git_buf_truncate(&winpath, winpath.size - 1); /* remove trailing '/' */
  266 + unposix_path(&winpath);
  267 + cl_git_pass(git_repository_open(&repo2, winpath.ptr));
  268 + cl_assert(git__suffixcmp(git_repository_path(repo2), repo_path) == 0);
  269 + cl_assert(git__suffixcmp(git_repository_workdir(repo2), repo_wd) == 0);
  270 + git_repository_free(repo2);
44 271
45   - cl_fixture_cleanup("empty_standard_repo");
  272 + git_buf_free(&winpath);
  273 +#endif
46 274 }

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.