Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
b45219f
Implement custom thread local storage for user of library
implausible Mar 29, 2021
7a76a33
checkout: cleanup duplication in checkout_create_the_new
implausible Aug 12, 2020
07493cc
thread checkout: move checkout buffers to tls
implausible Mar 29, 2021
e1a1eaa
thread checkout: add locks around shared state
implausible Aug 12, 2020
dc4e588
thread checkout: add locks around non thread-safe actions
implausible Apr 9, 2021
0739689
thread checkout: stub indirection for threading
implausible Aug 12, 2020
27f3c80
thread checkout: add threading to checkout_create_the_new
implausible Mar 29, 2021
37caa8d
meta: show build status for v1.3 branch
ethomson Feb 26, 2022
6b12762
online: test with https instead of git protocol
ethomson Jan 11, 2022
670415a
clone: update bitbucket tests
ethomson Mar 23, 2022
973d959
path: refactor ownership checks into current user and system
ethomson Apr 10, 2022
62d492d
repo: ensure that repo dir is owned by current user
ethomson Apr 11, 2022
e4eabb0
fs_path: mock ownership checks
ethomson Apr 12, 2022
caee92e
repo: test configuration ownership validation
ethomson Apr 11, 2022
f683806
repo: refactor global config loader function
ethomson Apr 11, 2022
eb8c3e5
repo: honor safe.directory during ownership checks
ethomson Apr 11, 2022
b58e905
repo: make ownership checks optional
ethomson Apr 12, 2022
a9eac6a
Merge pull request #6268 from libgit2/ethomson/ownership_13
ethomson Apr 12, 2022
1f39aac
meta: update version numbers for v1.3.1
ethomson Apr 12, 2022
23c24f8
meta: changelog for v1.3.1
ethomson Apr 12, 2022
1f5e7f9
Merge pull request #6271 from libgit2/ethomson/v1.3.1
ethomson Apr 12, 2022
6da6a10
Merge remote-tracking branch 'zawata/feature/custom-tls-for-external-…
zawata Apr 13, 2022
30d5c08
Merge remote-tracking branch 'zawata/multithread/checkout_create_the_…
zawata Apr 13, 2022
4b193b1
New checkout option: disabled_filters
julianmesa-gitkraken May 6, 2022
fe44f25
Merge branch 'disabled-filters-checkout' into libgit-next
ianhattendorf May 7, 2022
e78ee33
Fix degraded performance using GIT_USE_NSEC on repos cloned with GIT_…
julianmesa-gitkraken May 18, 2022
013d416
Merge pull request #7 from julianmesa-gitkraken/fix-nanoseconds-on-no…
ianhattendorf May 18, 2022
3ad710a
Fix the GIT_USE_NSEC performance fix
julianmesa-gitkraken May 26, 2022
4c98283
Merge pull request #8 from julianmesa-gitkraken/fix-nsecs-fix
ianhattendorf May 26, 2022
d6554d0
stash: introduce `build_stash_commit_from_index`
gitkraken-jacobw Jul 14, 2022
8280bb0
stash: implement partial stashing by path
gitkraken-jacobw Jul 14, 2022
65210e9
stash: implement CI testing
gitkraken-jacobw Jun 17, 2022
ec67f95
stash: better option validation for stash save
gitkraken-jacobw Jul 14, 2022
f2befe8
stash: add `const` to arguments
gitkraken-jacobw Jul 13, 2022
98faa3e
stash: test save options init
gitkraken-jacobw Jul 13, 2022
0ac7af7
Merge pull request #1 from gitkraken-jacobw/partialstashing
zawata Jul 28, 2022
e6b6ed0
Fix leak in git_tag_create_from_buffer
julianmesa-gitkraken Nov 3, 2022
d4b247d
Missing disposes
julianmesa-gitkraken Nov 3, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
stash: implement partial stashing by path
code has been moved from `git_stash_save` into `git_stash_save_with_opts` in order to handle cases where we want to partially stash using options or stash normally without partially stashed paths specified
  • Loading branch information
gitkraken-jacobw committed Jul 14, 2022
commit 8280bb0ec7dec79c1e1a88202d369b2225274a59
64 changes: 64 additions & 0 deletions include/git2/stash.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ typedef enum {
* the working directory
*/
GIT_STASH_INCLUDE_IGNORED = (1 << 2),

/**
* All changes in the index and working directory are left intact
*/
GIT_STASH_KEEP_ALL = (1 << 3),
} git_stash_flags;

