From: Junio C Hamano <hidden> Date: 2017-01-01 01:26:43
Stefan Beller [off-list ref] writes:
The checkout state was introduced via 16da134b1f9
(read-trees: refactor the unpack_trees() part, 2006-07-30). An attempt to
refactor the checkout state was done in b56aa5b268e (unpack-trees: pass
checkout state explicitly to check_updates(), 2016-09-13), but we can
go even further.
The `struct checkout state` is not used in unpack_trees apart from
initializing it, so move it into the function that makes use of it,
which is `check_updates`.
Signed-off-by: Stefan Beller <redacted>
---
I'd add René's Reviewed-by: here.
-static int check_updates(struct unpack_trees_options *o,
- const struct checkout *state)
+static int check_updates(struct unpack_trees_options *o)
{
unsigned cnt = 0, total = 0;
struct progress *progress = NULL;
struct index_state *index = &o->result;
- int i;
- int errs = 0;
+ struct checkout state = CHECKOUT_INIT;
+ int i, errs = 0;
+
+ state.force = 1;
+ state.quiet = 1;
+ state.refresh_cache = 1;
+ state.istate = index;
I think moving heavier and initialized variables earlier and more
lightweight and ephemeral ones like "i" later does make it easier to
follow. "errs" has the significance and the lifetime similar to
cnt/total, and logically should be higher, though. It is not a big
enough deal to reroll (but as your futzing of the variable definition
order was not a big enough deal to do in this patch, either, so...).
Queued. Thanks.
From: Stefan Beller <hidden> Date: 2017-01-06 21:04:10
On Sat, Dec 31, 2016 at 5:26 PM, Junio C Hamano [off-list ref] wrote:
quoted
Signed-off-by: Stefan Beller <redacted>
---
I'd add René's Reviewed-by: here.
done
I think moving heavier and initialized variables earlier and more
lightweight and ephemeral ones like "i" later does make it easier to
follow. "errs" has the significance and the lifetime similar to
cnt/total, and logically should be higher, though. It is not a big
enough deal to reroll (but as your futzing of the variable definition
order was not a big enough deal to do in this patch, either, so...).
I will send out a series, that is based on this patch shortly;
as I fuzzed again with the small variables, that series doesn't
apply on this version but the version to be sent out shortly.
From: Stefan Beller <hidden> Date: 2017-01-06 21:03:42
unpack-trees is a central file needed for the understanding
of working tree manipulation. To help with the understanding
refactor the code to be more readable.
The first patch was a standalone patch 8 days ago;
now incorporated into this series as a v3,
reducing the scope of the checkout state.
The second patch removes a single continue statement;
it needed some digging to explain, but looks trivial.
The last 3 patches shorten the check_updates function by adding more
functions. If we ever want to parallelize file IO then these smaller
functions would be the scope to do it, keeping the check_updates as
a high level function guiding through the steps what is happening during
a working tree update.
Thanks,
Stefan
Stefan Beller (5):
unpack-trees: move checkout state into check_updates
unpack-trees: remove unneeded continue
unpack-trees: factor progress setup out of check_updates
unpack-trees: factor file removal out of check_updates
unpack-trees: factor working tree update out of check_updates
unpack-trees.c | 96 ++++++++++++++++++++++++++++++++++++++--------------------
1 file changed, 64 insertions(+), 32 deletions(-)
--
2.11.0.31.g919a8d0.dirty
From: Stefan Beller <hidden> Date: 2017-01-06 21:03:46
The checkout state was introduced via 16da134b1f9
(read-trees: refactor the unpack_trees() part, 2006-07-30). An attempt to
refactor the checkout state was done in b56aa5b268e (unpack-trees: pass
checkout state explicitly to check_updates(), 2016-09-13), but we can
go even further.
The `struct checkout state` is not used in unpack_trees apart from
initializing it, so move it into the function that makes use of it,
which is `check_updates`.
Reviewed-by: René Scharfe <redacted>
Signed-off-by: Stefan Beller <redacted>
---
unpack-trees.c | 26 +++++++++++++-------------
1 file changed, 13 insertions(+), 13 deletions(-)
@@ -237,77 +237,82 @@ static void display_error_msgs(struct unpack_trees_options *o)}/**Unlinkthelastcomponentandscheduletheleadingdirectoriesfor*removal,suchthatemptydirectoriesgetremoved.*/staticvoidunlink_entry(conststructcache_entry*ce){if(!check_leading_path(ce->name,ce_namelen(ce)))return;if(remove_or_warn(ce->ce_mode,ce->name))return;schedule_dir_for_removal(ce->name,ce_namelen(ce));}-staticintcheck_updates(structunpack_trees_options*o,-conststructcheckout*state)+staticintcheck_updates(structunpack_trees_options*o){unsignedcnt=0,total=0;+inti,errs=0;+structprogress*progress=NULL;structindex_state*index=&o->result;-inti;-interrs=0;+structcheckoutstate=CHECKOUT_INIT;++state.force=1;+state.quiet=1;+state.refresh_cache=1;+state.istate=index;if(o->update&&o->verbose_update){for(total=cnt=0;cnt<index->cache_nr;cnt++){conststructcache_entry*ce=index->cache[cnt];if(ce->ce_flags&(CE_UPDATE|CE_WT_REMOVE))total++;}progress=start_progress_delay(_("Checking out files"),total,50,1);cnt=0;}if(o->update)-git_attr_set_direction(GIT_ATTR_CHECKOUT,&o->result);+git_attr_set_direction(GIT_ATTR_CHECKOUT,index);for(i=0;i<index->cache_nr;i++){conststructcache_entry*ce=index->cache[i];if(ce->ce_flags&CE_WT_REMOVE){display_progress(progress,++cnt);if(o->update&&!o->dry_run)unlink_entry(ce);continue;}}-remove_marked_cache_entries(&o->result);+remove_marked_cache_entries(index);remove_scheduled_dirs();for(i=0;i<index->cache_nr;i++){structcache_entry*ce=index->cache[i];if(ce->ce_flags&CE_UPDATE){if(ce->ce_flags&CE_WT_REMOVE)die("BUG: both update and delete flags are set on %s",ce->name);display_progress(progress,++cnt);ce->ce_flags&=~CE_UPDATE;if(o->update&&!o->dry_run){-errs|=checkout_entry(ce,state,NULL);+errs|=checkout_entry(ce,&state,NULL);}}}stop_progress(&progress);if(o->update)git_attr_set_direction(GIT_ATTR_CHECKIN,NULL);returnerrs!=0;}staticintverify_uptodate_sparse(conststructcache_entry*ce,structunpack_trees_options*o);staticintverify_absent_sparse(conststructcache_entry*ce,enumunpack_trees_error_types,structunpack_trees_options*o);
@@ -1113,38 +1118,33 @@ static void mark_new_skip_worktree(struct exclude_list *el,staticintverify_absent(conststructcache_entry*,enumunpack_trees_error_types,structunpack_trees_options*);/**N-waymerge"len"trees.Returns0onsuccess,-1onfailuretomanipulatethe*resultingindex,-2onfailuretoreflectthechangestotheworktree.**CE_ADDED,CE_UNPACKEDandCE_NEW_SKIP_WORKTREEareusedinternally*/intunpack_trees(unsignedlen,structtree_desc*t,structunpack_trees_options*o){inti,ret;staticstructcache_entry*dfc;structexclude_listel;-structcheckoutstate=CHECKOUT_INIT;if(len>MAX_UNPACK_TREES)die("unpack_trees takes at most %d trees",MAX_UNPACK_TREES);-state.force=1;-state.quiet=1;-state.refresh_cache=1;-state.istate=&o->result;memset(&el,0,sizeof(el));if(!core_apply_sparse_checkout||!o->update)o->skip_sparse_checkout=1;if(!o->skip_sparse_checkout){char*sparse=git_pathdup("info/sparse-checkout");if(add_excludes_from_file_to_list(sparse,"",0,&el,0)<0)o->skip_sparse_checkout=1;elseo->el=⪙free(sparse);}memset(&o->result,0,sizeof(o->result));o->result.initialized=1;
@@ -1257,31 +1257,31 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_optionsif(ret<0)gotoreturn_failed;/**Sparsecheckoutismeanttonarrowdowncheckoutarea*butitdoesnotmakesensetonarrowdowntoemptyworking*tree.Thisisusuallyamistakeinsparsecheckoutrules.*Donotallowuserstodothat.*/if(o->result.cache_nr&&empty_worktree){ret=unpack_failed(o,"Sparse checkout leaves no entry on working directory");gotodone;}}o->src_index=NULL;-ret=check_updates(o,&state)?(-2):0;+ret=check_updates(o)?(-2):0;if(o->dst_index){if(!ret){if(!o->result.cache_tree)o->result.cache_tree=cache_tree();if(!cache_tree_fully_valid(o->result.cache_tree))cache_tree_update(&o->result,WRITE_TREE_SILENT|WRITE_TREE_REPAIR);}discard_index(o->dst_index);*o->dst_index=o->result;}else{discard_index(&o->result);}
From: Stefan Beller <hidden> Date: 2017-01-06 21:04:13
This makes check_updates shorter and easier to understand.
Signed-off-by: Stefan Beller <redacted>
---
unpack-trees.c | 40 ++++++++++++++++++++++++++--------------
1 file changed, 26 insertions(+), 14 deletions(-)
@@ -275,67 +275,79 @@ static struct progress *get_progress(struct unpack_trees_options *o)structindex_state*index=&o->result;if(!o->update||!o->verbose_update)returnNULL;for(;cnt<index->cache_nr;cnt++){conststructcache_entry*ce=index->cache[cnt];if(ce->ce_flags&(CE_UPDATE|CE_WT_REMOVE))total++;}returnstart_progress_delay(_("Checking out files"),total,50,1);}-staticintcheck_updates(structunpack_trees_options*o)+staticintupdate_working_tree_files(structunpack_trees_options*o,+structprogress*progress,+unsignedstart_cnt){-unsignedcnt=0;+unsignedcnt=start_cnt;inti,errs=0;-structprogress*progress=NULL;structindex_state*index=&o->result;structcheckoutstate=CHECKOUT_INIT;state.force=1;state.quiet=1;state.refresh_cache=1;state.istate=index;-progress=get_progress(o);--if(o->update)-git_attr_set_direction(GIT_ATTR_CHECKOUT,index);--cnt=remove_workingtree_files(o,progress);-remove_marked_cache_entries(index);-remove_scheduled_dirs();-for(i=0;i<index->cache_nr;i++){structcache_entry*ce=index->cache[i];if(ce->ce_flags&CE_UPDATE){if(ce->ce_flags&CE_WT_REMOVE)die("BUG: both update and delete flags are set on %s",ce->name);display_progress(progress,++cnt);ce->ce_flags&=~CE_UPDATE;-if(o->update&&!o->dry_run){+if(o->update&&!o->dry_run)errs|=checkout_entry(ce,&state,NULL);-}}}++returnerrs;+}++staticintcheck_updates(structunpack_trees_options*o)+{+structprogress*progress=NULL;+structindex_state*index=&o->result;+interrs;+unsignedtotal_removed;++progress=get_progress(o);++if(o->update)+git_attr_set_direction(GIT_ATTR_CHECKOUT,index);++total_removed=remove_workingtree_files(o,progress);+remove_marked_cache_entries(index);+remove_scheduled_dirs();+errs=update_working_tree_files(o,progress,total_removed);+stop_progress(&progress);if(o->update)git_attr_set_direction(GIT_ATTR_CHECKIN,NULL);returnerrs!=0;}staticintverify_uptodate_sparse(conststructcache_entry*ce,structunpack_trees_options*o);staticintverify_absent_sparse(conststructcache_entry*ce,enumunpack_trees_error_types,structunpack_trees_options*o);staticintapply_sparse_checkout(structindex_state*istate,structcache_entry*ce,structunpack_trees_options*o)
From: Stefan Beller <hidden> Date: 2017-01-06 21:04:15
This makes check_updates shorter and easier to understand.
Signed-off-by: Stefan Beller <redacted>
---
unpack-trees.c | 29 +++++++++++++++++++++--------
1 file changed, 21 insertions(+), 8 deletions(-)
@@ -273,39 +293,32 @@ static int check_updates(struct unpack_trees_options *o)inti,errs=0;structprogress*progress=NULL;structindex_state*index=&o->result;structcheckoutstate=CHECKOUT_INIT;state.force=1;state.quiet=1;state.refresh_cache=1;state.istate=index;progress=get_progress(o);if(o->update)git_attr_set_direction(GIT_ATTR_CHECKOUT,index);-for(i=0;i<index->cache_nr;i++){-conststructcache_entry*ce=index->cache[i];-if(ce->ce_flags&CE_WT_REMOVE){-display_progress(progress,++cnt);-if(o->update&&!o->dry_run)-unlink_entry(ce);-}-}+cnt=remove_workingtree_files(o,progress);remove_marked_cache_entries(index);remove_scheduled_dirs();for(i=0;i<index->cache_nr;i++){structcache_entry*ce=index->cache[i];if(ce->ce_flags&CE_UPDATE){if(ce->ce_flags&CE_WT_REMOVE)die("BUG: both update and delete flags are set on %s",ce->name);display_progress(progress,++cnt);ce->ce_flags&=~CE_UPDATE;if(o->update&&!o->dry_run){errs|=checkout_entry(ce,&state,NULL);}
From: Stefan Beller <hidden> Date: 2017-01-06 21:04:29
This makes check_updates shorter and easier to understand.
Signed-off-by: Stefan Beller <redacted>
---
unpack-trees.c | 32 ++++++++++++++++++++------------
1 file changed, 20 insertions(+), 12 deletions(-)
From: Stefan Beller <hidden> Date: 2017-01-06 21:04:32
The continue is the last statement in the loop, so not needed.
This situation arose in 700e66d66 (2010-07-30, unpack-trees: let
read-tree -u remove index entries outside sparse area) when statements
after the continue were removed.
Signed-off-by: Stefan Beller <redacted>
---
unpack-trees.c | 1 -
1 file changed, 1 deletion(-)
@@ -272,31 +272,30 @@ static int check_updates(struct unpack_trees_options *o)progress=start_progress_delay(_("Checking out files"),total,50,1);cnt=0;}if(o->update)git_attr_set_direction(GIT_ATTR_CHECKOUT,index);for(i=0;i<index->cache_nr;i++){conststructcache_entry*ce=index->cache[i];if(ce->ce_flags&CE_WT_REMOVE){display_progress(progress,++cnt);if(o->update&&!o->dry_run)unlink_entry(ce);-continue;}}remove_marked_cache_entries(index);remove_scheduled_dirs();for(i=0;i<index->cache_nr;i++){structcache_entry*ce=index->cache[i];if(ce->ce_flags&CE_UPDATE){if(ce->ce_flags&CE_WT_REMOVE)die("BUG: both update and delete flags are set on %s",ce->name);display_progress(progress,++cnt);ce->ce_flags&=~CE_UPDATE;if(o->update&&!o->dry_run){