worktree: for-each function and list command
v6 cover worktree: for-each function and list command
Changes since v5:
- used already existing functions instead of reimplementing:
- resolve_ref_unsafe
- find_unique_abbrev
- shorten_unambiguous_ref
- added more tests for 'worktree list'
Michael Rappazzo (2):
worktree: add 'for_each_worktree' function
worktree: add 'list' command
Documentation/git-worktree.txt | 10 +++- builtin/worktree.c | 125 +++++++++++++++++++++++++++++++++++++++++ t/t2027-worktree-list.sh | 100 +++++++++++++++++++++++++++++++++ 3 files changed, 233 insertions(+), 2 deletions(-) create mode 100755 t/t2027-worktree-list.sh
Signature · 1 line
2.5.0
PATCH v6 1/2 worktree: add 'for_each_worktree' function
for_each_worktree iterates through each worktree and invokes a callback function. The main worktree (if not bare) is always encountered first, followed by worktrees created by git worktree add.
If the callback function returns a non-zero value, iteration stops, and the callback's return value is returned from the for_each_worktree function.
--- builtin/worktree.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+)
builtin/worktree.c +83 -0
@@ -26,6 +26,14 @@ static int show_only;static int verbose;static unsigned long expire; +/* +* The signature for the callback function for the for_each_worktree() +* function below. The memory pointed to by the callback arguments +* is only guaranteed to be valid for the duration of a single +* callback invocation. +*/ +typedef int each_worktree_fn (const char *path,const char *git_dir,void *cb_data); +static int prune_worktree (const char *id,struct strbuf *reason) {struct stat st;
@@ -359,6 +367,81 @@ static int add(int ac, const char **av, const char *prefix)return add_worktree (path, branch, &opts); } +/* +* Iterate through each worktree and invoke the callback function. If +* the callback function returns non-zero, the iteration stops, and +* this function returns that return value +*/ +static int for_each_worktree (each_worktree_fn fn,void *cb_data) +{ +struct strbuf worktree_path =STRBUF_INIT ; +struct strbuf worktree_git =STRBUF_INIT ; +const char *common_dir; +int main_is_bare =0 ; +int ret =0 ; + + common_dir =get_git_common_dir (); +if (!strcmp (common_dir,get_git_dir ())) { +/* simple case - this is the main repo */ + main_is_bare =is_bare_repository (); +if (!main_is_bare) { +strbuf_addstr (&worktree_path,get_git_work_tree ()); + }else { +strbuf_addstr (&worktree_path, common_dir); + } + }else { +strbuf_addstr (&worktree_path, common_dir); +/* If the common_dir DOES NOT end with '/.git', then it is bare */ + main_is_bare = !strbuf_strip_suffix (&worktree_path,"/.git" ); + } +strbuf_addstr (&worktree_git, worktree_path.buf); + +if (!main_is_bare) { +strbuf_addstr (&worktree_git,"/.git" ); + + ret =fn (worktree_path.buf, worktree_git.buf, cb_data); +if (ret) + goto done; + } +strbuf_addstr (&worktree_git,"/worktrees" ); + +if (is_directory (worktree_git.buf)) { +DIR *dir =opendir (worktree_git.buf); +if (dir) { +struct stat st; +struct dirent *d; +size_t base_path_len = worktree_git.len; + +while ((d =readdir (dir)) !=NULL ) { +if (!strcmp (d->d_name,"." ) || !strcmp (d->d_name,".." )) +continue ; + +strbuf_setlen (&worktree_git, base_path_len); +strbuf_addf (&worktree_git,"/%s/gitdir" , d->d_name); + +if (stat (worktree_git.buf, &st)) { +fprintf (stderr,"Unable to read worktree git dir: %s\n" , worktree_git.buf); +continue ; + } + +strbuf_reset (&worktree_path); +strbuf_read_file (&worktree_path, worktree_git.buf, st.st_size); +strbuf_strip_suffix (&worktree_path,"/.git\n" ); + +strbuf_strip_suffix (&worktree_git,"/gitdir" ); + ret =fn (worktree_path.buf, worktree_git.buf, cb_data); +if (ret) +break ; + } + } +closedir (dir); + } +done: +strbuf_release (&worktree_git); +strbuf_release (&worktree_path); +return ret; +} +int cmd_worktree (int ac,const char **av,const char *prefix) {struct option options[] = {
Signature · 1 line
2.5.0
Re: [PATCH v6 1/2] worktree: add 'for_each_worktree' function
Thanks for working on this. I apologize for not reviewing earlier rounds (other than v2 [1]); it's been difficult to find a block of time to do so...
Quoted 3 lines · Michael Rappazzo
for_each_worktreeiterates through each worktree and invokes a callback function. The main worktree (if not bare) is always encountered first, followed by worktrees created bygit worktree add.
Why does this iteration function specially filter out a bare main worktree? If it instead unconditionally vends the main worktree (bare or not), then the caller can make its own decision about what to do with it, thus empowering the caller, rather than imposing a (possibly) arbitrary restriction upon it.
For instance, the "git worktree list" command may very well want to show the main worktree, even if bare (possibly controlled by a command-line option), annotated appropriately ("[bare]"). This may be exactly the sort of information a user wants to know, and by leaving the decision up to the caller, then the caller ("git worktree list" in this example) has the opportunity to act accordingly, whereas if for_each_worktree() filters out a bare main worktree unconditionally, then the caller ("git worktree list") will never be able to offer such an option.
Quoted 3 lines
If the callback function returns a non-zero value, iteration stops, and the callback's return value is returned from the
for_each_worktreefunction.
Stepping back a bit, is a for-each-foo()-style interface desirable? This sort of interface imposes a good deal of complexity on callers, demanding a callback function and callback data (cb_data), and is generally (at least in C) more difficult to reason about than other simpler interfaces. Is such complexity warranted?
An alternate, much simpler interface would be to have a function, say get_worktrees(), return an array of 'worktree' structures to the caller, which the caller would iterate over (which is a common operation in C, thus easily reasoned about).
The one benefit of a for-each-foo()-style interface is that it's possible to "exit early", thus avoiding the cost of interrogating meta-data for worktrees in which the caller is not interested, however, it seems unlikely that there will be so many worktrees linked to a repository for this early exit to translate into any real savings.
Quoted 7 lines
Signed-off-by Michael Rappazzo <[email protected]>---
builtin/worktree.c +0
-0
builtin/worktree.c static int show_only; · line 35
Quoted 10 lines
builtin/worktree.c +7
-0@@ -26,6 +26,14 @@ static int show_only;static int verbose;static unsigned long expire; +/* +* The signature for the callback function for the for_each_worktree() +* function below. The memory pointed to by the callback arguments +* is only guaranteed to be valid for the duration of a single +* callback invocation. +*/ +typedef int each_worktree_fn (const char *path,const char *git_dir,void *cb_data);
In my review[1] of v2, I mentioned that (at some point) the "git worktree list" command might like to show additional information about the worktree other than just its location. Such information might include its tag, the checked out branch (or detached HEAD), whether it's locked (and the lock reason and whether the worktree is currently "online"), whether it can be pruned (and the prune reason).
Commands such as "git worktree add" and "git checkout" need to know if a branch is already checked out in some (other) worktree, thus they also need the "branch" information.
This need by clients for additional worktree meta-data suggests that the additional information ought to be encapsulated into a 'struct worktree', and that for_each_worktree() should vend a 'struct worktree' rather than vending merely the "git dir". (Alternately, the above-suggested get_worktrees() should return an array of 'struct worktree' items.)
An important reason for making for_each_worktree() vend a 'struct worktree' is that it facilitates centralizing all the logic for determining and computing the extra worktree meta-data in one place, thus relieving callers of burden of having to compute the extra information themselves. (Junio alluded to this in his v5 review[2].)
builtin/worktree.c static int add(int ac, const char **av, const char *prefix) · line 375
Quoted 13 lines
builtin/worktree.c +6
-0static int prune_worktree (const char *id,struct strbuf *reason) {struct stat st;@@ -359,6 +367,81 @@ static int add(int ac, const char **av, const char *prefix)return add_worktree (path, branch, &opts); } +/* +* Iterate through each worktree and invoke the callback function. If +* the callback function returns non-zero, the iteration stops, and +* this function returns that return value +*/ +static int for_each_worktree (each_worktree_fn fn,void *cb_data)
Ultimately, code outside of builtin/worktree.c will want to benefit from the encapsulation of this worktree iteration logic. For instance, the support code in (top-level) branch.c invoked by "git worktree add" and "git checkout" to determine if a branch is already checked out in some (other) branch could avail itself of this iteration interface.
As such, it would make sense to place this code at location where it can be accessed by callers other than just builtin/worktree.c. A good location for it to reside might be in a new top-level file worktree.c.
It doesn't have to be part of the current patch series, but, eventually, the code in branch.c for determining if a branch is already checked out elsewhere also should migrate to the new top-level worktree.c; in particular, die_if_checked_out(), find_shared_symref(), and find_linked_symref().
Quoted 12 lines
builtin/worktree.c +12
-0+{ +struct strbuf worktree_path =STRBUF_INIT ; +struct strbuf worktree_git =STRBUF_INIT ; +const char *common_dir; +int main_is_bare =0 ; +int ret =0 ; + + common_dir =get_git_common_dir (); +if (!strcmp (common_dir,get_git_dir ())) { +/* simple case - this is the main repo */ + main_is_bare =is_bare_repository (); +if (!main_is_bare) {
builtin/worktree.c static int add(int ac, const char **av, const char *prefix) · line 397
Quoted 10 lines
builtin/worktree.c +10
-0+strbuf_addstr (&worktree_path,get_git_work_tree ()); + }else { +strbuf_addstr (&worktree_path, common_dir); + } + }else { +strbuf_addstr (&worktree_path, common_dir); +/* If the common_dir DOES NOT end with '/.git', then it is bare */ + main_is_bare = !strbuf_strip_suffix (&worktree_path,"/.git" ); + } +strbuf_addstr (&worktree_git, worktree_path.buf);
I may have missed some discussion in earlier rounds (or perhaps I'm too simple-minded), but I'm confused about why this logic (and most of the rest of the function) differs so much from existing logic in branch.c:find_shared_symref() and find_linked_symref() for iterating over the worktrees and gleaning information about them. That logic in branch.c seems to do a pretty good job of reporting the worktree in which a branch is already checked out, so it's not clear why the above logic takes a different (and seemingly more complex) approach.
builtin/worktree.c static int add(int ac, const char **av, const char *prefix) · line 408
Quoted 10 lines
builtin/worktree.c +10
-0+if (!main_is_bare) { +strbuf_addstr (&worktree_git,"/.git" ); + + ret =fn (worktree_path.buf, worktree_git.buf, cb_data); +if (ret) + goto done; + } +strbuf_addstr (&worktree_git,"/worktrees" ); + +if (is_directory (worktree_git.buf)) {
As mentioned in my v2 review[1], this is_directory() invocation doesn't buy you anything. The following opendir() will either succeed or fail anyhow, so checking beforehand if 'worktree_git' is a directory is wasted work. If you eliminate is_directory(), you avoid that unnecessary work (and the rest of the code can be less deeply indented).
Quoted 19 lines
builtin/worktree.c +19
-0+DIR *dir =opendir (worktree_git.buf); +if (dir) { +struct stat st; +struct dirent *d; +size_t base_path_len = worktree_git.len; + +while ((d =readdir (dir)) !=NULL ) { +if (!strcmp (d->d_name,"." ) || !strcmp (d->d_name,".." )) +continue ; + +strbuf_setlen (&worktree_git, base_path_len); +strbuf_addf (&worktree_git,"/%s/gitdir" , d->d_name); + +if (stat (worktree_git.buf, &st)) { +fprintf (stderr,"Unable to read worktree git dir: %s\n" , worktree_git.buf); +continue ; + } + +strbuf_reset (&worktree_path);
builtin/worktree.c static int add(int ac, const char **av, const char *prefix) · line 437
Quoted 10 lines
builtin/worktree.c +10
-0+strbuf_read_file (&worktree_path, worktree_git.buf, st.st_size); +strbuf_strip_suffix (&worktree_path,"/.git\n" ); + +strbuf_strip_suffix (&worktree_git,"/gitdir" ); + ret =fn (worktree_path.buf, worktree_git.buf, cb_data); +if (ret) +break ; + } + } +closedir (dir);
This closedir() should be inside the `if (dir) {...}' block rather than outside.
Returning to the issue of this code differing from existing code in branch.c for iterating worktrees, I'm curious as to why this functionality was implemented again from scratch here rather than re-using the existing (somewhat) well proven code and logic in branch.c:find_shared_symref() and find_linked_symref(). My v2 review[1] hinted at re-use a couple times.
Considering that the existing code in branch.c for iterating worktrees has gone through several code reviews and is already (somewhat) proven, re-using that code for a general purpose worktree iterator makes lots of sense compared with writing it again from scratch, as is done here. Consequently, I, personally, would be happier to see the existing code refactored (perhaps over several patches) to the point that it can be re-used as a general purpose iterator. But, that's my opinion; I can't speak for others.
builtin/worktree.c static int add(int ac, const char **av, const char *prefix) · line 443
Quoted 6 lines
builtin/worktree.c +6
-0+ } +done: +strbuf_release (&worktree_git); +strbuf_release (&worktree_path); +return ret; +}
[1]: http://article.gmane.org/gmane.comp.version-control.git/275528
[2]: http://article.gmane.org/gmane.comp.version-control.git/276471
PATCH v6 2/2 worktree: add 'list' command
In reply to Michael Rappazzo'git worktree list' uses the for_each_worktree function to iterate, and outputs the worktree dir. With the verbose option set, it also shows the branch or revision currently checked out in that worktree.
--- Documentation/git-worktree.txt | 10 ++++- builtin/worktree.c | 42 +++++++++++++++++ t/t2027-worktree-list.sh | 100 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 150 insertions(+), 2 deletions(-) create mode 100755 t/t2027-worktree-list.sh
Documentation/git-worktree.txt +8 -2
@@ -11,6 +11,7 @@ SYNOPSIS [verse] 'git worktree add' [-f] [--detach] [-b <new-branch>] <path> [<branch>] 'git worktree prune' [-n] [-v] [--expire <expire>] +'git worktree list' [-v|--verbose] DESCRIPTION -----------
@@ -59,6 +60,10 @@ prune:: Prune working tree information in $GIT_DIR/worktrees. +list:: + +List the main worktree followed by all of the linked worktrees. + OPTIONS -------
@@ -88,11 +93,13 @@ OPTIONS -v:: --verbose:: - With `prune`, report all removals. + With `prune`, report all removals. + With `list` show the branch or revision currently checked out in that worktree. --expire <time>:: With `prune`, only expire unused working trees older than <time>. + DETAILS ------- Each linked working tree has a private sub-directory in the repository's
@@ -167,7 +174,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 -- `list` to list linked working trees - `lock` to prevent automatic pruning of administrative files (for instance, for a working tree on a portable device)
builtin/worktree.c +42 -0
59 lines not on this page · raw message
t/t2027-worktree-list.sh new file +100 -0
100 lines not on this page · raw message
Signature · 1 line
2.5.0
Re: [PATCH v6 2/2] worktree: add 'list' command
Quoted 11 lines · Michael Rappazzo
'git worktree list' uses the
for_each_worktreefunction to iterate, and outputs the worktree dir. With the verbose option set, it also shows the branch or revision currently checked out in that worktree.Signed-off-by Michael Rappazzo <[email protected]>---
Documentation/git-worktree.txt +0
-0
Documentation/git-worktree.txt SYNOPSIS · line 14
Quoted 4 lines
Documentation/git-worktree.txt +1
-0@@ -11,6 +11,7 @@ SYNOPSIS [verse] 'git worktree add' [-f] [--detach] [-b <new-branch>] <path> [<branch>] 'git worktree prune' [-n] [-v] [--expire <expire>] +'git worktree list' [-v|--verbose]
For conciseness and consistency with the "git worktree prune" synopsis, this should mention only -v (and omit --verbose).
Documentation/git-worktree.txt OPTIONS · line 97
Quoted 9 lines
Documentation/git-worktree.txt +2
-1DESCRIPTION -----------@@ -88,11 +93,13 @@ OPTIONS -v:: --verbose:: - With `prune`, report all removals. + With `prune`, report all removals. + With `list` show the branch or revision currently checked out in that worktree.
Add a comma after "With list".
Also: s/in that/in each/
This new line is overly long; wrapping it over a couple lines would help.
Quoted 6 lines
Documentation/git-worktree.txt +0
-0--expire <time>:: With `prune`, only expire unused working trees older than <time>.builtin/worktree.c +0
-0
builtin/worktree.c done: · line 456
Quoted 10 lines
builtin/worktree.c +10
-0+static int print_worktree_details (const char *path,const char *git_dir,void *cb_data) +{ +printf ("%s" , path); +if (verbose) { +enter_repo (git_dir,1 ); +printf (" [" ); +unsigned char sha1[20 ]; +int flag; + +const char *refname =resolve_ref_unsafe ("HEAD" ,0 , sha1, &flag);
Declarations after statement. Move the declarations of 'sha1', 'flag', and 'refname' above the statements (enter_repo() and printf()).
builtin/worktree.c done: · line 462
Quoted 6 lines
builtin/worktree.c +6
-0+if (!(flag &REF_ISSYMREF )) { +printf ("%s" ,find_unique_abbrev (sha1,DEFAULT_ABBREV )); + }else { + refname =shorten_unambiguous_ref (refname,0 ); +printf ("%s" , refname); + }
As mentioned in my review[1] of patch 1/2, it would be better for this sort of logic to be handled by the worktree iterator itself so that all callers can benefit, rather than having to repeat the work in each caller. This information would be encapsulated in a 'struct worktree' along with 'path' and 'git_dir' and other interesting information vended by the the iterator function.
Quoted 1 line
+
printf("]");
Rather than dropping printf()'s here and there to compose the output piecemeal, it would be cleaner, and facilitate future changes, to assign the refname/sha1 to a variable, and interpolate that variable into an all-encompasing printf(), like this:
printf(" [%s]", branch_or_sha1);builtin/worktree.c done: · line 469
Quoted 5 lines
builtin/worktree.c +5
-0+ } +printf ("\n" ); + +return 0 ; +}
[1]: http://article.gmane.org/gmane.comp.version-control.git/276847
Re: [PATCH v6 1/2] worktree: add 'for_each_worktree' function
In reply to Eric SunshineQuoted 11 lines · Eric Sunshine
Stepping back a bit, is a for-each-foo()-style interface desirable? This sort of interface imposes a good deal of complexity on callers, demanding a callback function and callback data (
cb_data), and is generally (at least in C) more difficult to reason about than other simpler interfaces. Is such complexity warranted?An alternate, much simpler interface would be to have a function, say
get_worktrees(), return an array of 'worktree' structures to the caller, which the caller would iterate over (which is a common operation in C, thus easily reasoned about).
Quoted 6 lines
The one benefit of a for-each-foo()-style interface is that it's possible to "exit early", thus avoiding the cost of interrogating meta-data for worktrees in which the caller is not interested, however, it seems unlikely that there will be so many worktrees linked to a repository for this early exit to translate into any real savings.
The other benefit is that there is no need to worry about deallocating the list. But that might be too minor to worry about.
Re: [PATCH v6 1/2] worktree: add 'for_each_worktree' function
Quoted 19 lines · David Turner
Quoted 11 lines · Eric Sunshine
Stepping back a bit, is a for-each-foo()-style interface desirable? This sort of interface imposes a good deal of complexity on callers, demanding a callback function and callback data (
cb_data), and is generally (at least in C) more difficult to reason about than other simpler interfaces. Is such complexity warranted?An alternate, much simpler interface would be to have a function, say
get_worktrees(), return an array of 'worktree' structures to the caller, which the caller would iterate over (which is a common operation in C, thus easily reasoned about).Quoted 6 lines
The one benefit of a for-each-foo()-style interface is that it's possible to "exit early", thus avoiding the cost of interrogating meta-data for worktrees in which the caller is not interested, however, it seems unlikely that there will be so many worktrees linked to a repository for this early exit to translate into any real savings.
Quoted 2 lines
The other benefit is that there is no need to worry about deallocating the list. But that might be too minor to worry about.
Probably. The burden of having to deallocate the returned array seems quite minor compared to the complexity of the callback function approach.
Also, unstated but implied with the suggestion of a get_worktrees() function was that there would be a corresponding free_worktrees() function to make cleanup easy.
Re: [PATCH v6 1/2] worktree: add 'for_each_worktree' function
Quoted 23 lines · Eric Sunshine
Quoted 19 lines · David Turner
Quoted 11 lines · Eric Sunshine
Stepping back a bit, is a for-each-foo()-style interface desirable? This sort of interface imposes a good deal of complexity on callers, demanding a callback function and callback data (
cb_data), and is generally (at least in C) more difficult to reason about than other simpler interfaces. Is such complexity warranted?An alternate, much simpler interface would be to have a function, say
get_worktrees(), return an array of 'worktree' structures to the caller, which the caller would iterate over (which is a common operation in C, thus easily reasoned about).Quoted 6 lines
The one benefit of a for-each-foo()-style interface is that it's possible to "exit early", thus avoiding the cost of interrogating meta-data for worktrees in which the caller is not interested, however, it seems unlikely that there will be so many worktrees linked to a repository for this early exit to translate into any real savings.
Quoted 2 lines
The other benefit is that there is no need to worry about deallocating the list. But that might be too minor to worry about.
Quoted 7 lines
Probably. The burden of having to deallocate the returned array seems quite minor compared to the complexity of the callback function approach.
Also, unstated but implied with the suggestion of a
get_worktrees()function was that there would be a correspondingfree_worktrees()function to make cleanup easy.
That's fine with me. Sorry for leading you down the wrong path, Michael.
Re: [PATCH v6 1/2] worktree: add 'for_each_worktree' function
In reply to Eric SunshineQuoted 3 lines · Eric Sunshine
Thanks for working on this. I apologize for not reviewing earlier rounds (other than v2 [1]); it's been difficult to find a block of time to do so...
I appreciate your time and comments.
Quoted 12 lines · Michael Rappazzo
Quoted 3 lines
for_each_worktreeiterates through each worktree and invokes a callback function. The main worktree (if not bare) is always encountered first, followed by worktrees created bygit worktree add.Why does this iteration function specially filter out a bare main worktree? If it instead unconditionally vends the main worktree (bare or not), then the caller can make its own decision about what to do with it, thus empowering the caller, rather than imposing a (possibly) arbitrary restriction upon it.
Quoted 9 lines
For instance, the "git worktree list" command may very well want to show the main worktree, even if bare (possibly controlled by a command-line option), annotated appropriately ("[bare]"). This may be exactly the sort of information a user wants to know, and by leaving the decision up to the caller, then the caller ("git worktree list" in this example) has the opportunity to act accordingly, whereas if
for_each_worktree()filters out a bare main worktree unconditionally, then the caller ("git worktree list") will never be able to offer such an option.
I wasn't sure that a bare repo would be considered a worktree. I don't think that it would be
a good idea to include it. In the same vein that I can't checkout a branch in a bare repo, it
figure that it shouldn't be in the list.
Quoted 16 lines
Quoted 4 lines
If the callback function returns a non-zero value, iteration stops, and the callback's return value is returned from the
for_each_worktreefunction.Stepping back a bit, is a for-each-foo()-style interface desirable? This sort of interface imposes a good deal of complexity on callers, demanding a callback function and callback data (
cb_data), and is generally (at least in C) more difficult to reason about than other simpler interfaces. Is such complexity warranted?An alternate, much simpler interface would be to have a function, say
get_worktrees(), return an array of 'worktree' structures to the caller, which the caller would iterate over (which is a common operation in C, thus easily reasoned about).
Quoted 6 lines
The one benefit of a for-each-foo()-style interface is that it's possible to "exit early", thus avoiding the cost of interrogating meta-data for worktrees in which the caller is not interested, however, it seems unlikely that there will be so many worktrees linked to a repository for this early exit to translate into any real savings.
I am not opposed to making a simple function as you describe. I think David was looking for a callback style function. I don't think it would be terrible to keep the
callback and then also include the simple function to return the struct array. I like
the memory management of the callback better than the struct array though.
Quoted 37 lines
Quoted 8 lines
Signed-off-by Michael Rappazzo <[email protected]>---
builtin/worktree.c +0
-0builtin/worktree.c static int show_only; · line 35
Quoted 10 lines
builtin/worktree.c +7
-0@@ -26,6 +26,14 @@ static int show_only;static int verbose;static unsigned long expire; +/* +* The signature for the callback function for the for_each_worktree() +* function below. The memory pointed to by the callback arguments +* is only guaranteed to be valid for the duration of a single +* callback invocation. +*/ +typedef int each_worktree_fn (const char *path,const char *git_dir,void *cb_data);In my review[1] of v2, I mentioned that (at some point) the "git worktree list" command might like to show additional information about the worktree other than just its location. Such information might include its tag, the checked out branch (or detached HEAD), whether it's locked (and the lock reason and whether the worktree is currently "online"), whether it can be pruned (and the prune reason).
Commands such as "git worktree add" and "git checkout" need to know if a branch is already checked out in some (other) worktree, thus they also need the "branch" information.
This need by clients for additional worktree meta-data suggests that the additional information ought to be encapsulated into a 'struct worktree', and that
for_each_worktree()should vend a 'struct worktree' rather than vending merely the "git dir". (Alternately, the above-suggestedget_worktrees()should return an array of 'struct worktree' items.)
Quoted 5 lines
An important reason for making
for_each_worktree()vend a 'struct worktree' is that it facilitates centralizing all the logic for determining and computing the extra worktree meta-data in one place, thus relieving callers of burden of having to compute the extra information themselves. (Junio alluded to this in his v5 review[2].)
I think that this makes sense. I will refactor to use a struct, and include that in
the callback signature and/or the struct array function.
Quoted 54 lines
Quoted 5 lines
builtin/worktree.c +0
-0static int prune_worktree (const char *id,struct strbuf *reason) {struct stat st;builtin/worktree.c static int add(int ac, const char **av, const char *prefix) · line 375
Quoted 9 lines
builtin/worktree.c +6
-0@@ -359,6 +367,81 @@ static int add(int ac, const char **av, const char *prefix)return add_worktree (path, branch, &opts); } +/* +* Iterate through each worktree and invoke the callback function. If +* the callback function returns non-zero, the iteration stops, and +* this function returns that return value +*/ +static int for_each_worktree (each_worktree_fn fn,void *cb_data)Ultimately, code outside of
builtin/worktree.cwill want to benefit from the encapsulation of this worktree iteration logic. For instance, the support code in (top-level)branch.cinvoked by "git worktree add" and "git checkout" to determine if a branch is already checked out in some (other) branch could avail itself of this iteration interface.As such, it would make sense to place this code at location where it can be accessed by callers other than just
builtin/worktree.c. A good location for it to reside might be in a new top-level fileworktree.c.It doesn't have to be part of the current patch series, but, eventually, the code in
branch.cfor determining if a branch is already checked out elsewhere also should migrate to the new top-levelworktree.c; in particular,die_if_checked_out(),find_shared_symref(), andfind_linked_symref().Quoted 12 lines
builtin/worktree.c +12
-0+{ +struct strbuf worktree_path =STRBUF_INIT ; +struct strbuf worktree_git =STRBUF_INIT ; +const char *common_dir; +int main_is_bare =0 ; +int ret =0 ; + + common_dir =get_git_common_dir (); +if (!strcmp (common_dir,get_git_dir ())) { +/* simple case - this is the main repo */ + main_is_bare =is_bare_repository (); +if (!main_is_bare) {builtin/worktree.c static int add(int ac, const char **av, const char *prefix) · line 397
Quoted 10 lines
builtin/worktree.c +10
-0+strbuf_addstr (&worktree_path,get_git_work_tree ()); + }else { +strbuf_addstr (&worktree_path, common_dir); + } + }else { +strbuf_addstr (&worktree_path, common_dir); +/* If the common_dir DOES NOT end with '/.git', then it is bare */ + main_is_bare = !strbuf_strip_suffix (&worktree_path,"/.git" ); + } +strbuf_addstr (&worktree_git, worktree_path.buf);
Quoted 8 lines
I may have missed some discussion in earlier rounds (or perhaps I'm too simple-minded), but I'm confused about why this logic (and most of the rest of the function) differs so much from existing logic in branch.c:find_shared_symref() and
find_linked_symref()for iterating over the worktrees and gleaning information about them. That logic inbranch.cseems to do a pretty good job of reporting the worktree in which a branch is already checked out, so it's not clear why the above logic takes a different (and seemingly more complex) approach.
This is due to my unfamiliarity of the code api. I keep trying to look for the right
functions to use, but I miss them. Sorry. I will rework using those functions.
Quoted 10 lines
Quoted 10 lines
builtin/worktree.c +9
-0+if (!main_is_bare) { +strbuf_addstr (&worktree_git,"/.git" ); + + ret =fn (worktree_path.buf, worktree_git.buf, cb_data); +if (ret) + goto done; + } +strbuf_addstr (&worktree_git,"/worktrees" ); +
Quoted 8 lines
builtin/worktree.c static int add(int ac, const char **av, const char *prefix) · line 408
Quoted 1 line
builtin/worktree.c +1
-0+if (is_directory (worktree_git.buf)) {As mentioned in my v2 review[1], this
is_directory()invocation doesn't buy you anything. The followingopendir()will either succeed or fail anyhow, so checking beforehand if 'worktree_git' is a directory is wasted work. If you eliminateis_directory(), you avoid that unnecessary work (and the rest of the code can be less deeply indented).
For some reason, in my testing, calling opendir was giving a segmentation fault regardless of how I checked the return value. Including the is_directory check allowed me to avoid that.
Quoted 58 lines
Quoted 20 lines
builtin/worktree.c +19
-0+DIR *dir =opendir (worktree_git.buf); +if (dir) { +struct stat st; +struct dirent *d; +size_t base_path_len = worktree_git.len; + +while ((d =readdir (dir)) !=NULL ) { +if (!strcmp (d->d_name,"." ) || !strcmp (d->d_name,".." )) +continue ; + +strbuf_setlen (&worktree_git, base_path_len); +strbuf_addf (&worktree_git,"/%s/gitdir" , d->d_name); + +if (stat (worktree_git.buf, &st)) { +fprintf (stderr,"Unable to read worktree git dir: %s\n" , worktree_git.buf); +continue ; + } + +strbuf_reset (&worktree_path);builtin/worktree.c static int add(int ac, const char **av, const char *prefix) · line 437
Quoted 10 lines
builtin/worktree.c +10
-0+strbuf_read_file (&worktree_path, worktree_git.buf, st.st_size); +strbuf_strip_suffix (&worktree_path,"/.git\n" ); + +strbuf_strip_suffix (&worktree_git,"/gitdir" ); + ret =fn (worktree_path.buf, worktree_git.buf, cb_data); +if (ret) +break ; + } + } +closedir (dir);This
closedir()should be inside the `if (dir) {...}' block rather than outside.Returning to the issue of this code differing from existing code in
branch.cfor iterating worktrees, I'm curious as to why this functionality was implemented again from scratch here rather than re-using the existing (somewhat) well proven code and logic in branch.c:find_shared_symref() andfind_linked_symref(). My v2 review[1] hinted at re-use a couple times.Considering that the existing code in
branch.cfor iterating worktrees has gone through several code reviews and is already (somewhat) proven, re-using that code for a general purpose worktree iterator makes lots of sense compared with writing it again from scratch, as is done here. Consequently, I, personally, would be happier to see the existing code refactored (perhaps over several patches) to the point that it can be re-used as a general purpose iterator. But, that's my opinion; I can't speak for others.builtin/worktree.c static int add(int ac, const char **av, const char *prefix) · line 443
Quoted 6 lines
builtin/worktree.c +6
-0+ } +done: +strbuf_release (&worktree_git); +strbuf_release (&worktree_path); +return ret; +}[1]: http://article.gmane.org/gmane.comp.version-control.git/275528
[2]: http://article.gmane.org/gmane.comp.version-control.git/276471
Re: [PATCH v6 1/2] worktree: add 'for_each_worktree' function
Quoted 5 lines · Mike Rappazzo
I wasn't sure that a bare repo would be considered a worktree. I don't think that it would be
a good idea to include it. In the same vein that I can't checkout a branch in a bare repo, it
figure that it shouldn't be in the list.
I forgot to mention in my previous response that I have the opposite view, and think that a bare repo should be included in the output of "git worktree list". The reason is that the intention of "git worktree list" is to give the user a consolidated view of the locations of all components of his "workspace". By "workspace", I mean the repository (bare or not) and its worktrees.
In the typical case, the .git directory resides within the main worktree (the first item output by "git worktree list"), thus is easily found, however, if "git worktree list" hides bare repos, then the user will have no way to easily locate the repository (without resorting to lower-level commands or peeking at configuration files).
Re: [PATCH v6 1/2] worktree: add 'for_each_worktree' function
Quoted 14 lines · Eric Sunshine
Quoted 5 lines · Mike Rappazzo
I wasn't sure that a bare repo would be considered a worktree. I don't think that it would be
a good idea to include it. In the same vein that I can't checkout a branch in a bare repo, it
figure that it shouldn't be in the list.I forgot to mention in my previous response that I have the opposite view, and think that a bare repo should be included in the output of "git worktree list". The reason is that the intention of "git worktree list" is to give the user a consolidated view of the locations of all components of his "workspace". By "workspace", I mean the repository (bare or not) and its worktrees.
Quoted 5 lines
In the typical case, the .git directory resides within the main worktree (the first item output by "git worktree list"), thus is easily found, however, if "git worktree list" hides bare repos, then the user will have no way to easily locate the repository (without resorting to lower-level commands or peeking at configuration files).
This makes sense, but would we also want to decorate it in the git worktree list
command? Would porcelain list output be able to differentiate it?
Re: [PATCH v6 1/2] worktree: add 'for_each_worktree' function
In reply to Mike RappazzoQuoted 18 lines · Mike Rappazzo
Quoted 7 lines · Eric Sunshine
On Sun, Aug 30, 2015 at 3:10 PM, Michael Rappazzo <[email protected]> wrote:
Why does this iteration function specially filter out a bare main worktree? If it instead unconditionally vends the main worktree (bare or not), then the caller can make its own decision about what to do with it, thus empowering the caller, rather than imposing a (possibly) arbitrary restriction upon it.Quoted 9 lines
For instance, the "git worktree list" command may very well want to show the main worktree, even if bare (possibly controlled by a command-line option), annotated appropriately ("[bare]"). This may be exactly the sort of information a user wants to know, and by leaving the decision up to the caller, then the caller ("git worktree list" in this example) has the opportunity to act accordingly, whereas if
for_each_worktree()filters out a bare main worktree unconditionally, then the caller ("git worktree list") will never be able to offer such an option.
Quoted 5 lines
I wasn't sure that a bare repo would be considered a worktree. I don't think that it would be
a good idea to include it. In the same vein that I can't checkout a branch in a bare repo, it
figure that it shouldn't be in the list.
This is a mechanism vs. policy issue[1]. Low-level worker code, such as this iteration function, should concern itself only with the mechanics of retrieving and vending the worktree meta-data, and should not make decisions (policy) about how that information is used by the caller. Policy decisions (how the meta-data is used or displayed) should be pushed to as high a level as possible, often up to the level of user-interface (which is what "git worktree list" is).
[1]: http://www.catb.org/esr/writings/taoup/html/ch01s06.html#id2877777
Quoted 18 lines
Quoted 11 lines
Stepping back a bit, is a for-each-foo()-style interface desirable? This sort of interface imposes a good deal of complexity on callers, demanding a callback function and callback data (
cb_data), and is generally (at least in C) more difficult to reason about than other simpler interfaces. Is such complexity warranted?An alternate, much simpler interface would be to have a function, say
get_worktrees(), return an array of 'worktree' structures to the caller, which the caller would iterate over (which is a common operation in C, thus easily reasoned about).Quoted 6 lines
The one benefit of a for-each-foo()-style interface is that it's possible to "exit early", thus avoiding the cost of interrogating meta-data for worktrees in which the caller is not interested, however, it seems unlikely that there will be so many worktrees linked to a repository for this early exit to translate into any real savings.
Quoted 6 lines
I am not opposed to making a simple function as you describe. I think David was looking for a callback style function. I don't think it would be terrible to keep the
callback and then also include the simple function to return the struct array. I like
the memory management of the callback better than the struct array though.
We should stick with one or the other. Having both complicates the code unnecessarily and increases maintenance costs. I, personally, prefer the get_worktrees() approach for its client-side simplicity. With a corresponding free_worktrees() to dispose of the resources allocated by get_worktrees(), memory management shouldn't be much of a burden for callers (other than having to remember to call it).
Quoted 9 lines
Quoted 8 lines
I may have missed some discussion in earlier rounds (or perhaps I'm too simple-minded), but I'm confused about why this logic (and most of the rest of the function) differs so much from existing logic in branch.c:find_shared_symref() and
find_linked_symref()for iterating over the worktrees and gleaning information about them. That logic inbranch.cseems to do a pretty good job of reporting the worktree in which a branch is already checked out, so it's not clear why the above logic takes a different (and seemingly more complex) approach.
Quoted 3 lines
This is due to my unfamiliarity of the code api. I keep trying to look for the right
functions to use, but I miss them. Sorry. I will rework using those functions.
The API is indeed large and complex, and it can be difficult to get a handle on how to accomplish various tasks. That's also a good argument for re-using the existing (proven) code in branch.c:find_shared_symref() and find_linked_symref() rather than re-inventing it from scratch. In its current form, the branch.c code doesn't look much like a general-purpose iterator function, but it should be possible to refactor if over the course of a few patches so that it does satisfy that goal.
Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
builtin/worktree.c +9
-0+if (!main_is_bare) { +strbuf_addstr (&worktree_git,"/.git" ); + + ret =fn (worktree_path.buf, worktree_git.buf, cb_data); +if (ret) + goto done; + } +strbuf_addstr (&worktree_git,"/worktrees" ); +Quoted 8 lines
Quoted 1 line
+ if (
is_directory(worktree_git.buf)) {As mentioned in my v2 review[1], this
is_directory()invocation doesn't buy you anything. The followingopendir()will either succeed or fail anyhow, so checking beforehand if 'worktree_git' is a directory is wasted work. If you eliminateis_directory(), you avoid that unnecessary work (and the rest of the code can be less deeply indented).
Quoted 3 lines
For some reason, in my testing, calling opendir was giving a segmentation fault regardless of how I checked the return value. Including the
is_directorycheck allowed me to avoid that.
Was it the opendir() that was crashing or the closedir()? Considering that the closedir() incorrectly resides outside the `if (dir) {...}' block, rather than inside, I could easily see the closedir() crashing when called with NULL.
Re: [PATCH v6 1/2] worktree: add 'for_each_worktree' function
In reply to Eric SunshineQuoted 1 line · Eric Sunshine
Thanks for working on this....
<aol>metoo</aol>
Quoted 10 lines
Stepping back a bit, is a for-each-foo()-style interface desirable? This sort of interface imposes a good deal of complexity on callers, demanding a callback function and callback data (
cb_data), and is generally (at least in C) more difficult to reason about than other simpler interfaces. Is such complexity warranted?An alternate, much simpler interface would be to have a function, say
get_worktrees(), return an array of 'worktree' structures to the caller, which the caller would iterate over (which is a common operation in C, thus easily reasoned about).
Nice.
Re: [PATCH v6 1/2] worktree: add 'for_each_worktree' function
In reply to Mike RappazzoQuoted 21 lines · Mike Rappazzo
Quoted 14 lines · Eric Sunshine
Quoted 5 lines · Mike Rappazzo
I wasn't sure that a bare repo would be considered a worktree. I don't think that it would be
a good idea to include it. In the same vein that I can't checkout a branch in a bare repo, it
figure that it shouldn't be in the list.I forgot to mention in my previous response that I have the opposite view, and think that a bare repo should be included in the output of "git worktree list". The reason is that the intention of "git worktree list" is to give the user a consolidated view of the locations of all components of his "workspace". By "workspace", I mean the repository (bare or not) and its worktrees.
Quoted 5 lines
In the typical case, the .git directory resides within the main worktree (the first item output by "git worktree list"), thus is easily found, however, if "git worktree list" hides bare repos, then the user will have no way to easily locate the repository (without resorting to lower-level commands or peeking at configuration files).
Quoted 3 lines
This makes sense, but would we also want to decorate it in the
git worktree list
command? Would porcelain list output be able to differentiate it?
I don't have strong feelings about decorating the bare repository, if by "decorate", you mean adding a "[bare]" annotation or something. Verbose mode can certainly do so.
For porcelain mode, we can (and should) be explicit about it.