Sparse checkout resurrection
cover Sparse checkout resurrection
Hi,
I rip most of the code from my sparse checkout series, leaving only basic stuff. Now there is no CE_NO_CHECKOUT bit (CE_VALID will be used), no sparse pattterns. You will need to provide a script, namely "git-shape-workdir", to specify how you want to shape your workdir. That's all. No porcelain, only plumbing. Need to add some options to read-tree and checkout in order to enable/disable this, but I haven't got that far.
The first four patches is more or less CE_VALID fixups. Real stuff is in the last patch. Sparse patterns may return as a separate script. Currently no idea how the porcelain UI should be. The only thing that is probably impossible this way is "git clone --sparse". You may need to "clone -n" first, then apply sparse checkout later.
Comments?
Nguyễn Thái Ng�c Duy (5):
Prevent diff machinery from examining worktree outside sparse checkout
grep: skip files outside sparse checkout area
gitignore: read from index if .gitignore is not in worktree unpack_trees(): keep track of unmerged entries unpack_trees(): add support for sparse checkout
Documentation/git-grep.txt | 4 +- builtin-grep.c | 7 ++- builtin-read-tree.c | 6 ++- cache.h | 4 + diff-lib.c | 5 +- diff.c | 4 +- dir.c | 70 ++++++++++++------ t/t3001-ls-files-others-exclude.sh | 20 +++++ unpack-trees.c | 140 ++++++++++++++++++++++++++++++++++-- unpack-trees.h | 4 + 10 files changed, 229 insertions(+), 35 deletions(-)
PATCH 1/5 Prevent diff machinery from examining worktree outside sparse checkout
--- diff-lib.c | 5 +++-- diff.c | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-)
diff-lib.c +3 -2
@@ -159,7 +159,7 @@ int run_diff_files(struct rev_info *revs, unsigned int option)continue ; } -if (ce_uptodate (ce)) +if (ce_uptodate (ce) || ce->ce_flags &CE_VALID )continue ; changed =check_removed (ce, &st);
@@ -337,6 +337,8 @@ static void do_oneway_diff(struct unpack_trees_options *o,struct rev_info *revs = o->unpack_data;int match_missing, cached; +/* if the entry is not checked out, don't examine work tree */ + cached = o->index_only || (idx && idx->ce_flags &CE_VALID );/* * Backward compatibility wart - "diff-index -m" does * not mean "do not ignore merges", but "match_missing".
@@ -344,7 +346,6 @@ static void do_oneway_diff(struct unpack_trees_options *o,* But with the revision flag parsing, that's found in * "!revs->ignore_merges". */ - cached = o->index_only; match_missing = !revs->ignore_merges;if (cached && idx &&ce_stage (idx)) {
diff.c +3 -1
@@ -1810,8 +1810,10 @@ static int reuse_worktree_file(const char *name, const unsigned char *sha1, int/* * If ce matches the file in the work tree, we can reuse it. +* For sparse checkout case, ce_uptodate() may be true although +* the file may or may not exist in the work tree. */ -if (ce_uptodate (ce) || +if ((ce_uptodate (ce) && !(ce->ce_flags &CE_VALID )) || (!lstat (name, &st) && !ce_match_stat (ce, &st,0 )))return 1 ;
Signature · 1 line
1.6.3.2.448.gdf8b6
PATCH 2/5 grep: skip files outside sparse checkout area
--- Documentation/git-grep.txt | 4 +++- builtin-grep.c | 7 ++++++- 2 files changed, 9 insertions(+), 2 deletions(-)
Documentation/git-grep.txt +3 -1
@@ -27,7 +27,9 @@ SYNOPSIS DESCRIPTION ----------- Look for specified patterns in the working tree files, blobs -registered in the index file, or given tree objects. +registered in the index file, or given tree objects. By default +it will search in the working tree files. When in sparse checkout +mode, it only searches checked-out files. OPTIONS
builtin-grep.c +6 -1
@@ -455,6 +455,8 @@ static int external_grep(struct grep_opt *opt, const char **paths, int cached)continue ;if (!pathspec_matches (paths, ce->name, opt->max_depth))continue ; +if (ce->ce_flags &CE_VALID ) +continue ; name = ce->name;if (name[0 ] =='-' ) {int len =ce_namelen (ce);
@@ -522,8 +524,11 @@ static int grep_cache(struct grep_opt *opt, const char **paths, int cached,continue ; hit |=grep_sha1 (opt, ce->sha1, ce->name,0 ); } -else +else { +if (ce->ce_flags &CE_VALID ) +continue ; hit |=grep_file (opt, ce->name); + }if (ce_stage (ce)) { do { nr++;
Signature · 1 line
1.6.3.2.448.gdf8b6
PATCH 3/5 gitignore: read from index if .gitignore is not in worktree
this allows to use .gitignore in sparse mode, where .gitignore may or may not be checked out (CE_VALID)
--- dir.c | 70 ++++++++++++++++++++++++------------ t/t3001-ls-files-others-exclude.sh | 20 ++++++++++ 2 files changed, 67 insertions(+), 23 deletions(-)
dir.c +47 -23
@@ -200,11 +200,36 @@ void add_exclude(const char *string, const char *base, which->excludes[which->nr++] = x; } +static void *read_index_data (const char *path,size_t *size) +{ +int pos, len; +unsigned long sz; +enum object_type type; +void *data; +struct index_state *istate = &the_index; + + len =strlen (path); + pos =index_name_pos (istate, path, len); +if (pos <0 ) +return NULL ; +/* only applies to no-checkout items */ +if (!(istate->cache[pos]->ce_flags &CE_VALID )) +return NULL ; + data =read_sha1_file (istate->cache[pos]->sha1, &type, &sz); +if (!data || type !=OBJ_BLOB ) { +free (data); +return NULL ; + } + *size =xsize_t (sz); +return data; +} +static int add_excludes_from_file_1 (const char *fname,const char *base,int baselen,char **buf_p, -struct exclude_list *which) +struct exclude_list *which, +int check_index) {struct stat st;int fd, i;
@@ -212,27 +237,31 @@ static int add_excludes_from_file_1(const char *fname,char *buf, *entry; fd =open (fname,O_RDONLY ); -if (fd <0 ||fstat (fd, &st) <0 ) - goto err; - size =xsize_t (st.st_size); -if (size ==0 ) { -close (fd); -return 0 ; +if (fd <0 ||fstat (fd, &st) <0 ) { +if (0 <= fd) +close (fd); +if (!check_index || (buf =read_index_data (fname, &size)) ==NULL ) +return -1 ; } - buf =xmalloc (size+1 ); -if (read_in_full (fd, buf, size) != size) - { -free (buf); - goto err; +else { + size =xsize_t (st.st_size); +if (size ==0 ) { +close (fd); +return 0 ; + } + buf =xmalloc (size); +if (read_in_full (fd, buf, size) != size) { +close (fd); +return -1 ; + } +close (fd); } -close (fd);if (buf_p) *buf_p = buf; - buf[size++] ='\n' ; entry = buf; -for (i =0 ; i < size; i++) { -if (buf[i] =='\n' ) { +for (i =0 ; i <= size; i++) { +if (i == size || buf[i] =='\n' ) {if (entry != buf + i && entry[0 ] !='#' ) { buf[i - (i && buf[i-1 ] =='\r' )] =0 ;add_exclude (entry, base, baselen, which);
@@ -241,17 +270,12 @@ static int add_excludes_from_file_1(const char *fname, } }return 0 ; - - err: -if (0 <= fd) -close (fd); -return -1 ; }void add_excludes_from_file (struct dir_struct *dir,const char *fname) {if (add_excludes_from_file_1 (fname,"" ,0 ,NULL , - &dir->exclude_list[EXC_FILE ]) <0 ) + &dir->exclude_list[EXC_FILE ],0 ) <0 )die ("cannot use %s as an exclude file" , fname); }
@@ -302,7 +326,7 @@ static void prep_exclude(struct dir_struct *dir, const char *base, int baselen)strcpy (dir->basebuf + stk->baselen, dir->exclude_per_dir);add_excludes_from_file_1 (dir->basebuf, dir->basebuf, stk->baselen, - &stk->filebuf, el); + &stk->filebuf, el,1 ); dir->exclude_stack = stk; current = stk->baselen; }
t/t3001-ls-files-others-exclude.sh +20 -0
@@ -85,6 +85,26 @@ test_expect_success \ >output && test_cmp expect output' +test_expect_success 'setup sparse gitignore' ' + git add .gitignore one/.gitignore one/two/.gitignore && + git update-index --no-checkout .gitignore one/.gitignore one/two/.gitignore && + rm .gitignore one/.gitignore one/two/.gitignore +' + +test_expect_success \ + 'git ls-files --others with various exclude options.' \ + 'git ls-files --others \ + --exclude=\*.6 \ + --exclude-per-directory=.gitignore \ + --exclude-from=.git/ignore \ + >output && + test_cmp expect output' + +test_expect_success 'restore gitignore' ' + git checkout .gitignore one/.gitignore one/two/.gitignore && + rm .git/index +' + cat > excludes-file <<\EOF *.[1-8] e*
Signature · 1 line
1.6.3.2.448.gdf8b6
PATCH 4/5 unpack_trees(): keep track of unmerged entries
"git read-tree --reset" will currently remove all unmerged entries in index before feeding the index to unpack_trees(). Because the lack of unmerged entries, these entries, when read from tree, will be seen as "new entries" by {one,two,three}way_merge().
This is fine for now. But for sparse checkout, it needs to know whether an entry is new entry, because it will handle it different way than already-in entry.
So the patch moves "unmerged entries removal" part into unpack_trees(), actually unpack_callback(). The function then can turn on o->has_unmerged flag, which can be utilized by sparse checkout.
--- builtin-read-tree.c | 6 +++++- unpack-trees.c | 7 +++++++ unpack-trees.h | 2 ++ 3 files changed, 14 insertions(+), 1 deletions(-)
builtin-read-tree.c +5 -1
@@ -113,7 +113,11 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix) argc =parse_options (argc, argv, unused_prefix, read_tree_options, read_tree_usage,0 ); -if (read_cache_unmerged () && (opts.prefix || opts.merge)) +if (opts.reset) { + opts.prune_unmerged =1 ; +read_cache (); + } +else if (read_cache_unmerged () && (opts.prefix || opts.merge))die ("You need to resolve your current index first" ); prefix_set = opts.prefix ?1 :0 ;
unpack-trees.c +7 -0
@@ -289,6 +289,7 @@ static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, str/* Are we supposed to look at the index too? */ if (o->merge) { + o->has_unmerged =0 ;while (o->pos < o->src_index->cache_nr) {struct cache_entry *ce = o->src_index->cache[o->pos];int cmp =compare_entry (ce, info, p);
@@ -308,6 +309,12 @@ static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, stradd_entry (o, ce,0 ,0 );return mask; } + +if (o->prune_unmerged) { + o->has_unmerged =1 ; +/* leave src[0] as NULL and go over all other staged entries */ +continue ; + } } src[0 ] = ce; }
unpack-trees.h +2 -0
@@ -26,6 +26,8 @@ struct unpack_trees_options { verbose_update, aggressive, skip_unmerged, + prune_unmerged, + has_unmerged, initial_checkout, diff_index_cached, gently;
Signature · 1 line
1.6.3.2.448.gdf8b6
PATCH 5/5 unpack_trees(): add support for sparse checkout
This patch teaches unpack_trees() to checkout/remove entries on working directories appropriately when sparse checkout area is changed. A helper "git shape-workdir" is needed to help determine which entry will be checked out, which will be not.
"git shape-workdir" will receive from stdin in this format
X\tpathname
where X is either
- '!' current entry is already CE_VALID
- 'N' current entry is "new" (it has not been in index before)
- '-' current entry is "normal" entry
"git shape-workdir" is expected to return either "1" or "0" immediately. "1" means the entry should be in workdir. "0" means setting CE_VALID and get rid of it from workdir.
--- cache.h | 4 ++ unpack-trees.c | 133 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- unpack-trees.h | 2 + 3 files changed, 133 insertions(+), 6 deletions(-)
cache.h +4 -0
@@ -177,6 +177,10 @@ struct cache_entry {#define CE_HASHED (0x100000)#define CE_UNHASHED (0x200000) +/* Only remove in work directory, not index */ +#define CE_WT_REMOVE (0x400000) +#define CE_NEW (0x800000) +/* * Extended on-disk flags */
unpack-trees.c +127 -6
209 lines not on this page · raw message
unpack-trees.h +2 -0
@@ -14,6 +14,8 @@ struct unpack_trees_error_msgs {const char *not_uptodate_dir;const char *would_lose_untracked;const char *bind_overlap; +const char *sparse_not_uptodate_file; +const char *would_lose_orphaned; };struct unpack_trees_options {
Signature · 1 line
1.6.3.2.448.gdf8b6
Re: [PATCH 5/5] unpack_trees(): add support for sparse checkout
Quoted 4 lines · Nguyễn Thái Ngọc Duy
This patch teaches
unpack_trees()to checkout/remove entries on working directories appropriately when sparse checkout area is changed. A helper "git shape-workdir" is needed to help determine which entry will be checked out, which will be not.
Wouldn't "git update-index --index-info" (perhaps extended) be enough?
Quoted 5 lines
"git shape-workdir" will receive from stdin in this format
X\tpathname
Quoted 8 lines
where X is either
- '!' current entry is alreadyCE_VALID
- 'N' current entry is "new" (it has not been in index before)
- '-' current entry is "normal" entry"git shape-workdir" is expected to return either "1" or "0" immediately. "1" means the entry should be in workdir. "0" means setting
CE_VALIDand get rid of it from workdir.
Just in case it would be better IMVHO if it returned "1\tpathname" or "2\tpathname". By the way, is 'pathname' quoted if necessary, and does git-shape-workdir support -z/--null option?
Signoff (also in some other patches in this series)?
Signature · 3 lines
Jakub Narebski Poland ShadeHawk on #git
Re: [PATCH 5/5] unpack_trees(): add support for sparse checkout
2009/7/29 Jakub Narebski <[email protected]>:
Quoted 8 lines
Quoted 4 lines · Nguyễn Thái Ngọc Duy
This patch teaches
unpack_trees()to checkout/remove entries on working directories appropriately when sparse checkout area is changed. A helper "git shape-workdir" is needed to help determine which entry will be checked out, which will be not.Wouldn't "git update-index --index-info" (perhaps extended) be enough?
It's a bit more complicated because the in-memory index is filled with various information and can't just be dumped out to be manipulated with "git update-index".
Quoted 10 lines
Quoted 10 lines
"git shape-workdir" will receive from stdin in this format
X\tpathname
where X is either
- '!' current entry is alreadyCE_VALID
- 'N' current entry is "new" (it has not been in index before)
- '-' current entry is "normal" entry
Quoted 7 lines
Quoted 3 lines
"git shape-workdir" is expected to return either "1" or "0" immediately. "1" means the entry should be in workdir. "0" means setting
CE_VALIDand get rid of it from workdir.Just in case it would be better IMVHO if it returned "1\tpathname" or "2\tpathname". By the way, is 'pathname' quoted if necessary, and does git-shape-workdir support -z/--null option?
It doesn't currently. Thanks for the suggestion.
Quoted 1 line
Signoff (also in some other patches in this series)?
This series is mainly for taking input on how git-shape-workdir should behave, what format is good... That's why I did not sign off. Anyway the series is not really well tested (and obvious lacks tests)
Signature · 1 line
Duy
Re: [PATCH 5/5] unpack_trees(): add support for sparse checkout
Quoted 7 lines · Nguyen Thai Ngoc Duy
2009/7/29 Jakub Narebski <[email protected]>:
Quoted 1 line
Signoff (also in some other patches in this series)?
This series is mainly for taking input on how git-shape-workdir should behave, what format is good... That's why I did not sign off. Anyway the series is not really well tested (and obvious lacks tests)
In that case it would be IMHO a good idea to mark those patches also by using "[PATCH/RFC m/n] ..." or "[RFC/PATCH m/n] ..." in subject.
Signature · 2 lines
Jakub Narebski Poland
Re: [PATCH 3/5] gitignore: read from index if .gitignore is not in worktree
In reply to Nguyễn Thái Ngọc DuyQuoted 2 lines · Nguyễn Thái Ngọc Duy
this allows to use .gitignore in sparse mode, where .gitignore may or may not be checked out (
CE_VALID)
Hmm, do we have a guarantee that we have read the index when .gitignore is used in all the codepath in the current codebase?
"git add" used to do the fill_directory() traversal before reading the index in some cases back in v1.6.0 days for a reason. "git add" does not do so anymore, but I am wondering if there are others.
Re: [PATCH 2/5] grep: skip files outside sparse checkout area
In reply to Nguyễn Thái Ngọc DuyQuoted 4 lines · Nguyễn Thái Ngọc Duy
--- Documentation/git-grep.txt | 4 +++- builtin-grep.c | 7 ++++++- 2 files changed, 9 insertions(+), 2 deletions(-)
I do not think you can call this "CE_VALID fixup". This is a possibly useful but nevertheless a new, different behaviour.
We probably want opt->ignoreassumeunchanged in struct grep_opt, and control it via grep.ignoreassumeunchanged configuration, that perhaps defaults to true (i.e. "ignore"), with a command line override.
Oh, and perhaps a shorter-and-sweeter name than "ignoreAssumeUnchanged".
builtin-grep.c static int external_grep(struct grep_opt *opt, const char **paths, int cached) · line 459
Quoted 10 lines
builtin-grep.c +2
-0@@ -455,6 +455,8 @@ static int external_grep(struct grep_opt *opt, const char **paths, int cached)continue ;if (!pathspec_matches (paths, ce->name, opt->max_depth))continue ; +if (ce->ce_flags &CE_VALID ) +continue ;
The test probably should come before pathspec_matches().
builtin-grep.c static int grep_cache(struct grep_opt *opt, const char **paths, int cached, · line 534
Quoted 13 lines
builtin-grep.c +4
-1@@ -522,8 +524,11 @@ static int grep_cache(struct grep_opt *opt, const char **paths, int cached,continue ; hit |=grep_sha1 (opt, ce->sha1, ce->name,0 ); } -else +else { +if (ce->ce_flags &CE_VALID ) +continue ; hit |=grep_file (opt, ce->name); + }if (ce_stage (ce)) { do { nr++;
I do not think the new code in this hunk will be reached as the other arm of the "else" greps in the index when CE_VALID is set.
Has this series been tested or self-reviewed yet?
Re: [PATCH 2/5] grep: skip files outside sparse checkout area
2009/7/31 Junio C Hamano <[email protected]>:
Quoted 17 lines
builtin-grep.c static int grep_cache(struct grep_opt *opt, const char **paths, int cached, · line 534
Quoted 13 lines
builtin-grep.c +4
-1@@ -522,8 +524,11 @@ static int grep_cache(struct grep_opt *opt, const char **paths, int cached, continue; hit |= grep_sha1(opt, ce->sha1, ce->name, 0); } - else + else { + if (ce->ce_flags & CE_VALID) + continue; hit |= grep_file(opt, ce->name); + } if (ce_stage(ce)) { do { nr++;I do not think the new code in this hunk will be reached as the other arm of the "else" greps in the index when
CE_VALIDis set.
Quoted 1 line
Has this series been tested or self-reviewed yet?
Yes and no. It had been tested and self-reviewed back when it was part of original series. There the new code was CE_NO_CHECKOUT check, not CE_VALID. I grepped through and replaced with CE_VALID to form a new draft in order to get comments about this direction, this time it was not well tested because my focus was in the last patch.
Signature · 1 line
Duy
Re: [PATCH 1/5] Prevent diff machinery from examining worktree outside sparse checkout
In reply to Nguyễn Thái Ngọc DuyQuoted 1 line · Nguyễn Thái Ngọc Duy
---
Sign off? Oh, this is an RFC... Ok...
Quoted 14 lines
diff-lib.c +2
-0@@ -337,6 +337,8 @@ static void do_oneway_diff(struct unpack_trees_options *o,struct rev_info *revs = o->unpack_data;int match_missing, cached; +/* if the entry is not checked out, don't examine work tree */ + cached = o->index_only || (idx && idx->ce_flags &CE_VALID );/* * Backward compatibility wart - "diff-index -m" does * not mean "do not ignore merges", but "match_missing".
diff-lib.c static void do_oneway_diff(struct unpack_trees_options *o, · line 351
Quoted 7 lines
diff-lib.c +0
-1@@ -344,7 +346,6 @@ static void do_oneway_diff(struct unpack_trees_options *o,* But with the revision flag parsing, that's found in * "!revs->ignore_merges". */ - cached = o->index_only; match_missing = !revs->ignore_merges;if (cached && idx &&ce_stage (idx)) {
The big comment is about match_missing and not about cached, so the code movement is good. When we pass NULL idx to this function, it means that the tree has it but the index doesn't, so it cannot possibly be marked as "exists in the index but is not checked out". So the hunk makes sense.
Quoted 5 lines
diff.c +0
-0
diff.c static int reuse_worktree_file(const char *name, const unsigned char *sha1, int · line 1818
Quoted 10 lines
diff.c +3
-1@@ -1810,8 +1810,10 @@ static int reuse_worktree_file(const char *name, const unsigned char *sha1, int/* * If ce matches the file in the work tree, we can reuse it. +* For sparse checkout case, ce_uptodate() may be true although +* the file may or may not exist in the work tree. */ -if (ce_uptodate (ce) || +if ((ce_uptodate (ce) && !(ce->ce_flags &CE_VALID )) || (!lstat (name, &st) && !ce_match_stat (ce, &st,0 )))return 1 ;
Why is this hunk necessary? A few lines above this we return 0 for any CE_VALID cache entry.
2009/7/28 Nguyễn Thái Ngọc Duy <[email protected]>:
Quoted 10 lines
Hi,
I rip most of the code from my sparse checkout series, leaving only basic stuff. Now there is no
CE_NO_CHECKOUTbit (CE_VALIDwill be used), no sparse pattterns. You will need to provide a script, namely "git-shape-workdir", to specify how you want to shape your workdir. That's all. No porcelain, only plumbing. Need to add some options to read-tree and checkout in order to enable/disable this, but I haven't got that far.
Quoted 8 lines
The first four patches is more or less
CE_VALIDfixups. Real stuff is in the last patch. Sparse patterns may return as a separate script. Currently no idea how the porcelain UI should be. The only thing that is probably impossible this way is "git clone --sparse". You may need to "clone -n" first, then apply sparse checkout later.Comments?
I don't know the git code enough to really comment on the details yet, but I just wanted to say that I'd love to see sparse checkout support in git. I tried your other patch series using a config variable instead of the "git-shape-workdir" script and I liked it (although I didn't thoroughly test it). It shouldn't be hard to write a git-shape-workdir script to parse a modules file to decide which files to include/exclude (I'm hoping to do that soon).
Judging by the surprisingly large number of people commenting in the recent git survey that they want some form of sparse/partial checkout support (myself included), I hope something like this patch series gets some traction.
BTW...Is there a repository I can clone from that has these changes and possibly future enhancements? I checked
<http://repo.or.cz/w/git/pclouds.git>, but I didn't see them.
2009/8/1 <[email protected]>:
Quoted 3 lines
BTW...Is there a repository I can clone from that has these changes and possibly future enhancements? I checked
<http://repo.or.cz/w/git/pclouds.git>, but I didn't see them.
No. This is far from usable. I think I'll make "git-shape-workdir" a hook, put an example hook that should cover basic cases and repost.
Signature · 1 line
Duy