nd/worktree-move update
cover nd/worktree-move update
This squashes two changes from Johannes and Ramsay:
builtin/worktree.c +1 -1
@@ -528,7 +528,7 @@ static int unlock_worktree(int ac, const char **av, const char *prefix)static void validate_no_submodules (const struct worktree *wt) { -struct index_state istate = {0 }; +struct index_state istate = {NULL };int i, found_submodules =0 ;if (read_index_from (&istate,worktree_git_path (wt,"index" )) >0 ) {
t/t2028-worktree-move.sh +4 -3
@@ -71,13 +71,14 @@ test_expect_success 'move locked worktree' ' ' test_expect_success 'move worktree' ' + toplevel="$(pwd)" && git worktree move source destination && test_path_is_missing source && git worktree list --porcelain | grep "^worktree" >actual && cat <<-EOF >expected && - worktree $TRASH_DIRECTORY - worktree $TRASH_DIRECTORY/destination - worktree $TRASH_DIRECTORY/elsewhere + worktree $toplevel + worktree $toplevel/destination + worktree $toplevel/elsewhere EOF test_cmp expected actual && git -C destination log --format=%s >actual2 &&
Nguyễn Thái Ngọc Duy (11):
worktree.c: zero new 'struct worktree' on allocation
worktree: reorder an if statement
get_worktrees() must return main worktree as first item even on error worktree.c: get_worktrees() takes a new flag argument worktree list: keep the list sorted
worktree.c: add validate_worktree()
worktree.c: add update_worktree_location()
worktree move: new command
worktree move: accept destination as directory
worktree move: refuse to move worktrees with submodules worktree remove: new command
Documentation/git-worktree.txt | 28 ++++-- branch.c | 2 +- builtin/branch.c | 2 +- builtin/worktree.c | 176 +++++++++++++++++++++++++++++++-- contrib/completion/git-completion.bash | 5 +- t/t2027-worktree-list.sh | 40 ++++++++ t/t2028-worktree-move.sh | 57 +++++++++++ worktree.c | 126 +++++++++++++++++++---- worktree.h | 15 ++- 9 files changed, 410 insertions(+), 41 deletions(-)
Signature · 1 line
2.11.0.157.gd943d85
PATCH 1/11 worktree.c: zero new 'struct worktree' on allocation
This keeps things a bit simpler when we add more fields, knowing that default values are always zero.
--- worktree.c | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-)
worktree.c +2 -12
@@ -91,16 +91,11 @@ static struct worktree *get_main_worktree(void)if (parse_ref (path.buf, &head_ref, &is_detached) <0 ) goto done; - worktree =xmalloc (sizeof (struct worktree )); + worktree =xcalloc (1 ,sizeof (*worktree)); worktree->path =strbuf_detach (&worktree_path,NULL ); - worktree->id =NULL ; worktree->is_bare = is_bare; - worktree->head_ref =NULL ; worktree->is_detached = is_detached; - worktree->is_current =0 ;add_head_info (&head_ref, worktree); - worktree->lock_reason =NULL ; - worktree->lock_reason_valid =0 ; done:strbuf_release (&path);
@@ -138,16 +133,11 @@ static struct worktree *get_linked_worktree(const char *id)if (parse_ref (path.buf, &head_ref, &is_detached) <0 ) goto done; - worktree =xmalloc (sizeof (struct worktree )); + worktree =xcalloc (1 ,sizeof (*worktree)); worktree->path =strbuf_detach (&worktree_path,NULL ); worktree->id =xstrdup (id); - worktree->is_bare =0 ; - worktree->head_ref =NULL ; worktree->is_detached = is_detached; - worktree->is_current =0 ;add_head_info (&head_ref, worktree); - worktree->lock_reason =NULL ; - worktree->lock_reason_valid =0 ; done:strbuf_release (&path);
Signature · 1 line
2.11.0.157.gd943d85
PATCH 2/11 worktree: reorder an if statement
In reply to Nguyễn Thái Ngọc DuyThis is no-op. But it helps reduce diff noise in the next patch.
--- builtin/worktree.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
builtin/worktree.c +3 -3
@@ -406,10 +406,10 @@ static void show_worktree(struct worktree *wt, int path_maxlen, int abbrev_len)else {strbuf_addf (&sb,"%-*s " , abbrev_len,find_unique_abbrev (wt->head_sha1,DEFAULT_ABBREV )); -if (!wt->is_detached) -strbuf_addf (&sb,"[%s]" ,shorten_unambiguous_ref (wt->head_ref,0 )); -else +if (wt->is_detached)strbuf_addstr (&sb,"(detached HEAD)" ); +else +strbuf_addf (&sb,"[%s]" ,shorten_unambiguous_ref (wt->head_ref,0 )); }printf ("%s\n" , sb.buf);
Signature · 1 line
2.11.0.157.gd943d85
PATCH 3/11 get_worktrees() must return main worktree as first item even on error
In reply to Nguyễn Thái Ngọc DuyThis is required by git-worktree.txt, stating that the main worktree is the first line (especially in --porcelain mode when we can't just change behavior at will).
There's only one case when get_worktrees() may skip main worktree, when parse_ref() fails. Update the code so that we keep first item as main worktree and return something sensible in this case:
- In user-friendly mode, since we're not constraint by anything, returning "(error)" should do the job (we already show "(detached HEAD)" which is not machine-friendly). Actually errors should be printed on stderr by parse_ref() (*)
- In plumbing mode, we do not show neither 'bare', 'detached' or 'branch ...', which is possible by the format description if I read it right.
Careful readers may realize that when the local variable "head_ref" in get_main_worktree() is emptied, add_head_info() will do nothing to wt->head_sha1. But that's ok because head_sha1 is zero-ized in the previous patch.
(*) Well, it does not. But it's supposed to be a stop gap implementation
until we can reuse refs code to parse "ref: " stuff in HEAD, from resolve_refs_unsafe(). Now may be the time since refs refactoring is mostly done.
--- builtin/worktree.c | 6 ++++-- t/t2027-worktree-list.sh | 21 +++++++++++++++++++++ worktree.c | 10 +++------- 3 files changed, 28 insertions(+), 9 deletions(-)
builtin/worktree.c +4 -2
@@ -388,7 +388,7 @@ static void show_worktree_porcelain(struct worktree *wt)printf ("HEAD %s\n" ,sha1_to_hex (wt->head_sha1));if (wt->is_detached)printf ("detached\n" ); -else +else if (wt->head_ref)printf ("branch %s\n" , wt->head_ref); }printf ("\n" );
@@ -408,8 +408,10 @@ static void show_worktree(struct worktree *wt, int path_maxlen, int abbrev_len) find_unique_abbrev(wt->head_sha1, DEFAULT_ABBREV));if (wt->is_detached)strbuf_addstr (&sb,"(detached HEAD)" ); -else +else if (wt->head_ref)strbuf_addf (&sb,"[%s]" ,shorten_unambiguous_ref (wt->head_ref,0 )); +else +strbuf_addstr (&sb,"(error)" ); }printf ("%s\n" , sb.buf);
t/t2027-worktree-list.sh +21 -0
@@ -96,4 +96,25 @@ test_expect_success 'bare repo cleanup' ' rm -rf bare1 ' +test_expect_success 'broken main worktree still at the top' ' + git init broken-main && + ( + cd broken-main && + test_commit new && + git worktree add linked && + cat >expected <<-EOF && + worktree $(pwd) + HEAD $_z40 + + EOF + cd linked && + echo "worktree $(pwd)" >expected && + echo "ref: .broken" >../.git/HEAD && + git worktree list --porcelain | head -n 3 >actual && + test_cmp ../expected actual && + git worktree list | head -n 1 >actual.2 && + grep -F "(error)" actual.2 + ) +' + test_done
worktree.c +3 -7
@@ -88,16 +88,13 @@ static struct worktree *get_main_worktree(void)strbuf_addf (&path,"%s/HEAD" ,get_git_common_dir ()); -if (parse_ref (path.buf, &head_ref, &is_detached) <0 ) - goto done; - worktree =xcalloc (1 ,sizeof (*worktree)); worktree->path =strbuf_detach (&worktree_path,NULL ); worktree->is_bare = is_bare; worktree->is_detached = is_detached; -add_head_info (&head_ref, worktree); +if (!parse_ref (path.buf, &head_ref, &is_detached)) +add_head_info (&head_ref, worktree); -done:strbuf_release (&path);strbuf_release (&worktree_path);strbuf_release (&head_ref);
@@ -173,8 +170,7 @@ struct worktree **get_worktrees(void) list =xmalloc (alloc *sizeof (struct worktree *)); -if ((list[counter] =get_main_worktree ())) - counter++; + list[counter++] =get_main_worktree ();strbuf_addf (&path,"%s/worktrees" ,get_git_common_dir ()); dir =opendir (path.buf);
Signature · 1 line
2.11.0.157.gd943d85
PATCH 5/11 worktree list: keep the list sorted
In reply to Nguyễn Thái Ngọc DuyIt makes it easier to write tests for. But it should also be good for the user since locating a worktree by eye would be easier once they notice this.
--- builtin/worktree.c | 2 +- t/t2027-worktree-list.sh | 19 +++++++++++++++++++ worktree.c | 14 ++++++++++++++ worktree.h | 2 ++ 4 files changed, 36 insertions(+), 1 deletion(-)
builtin/worktree.c +1 -1
@@ -447,7 +447,7 @@ static int list(int ac, const char **av, const char *prefix)if (ac)usage_with_options (worktree_usage, options);else { -struct worktree **worktrees =get_worktrees (0 ); +struct worktree **worktrees =get_worktrees (GWT_SORT_LINKED );int path_maxlen =0 , abbrev =DEFAULT_ABBREV , i;if (!porcelain)
t/t2027-worktree-list.sh +19 -0
@@ -117,4 +117,23 @@ test_expect_success 'broken main worktree still at the top' ' ) ' +test_expect_success 'linked worktrees are sorted' ' + mkdir sorted && + git init sorted/main && + ( + cd sorted/main && + test_tick && + test_commit new && + git worktree add ../first && + git worktree add ../second && + git worktree list --porcelain | grep ^worktree >actual + ) && + cat >expected <<-EOF && + worktree $(pwd)/sorted/main + worktree $(pwd)/sorted/first + worktree $(pwd)/sorted/second + EOF + test_cmp expected sorted/main/actual +' + test_done
worktree.c +14 -0
@@ -160,6 +160,13 @@ static void mark_current_worktree(struct worktree **worktrees)free (git_dir); } +static int compare_worktree (const void *a_,const void *b_) +{ +const struct worktree *const *a = a_; +const struct worktree *const *b = b_; +return fspathcmp ((*a)->path, (*b)->path); +} +struct worktree **get_worktrees (unsigned flags) {struct worktree **list =NULL ;
@@ -191,6 +198,13 @@ struct worktree **get_worktrees(unsigned flags)ALLOC_GROW (list, counter +1 , alloc); list[counter] =NULL ; +if (flags &GWT_SORT_LINKED ) +/* +* don't sort the first item (main worktree), which will +* always be the first +*/ +QSORT (list +1 , counter -1 , compare_worktree); +mark_current_worktree (list);return list; }
worktree.h +2 -0
@@ -15,6 +15,8 @@ struct worktree {/* Functions for acting on the information about worktrees. */ +#define GWT_SORT_LINKED (1 << 0)/* keeps linked worktrees sorted */ +/* * Get the worktrees. The primary worktree will always be the first returned, * and linked worktrees will be pointed to by 'next' in each subsequent
Signature · 1 line
2.11.0.157.gd943d85
PATCH 4/11 worktree.c: get_worktrees() takes a new flag argument
In reply to Nguyễn Thái Ngọc DuyThis is another no-op patch, in preparation for get_worktrees() to do optional things, like sorting.
--- branch.c | 2 +- builtin/branch.c | 2 +- builtin/worktree.c | 6 +++--- worktree.c | 4 ++-- worktree.h | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-)
branch.c +1 -1
@@ -348,7 +348,7 @@ void die_if_checked_out(const char *branch, int ignore_current_worktree)int replace_each_worktree_head_symref (const char *oldref,const char *newref) {int ret =0 ; -struct worktree **worktrees =get_worktrees (); +struct worktree **worktrees =get_worktrees (0 );int i;for (i =0 ; worktrees[i]; i++) {
builtin/branch.c +1 -1
@@ -531,7 +531,7 @@ static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sortinstatic void reject_rebase_or_bisect_branch (const char *target) { -struct worktree **worktrees =get_worktrees (); +struct worktree **worktrees =get_worktrees (0 );int i;for (i =0 ; worktrees[i]; i++) {
builtin/worktree.c +3 -3
@@ -447,7 +447,7 @@ static int list(int ac, const char **av, const char *prefix)if (ac)usage_with_options (worktree_usage, options);else { -struct worktree **worktrees =get_worktrees (); +struct worktree **worktrees =get_worktrees (0 );int path_maxlen =0 , abbrev =DEFAULT_ABBREV , i;if (!porcelain)
@@ -478,7 +478,7 @@ static int lock_worktree(int ac, const char **av, const char *prefix)if (ac !=1 )usage_with_options (worktree_usage, options); - worktrees =get_worktrees (); + worktrees =get_worktrees (0 ); wt =find_worktree (worktrees, prefix, av[0 ]);if (!wt)die (_ ("'%s' is not a working tree" ), av[0 ]);
@@ -511,7 +511,7 @@ static int unlock_worktree(int ac, const char **av, const char *prefix)if (ac !=1 )usage_with_options (worktree_usage, options); - worktrees =get_worktrees (); + worktrees =get_worktrees (0 ); wt =find_worktree (worktrees, prefix, av[0 ]);if (!wt)die (_ ("'%s' is not a working tree" ), av[0 ]);
worktree.c +2 -2
@@ -160,7 +160,7 @@ static void mark_current_worktree(struct worktree **worktrees)free (git_dir); } -struct worktree **get_worktrees (void ) +struct worktree **get_worktrees (unsigned flags) {struct worktree **list =NULL ;struct strbuf path =STRBUF_INIT ;
@@ -327,7 +327,7 @@ const struct worktree *find_shared_symref(const char *symref,if (worktrees)free_worktrees (worktrees); - worktrees =get_worktrees (); + worktrees =get_worktrees (0 );for (i =0 ; worktrees[i]; i++) {struct worktree *wt = worktrees[i];
worktree.h +1 -1
@@ -23,7 +23,7 @@ struct worktree {* The caller is responsible for freeing the memory from the returned * worktree(s). */ -extern struct worktree **get_worktrees (void ); +extern struct worktree **get_worktrees (unsigned flags);/* * Return git dir of the worktree. Note that the path may be relative.
Signature · 1 line
2.11.0.157.gd943d85
PATCH 6/11 worktree.c: add validate_worktree()
In reply to Nguyễn Thái Ngọc DuyThis function is later used by "worktree move" and "worktree remove" to ensure that we have a good connection between the repository and the worktree. For example, if a worktree is moved manually, the worktree location recorded in $GIT_DIR/worktrees/.../gitdir is incorrect and we should not move that one.
--- worktree.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ worktree.h | 5 +++++ 2 files changed, 68 insertions(+)
worktree.c +63 -0
@@ -291,6 +291,69 @@ const char *is_worktree_locked(struct worktree *wt)return wt->lock_reason; } +static int report (int quiet,const char *fmt, ...) +{ +va_list params; + +if (quiet) +return -1 ; + +va_start (params, fmt); +vfprintf (stderr, fmt, params); +fputc ('\n' , stderr); +va_end (params); +return -1 ; +} + +int validate_worktree (const struct worktree *wt,int quiet) +{ +struct strbuf sb =STRBUF_INIT ; +const char *path; +int err; + +if (is_main_worktree (wt)) { +/* +* Main worktree using .git file to point to the +* repository would make it impossible to know where +* the actual worktree is if this function is executed +* from another worktree. No .git file support for now. +*/ +strbuf_addf (&sb,"%s/.git" , wt->path); +if (!is_directory (sb.buf)) { +strbuf_release (&sb); +return report (quiet,_ ("'%s/.git' at main worktree is not the repository directory" ), + wt->path); + } +return 0 ; + } + +/* +* Make sure "gitdir" file points to a real .git file and that +* file points back here. +*/ +if (!is_absolute_path (wt->path)) +return report (quiet,_ ("'%s' file does not contain absolute path to the worktree location" ), +git_common_path ("worktrees/%s/gitdir" , wt->id)); + +strbuf_addf (&sb,"%s/.git" , wt->path); +if (!file_exists (sb.buf)) { +strbuf_release (&sb); +return report (quiet,_ ("'%s/.git' does not exist" ), wt->path); + } + + path =read_gitfile_gently (sb.buf, &err); +strbuf_release (&sb); +if (!path) +return report (quiet,_ ("'%s/.git' is not a .git file, error code %d" ), + wt->path, err); + +if (fspathcmp (path,real_path (git_common_path ("worktrees/%s" , wt->id)))) +return report (quiet,_ ("'%s' does not point back to" ), + wt->path,git_common_path ("worktrees/%s" , wt->id)); + +return 0 ; +} +int is_worktree_being_rebased (const struct worktree *wt,const char *target) {
worktree.h +5 -0
@@ -53,6 +53,11 @@ extern int is_main_worktree(const struct worktree *wt);extern const char *is_worktree_locked (struct worktree *wt);/* +* Return zero if the worktree is in good condition. +*/ +extern int validate_worktree (const struct worktree *wt,int quiet); + +/* * Free up the memory for worktree(s) */ extern void free_worktrees (struct worktree **);
Signature · 1 line
2.11.0.157.gd943d85
PATCH 8/11 worktree move: new command
In reply to Nguyễn Thái Ngọc DuyThere are two options to move the main worktree, but both have complications, so it's not implemented yet. Anyway the options are:
- convert the main worktree to a linked one and move it away, leave the git repository where it is. The repo essentially becomes bare after this move.
- move the repository with the main worktree. The tricky part is make sure all file descriptors to the repository are closed, or it may fail on Windows.
--- Documentation/git-worktree.txt | 7 +++++- builtin/worktree.c | 43 ++++++++++++++++++++++++++++++++++ contrib/completion/git-completion.bash | 2 +- t/t2028-worktree-move.sh | 31 ++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 2 deletions(-)
Documentation/git-worktree.txt +6 -1
@@ -12,6 +12,7 @@ SYNOPSIS 'git worktree add' [-f] [--detach] [--checkout] [-b <new-branch>] <path> [<branch>] 'git worktree list' [--porcelain] 'git worktree lock' [--reason <string>] <worktree> +'git worktree move' <worktree> <new-path> 'git worktree prune' [-n] [-v] [--expire <expire>] 'git worktree unlock' <worktree>
@@ -71,6 +72,11 @@ files from being pruned automatically. This also prevents it from being moved or deleted. Optionally, specify a reason for the lock with `--reason`. +move:: + +Move a working tree to a new location. Note that the main working tree +cannot be moved yet. + prune:: Prune working tree information in $GIT_DIR/worktrees.
@@ -252,7 +258,6 @@ performed manually, such as: - `remove` to remove a linked working tree and its administrative files (and warn if the working tree is dirty) -- `mv` to move or rename a working tree and update its administrative files GIT ---
builtin/worktree.c +43 -0
60 lines not on this page · raw message
contrib/completion/git-completion.bash +1 -1
@@ -2715,7 +2715,7 @@ _git_whatchanged () _git_worktree () { - local subcommands="add list lock prune unlock" + local subcommands="add list lock move prune unlock" local subcommand="$(__git_find_on_cmdline "$subcommands")" if [ -z "$subcommand" ]; then __gitcomp "$subcommands"
t/t2028-worktree-move.sh +31 -0
@@ -59,4 +59,35 @@ test_expect_success 'unlock worktree twice' ' test_path_is_missing .git/worktrees/source/locked ' +test_expect_success 'move non-worktree' ' + mkdir abc && + test_must_fail git worktree move abc def +' + +test_expect_success 'move locked worktree' ' + git worktree lock source && + test_must_fail git worktree move source destination && + git worktree unlock source +' + +test_expect_success 'move worktree' ' + toplevel="$(pwd)" && + git worktree move source destination && + test_path_is_missing source && + git worktree list --porcelain | grep "^worktree" >actual && + cat <<-EOF >expected && + worktree $toplevel + worktree $toplevel/destination + worktree $toplevel/elsewhere + EOF + test_cmp expected actual && + git -C destination log --format=%s >actual2 && + echo init >expected2 && + test_cmp expected2 actual2 +' + +test_expect_success 'move main worktree' ' + test_must_fail git worktree move . def +' + test_done
Signature · 1 line
2.11.0.157.gd943d85
PATCH 7/11 worktree.c: add update_worktree_location()
In reply to Nguyễn Thái Ngọc Duy--- worktree.c | 21 +++++++++++++++++++++ worktree.h | 6 ++++++ 2 files changed, 27 insertions(+)
worktree.c +21 -0
@@ -354,6 +354,27 @@ int validate_worktree(const struct worktree *wt, int quiet)return 0 ; } +int update_worktree_location (struct worktree *wt,const char *path_) +{ +struct strbuf path =STRBUF_INIT ; +int ret =0 ; + +if (is_main_worktree (wt)) +return 0 ; + +strbuf_add_absolute_path (&path, path_); +if (fspathcmp (wt->path, path.buf)) { +write_file (git_common_path ("worktrees/%s/gitdir" , + wt->id), +"%s/.git" ,real_path (path.buf)); +free (wt->path); + wt->path =strbuf_detach (&path,NULL ); + ret =0 ; + } +strbuf_release (&path); +return ret; +} +int is_worktree_being_rebased (const struct worktree *wt,const char *target) {
worktree.h +6 -0
@@ -58,6 +58,12 @@ extern const char *is_worktree_locked(struct worktree *wt);extern int validate_worktree (const struct worktree *wt,int quiet);/* +* Update worktrees/xxx/gitdir with the new path. +*/ +extern int update_worktree_location (struct worktree *wt, +const char *path_); + +/* * Free up the memory for worktree(s) */ extern void free_worktrees (struct worktree **);
Signature · 1 line
2.11.0.157.gd943d85
PATCH 9/11 worktree move: accept destination as directory
In reply to Nguyễn Thái Ngọc DuySimilar to "mv a b/", which is actually "mv a b/a", we extract basename of source worktree and create a directory of the same name at destination if dst path is a directory.
--- builtin/worktree.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-)
builtin/worktree.c +18 -1
@@ -541,7 +541,13 @@ static int move_worktree(int ac, const char **av, const char *prefix)strbuf_addstr (&dst,prefix_filename (prefix,strlen (prefix), av[1 ])); -if (file_exists (dst.buf)) +if (is_directory (dst.buf)) +/* +* keep going, dst will be appended after we get the +* source's absolute path +*/ + ; +else if (file_exists (dst.buf))die (_ ("target '%s' already exists" ), av[1 ]); worktrees =get_worktrees (0 );
@@ -559,6 +565,17 @@ static int move_worktree(int ac, const char **av, const char *prefix)if (validate_worktree (wt,0 ))return -1 ; +if (is_directory (dst.buf)) { +const char *sep =find_last_dir_sep (wt->path); + +if (!sep) +die (_ ("could not figure out destination name from '%s'" ), + wt->path); +strbuf_addstr (&dst, sep); +if (file_exists (dst.buf)) +die (_ ("target '%s' already exists" ), dst.buf); + } +if (rename (wt->path, dst.buf) ==-1 )die_errno (_ ("failed to move '%s' to '%s'" ), wt->path, dst.buf);
Signature · 1 line
2.11.0.157.gd943d85
PATCH 11/11 worktree remove: new command
In reply to Nguyễn Thái Ngọc Duy--- Documentation/git-worktree.txt | 21 +++++---- builtin/worktree.c | 79 ++++++++++++++++++++++++++++++++++ contrib/completion/git-completion.bash | 5 ++- t/t2028-worktree-move.sh | 26 +++++++++++ 4 files changed, 121 insertions(+), 10 deletions(-)
Documentation/git-worktree.txt +12 -9
@@ -14,6 +14,7 @@ SYNOPSIS 'git worktree lock' [--reason <string>] <worktree> 'git worktree move' <worktree> <new-path> 'git worktree prune' [-n] [-v] [--expire <expire>] +'git worktree remove' [--force] <worktree> 'git worktree unlock' <worktree> DESCRIPTION
@@ -81,6 +82,13 @@ prune:: Prune working tree information in $GIT_DIR/worktrees. +remove:: + +Remove a working tree. Only clean working trees (no untracked files +and no modification in tracked files) can be removed. Unclean working +trees can be removed with `--force`. The main working tree cannot be +removed. + unlock:: Unlock a working tree, allowing it to be pruned, moved or deleted.
@@ -90,9 +98,10 @@ OPTIONS -f:: --force:: - By default, `add` refuses to create a new working tree when `<branch>` - is already checked out by another working tree. This option overrides - that safeguard. + By default, `add` refuses to create a new working tree when + `<branch>` is already checked out by another working tree and + `remove` refuses to remove an unclean working tree. This option + overrides that safeguard. -b <new-branch>:: -B <new-branch>::
@@ -253,12 +262,6 @@ Multiple checkout in general is still experimental, and the support for submodules is incomplete. It is NOT recommended to make multiple checkouts of a superproject. -git-worktree could provide more automation for tasks currently -performed manually, such as: - -- `remove` to remove a linked working tree and its administrative files (and - warn if the working tree is dirty) - GIT --- Part of the linkgit:git[1] suite
builtin/worktree.c +79 -0
96 lines not on this page · raw message
contrib/completion/git-completion.bash +4 -1
@@ -2715,7 +2715,7 @@ _git_whatchanged () _git_worktree () { - local subcommands="add list lock move prune unlock" + local subcommands="add list lock move prune remove unlock" local subcommand="$(__git_find_on_cmdline "$subcommands")" if [ -z "$subcommand" ]; then __gitcomp "$subcommands"
@@ -2733,6 +2733,9 @@ _git_worktree () prune,--*) __gitcomp "--dry-run --expire --verbose" ;; + remove,--*) + __gitcomp "--force" + ;; *) ;; esac
t/t2028-worktree-move.sh +26 -0
@@ -90,4 +90,30 @@ test_expect_success 'move main worktree' ' test_must_fail git worktree move . def ' +test_expect_success 'remove main worktree' ' + test_must_fail git worktree remove . +' + +test_expect_success 'remove locked worktree' ' + git worktree lock destination && + test_must_fail git worktree remove destination && + git worktree unlock destination +' + +test_expect_success 'remove worktree with dirty tracked file' ' + echo dirty >>destination/init.t && + test_must_fail git worktree remove destination +' + +test_expect_success 'remove worktree with untracked file' ' + git -C destination checkout init.t && + : >destination/untracked && + test_must_fail git worktree remove destination +' + +test_expect_success 'force remove worktree with untracked file' ' + git worktree remove --force destination && + test_path_is_missing destination +' + test_done
Signature · 1 line
2.11.0.157.gd943d85
PATCH 10/11 worktree move: refuse to move worktrees with submodules
In reply to Nguyễn Thái Ngọc DuySubmodules contains .git files with relative paths. After a worktree move, these files need to be updated or they may point to nowhere.
This is a bandage patch to make sure "worktree move" don't break people's worktrees by accident. When .git file update code is in place, this validate_no_submodules() could be removed.
--- builtin/worktree.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
builtin/worktree.c +23 -0
@@ -525,6 +525,27 @@ static int unlock_worktree(int ac, const char **av, const char *prefix)return ret; } +static void validate_no_submodules (const struct worktree *wt) +{ +struct index_state istate = {NULL }; +int i, found_submodules =0 ; + +if (read_index_from (&istate,worktree_git_path (wt,"index" )) >0 ) { +for (i =0 ; i < istate.cache_nr; i++) { +struct cache_entry *ce = istate.cache[i]; + +if (S_ISGITLINK (ce->ce_mode)) { + found_submodules =1 ; +break ; + } + } + } +discard_index (&istate); + +if (found_submodules) +die (_ ("This working tree contains submodules and cannot be moved yet" )); +} +static int move_worktree (int ac,const char **av,const char *prefix) {struct option options[] = {
@@ -565,6 +586,8 @@ static int move_worktree(int ac, const char **av, const char *prefix)if (validate_worktree (wt,0 ))return -1 ; +validate_no_submodules (wt); +if (is_directory (dst.buf)) {const char *sep =find_last_dir_sep (wt->path);
Signature · 1 line
2.11.0.157.gd943d85
Hi Duy,
Quoted 1 line · Nguyễn Thái Ngọc Duy
This squashes two changes from Johannes and Ramsay: [...]
Sorry, I lost track of the worktree discussions... Could you remind me which patch is supposed to fix my continuous reflog corruption?
Thanks,
Dscho
Quoted 8 lines · Johannes Schindelin
Hi Duy,
Quoted 1 line · Nguyễn Thái Ngọc Duy
This squashes two changes from Johannes and Ramsay: [...]
Sorry, I lost track of the worktree discussions... Could you remind me which patch is supposed to fix my continuous reflog corruption?
The corruption caused by git-gc? It's not fixed. All the changes in this series is shown here.
Signature · 1 line
Duy
Hi Duy,
Quoted 6 lines · Duy Nguyen
Quoted 4 lines · Johannes Schindelin
Hi Duy,
On Thu, 2 Feb 2017, Nguyễn Thái Ngọc Duy wrote:
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
This squashes two changes from Johannes and Ramsay: [...]
Sorry, I lost track of the worktree discussions... Could you remind me which patch is supposed to fix my continuous reflog corruption?
The corruption caused by git-gc? It's not fixed. All the changes in this series is shown here.
Oh sorry, I meant to ask "and if it is not in this patch series, would you mind pointing me at the patch series that has that fix?"
Thanks,
Johannes
Quoted 12 lines · Johannes Schindelin
Hi Duy,
Quoted 8 lines · Duy Nguyen
Quoted 6 lines · Johannes Schindelin
Hi Duy,
Quoted 1 line · Nguyễn Thái Ngọc Duy
This squashes two changes from Johannes and Ramsay: [...]
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Sorry, I lost track of the worktree discussions... Could you remind me which patch is supposed to fix my continuous reflog corruption?
The corruption caused by git-gc? It's not fixed. All the changes in this series is shown here.
Oh sorry, I meant to ask "and if it is not in this patch series, would you mind pointing me at the patch series that has that fix?"
You meant this one [1]? There is nothing substantial since then.
[1] https://public-inbox.org/git/%[email protected]%3E/
Signature · 1 line
Duy
Hi Duy,
Quoted 18 lines · Duy Nguyen
Quoted 16 lines · Johannes Schindelin
Quoted 12 lines · Duy Nguyen
Quoted 7 lines · Johannes Schindelin
Quoted 1 line · Nguyễn Thái Ngọc Duy
This squashes two changes from Johannes and Ramsay: [...]
Sorry, I lost track of the worktree discussions... Could you remind me which patch is supposed to fix my continuous reflog corruption?
The corruption caused by git-gc? It's not fixed. All the changes in this series is shown here.
Quoted 6 lines
Quoted 2 lines
Oh sorry, I meant to ask "and if it is not in this patch series, would you mind pointing me at the patch series that has that fix?"
You meant this one [1]? There is nothing substantial since then.
I guess I mean that.
Given that this results in real data loss, it is surprising that this has not made it even into pu yet!
Would you mind rebasing and re-submitting?
Thanks,
Johannes
Quoted 25 lines · Johannes Schindelin
Hi Duy,
Quoted 21 lines · Duy Nguyen
Quoted 18 lines · Johannes Schindelin
Quoted 5 lines · Duy Nguyen
Quoted 3 lines · Johannes Schindelin
On Thu, 2 Feb 2017, Nguyễn Thái Ngọc Duy wrote:
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
This squashes two changes from Johannes and Ramsay: [...]
Sorry, I lost track of the worktree discussions... Could you remind me which patch is supposed to fix my continuous reflog corruption?
The corruption caused by git-gc? It's not fixed. All the changes in this series is shown here.
Oh sorry, I meant to ask "and if it is not in this patch series, would you mind pointing me at the patch series that has that fix?"
Quoted 8 lines
Quoted 3 lines
You meant this one [1]? There is nothing substantial since then.
I guess I mean that.
Given that this results in real data loss, it is surprising that this has not made it even into
puyet!
I could rebase and clean it up a bit if you need it, but I don't think it'll end up in 'pu' or anywhere near since Junio wanted a cleaner approach [1]. That means (as far as I can see) a lot more work around refs store and backend area before it's ready to handle "get refs from this worktree store" (or "get refs from every reachable stores").
[1] https://public-inbox.org/git/[email protected]/
Signature · 1 line
Duy
Hi Duy,
Quoted 36 lines · Duy Nguyen
Quoted 25 lines · Johannes Schindelin
Quoted 22 lines · Duy Nguyen
Quoted 19 lines · Johannes Schindelin
Quoted 5 lines · Duy Nguyen
Quoted 3 lines · Johannes Schindelin
On Thu, 2 Feb 2017, Nguyễn Thái Ngọc Duy wrote:
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
This squashes two changes from Johannes and Ramsay: [...]
Sorry, I lost track of the worktree discussions... Could you remind me which patch is supposed to fix my continuous reflog corruption?
The corruption caused by git-gc? It's not fixed. All the changes in this series is shown here.
Oh sorry, I meant to ask "and if it is not in this patch series, would you mind pointing me at the patch series that has that fix?"
Quoted 8 lines
Quoted 3 lines
You meant this one [1]? There is nothing substantial since then.
I guess I mean that.
Given that this results in real data loss, it is surprising that this has not made it even into
puyet!
Quoted 7 lines
I could rebase and clean it up a bit if you need it, but I don't think it'll end up in 'pu' or anywhere near since Junio wanted a cleaner approach [1]. That means (as far as I can see) a lot more work around refs store and backend area before it's ready to handle "get refs from this worktree store" (or "get refs from every reachable stores").
That is a big, big bummer.
We are talking about a data corrupting bug here, yes? It should be possible to do that redesign work while having a small workaround in place that unbreaks, say, me?
Ciao,
Johannes
Hi Duy,
Quoted 21 lines · Johannes Schindelin
Hi Duy,
Quoted 17 lines · Duy Nguyen
Quoted 6 lines · Johannes Schindelin
Quoted 3 lines · Duy Nguyen
You meant this one [1]? There is nothing substantial since then.
I could rebase and clean it up a bit if you need it, but I don't think it'll end up in 'pu' or anywhere near since Junio wanted a cleaner approach [1]. That means (as far as I can see) a lot more work around refs store and backend area before it's ready to handle "get refs from this worktree store" (or "get refs from every reachable stores").
Given that
https://public-inbox.org/git/[email protected]/
seems to have expected something to happen within a reasonable time frame, and that 8 months is substantially longer than a reasonable time frame, I am not sure that that position can still be defended.
Also, the more important reply was Peff's reply that suggested that the proposed fix was incomplete, as it misses --unpack-unreachable:
https://public-inbox.org/git/[email protected]/
Ciao,
Johannes
Quoted 3 lines · Johannes Schindelin
Also, the more important reply was Peff's reply that suggested that the proposed fix was incomplete, as it misses --unpack-unreachable:
https://public-inbox.org/git/[email protected]/
While I think that --unpack-unreachable thing is a symptom of the basic approach of patching things up at the wrong level, if you are hinting that fix to the issue that gc does not pay attention to various anchoring point in other worktrees is more important than adding new commands to "worktree", I fully agree with that.
We have a basic structure of "worktree" working well enough to allow adventurous folks to experiment (there is a reason why we keep calling it experimental). mv and other additions are primarily to make things _easier_ to use, but we shouldn't be encouraging its use to general public by making it easier for them to hurt themselves, if we know there still are sharp edges.
Thanks for bringing it up.
Quoted 11 lines · Junio C Hamano
Quoted 3 lines · Johannes Schindelin
Also, the more important reply was Peff's reply that suggested that the proposed fix was incomplete, as it misses --unpack-unreachable:
https://public-inbox.org/git/[email protected]/While I think that --unpack-unreachable thing is a symptom of the basic approach of patching things up at the wrong level, if you are hinting that fix to the issue that gc does not pay attention to various anchoring point in other worktrees is more important than adding new commands to "worktree", I fully agree with that.
Just to make it clear. It's not like I put new worktree commands on higher priority. "worktree move/remove" was more or less ready for a long time and the gc problem was blocked by ref-iterator series (which entered master a few moths ago, but then I was busy with other things and couldn't get right back to the gc issue).
You didn't answer Johannes's rhetoric question though: "It should be possible to do that redesign work while having a small workaround in place that unbreaks, say, me?"
I assume "the right way" is still updating refs subsystem so that we can have a ref iterator to traverse all refs, or just one worktree, etc. Should I keep looking for a middle ground (maybe better than the linked series) to "unbreak Johannes"? I ignored all those comments (about --unpack-reachable and bisect refs) because I saw no chance of an updated series getting in git.git anyway.
Signature · 1 line
Duy
Quoted 7 lines · Duy Nguyen
Quoted 6 lines · Junio C Hamano
Quoted 3 lines · Johannes Schindelin
Also, the more important reply was Peff's reply that suggested that the proposed fix was incomplete, as it misses --unpack-unreachable:
https://public-inbox.org/git/[email protected]/
Quoted 8 lines
Quoted 5 lines
While I think that --unpack-unreachable thing is a symptom of the basic approach of patching things up at the wrong level, if you are hinting that fix to the issue that gc does not pay attention to various anchoring point in other worktrees is more important than adding new commands to "worktree", I fully agree with that.
Just to make it clear. It's not like I put new worktree commands on higher priority.
Good; we are on the same page.
Quoted 4 lines
"worktree move/remove" was more or less ready for a
long time and the gc problem was blocked by ref-iterator series (which entered master a few moths ago, but then I was busy with other things and couldn't get right back to the gc issue).
OK.
Quoted 3 lines
You didn't answer Johannes's rhetoric question though: "It should be possible to do that redesign work while having a small workaround in place that unbreaks, say, me?"
I do not recall seeing that. I however deliberately ignored another statement because I thought it enough to answer, which was:
Given that
https://public-inbox.org/git/[email protected]/
seems to have expected something to happen within a reasonable time frame, and that 8 months is substantially longer than a reasonable time frame, I am not sure that that position can still be defended.
In the message xmqqy46ntrhk is a response to, Michael enumerated the issues need to be solved with priorities, and listed reachability from the index and per-worktree detached HEAD are more important than others. I was hoping that even these "relatively more important" ones would turn out be of less importance compared to fixing all on the right foundation as long as it won't take forever, but I do agree with Dscho that 8 months is way too long.
Quoted 6 lines
I assume "the right way" is still updating refs subsystem so that we can have a ref iterator to traverse all refs, or just one worktree, etc. Should I keep looking for a middle ground (maybe better than the linked series) to "unbreak Johannes"? I ignored all those comments (about --unpack-reachable and bisect refs) because I saw no chance of an updated series getting in git.git anyway.
So, you may have seen no chance 8 months ago; after we have been waiting for a better fix, which hadn't materialized for a while, I am very much open to change the priority.
Even if you think "the right way" is to add to the iterators, I suspect that we can still do incremental fixes? I agree with the order of importance Michael listed in his message (i.e. the index and the HEAD first, and then other per-worktree hierarchies at lower priority), and I suspect you do, too. I am not sure that is what you called "middle ground", but I think such an incremental approach is totally fine.
Quoted 2 lines · Junio C Hamano
I do not recall seeing that. I however deliberately ignored another statement because I thought it enough to answer, which was:
Oops. "because I didn't think I thought it enough to answer" was what I meant.
Quoted 7 lines · Junio C Hamano
Even if you think "the right way" is to add to the iterators, I suspect that we can still do incremental fixes? I agree with the order of importance Michael listed in his message (i.e. the index and the HEAD first, and then other per-worktree hierarchies at lower priority), and I suspect you do, too. I am not sure that is what you called "middle ground", but I think such an incremental approach is totally fine.
Yeah index should be fixed independently. Sorry that thought did not occur to me. I counted HEAD as a ref too (i.e. to be managed/iterated by refs subsystem) but I guess it's special enough to be dealt with without ref iterator. I will try to fix these two first.
By middle ground, I was thinking that, now that we have ref iterator interface, we more or less know what the api should look like for a ref user like rev-list, so perhaps we could add a custom (ugly) iterator for per-worktree refs (including reflog). The ugliness is probably buried in refs/files-backend.c again until we clean it up.
Signature · 1 line
Duy