Fix git_worktree_validate failing on bare repositories#4686
Fix git_worktree_validate failing on bare repositories#4686pks-t merged 7 commits intolibgit2:masterfrom
Conversation
I wonder if this was an oversight from debugging? The documentation mentions that these will return 0 "or an error code". I would much rather see this returning something in the scope of |
|
Well, I'm not against changing it, but that's against the no-API-change rule, so we'd have to put up a replacement. typedef enum {
ok = 0,
bad = -1,
…
} git_worktree_status;
int git_worktree_is_valid(git_worktree_status *status, git_worktree *wt);That would return a 1 on success, 0 on failure, and set *status to the error, which makes it usable like this : int state;
if (!git_worktree_is_valid(&state, wt) && state == bad1) {
}Not happy with the enum name though… |
|
We're not changing the API. We're fixing the function to behave as it's documented. |
|
I might be missing something, but I see only three ways to fix that function :
|
The documentation is right. The code is wrong. We are not returning error codes, we are returning random numbers. Nobody is relying on that code returning No need to overthink this, we should just fix the code. |
|
Okay, thanks for the clarification. I'll remap everything to |
|
Awesome, thanks! |
pks-t
left a comment
There was a problem hiding this comment.
I like these changes, thanks for working on them! Some comments below
| git_worktree *wt; | ||
|
|
||
| cl_git_pass(git_worktree_add(&wt, g_repo, "name", WORKTREE_REPO, NULL)); | ||
| cl_assert_equal_i(0, git_worktree_validate(wt)); |
There was a problem hiding this comment.
I would've just added this additional call to the previous test, but yeah, doesn't really matter too much.
There was a problem hiding this comment.
Also it should probably be cl_git_pass instead of cl_assert_equal_i(0, ...
|
|
||
| const char *cl_git_sandbox_path(int is_dir, const char *path, ...) | ||
| { | ||
| static char _temp[4096]; |
There was a problem hiding this comment.
True (inspiration was clar_sandbox_path(), which has no such define). Fixed.
| if (is_dir) | ||
| git_path_to_dir(&buf); | ||
|
|
||
| git_buf_copy_cstr(_temp, 4096, &buf); |
There was a problem hiding this comment.
You should check whether this truncates buf, first. Agreed, it is very unlikely, but I don't want to clean up (rm -rf) truncated paths at any point in time.
There was a problem hiding this comment.
Fixed by checking the length of buf before copying.
| git_buf_dispose(&path); | ||
| cl_assert_equal_s(wt->workdir, cl_git_sandbox_path(1, wtdir, NULL)); | ||
| cl_assert_equal_s(wt->gitlink, cl_git_sandbox_path(0, wtdir, ".git", NULL)); | ||
| cl_assert_equal_s(wt->gitdir, cl_git_sandbox_path(1, parentdir, ".git", "worktrees", wtdir, NULL)); |
| va_list arg; | ||
|
|
||
| cl_git_pass(git_buf_sets(&buf, clar_sandbox_path())); | ||
| if (path) { |
There was a problem hiding this comment.
This check is kind of superfluous. The signature should rather be const char *cl_git_sandbox_path(int is_dir, ...). Then you could avoid this additional check and just enter right into the while (va_arg(...) != NULL) loop.
There was a problem hiding this comment.
True. is_dir was added afterward, hence the path test. Fixed.
0ac8a8c to
bc9bf5d
Compare
|
Ok, this is now green and ready to go! |
|
|
||
| /* make sure we won't truncate */ | ||
| cl_assert(git_buf_len(&buf) < GIT_PATH_MAX); | ||
| git_buf_copy_cstr(_temp, 4096, &buf); |
There was a problem hiding this comment.
Sorry to be nitpicky, but this should probably use the same constant GIT_PATH_MAX or sizeof(_temp), shouldn't it?
bc9bf5d to
1da6329
Compare
|
Thanks, @tiennou! |
This fixes #4669, as well as checking that #4684 really isn't our fault (I wasn't sure, since #4630 was about that problem).
Sidenotes :
I was expecting
git_worktree_validateto return a "logical C bool", but it instead return negative error codes that don't map to "the standard" ones. That was surprising 😉.Looking at the diff, I'm also unsure what the
list_baretest I moved was actually testing, as it's not run against the testsuite's repository.