/**
Expand All @@ -71,6 +76,65 @@ GIT_EXTERN(int) git_stash_save(
const char *message,
uint32_t flags);

/**
* Stash save options structure
*
* Initialize with `GIT_STASH_SAVE_OPTIONS_INIT`. Alternatively, you can
* use `git_stash_save_options_init`.
*
*/
typedef struct git_stash_save_options {
unsigned int version;

/** The identity of the person performing the stashing. */
const git_signature *stasher;

/** Optional description along with the stashed state. */
const char *message;

/** Flags to control the stashing process. (see GIT_STASH_* above) */
uint32_t flags;

/** Optional paths that control which files are stashed. */
git_strarray paths;
} git_stash_save_options;

#define GIT_STASH_SAVE_OPTIONS_VERSION 1
#define GIT_STASH_SAVE_OPTIONS_INIT { \
GIT_STASH_SAVE_OPTIONS_VERSION, \
NULL, \
NULL, \
GIT_STASH_DEFAULT }

/**
* Initialize git_stash_save_options structure
*
* Initializes a `git_stash_save_options` with default values. Equivalent to
* creating an instance with `GIT_STASH_SAVE_OPTIONS_INIT`.
*
* @param opts The `git_stash_save_options` struct to initialize.
* @param version The struct version; pass `GIT_STASH_SAVE_OPTIONS_VERSION`.
* @return Zero on success; -1 on failure.
*/
GIT_EXTERN(int) git_stash_save_options_init(
git_stash_save_options *opts, unsigned int version);

/**
* Save the local modifications to a new stash, with options.
*
* @param out Object id of the commit containing the stashed state.
* This commit is also the target of the direct reference refs/stash.
*
* @param repo The owning repository.
*
* @param opts The stash options.
*
* @return 0 on success, GIT_ENOTFOUND where there's nothing to stash,
* or error code.
*/
GIT_EXTERN(int) git_stash_save_with_opts(
git_oid *out, git_repository *repo, git_stash_save_options *opts);

