From 86db1ad5dc9d878f463ac6c9d5ee412b62b74340 Mon Sep 17 00:00:00 2001 From: Reginald McLean Date: Tue, 24 Nov 2020 18:45:55 -0500 Subject: Added check if gitdir exists in is_prunable() Fixes #5598 --- src/libgit2/worktree.c | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src') diff --git a/src/libgit2/worktree.c b/src/libgit2/worktree.c index 82e1d2d7e..a8460380f 100644 --- a/src/libgit2/worktree.c +++ b/src/libgit2/worktree.c @@ -565,6 +565,7 @@ int git_worktree_is_prunable(git_worktree *wt, git_worktree_prune_options *opts) { git_worktree_prune_options popts = GIT_WORKTREE_PRUNE_OPTIONS_INIT; + git_buf path = GIT_BUF_INIT; GIT_ERROR_CHECK_VERSION( opts, GIT_WORKTREE_PRUNE_OPTIONS_VERSION, @@ -595,6 +596,14 @@ int git_worktree_is_prunable(git_worktree *wt, return 0; } + if (git_buf_printf(&path, "%s/worktrees/%s", wt->commondir_path, wt->name) < 0) + return 0; + if (!git_path_exists(path.ptr)) + { + git_error_set(GIT_ERROR_WORKTREE, "worktree gitdir '%s' does not exist", path.ptr); + return 0; + } + return 1; } -- cgit v1.2.1 From 05719c55ff6a8780d7d3b5ae5c55d81f981fe7b3 Mon Sep 17 00:00:00 2001 From: Reginald McLean Date: Tue, 24 Nov 2020 19:42:54 -0500 Subject: Refactor git_worktree_is_prunable slightly to fix memory leak --- src/libgit2/worktree.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/libgit2/worktree.c b/src/libgit2/worktree.c index a8460380f..702b7b774 100644 --- a/src/libgit2/worktree.c +++ b/src/libgit2/worktree.c @@ -566,6 +566,7 @@ int git_worktree_is_prunable(git_worktree *wt, { git_worktree_prune_options popts = GIT_WORKTREE_PRUNE_OPTIONS_INIT; git_buf path = GIT_BUF_INIT; + int ret = 0; GIT_ERROR_CHECK_VERSION( opts, GIT_WORKTREE_PRUNE_OPTIONS_VERSION, @@ -576,35 +577,40 @@ int git_worktree_is_prunable(git_worktree *wt, if ((popts.flags & GIT_WORKTREE_PRUNE_LOCKED) == 0) { git_str reason = GIT_STR_INIT; - int error; - if ((error = git_worktree__is_locked(&reason, wt)) < 0) - return error; + if ((ret = git_worktree_is_locked(&reason, wt)) < 0) + goto out; - if (error) { + if (ret) { if (!reason.size) git_str_attach_notowned(&reason, "no reason given", 15); git_error_set(GIT_ERROR_WORKTREE, "not pruning locked working tree: '%s'", reason.ptr); git_str_dispose(&reason); - return 0; + ret = 0; + goto out; } } if ((popts.flags & GIT_WORKTREE_PRUNE_VALID) == 0 && git_worktree_validate(wt) == 0) { git_error_set(GIT_ERROR_WORKTREE, "not pruning valid working tree"); - return 0; + goto out; } - if (git_buf_printf(&path, "%s/worktrees/%s", wt->commondir_path, wt->name) < 0) - return 0; + if ((ret = git_buf_printf(&path, "%s/worktrees/%s", wt->commondir_path, wt->name) < 0)) + goto out; if (!git_path_exists(path.ptr)) { git_error_set(GIT_ERROR_WORKTREE, "worktree gitdir '%s' does not exist", path.ptr); - return 0; + goto out; } - return 1; + ret = 1; + +out: + + git_buf_dispose(&path); + return ret; } int git_worktree_prune(git_worktree *wt, -- cgit v1.2.1 From 372143244dc82551eccb0503ef58d9850d530346 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Tue, 16 May 2023 12:38:24 +0100 Subject: worktree: update prunable to reflect refactorings PR #5712 predates several refactorings to move the utility code into a more general purpose codebase. Update to reflect the refactorings. --- src/libgit2/worktree.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/libgit2/worktree.c b/src/libgit2/worktree.c index 702b7b774..a878634ca 100644 --- a/src/libgit2/worktree.c +++ b/src/libgit2/worktree.c @@ -565,7 +565,7 @@ int git_worktree_is_prunable(git_worktree *wt, git_worktree_prune_options *opts) { git_worktree_prune_options popts = GIT_WORKTREE_PRUNE_OPTIONS_INIT; - git_buf path = GIT_BUF_INIT; + git_str path = GIT_STR_INIT; int ret = 0; GIT_ERROR_CHECK_VERSION( @@ -578,13 +578,14 @@ int git_worktree_is_prunable(git_worktree *wt, if ((popts.flags & GIT_WORKTREE_PRUNE_LOCKED) == 0) { git_str reason = GIT_STR_INIT; - if ((ret = git_worktree_is_locked(&reason, wt)) < 0) + if ((ret = git_worktree__is_locked(&reason, wt)) < 0) goto out; if (ret) { - if (!reason.size) - git_str_attach_notowned(&reason, "no reason given", 15); - git_error_set(GIT_ERROR_WORKTREE, "not pruning locked working tree: '%s'", reason.ptr); + git_error_set(GIT_ERROR_WORKTREE, + "not pruning locked working tree: '%s'", + reason.size ? reason.ptr : "is locked"); + git_str_dispose(&reason); ret = 0; goto out; @@ -597,19 +598,18 @@ int git_worktree_is_prunable(git_worktree *wt, goto out; } - if ((ret = git_buf_printf(&path, "%s/worktrees/%s", wt->commondir_path, wt->name) < 0)) + if ((ret = git_str_printf(&path, "%s/worktrees/%s", wt->commondir_path, wt->name) < 0)) goto out; - if (!git_path_exists(path.ptr)) - { - git_error_set(GIT_ERROR_WORKTREE, "worktree gitdir '%s' does not exist", path.ptr); + + if (!git_fs_path_exists(path.ptr)) { + git_error_set(GIT_ERROR_WORKTREE, "worktree gitdir ('%s') does not exist", path.ptr); goto out; } ret = 1; out: - - git_buf_dispose(&path); + git_str_dispose(&path); return ret; } -- cgit v1.2.1