read-tree: improve untracked file support
cover read-tree: improve untracked file support
From: Phillip Wood <[email protected]>
These two patches teach read-tree how to avoid overwriting untracked files when doing '--reset -u' and also how to respect all of git's standard excludes files. I'd like to see the porcelain commands stop overwriting untracked files, this is a first step on the way. I'm not sure if we want to add options to the porcelain commands to protect untracked files or just change their behavior and add an option to override that. I'm leaning towards the latter but I'd be interested to hear what others think.
Phillip Wood (2):
read-tree --reset: add --protect-untracked
read-tree: add --exclude-standard
Documentation/git-read-tree.txt | 19 ++++++++-- builtin/am.c | 8 +++-- builtin/checkout.c | 2 +- builtin/read-tree.c | 61 +++++++++++++++++++++++++++++-- builtin/rebase.c | 2 +- builtin/reset.c | 2 +- builtin/stash.c | 7 ++-- t/lib-read-tree.sh | 11 ++++++ t/t1005-read-tree-reset.sh | 63 +++++++++++++++++++++++++++++++-- t/t1013-read-tree-submodule.sh | 3 +- unpack-trees.c | 3 +- unpack-trees.h | 10 ++++-- 12 files changed, 170 insertions(+), 21 deletions(-)
Signature · 1 line
2.21.0
PATCH 1/2 read-tree --reset: add --protect-untracked
From: Phillip Wood <[email protected]>
Currently there is no way to get git to discard changes to the worktree without overwriting untracked files. reset --hard, checkout --force, checkout <rev> :/ and read-tree --reset -u will all overwrite untracked files. unpack_trees() has known how to avoid overwriting untracked files since commit fcc387db9b ("read-tree -m -u: do not overwrite or remove untracked working tree files.", 2006-05-17) in response to concerns about lost files [1] but those protections do not apply to --reset. This patch adds an --protect-untracked option for read-tree/unpack_trees() to apply the same protections with --reset. It does not change the behavior of any of the porcelain commands but paves the way for adding options or changing the default for those in future.
Note the actual change in unpack-trees.c is quite simple, the majority of this patch is converting existing callers to use the new unpack_trees_reset_type enum.
[1] https://public-inbox.org/git/[email protected]/
---
Notes:
adding --protect-untracked to the invocation of test_submodule_forced_switch() in t1013 fixes the known breakage of tests 57 & 58 but breaks test 64 which relies on forcing. I'm not sure that the expected results of 57 & 58 are correct if we're forcing.
Documentation/git-read-tree.txt | 10 +++++++-- builtin/am.c | 8 ++++--- builtin/checkout.c | 2 +- builtin/read-tree.c | 12 +++++++++++ builtin/rebase.c | 2 +- builtin/reset.c | 2 +- builtin/stash.c | 7 ++++--- t/lib-read-tree.sh | 11 ++++++++++ t/t1005-read-tree-reset.sh | 37 +++++++++++++++++++++++++++++++-- unpack-trees.c | 3 ++- unpack-trees.h | 10 +++++++-- 11 files changed, 88 insertions(+), 16 deletions(-)
Documentation/git-read-tree.txt +8 -2
@@ -40,12 +40,18 @@ OPTIONS --reset:: Same as -m, except that unmerged entries are discarded instead of failing. When used with `-u`, updates leading to loss of - working tree changes will not abort the operation. + working tree changes will not abort the operation and + untracked files will be overwritten. Use `--protect-untracked` + to avoid overwriting untracked files. -u:: After a successful merge, update the files in the work tree with the result of the merge. +--protect-untracked:: + Prevent `--reset` `-u` from overwriting untracked files. Requires + `--reset` and `-u` (`-m` never overwrites untracked files). + -i:: Usually a merge requires the index file as well as the files in the working tree to be up to date with the
@@ -89,7 +95,7 @@ OPTIONS existed in the original index file. --exclude-per-directory=<gitignore>:: - When running the command with `-u` and `-m` options, the + When using `-u` with `-m` or `--reset` `--protect-untracked` options, the merge result may need to overwrite paths that are not tracked in the current branch. The command usually refuses to proceed with the merge to avoid losing such a
builtin/am.c +5 -3
@@ -1854,7 +1854,8 @@ static void am_resolve(struct am_state *state)* true, any unmerged entries will be discarded. Returns 0 on success, -1 on * failure. */ -static int fast_forward_to (struct tree *head,struct tree *remote,int reset) +static int fast_forward_to (struct tree *head,struct tree *remote, +enum unpack_trees_reset_type reset) {struct lock_file lock_file =LOCK_INIT ;struct unpack_trees_options opts;
@@ -1942,7 +1943,8 @@ static int clean_index(const struct object_id *head, const struct object_id *remread_cache_unmerged (); -if (fast_forward_to (head_tree, head_tree,1 )) +if (fast_forward_to (head_tree, head_tree, +UNPACK_RESET_OVERWRITE_UNTRACKED ))return -1 ;if (write_cache_as_tree (&index,0 ,NULL ))
@@ -1952,7 +1954,7 @@ static int clean_index(const struct object_id *head, const struct object_id *remif (!index_tree)return error (_ ("Could not parse object '%s'." ),oid_to_hex (&index)); -if (fast_forward_to (index_tree, remote_tree,0 )) +if (fast_forward_to (index_tree, remote_tree,UNPACK_NO_RESET ))return -1 ;if (merge_tree (remote_tree))
builtin/checkout.c +1 -1
@@ -506,7 +506,7 @@ static int reset_tree(struct tree *tree, const struct checkout_opts *o, opts.head_idx =-1 ; opts.update = worktree; opts.skip_unmerged = !worktree; - opts.reset =1 ; + opts.reset =UNPACK_RESET_OVERWRITE_UNTRACKED ; opts.merge =1 ; opts.fn = oneway_merge; opts.verbose_update = o->show_progress;
builtin/read-tree.c +12 -0
@@ -114,6 +114,7 @@ static int git_read_tree_config(const char *var, const char *value, void *cb)int cmd_read_tree (int argc,const char **argv,const char *unused_prefix) {int i, stage =0 ; +int protect_untracked =-1 ;struct object_id oid;struct tree_desc t[MAX_UNPACK_TREES ];struct unpack_trees_options opts;
@@ -140,6 +141,8 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix) PARSE_OPT_NONEG },OPT_BOOL ('u' ,NULL , &opts.update,N_ ("update working tree with merge result" )), +OPT_BOOL (0 ,"protect-untracked" , &protect_untracked, +N_ ("do not overwrite untracked files" )), { OPTION_CALLBACK, 0, "exclude-per-directory", &opts,N_ ("gitignore" ), N_("allow explicitly ignored files to be overwritten"),
@@ -209,8 +212,17 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)if ((opts.update || opts.index_only) && !opts.merge)die ("%s is meaningless without -m, --reset, or --prefix" , opts.update ?"-u" :"-i" ); +if (protect_untracked >=0 ) { +if (!opts.reset || !opts.update) +die ("-%s-protect-untracked requires --reset and -u" , + protect_untracked ?"" :"-no" ); + opts.reset =UNPACK_RESET_PROTECT_UNTRACKED ; + }if ((opts.dir && !opts.update))die ("--exclude-per-directory is meaningless unless -u" ); +if (opts.dir && opts.reset ==UNPACK_RESET_OVERWRITE_UNTRACKED ) +warning ("--exclude-per-directory without --preserve-untracked " +"has no effect" );if (opts.merge && !opts.index_only)setup_work_tree ();
builtin/rebase.c +1 -1
@@ -398,7 +398,7 @@ static int reset_head(struct object_id *oid, const char *action, unpack_tree_opts.update =1 ; unpack_tree_opts.merge =1 ;if (!detach_head) - unpack_tree_opts.reset =1 ; + unpack_tree_opts.reset =UNPACK_RESET_OVERWRITE_UNTRACKED ;if (repo_read_index_unmerged (the_repository) <0 ) { ret =error (_ ("could not read index" ));
builtin/reset.c +1 -1
@@ -70,7 +70,7 @@ static int reset_index(const struct object_id *oid, int reset_type, int quiet) opts.update =1 ;/* fallthrough */ default : - opts.reset =1 ; + opts.reset =UNPACK_RESET_OVERWRITE_UNTRACKED ; }read_cache_unmerged ();
builtin/stash.c +4 -3
@@ -227,7 +227,8 @@ static int clear_stash(int argc, const char **argv, const char *prefix)return do_clear_stash (); } -static int reset_tree (struct object_id *i_tree,int update,int reset) +static int reset_tree (struct object_id *i_tree,int update, +enum unpack_trees_reset_type reset) {int nr_trees =1 ;struct unpack_trees_options opts;
@@ -461,7 +462,7 @@ static int do_apply_stash(const char *prefix, struct stash_info *info, }if (has_index) { -if (reset_tree (&index_tree,0 ,0 )) +if (reset_tree (&index_tree,0 ,UNPACK_NO_RESET ))return -1 ; }else {struct strbuf out =STRBUF_INIT ;
@@ -471,7 +472,7 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,return -1 ; } -if (reset_tree (&c_tree,0 ,1 )) { +if (reset_tree (&c_tree,0 ,UNPACK_RESET_OVERWRITE_UNTRACKED )) {strbuf_release (&out);return -1 ; }
t/lib-read-tree.sh +11 -0
@@ -39,3 +39,14 @@ read_tree_u_must_fail () { test_cmp pre-dry-run-wt post-dry-run-wt && test_must_fail git read-tree "$@" } + +read_tree_u_must_fail_save_err () { + git ls-files -s >pre-dry-run && + git diff-files -p >pre-dry-run-wt && + test_must_fail git read-tree -n "$@" && + git ls-files -s >post-dry-run && + git diff-files -p >post-dry-run-wt && + test_cmp pre-dry-run post-dry-run && + test_cmp pre-dry-run-wt post-dry-run-wt && + test_must_fail git read-tree "$@" 2>actual-err +}
t/t1005-read-tree-reset.sh +35 -2
@@ -19,15 +19,48 @@ test_expect_success 'setup' ' git add df && echo content >new && git add new && - git commit -m two + git commit -m two && + git ls-files >expect-two ' -test_expect_success 'reset should work' ' +test_expect_success '--protect-untracked option sanity checks' ' + read_tree_u_must_fail --reset --protect-untracked HEAD && + read_tree_u_must_fail --reset --no-protect-untracked HEAD && + read_tree_u_must_fail -m -u --protect-untracked HEAD && + read_tree_u_must_fail -m -u --no-protect-untracked +' + +test_expect_success 'reset should reset worktree' ' + echo changed >df && read_tree_u_must_succeed -u --reset HEAD^ && git ls-files >actual && test_cmp expect actual ' +test_expect_success 'reset --protect-untracked protects untracked file' ' + echo changed >new && + read_tree_u_must_fail_save_err -u --reset --protect-untracked HEAD && + echo "error: Untracked working tree file '\'new\'' would be overwritten by merge." >expected-err && + test_cmp expected-err actual-err +' + +test_expect_success 'reset --protect-untracked protects untracked directory' ' + rm new && + mkdir new && + echo untracked >new/untracked && + read_tree_u_must_fail_save_err -u --reset --protect-untracked HEAD && + echo "error: Updating '\'new\'' would lose untracked files in it" >expected-err && + test_cmp expected-err actual-err +' + +test_expect_success 'reset --protect-untracked resets' ' + rm -rf new && + echo changed >df/file && + read_tree_u_must_succeed -u --reset --protect-untracked HEAD && + git ls-files >actual-two && + test_cmp expect-two actual-two +' + test_expect_success 'reset should remove remnants from a failed merge' ' read_tree_u_must_succeed --reset -u HEAD && git ls-files -s >expect &&
unpack-trees.c +2 -1
@@ -1917,7 +1917,8 @@ static int verify_absent_1(const struct cache_entry *ce,int len;struct stat st; -if (o->index_only || o->reset || !o->update) +if (o->index_only || o->reset ==UNPACK_RESET_OVERWRITE_UNTRACKED || + !o->update)return 0 ; len =check_leading_path (ce->name,ce_namelen (ce));
unpack-trees.h +8 -2
@@ -41,9 +41,15 @@ void setup_unpack_trees_porcelain(struct unpack_trees_options *opts,*/ void clear_unpack_trees_porcelain (struct unpack_trees_options *opts); +enum unpack_trees_reset_type { +UNPACK_NO_RESET =0 , +UNPACK_RESET_OVERWRITE_UNTRACKED , +UNPACK_RESET_PROTECT_UNTRACKED +}; +struct unpack_trees_options { -unsigned int reset, - merge, +enum unpack_trees_reset_type reset; +unsigned int merge, update, clone, index_only,
Signature · 1 line
2.21.0
Re: [PATCH 1/2] read-tree --reset: add --protect-untracked
Quoted 8 lines · Phillip Wood
unpack-trees.h +0
-0@@ -41,9 +41,15 @@ void setup_unpack_trees_porcelain(struct unpack_trees_options *opts,*/ void clear_unpack_trees_porcelain (struct unpack_trees_options *opts);
unpack-trees.h void setup_unpack_trees_porcelain(struct unpack_trees_options *opts, · line 51
Quoted 10 lines
unpack-trees.h +7
-2+enum unpack_trees_reset_type { +UNPACK_NO_RESET =0 , +UNPACK_RESET_OVERWRITE_UNTRACKED , +UNPACK_RESET_PROTECT_UNTRACKED +}; +struct unpack_trees_options { -unsigned int reset, - merge, +enum unpack_trees_reset_type reset;
Can we add protected_untracked that can be used in combination with the current "reset"?
This opens up (or raises the question about) the opportunity to protect untracked changes on non-reset updates.
Quoted 7 lines
unpack-trees.h +1
-1+unsigned int merge, update, clone, index_only, --2.21.0
Signature · 1 line
Duy
Re: [PATCH 1/2] read-tree --reset: add --protect-untracked
Quoted 24 lines · Duy Nguyen
Quoted 8 lines · Phillip Wood
unpack-trees.h +0
-0@@ -41,9 +41,15 @@ void setup_unpack_trees_porcelain(struct unpack_trees_options *opts,*/ void clear_unpack_trees_porcelain (struct unpack_trees_options *opts);unpack-trees.h void setup_unpack_trees_porcelain(struct unpack_trees_options *opts, · line 51
Quoted 10 lines
unpack-trees.h +7
-2+enum unpack_trees_reset_type { +UNPACK_NO_RESET =0 , +UNPACK_RESET_OVERWRITE_UNTRACKED , +UNPACK_RESET_PROTECT_UNTRACKED +}; +struct unpack_trees_options { -unsigned int reset, - merge, +enum unpack_trees_reset_type reset;Can we add
protected_untrackedthat can be used in combination with the current "reset"?
Quoted 2 lines
This opens up (or raises the question about) the opportunity to protect untracked changes on non-reset updates.
Bahh. "protect_untracked" is already on for non-reset updates. Sorry for the noise.
Signature · 1 line
Duy
Re: [PATCH 1/2] read-tree --reset: add --protect-untracked
In reply to Phillip WoodQuoted 6 lines · Phillip Wood
From: Phillip Wood <[email protected]>
Currently there is no way to get git to discard changes to the worktree without overwriting untracked files.
reset --hard,checkout --force,checkout <rev> :/andread-tree --reset -u
"checkout <rev> :/" does not use unpack-trees, I think, so it does not belong to this group.
Quoted 7 lines
will all overwrite untracked files.
unpack_trees()has known how to avoid overwriting untracked files since commitfcc387db9b("read-tree -m -u: do not overwrite or remove untracked working tree files.", 2006-05-17) in response to concerns about lost files [1] but those protections do not apply to --reset. This patch adds an --protect-untracked option for read-tree/unpack_trees() to apply th same protections with --reset.
Your enum makes me wonder if we should have --reset-tracked instead of --protect-untracked (say what you want to reset seems tiny bit easier to understand than "reset except untracked"). Which leads to another question, do we ever need --reset-untracked (i.e. overwriting untracked files but never ever touch tracked ones).
I have one use case that --reset-untracked might make sense. Suppose you do "git reset HEAD^" where HEAD has some new files. The new files will be left alone in worktree of course, but switching back to HEAD after that, unpack-trees will cry murder because it would otherwise overwrite untracked files (that have the exact same content).
--reset-untracked might be useful for that. I'm carrying a patch to detect identical file content though which addresses this very specific case. Maybe it's the only case that --reset-untracked may be useful.
Quoted 7 lines
It does not change the behavior of any
of the porcelain commands but paves the way for adding options or changing the default for those in future.Note the actual change in
unpack-trees.cis quite simple, the majority of this patch is converting existing callers to use the newunpack_trees_reset_typeenum.
This could be split in a separate patch, then the actual change would become small and more to the point.
Quoted 313 lines
[1] https://public-inbox.org/git/[email protected]/
Signed-off-by Phillip Wood <[email protected]>---
Notes:
adding --protect-untracked to the invocation oftest_submodule_forced_switch()in t1013 fixes the known breakage of tests 57 & 58 but breaks test 64 which relies on forcing. I'm not sure that the expected results of 57 & 58 are correct if we're forcing.Documentation/git-read-tree.txt | 10 +++++++-- builtin/am.c | 8 ++++--- builtin/checkout.c | 2 +- builtin/read-tree.c | 12 +++++++++++ builtin/rebase.c | 2 +- builtin/reset.c | 2 +- builtin/stash.c | 7 ++++--- t/lib-read-tree.sh | 11 ++++++++++ t/t1005-read-tree-reset.sh | 37 +++++++++++++++++++++++++++++++-- unpack-trees.c | 3 ++- unpack-trees.h | 10 +++++++-- 11 files changed, 88 insertions(+), 16 deletions(-)Documentation/git-read-tree.txt +8
-2@@ -40,12 +40,18 @@ OPTIONS --reset:: Same as -m, except that unmerged entries are discarded instead of failing. When used with `-u`, updates leading to loss of - working tree changes will not abort the operation. + working tree changes will not abort the operation and + untracked files will be overwritten. Use `--protect-untracked` + to avoid overwriting untracked files. -u:: After a successful merge, update the files in the work tree with the result of the merge. +--protect-untracked:: + Prevent `--reset` `-u` from overwriting untracked files. Requires + `--reset` and `-u` (`-m` never overwrites untracked files). + -i:: Usually a merge requires the index file as well as the files in the working tree to be up to date with the@@ -89,7 +95,7 @@ OPTIONS existed in the original index file. --exclude-per-directory=<gitignore>:: - When running the command with `-u` and `-m` options, the + When using `-u` with `-m` or `--reset` `--protect-untracked` options, the merge result may need to overwrite paths that are not tracked in the current branch. The command usually refuses to proceed with the merge to avoid losing such abuiltin/am.c +5
-3@@ -1854,7 +1854,8 @@ static void am_resolve(struct am_state *state)* true, any unmerged entries will be discarded. Returns 0 on success, -1 on * failure. */ -static int fast_forward_to (struct tree *head,struct tree *remote,int reset) +static int fast_forward_to (struct tree *head,struct tree *remote, +enum unpack_trees_reset_type reset) {struct lock_file lock_file =LOCK_INIT ;struct unpack_trees_options opts;@@ -1942,7 +1943,8 @@ static int clean_index(const struct object_id *head, const struct object_id *remread_cache_unmerged (); -if (fast_forward_to (head_tree, head_tree,1 )) +if (fast_forward_to (head_tree, head_tree, +UNPACK_RESET_OVERWRITE_UNTRACKED ))return -1 ;if (write_cache_as_tree (&index,0 ,NULL ))@@ -1952,7 +1954,7 @@ static int clean_index(const struct object_id *head, const struct object_id *remif (!index_tree)return error (_ ("Could not parse object '%s'." ),oid_to_hex (&index)); -if (fast_forward_to (index_tree, remote_tree,0 )) +if (fast_forward_to (index_tree, remote_tree,UNPACK_NO_RESET ))return -1 ;if (merge_tree (remote_tree))builtin/checkout.c +1
-1@@ -506,7 +506,7 @@ static int reset_tree(struct tree *tree, const struct checkout_opts *o, opts.head_idx =-1 ; opts.update = worktree; opts.skip_unmerged = !worktree; - opts.reset =1 ; + opts.reset =UNPACK_RESET_OVERWRITE_UNTRACKED ; opts.merge =1 ; opts.fn = oneway_merge; opts.verbose_update = o->show_progress;builtin/read-tree.c +12
-0@@ -114,6 +114,7 @@ static int git_read_tree_config(const char *var, const char *value, void *cb)int cmd_read_tree (int argc,const char **argv,const char *unused_prefix) {int i, stage =0 ; +int protect_untracked =-1 ;struct object_id oid;struct tree_desc t[MAX_UNPACK_TREES ];struct unpack_trees_options opts;@@ -140,6 +141,8 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix) PARSE_OPT_NONEG },OPT_BOOL ('u' ,NULL , &opts.update,N_ ("update working tree with merge result" )), +OPT_BOOL (0 ,"protect-untracked" , &protect_untracked, +N_ ("do not overwrite untracked files" )), { OPTION_CALLBACK, 0, "exclude-per-directory", &opts,N_ ("gitignore" ), N_("allow explicitly ignored files to be overwritten"),@@ -209,8 +212,17 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)if ((opts.update || opts.index_only) && !opts.merge)die ("%s is meaningless without -m, --reset, or --prefix" , opts.update ?"-u" :"-i" ); +if (protect_untracked >=0 ) { +if (!opts.reset || !opts.update) +die ("-%s-protect-untracked requires --reset and -u" , + protect_untracked ?"" :"-no" ); + opts.reset =UNPACK_RESET_PROTECT_UNTRACKED ; + }if ((opts.dir && !opts.update))die ("--exclude-per-directory is meaningless unless -u" ); +if (opts.dir && opts.reset ==UNPACK_RESET_OVERWRITE_UNTRACKED ) +warning ("--exclude-per-directory without --preserve-untracked " +"has no effect" );if (opts.merge && !opts.index_only)setup_work_tree ();builtin/rebase.c +1
-1@@ -398,7 +398,7 @@ static int reset_head(struct object_id *oid, const char *action, unpack_tree_opts.update =1 ; unpack_tree_opts.merge =1 ;if (!detach_head) - unpack_tree_opts.reset =1 ; + unpack_tree_opts.reset =UNPACK_RESET_OVERWRITE_UNTRACKED ;if (repo_read_index_unmerged (the_repository) <0 ) { ret =error (_ ("could not read index" ));builtin/reset.c +1
-1@@ -70,7 +70,7 @@ static int reset_index(const struct object_id *oid, int reset_type, int quiet) opts.update =1 ;/* fallthrough */ default : - opts.reset =1 ; + opts.reset =UNPACK_RESET_OVERWRITE_UNTRACKED ; }read_cache_unmerged ();builtin/stash.c +4
-3@@ -227,7 +227,8 @@ static int clear_stash(int argc, const char **argv, const char *prefix)return do_clear_stash (); } -static int reset_tree (struct object_id *i_tree,int update,int reset) +static int reset_tree (struct object_id *i_tree,int update, +enum unpack_trees_reset_type reset) {int nr_trees =1 ;struct unpack_trees_options opts;@@ -461,7 +462,7 @@ static int do_apply_stash(const char *prefix, struct stash_info *info, }if (has_index) { -if (reset_tree (&index_tree,0 ,0 )) +if (reset_tree (&index_tree,0 ,UNPACK_NO_RESET ))return -1 ; }else {struct strbuf out =STRBUF_INIT ;@@ -471,7 +472,7 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,return -1 ; } -if (reset_tree (&c_tree,0 ,1 )) { +if (reset_tree (&c_tree,0 ,UNPACK_RESET_OVERWRITE_UNTRACKED )) {strbuf_release (&out);return -1 ; }t/lib-read-tree.sh +11
-0@@ -39,3 +39,14 @@ read_tree_u_must_fail () { test_cmp pre-dry-run-wt post-dry-run-wt && test_must_fail git read-tree "$@" } + +read_tree_u_must_fail_save_err () { + git ls-files -s >pre-dry-run && + git diff-files -p >pre-dry-run-wt && + test_must_fail git read-tree -n "$@" && + git ls-files -s >post-dry-run && + git diff-files -p >post-dry-run-wt && + test_cmp pre-dry-run post-dry-run && + test_cmp pre-dry-run-wt post-dry-run-wt && + test_must_fail git read-tree "$@" 2>actual-err +}t/t1005-read-tree-reset.sh +35
-2@@ -19,15 +19,48 @@ test_expect_success 'setup' ' git add df && echo content >new && git add new && - git commit -m two + git commit -m two && + git ls-files >expect-two ' -test_expect_success 'reset should work' ' +test_expect_success '--protect-untracked option sanity checks' ' + read_tree_u_must_fail --reset --protect-untracked HEAD && + read_tree_u_must_fail --reset --no-protect-untracked HEAD && + read_tree_u_must_fail -m -u --protect-untracked HEAD && + read_tree_u_must_fail -m -u --no-protect-untracked +' + +test_expect_success 'reset should reset worktree' ' + echo changed >df && read_tree_u_must_succeed -u --reset HEAD^ && git ls-files >actual && test_cmp expect actual ' +test_expect_success 'reset --protect-untracked protects untracked file' ' + echo changed >new && + read_tree_u_must_fail_save_err -u --reset --protect-untracked HEAD && + echo "error: Untracked working tree file '\'new\'' would be overwritten by merge." >expected-err && + test_cmp expected-err actual-err +' + +test_expect_success 'reset --protect-untracked protects untracked directory' ' + rm new && + mkdir new && + echo untracked >new/untracked && + read_tree_u_must_fail_save_err -u --reset --protect-untracked HEAD && + echo "error: Updating '\'new\'' would lose untracked files in it" >expected-err && + test_cmp expected-err actual-err +' + +test_expect_success 'reset --protect-untracked resets' ' + rm -rf new && + echo changed >df/file && + read_tree_u_must_succeed -u --reset --protect-untracked HEAD && + git ls-files >actual-two && + test_cmp expect-two actual-two +' + test_expect_success 'reset should remove remnants from a failed merge' ' read_tree_u_must_succeed --reset -u HEAD && git ls-files -s >expect &&unpack-trees.c +2
-1@@ -1917,7 +1917,8 @@ static int verify_absent_1(const struct cache_entry *ce,int len;struct stat st; -if (o->index_only || o->reset || !o->update) +if (o->index_only || o->reset ==UNPACK_RESET_OVERWRITE_UNTRACKED || + !o->update)return 0 ; len =check_leading_path (ce->name,ce_namelen (ce));unpack-trees.h +8
-3@@ -41,9 +41,15 @@ void setup_unpack_trees_porcelain(struct unpack_trees_options *opts,*/ void clear_unpack_trees_porcelain (struct unpack_trees_options *opts); +enum unpack_trees_reset_type { +UNPACK_NO_RESET =0 , +UNPACK_RESET_OVERWRITE_UNTRACKED , +UNPACK_RESET_PROTECT_UNTRACKED +}; +struct unpack_trees_options { -unsigned int reset, - merge, +enum unpack_trees_reset_type reset; +unsigned int merge, update, clone, index_only, --2.21.0
Signature · 1 line
Duy
Re: [PATCH 1/2] read-tree --reset: add --protect-untracked
Quoted 10 lines · Duy Nguyen
Quoted 6 lines · Phillip Wood
From: Phillip Wood <[email protected]>
Currently there is no way to get git to discard changes to the worktree without overwriting untracked files.
reset --hard,checkout --force,checkout <rev> :/andread-tree --reset -u"checkout <rev> :/" does not use unpack-trees, I think, so it does not belong to this group.
You're right that it does not use unpack-trees, but I'm making the point that there is no way to discard changes to tracked files without overwriting untracked files whatever one uses.
Quoted 20 lines
Quoted 7 lines
will all overwrite untracked files.
unpack_trees()has known how to avoid overwriting untracked files since commitfcc387db9b("read-tree -m -u: do not overwrite or remove untracked working tree files.", 2006-05-17) in response to concerns about lost files [1] but those protections do not apply to --reset. This patch adds an --protect-untracked option for read-tree/unpack_trees() to apply th same protections with --reset.Your enum makes me wonder if we should have --reset-tracked instead of --protect-untracked (say what you want to reset seems tiny bit easier to understand than "reset except untracked"). Which leads to another question, do we ever need --reset-untracked (i.e. overwriting untracked files but never ever touch tracked ones).
I have one use case that --reset-untracked might make sense. Suppose you do "git reset HEAD^" where HEAD has some new files. The new files will be left alone in worktree of course, but switching back to HEAD after that, unpack-trees will cry murder because it would otherwise overwrite untracked files (that have the exact same content).
Quoted 4 lines
--reset-untracked might be useful for that. I'm carrying a patch to detect identical file content though which addresses this very specific case. Maybe it's the only case that --reset-untracked may be useful.
That patch sounds useful (I played around with "hg update" the other day and I think it does the same). I think that's probably a common case, it also works if one does "git checkout <other-branch> file" then "git reset HEAD" followed "git checkout <other-branch>". --reset-untracked is just like --reset which we can't get rid of without breaking backwards compatibility. Having --reset-tracked is maybe better than having a --protect-tracked option that only works with --reset -u.
Quoted 10 lines
Quoted 7 lines
It does not change the behavior of any
of the porcelain commands but paves the way for adding options or changing the default for those in future.Note the actual change in
unpack-trees.cis quite simple, the majority of this patch is converting existing callers to use the newunpack_trees_reset_typeenum.This could be split in a separate patch, then the actual change would become small and more to the point.
Yes that would make it clearer.
Best Wishes
Phillip
Quoted 316 lines
Quoted 314 lines
[1] https://public-inbox.org/git/[email protected]/
Signed-off-by Phillip Wood <[email protected]>---
Notes:
adding --protect-untracked to the invocation oftest_submodule_forced_switch()in t1013 fixes the known breakage of tests 57 & 58 but breaks test 64 which relies on forcing. I'm not sure that the expected results of 57 & 58 are correct if we're forcing.Documentation/git-read-tree.txt | 10 +++++++-- builtin/am.c | 8 ++++--- builtin/checkout.c | 2 +- builtin/read-tree.c | 12 +++++++++++ builtin/rebase.c | 2 +- builtin/reset.c | 2 +- builtin/stash.c | 7 ++++--- t/lib-read-tree.sh | 11 ++++++++++ t/t1005-read-tree-reset.sh | 37 +++++++++++++++++++++++++++++++-- unpack-trees.c | 3 ++- unpack-trees.h | 10 +++++++-- 11 files changed, 88 insertions(+), 16 deletions(-)Documentation/git-read-tree.txt +8
-2@@ -40,12 +40,18 @@ OPTIONS --reset:: Same as -m, except that unmerged entries are discarded instead of failing. When used with `-u`, updates leading to loss of - working tree changes will not abort the operation. + working tree changes will not abort the operation and + untracked files will be overwritten. Use `--protect-untracked` + to avoid overwriting untracked files. -u:: After a successful merge, update the files in the work tree with the result of the merge. +--protect-untracked:: + Prevent `--reset` `-u` from overwriting untracked files. Requires + `--reset` and `-u` (`-m` never overwrites untracked files). + -i:: Usually a merge requires the index file as well as the files in the working tree to be up to date with the@@ -89,7 +95,7 @@ OPTIONS existed in the original index file. --exclude-per-directory=<gitignore>:: - When running the command with `-u` and `-m` options, the + When using `-u` with `-m` or `--reset` `--protect-untracked` options, the merge result may need to overwrite paths that are not tracked in the current branch. The command usually refuses to proceed with the merge to avoid losing such abuiltin/am.c +5
-3@@ -1854,7 +1854,8 @@ static void am_resolve(struct am_state *state)* true, any unmerged entries will be discarded. Returns 0 on success, -1 on * failure. */ -static int fast_forward_to (struct tree *head,struct tree *remote,int reset) +static int fast_forward_to (struct tree *head,struct tree *remote, +enum unpack_trees_reset_type reset) {struct lock_file lock_file =LOCK_INIT ;struct unpack_trees_options opts;@@ -1942,7 +1943,8 @@ static int clean_index(const struct object_id *head, const struct object_id *remread_cache_unmerged (); -if (fast_forward_to (head_tree, head_tree,1 )) +if (fast_forward_to (head_tree, head_tree, +UNPACK_RESET_OVERWRITE_UNTRACKED ))return -1 ;if (write_cache_as_tree (&index,0 ,NULL ))@@ -1952,7 +1954,7 @@ static int clean_index(const struct object_id *head, const struct object_id *remif (!index_tree)return error (_ ("Could not parse object '%s'." ),oid_to_hex (&index)); -if (fast_forward_to (index_tree, remote_tree,0 )) +if (fast_forward_to (index_tree, remote_tree,UNPACK_NO_RESET ))return -1 ;if (merge_tree (remote_tree))builtin/checkout.c +1
-1@@ -506,7 +506,7 @@ static int reset_tree(struct tree *tree, const struct checkout_opts *o, opts.head_idx =-1 ; opts.update = worktree; opts.skip_unmerged = !worktree; - opts.reset =1 ; + opts.reset =UNPACK_RESET_OVERWRITE_UNTRACKED ; opts.merge =1 ; opts.fn = oneway_merge; opts.verbose_update = o->show_progress;builtin/read-tree.c +12
-0@@ -114,6 +114,7 @@ static int git_read_tree_config(const char *var, const char *value, void *cb)int cmd_read_tree (int argc,const char **argv,const char *unused_prefix) {int i, stage =0 ; +int protect_untracked =-1 ;struct object_id oid;struct tree_desc t[MAX_UNPACK_TREES ];struct unpack_trees_options opts;@@ -140,6 +141,8 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix) PARSE_OPT_NONEG },OPT_BOOL ('u' ,NULL , &opts.update,N_ ("update working tree with merge result" )), +OPT_BOOL (0 ,"protect-untracked" , &protect_untracked, +N_ ("do not overwrite untracked files" )), { OPTION_CALLBACK, 0, "exclude-per-directory", &opts,N_ ("gitignore" ), N_("allow explicitly ignored files to be overwritten"),@@ -209,8 +212,17 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)if ((opts.update || opts.index_only) && !opts.merge)die ("%s is meaningless without -m, --reset, or --prefix" , opts.update ?"-u" :"-i" ); +if (protect_untracked >=0 ) { +if (!opts.reset || !opts.update) +die ("-%s-protect-untracked requires --reset and -u" , + protect_untracked ?"" :"-no" ); + opts.reset =UNPACK_RESET_PROTECT_UNTRACKED ; + }if ((opts.dir && !opts.update))die ("--exclude-per-directory is meaningless unless -u" ); +if (opts.dir && opts.reset ==UNPACK_RESET_OVERWRITE_UNTRACKED ) +warning ("--exclude-per-directory without --preserve-untracked " +"has no effect" );if (opts.merge && !opts.index_only)setup_work_tree ();builtin/rebase.c +1
-1@@ -398,7 +398,7 @@ static int reset_head(struct object_id *oid, const char *action, unpack_tree_opts.update =1 ; unpack_tree_opts.merge =1 ;if (!detach_head) - unpack_tree_opts.reset =1 ; + unpack_tree_opts.reset =UNPACK_RESET_OVERWRITE_UNTRACKED ;if (repo_read_index_unmerged (the_repository) <0 ) { ret =error (_ ("could not read index" ));builtin/reset.c +1
-1@@ -70,7 +70,7 @@ static int reset_index(const struct object_id *oid, int reset_type, int quiet) opts.update =1 ;/* fallthrough */ default : - opts.reset =1 ; + opts.reset =UNPACK_RESET_OVERWRITE_UNTRACKED ; }read_cache_unmerged ();builtin/stash.c +4
-3@@ -227,7 +227,8 @@ static int clear_stash(int argc, const char **argv, const char *prefix)return do_clear_stash (); } -static int reset_tree (struct object_id *i_tree,int update,int reset) +static int reset_tree (struct object_id *i_tree,int update, +enum unpack_trees_reset_type reset) {int nr_trees =1 ;struct unpack_trees_options opts;@@ -461,7 +462,7 @@ static int do_apply_stash(const char *prefix, struct stash_info *info, }if (has_index) { -if (reset_tree (&index_tree,0 ,0 )) +if (reset_tree (&index_tree,0 ,UNPACK_NO_RESET ))return -1 ; }else {struct strbuf out =STRBUF_INIT ;@@ -471,7 +472,7 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,return -1 ; } -if (reset_tree (&c_tree,0 ,1 )) { +if (reset_tree (&c_tree,0 ,UNPACK_RESET_OVERWRITE_UNTRACKED )) {strbuf_release (&out);return -1 ; }t/lib-read-tree.sh +11
-0@@ -39,3 +39,14 @@ read_tree_u_must_fail () { test_cmp pre-dry-run-wt post-dry-run-wt && test_must_fail git read-tree "$@" } + +read_tree_u_must_fail_save_err () { + git ls-files -s >pre-dry-run && + git diff-files -p >pre-dry-run-wt && + test_must_fail git read-tree -n "$@" && + git ls-files -s >post-dry-run && + git diff-files -p >post-dry-run-wt && + test_cmp pre-dry-run post-dry-run && + test_cmp pre-dry-run-wt post-dry-run-wt && + test_must_fail git read-tree "$@" 2>actual-err +}t/t1005-read-tree-reset.sh +35
-2@@ -19,15 +19,48 @@ test_expect_success 'setup' ' git add df && echo content >new && git add new && - git commit -m two + git commit -m two && + git ls-files >expect-two ' -test_expect_success 'reset should work' ' +test_expect_success '--protect-untracked option sanity checks' ' + read_tree_u_must_fail --reset --protect-untracked HEAD && + read_tree_u_must_fail --reset --no-protect-untracked HEAD && + read_tree_u_must_fail -m -u --protect-untracked HEAD && + read_tree_u_must_fail -m -u --no-protect-untracked +' + +test_expect_success 'reset should reset worktree' ' + echo changed >df && read_tree_u_must_succeed -u --reset HEAD^ && git ls-files >actual && test_cmp expect actual ' +test_expect_success 'reset --protect-untracked protects untracked file' ' + echo changed >new && + read_tree_u_must_fail_save_err -u --reset --protect-untracked HEAD && + echo "error: Untracked working tree file '\'new\'' would be overwritten by merge." >expected-err && + test_cmp expected-err actual-err +' + +test_expect_success 'reset --protect-untracked protects untracked directory' ' + rm new && + mkdir new && + echo untracked >new/untracked && + read_tree_u_must_fail_save_err -u --reset --protect-untracked HEAD && + echo "error: Updating '\'new\'' would lose untracked files in it" >expected-err && + test_cmp expected-err actual-err +' + +test_expect_success 'reset --protect-untracked resets' ' + rm -rf new && + echo changed >df/file && + read_tree_u_must_succeed -u --reset --protect-untracked HEAD && + git ls-files >actual-two && + test_cmp expect-two actual-two +' + test_expect_success 'reset should remove remnants from a failed merge' ' read_tree_u_must_succeed --reset -u HEAD && git ls-files -s >expect &&unpack-trees.c +2
-1@@ -1917,7 +1917,8 @@ static int verify_absent_1(const struct cache_entry *ce,int len;struct stat st; -if (o->index_only || o->reset || !o->update) +if (o->index_only || o->reset ==UNPACK_RESET_OVERWRITE_UNTRACKED || + !o->update)return 0 ; len =check_leading_path (ce->name,ce_namelen (ce));unpack-trees.h +8
-3@@ -41,9 +41,15 @@ void setup_unpack_trees_porcelain(struct unpack_trees_options *opts,*/ void clear_unpack_trees_porcelain (struct unpack_trees_options *opts); +enum unpack_trees_reset_type { +UNPACK_NO_RESET =0 , +UNPACK_RESET_OVERWRITE_UNTRACKED , +UNPACK_RESET_PROTECT_UNTRACKED +}; +struct unpack_trees_options { -unsigned int reset, - merge, +enum unpack_trees_reset_type reset; +unsigned int merge, update, clone, index_only, --2.21.0
PATCH 2/2 read-tree: add --exclude-standard
In reply to Phillip WoodFrom: Phillip Wood <[email protected]>
Currently there is no way to get read-tree to respect .git/info/exclude or core.excludesFile so scripts using read-tree -u have subtly different behavior to porcelain commands like checkout even when they use --exclude-per-directory. This new option is copied from ls-tree's --exclude-standard option to setup the standard excludes. The new option is also used to fix a known submodule test failure.
Note that KNOWN_FAILURE_SUBMODULE_OVERWRITE_IGNORED_UNTRACKED is still used by t7112-reset-submodule.sh as it is not removed (apparently reset does not call setup_standard_excludes()).
--- Documentation/git-read-tree.txt | 9 +++++- builtin/read-tree.c | 55 ++++++++++++++++++++++++++++++--- t/t1005-read-tree-reset.sh | 36 ++++++++++++++++++--- t/t1013-read-tree-submodule.sh | 3 +- 4 files changed, 90 insertions(+), 13 deletions(-)
Documentation/git-read-tree.txt +8 -1
@@ -107,7 +107,14 @@ OPTIONS running `make clean` to remove the generated file. This option tells the command to read per-directory exclude file (usually '.gitignore') and allows such an untracked - but explicitly ignored file to be overwritten. + but explicitly ignored file to be overwritten. Incompatible + with `--exclude-standard`. + +--exclude-standard:: + When updating the worktree use the standard Git exclusions: + .git/info/exclude, .gitignore in each directory, and the user's global + exclusion file when deciding if it is safe to overwrite a file. + Incompatible with `--exclude-per-directory`. --index-output=<file>:: Instead of writing the results out to `$GIT_INDEX_FILE`,
builtin/read-tree.c +50 -5
84 lines not on this page · raw message
t/t1005-read-tree-reset.sh +31 -5
@@ -30,6 +30,20 @@ test_expect_success '--protect-untracked option sanity checks' ' read_tree_u_must_fail -m -u --no-protect-untracked ' +test_expect_success 'exclude option sanity checks' ' + read_tree_u_must_fail --reset -u --exclude-standard HEAD && + read_tree_u_must_fail --reset --protect-untracked --exclude-standard && + read_tree_u_must_fail --reset -u --protect-untracked \ + --exclude-standard \ + --exclude-per-directory=.gitignore HEAD && + read_tree_u_must_fail --reset -u --protect-untracked \ + --exclude-per-directory=gitignore \ + --exclude-per-directory=.gitignore HEAD && + read_tree_u_must_fail --reset --exclude-per-directory=.gitignore HEAD && + read_tree_u_must_succeed --reset -u --exclude-per-directory=.gitignore \ + HEAD +' + test_expect_success 'reset should reset worktree' ' echo changed >df && read_tree_u_must_succeed -u --reset HEAD^ &&
@@ -53,12 +67,24 @@ test_expect_success 'reset --protect-untracked protects untracked directory' ' test_cmp expected-err actual-err ' -test_expect_success 'reset --protect-untracked resets' ' - rm -rf new && +test_expect_success 'reset --protect-untracked --exclude-standard overwrites ignored path' ' + test_when_finished "rm .git/info/exclude" && + echo missing >.git/info/exclude && + read_tree_u_must_fail -u --reset --protect-untracked \ + --exclude-standard HEAD && + echo new >.git/info/exclude && echo changed >df/file && - read_tree_u_must_succeed -u --reset --protect-untracked HEAD && - git ls-files >actual-two && - test_cmp expect-two actual-two + read_tree_u_must_succeed -u --reset --protect-untracked \ + --exclude-standard HEAD && + git ls-files >actual && + test_cmp expect-two actual +' + +test_expect_success 'reset --protect-untracked resets' ' + echo changed >df && + read_tree_u_must_succeed -u --reset --protect-untracked HEAD^ && + git ls-files >actual && + test_cmp expect actual ' test_expect_success 'reset should remove remnants from a failed merge' '
t/t1013-read-tree-submodule.sh +1 -2
@@ -6,9 +6,8 @@ test_description='read-tree can handle submodules' . "$TEST_DIRECTORY"/lib-submodule-update.sh KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS=1 -KNOWN_FAILURE_SUBMODULE_OVERWRITE_IGNORED_UNTRACKED=1 -test_submodule_switch_recursing_with_args "read-tree -u -m" +test_submodule_switch_recursing_with_args "read-tree -u -m --exclude-standard" test_submodule_forced_switch_recursing_with_args "read-tree -u --reset"
Signature · 1 line
2.21.0
Quoted 11 lines · Phillip Wood
From: Phillip Wood <[email protected]>
These two patches teach read-tree how to avoid overwriting untracked files when doing '--reset -u' and also how to respect all of git's standard excludes files. I'd like to see the porcelain commands stop overwriting untracked files, this is a first step on the way. I'm not sure if we want to add options to the porcelain commands to protect untracked files or just change their behavior and add an option to override that. I'm leaning towards the latter but I'd be interested to hear what others think.
For new commands like git-restore, it's definitely a good thing to not overwrite untracked files. For existing commands I guess we have to go over them one by one. For "git reset --hard", it should really just overwrite whatever needed to get back to the known good state. "git checkout -f" , not so sure (seems weird that we need force-level-two option to override the protection provided by -f, if we change default behavior)
Signature · 1 line
Duy
Quoted 13 lines · Duy Nguyen
Quoted 11 lines · Phillip Wood
From: Phillip Wood <[email protected]>
These two patches teach read-tree how to avoid overwriting untracked files when doing '--reset -u' and also how to respect all of git's standard excludes files. I'd like to see the porcelain commands stop overwriting untracked files, this is a first step on the way. I'm not sure if we want to add options to the porcelain commands to protect untracked files or just change their behavior and add an option to override that. I'm leaning towards the latter but I'd be interested to hear what others think.
Quoted 2 lines
For new commands like git-restore, it's definitely a good thing to not overwrite untracked files.
I agree, unfortunately this series does not help with git-restore, only git-switch. For restore on an index without conflicts I think it could just use the pathspec in struct unpack_trees_options and set opts.rest = UNPACK_RESET_PROTECT_UNTRACKED but that does not help if we want to handle conflicted paths differently to non-conflicted paths.
Quoted 6 lines
For existing commands I guess we have to go
over them one by one. For "git reset --hard", it should really just overwrite whatever needed to get back to the known good state. "git checkout -f" , not so sure (seems weird that we need force-level-two option to override the protection provided by -f, if we change default behavior)
I think it's fine for "checkout -f" to overwrite untracked files (and if "switch --discard-changes" does not then there is no pressing need to add such a mode to checkout), --force is a good name for an option that nukes everything that gets in it's way. For "reset --hard" I'm not so sure, if I have changes to an untracked file I don't wont them overwritten by default. There is no porcelain equivalent to "read-tree --reset --protect-untracked -u" and I was hoping "reset --hard" may become that porcelain equivalent with a new --force or --overwrite-untracked option.
For the various "foo --abort" some (most?) are using "reset --merge" which I think declines to overwrite untracked files but rebase uses "reset --hard" which I'd like to change to protect untracked files in the same way that rebase does for the initial checkout and when picking commits. I haven't thought about stash.
Best Wishes
Phillip
Quoted 17 lines · Phillip Wood
Quoted 13 lines · Duy Nguyen
Quoted 11 lines · Phillip Wood
From: Phillip Wood <[email protected]>
These two patches teach read-tree how to avoid overwriting untracked files when doing '--reset -u' and also how to respect all of git's standard excludes files. I'd like to see the porcelain commands stop overwriting untracked files, this is a first step on the way. I'm not sure if we want to add options to the porcelain commands to protect untracked files or just change their behavior and add an option to override that. I'm leaning towards the latter but I'd be interested to hear what others think.
Quoted 8 lines
Quoted 2 lines
For new commands like git-restore, it's definitely a good thing to not overwrite untracked files.
I agree, unfortunately this series does not help with git-restore, only git-switch. For restore on an index without conflicts I think it could just use the pathspec in struct
unpack_trees_optionsand set opts.rest =UNPACK_RESET_PROTECT_UNTRACKEDbut that does not help if we want to handle conflicted paths differently to non-conflicted paths.
Right. I got confused. You did mention "git checkout <rev> :/" in 1/2, which is the same as "git restore --source <rev> --staged --worktree :/" and can also potentially overwrite untracked files, even though it does not use unpack-trees and cannot be fixed with this. Never mind. Let's leave git-restore out of the discussion for now.
Quoted 7 lines
Quoted 6 lines
For existing commands I guess we have to go
over them one by one. For "git reset --hard", it should really just overwrite whatever needed to get back to the known good state. "git checkout -f" , not so sure (seems weird that we need force-level-two option to override the protection provided by -f, if we change default behavior)
Quoted 9 lines
I think it's fine for "checkout -f" to overwrite untracked files (and if "switch --discard-changes" does not then there is no pressing need to add such a mode to checkout), --force is a good name for an option that nukes everything that gets in it's way. For "reset --hard" I'm not so sure, if I have changes to an untracked file I don't wont them overwritten by default. There is no porcelain equivalent to "read-tree --reset --protect-untracked -u" and I was hoping "reset --hard" may become that porcelain equivalent with a new --force or --overwrite-untracked option.
My (biased, obviously) view is that "git reset --hard" is very dangerous and I'm not trying to change that, especially when its behavior has been like this since forever and I'm sure it's used in scripts.
Instead "git restore" should be used when you need "git reset --hard HEAD", the most often use case. And since it's new, changing default behavior is not a problem. Which brings us back to git-restore :)
But either way, git-restore or git-reset, I still don't see why untracked files are more valuable in this case than tracked ones to change the default. I can see that sometimes you may want to restore just tracked files, or untracked files, almost like filtering with pathspec.
Quoted 5 lines
For the various "foo --abort" some (most?) are using "reset --merge" which I think declines to overwrite untracked files but rebase uses "reset --hard" which I'd like to change to protect untracked files in the same way that rebase does for the initial checkout and when picking commits. I haven't thought about stash.
Yeah it looks like cherry-pick and revert use "reset --merge" too (reset_for_rollback function). That's all of them. Probably a stupid question, why can't rebase just use "rebase --merge" like everybody else?
Quoted 5 lines
Best Wishes
Phillip
Signature · 1 line
Duy
Quoted 47 lines · Duy Nguyen
Quoted 14 lines · Phillip Wood
Quoted 13 lines · Duy Nguyen
Quoted 11 lines · Phillip Wood
From: Phillip Wood <[email protected]>
These two patches teach read-tree how to avoid overwriting untracked files when doing '--reset -u' and also how to respect all of git's standard excludes files. I'd like to see the porcelain commands stop overwriting untracked files, this is a first step on the way. I'm not sure if we want to add options to the porcelain commands to protect untracked files or just change their behavior and add an option to override that. I'm leaning towards the latter but I'd be interested to hear what others think.
Quoted 8 lines
Quoted 2 lines
For new commands like git-restore, it's definitely a good thing to not overwrite untracked files.
I agree, unfortunately this series does not help with git-restore, only git-switch. For restore on an index without conflicts I think it could just use the pathspec in struct
unpack_trees_optionsand set opts.rest =UNPACK_RESET_PROTECT_UNTRACKEDbut that does not help if we want to handle conflicted paths differently to non-conflicted paths.Right. I got confused. You did mention "git checkout <rev> :/" in 1/2, which is the same as "git restore --source <rev> --staged --worktree :/" and can also potentially overwrite untracked files, even though it does not use unpack-trees and cannot be fixed with this. Never mind. Let's leave git-restore out of the discussion for now.
Quoted 7 lines
Quoted 6 lines
For existing commands I guess we have to go
over them one by one. For "git reset --hard", it should really just overwrite whatever needed to get back to the known good state. "git checkout -f" , not so sure (seems weird that we need force-level-two option to override the protection provided by -f, if we change default behavior)Quoted 9 lines
I think it's fine for "checkout -f" to overwrite untracked files (and if "switch --discard-changes" does not then there is no pressing need to add such a mode to checkout), --force is a good name for an option that nukes everything that gets in it's way. For "reset --hard" I'm not so sure, if I have changes to an untracked file I don't wont them overwritten by default. There is no porcelain equivalent to "read-tree --reset --protect-untracked -u" and I was hoping "reset --hard" may become that porcelain equivalent with a new --force or --overwrite-untracked option.
Quoted 8 lines
My (biased, obviously) view is that "git reset --hard" is very dangerous and I'm not trying to change that, especially when its behavior has been like this since forever and I'm sure it's used in scripts.
Instead "git restore" should be used when you need "git reset --hard HEAD", the most often use case. And since it's new, changing default behavior is not a problem. Which brings us back to git-restore :)
Does restore clean up the branch state like reset? It's tricky because you only want to do that if there is no pathspec (or the pathspec is :/ or equivalent - I can't remember if restore always requires paths or not)
Quoted 3 lines
But either way, git-restore or git-reset, I still don't see why untracked files are more valuable in this case than tracked ones to change the default.
My issue is that is easy to see what changes you're going to lose in tracked files by running diff. For untracked files diff just says a new file will be created, it ignores the current contents as the path is in the index so it is easy to overwrite changes without realizing. There's also a philosophical point that git should not be stomping on paths that it is not tracking though that's a bit moot if a path is tracked in one revision but not another.
Quoted 10 lines
I can see that sometimes you may want to restore
just tracked files, or untracked files, almost like filtering with pathspec.Quoted 5 lines
For the various "foo --abort" some (most?) are using "reset --merge" which I think declines to overwrite untracked files but rebase uses "reset --hard" which I'd like to change to protect untracked files in the same way that rebase does for the initial checkout and when picking commits. I haven't thought about stash.
Quoted 4 lines
Yeah it looks like cherry-pick and revert use "reset --merge" too (
reset_for_rollbackfunction). That's all of them. Probably a stupid question, why can't rebase just use "rebase --merge" like everybody else?
I'm not sure - if --merge works for the others I can't see why it shouldn't work for rebase as well.
Best Wishes
Phillip
Quoted 6 lines
Quoted 4 lines
Best Wishes
Phillip
Quoted 5 lines · Phillip Wood
Quoted 5 lines
My (biased, obviously) view is that "git reset --hard" is very dangerous and I'm not trying to change that, especially when its behavior has been like this since forever and I'm sure it's used in scripts.
Quoted 7 lines
Quoted 3 lines
Instead "git restore" should be used when you need "git reset --hard HEAD", the most often use case. And since it's new, changing default behavior is not a problem. Which brings us back to git-restore :)
Does restore clean up the branch state like reset? It's tricky because you only want to do that if there is no pathspec (or the pathspec is :/ or equivalent - I can't remember if restore always requires paths or not)
Nope. git-restore cares about files, not branches. Yes git-restore always requires paths, just in case people type "git restore" and expect to see help usage or something.
Quoted 11 lines
Quoted 3 lines
But either way, git-restore or git-reset, I still don't see why untracked files are more valuable in this case than tracked ones to change the default.
My issue is that is easy to see what changes you're going to lose in tracked files by running diff. For untracked files diff just says a new file will be created, it ignores the current contents as the path is in the index so it is easy to overwrite changes without realizing. There's also a philosophical point that git should not be stomping on paths that it is not tracking though that's a bit moot if a path is tracked in one revision but not another.
Ah good point about diff. If only we had "git reset --dry-run" (that shows the diff, including untracked files; or perhaps --diff would be a better name for that imaginary option)
Signature · 1 line
Duy