/** Stash application flags. */
typedef enum {
GIT_STASH_APPLY_DEFAULT = 0,
Expand Down
144 changes: 130 additions & 14 deletions src/stash.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,31 @@ static int stash_to_index(
return git_index_add(index, &entry);
}

static int stash_update_index_from_paths(
git_repository *repo,
git_index *index,
git_strarray *paths)
{
unsigned int status_flags;
size_t i;
int error = 0;

for(i = 0; i < paths->count; i++) {
git_status_file(&status_flags, repo, paths->strings[i]);

if (status_flags & (GIT_STATUS_WT_DELETED | GIT_STATUS_INDEX_DELETED)) {
if ((error = git_index_remove(index, paths->strings[i], 0)) < 0)
return error;
}
else {
if ((error = stash_to_index(repo, index, paths->strings[i])) < 0)
return error;
}
}

return error;
}

static int stash_update_index_from_diff(
git_repository *repo,
git_index *index,
Expand Down Expand Up @@ -576,6 +601,50 @@ static int ensure_there_are_changes_to_stash(git_repository *repo, uint32_t flag
return error;
}

static int has_changes_cb(const char *path, unsigned int status, void *payload) {
GIT_UNUSED(path);
GIT_UNUSED(status);
GIT_UNUSED(payload);

if (status == GIT_STATUS_CURRENT)
return GIT_ENOTFOUND;

return 0;
}

static int ensure_there_are_changes_to_stash_paths(
git_repository *repo,
uint32_t flags,
git_strarray *paths)
{
int error;
git_status_options opts = GIT_STATUS_OPTIONS_INIT;

opts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
opts.flags = GIT_STATUS_OPT_EXCLUDE_SUBMODULES
| GIT_STATUS_OPT_INCLUDE_UNMODIFIED
| GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH;

if (flags & GIT_STASH_INCLUDE_UNTRACKED)
opts.flags |= GIT_STATUS_OPT_INCLUDE_UNTRACKED |
GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;

if (flags & GIT_STASH_INCLUDE_IGNORED)
opts.flags |= GIT_STATUS_OPT_INCLUDE_IGNORED |
GIT_STATUS_OPT_RECURSE_IGNORED_DIRS;

git_strarray_copy(&opts.pathspec, paths);

error = git_status_foreach_ext(repo, &opts, has_changes_cb, NULL);

git_strarray_dispose(&opts.pathspec);

if (error == GIT_ENOTFOUND)
return create_error(GIT_ENOTFOUND, "one of the files does not have any changes to stash.");

return error;
}

static int reset_index_and_workdir(git_repository *repo, git_commit *commit, uint32_t flags)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
Expand All @@ -596,50 +665,87 @@ int git_stash_save(
const char *message,
uint32_t flags)
{
git_index *index = NULL;
git_stash_save_options opts = GIT_STASH_SAVE_OPTIONS_INIT;
opts.stasher = stasher;
opts.message = message;
opts.flags = flags;
return git_stash_save_with_opts(out, repo, &opts);
}

int git_stash_save_with_opts(
git_oid *out, git_repository *repo, git_stash_save_options *opts)
{
git_index *index = NULL, *paths_index = NULL;
git_commit *b_commit = NULL, *i_commit = NULL, *u_commit = NULL;
git_buf msg = GIT_BUF_INIT;
git_tree *tree = NULL;
git_reference *head = NULL;
int error;

GIT_ASSERT_ARG(out);
GIT_ASSERT_ARG(repo);
GIT_ASSERT_ARG(stasher);

if ((error = git_repository__ensure_not_bare(repo, "stash save")) < 0)
return error;

if ((error = retrieve_base_commit_and_message(&b_commit, &msg, repo)) < 0)
goto cleanup;

if ((error = ensure_there_are_changes_to_stash(repo, flags)) < 0)
if (opts->paths.count == 0 &&
(error = ensure_there_are_changes_to_stash(repo, opts->flags)) < 0)
goto cleanup;
else if (opts->paths.count > 0 &&
(error = ensure_there_are_changes_to_stash_paths(
repo, opts->flags, &opts->paths)) < 0)
goto cleanup;

if ((error = git_repository_index(&index, repo)) < 0)
goto cleanup;

if ((error = commit_index(&i_commit, repo, index, stasher,
git_buf_cstr(&msg), b_commit)) < 0)
if ((error = commit_index(&i_commit, repo, index, opts->stasher,
git_buf_cstr(&msg), b_commit)) < 0)
goto cleanup;

if ((flags & (GIT_STASH_INCLUDE_UNTRACKED | GIT_STASH_INCLUDE_IGNORED)) &&
(error = commit_untracked(&u_commit, repo, stasher,
git_buf_cstr(&msg), i_commit, flags)) < 0)
if ((opts->flags & (GIT_STASH_INCLUDE_UNTRACKED | GIT_STASH_INCLUDE_IGNORED)) &&
(error = commit_untracked(&u_commit, repo, opts->stasher,
git_buf_cstr(&msg), i_commit, opts->flags)) < 0)
goto cleanup;

if ((error = prepare_worktree_commit_message(&msg, message)) < 0)
if ((error = prepare_worktree_commit_message(&msg, opts->message)) < 0)
goto cleanup;

if ((error = commit_worktree(out, repo, stasher, git_buf_cstr(&msg),
i_commit, b_commit, u_commit)) < 0)
goto cleanup;
if (opts->paths.count == 0) {
if ((error = commit_worktree(out, repo, opts->stasher, git_buf_cstr(&msg),
i_commit, b_commit, u_commit)) < 0)
goto cleanup;
} else {
if ((error = git_index_new(&paths_index)) < 0)
goto cleanup;

if ((error = retrieve_head(&head, repo)) < 0)
goto cleanup;

if ((error = git_reference_peel((git_object**)&tree, head, GIT_OBJECT_TREE)) < 0)
goto cleanup;

if ((error = git_index_read_tree(paths_index, tree)) < 0)
goto cleanup;

if ((error = stash_update_index_from_paths(repo, paths_index, &opts->paths)) < 0)
goto cleanup;

if ((error = build_stash_commit_from_index(out, repo, opts->stasher, git_buf_cstr(&msg),
i_commit, b_commit, u_commit, paths_index)) < 0)
goto cleanup;
}

git_buf_rtrim(&msg);

if ((error = update_reflog(out, repo, git_buf_cstr(&msg))) < 0)
goto cleanup;

if ((error = reset_index_and_workdir(repo, (flags & GIT_STASH_KEEP_INDEX) ? i_commit : b_commit,
flags)) < 0)
if (!(opts->flags & GIT_STASH_KEEP_ALL) && (error = reset_index_and_workdir(repo,
(opts->flags & GIT_STASH_KEEP_INDEX) ? i_commit : b_commit, opts->flags)) < 0)
goto cleanup;

cleanup:
Expand All @@ -648,7 +754,10 @@ int git_stash_save(
git_commit_free(i_commit);
git_commit_free(b_commit);
git_commit_free(u_commit);
git_tree_free(tree);
git_reference_free(head);
git_index_free(index);
git_index_free(paths_index);

return error;
}
Expand Down Expand Up @@ -833,6 +942,13 @@ int git_stash_apply_options_init(git_stash_apply_options *opts, unsigned int ver
return 0;
}

int git_stash_save_options_init(git_stash_save_options *opts, unsigned int version)
{
GIT_INIT_STRUCTURE_FROM_TEMPLATE(
opts, version, git_stash_save_options, GIT_STASH_SAVE_OPTIONS_INIT);
return 0;
}

#ifndef GIT_DEPRECATE_HARD
int git_stash_apply_init_options(git_stash_apply_options *opts, unsigned int version)
{
Expand Down