From: Don Zickus <hidden> Date: 2016-06-15 22:44:44
When working with a lot of people who backport patches all day long, every
once in a while I get a patch that modifies the same file more than once
inside the same patch. git-apply either fails if the second change relies
on the first change or silently drops the first change if the second change
is independent.
The silent part is the scary scenario for us. Also this behaviour is
different from the patch-utils.
I have modified git-apply to cache the filenames of files it modifies such
that if a later patch chunk modifies a file in the cache it will buffer the
previously changed file instead of reading the original file from disk.
Logic has been put in to handle creations/deletions/renames/copies. All the
relevant tests of git-apply succeed.
A new test has been added to cover the two cases I addressed.
The fix is relatively straight-forward. But I'm not sure if this new
behaviour is something the git community wants.
Signed-off-by: Don Zickus <redacted>
---
builtin-apply.c | 69 +++++++++++++++++++++++++++++++++++++++++++++-
t/t4127-apply-same-fn.sh | 40 ++++++++++++++++++++++++++
2 files changed, 108 insertions(+), 1 deletions(-)
create mode 100755 t/t4127-apply-same-fn.sh
@@ -2176,6 +2188,51 @@ static int read_file_or_gitlink(struct cache_entry *ce, struct strbuf *buf)return0;}+structpatch*in_fn_cache(char*name)+{+structfn_cache*p;++for(p=fn_cache_top;p;p=p->next){+if(!strcmp(name,p->name))+returnp->patch;+}++returnNULL;+}++voidadd_to_fn_cache(char*name,structpatch*patch)+{+structfn_cache*fn_cache;++/* Always add new_name unless patch is a deletion */+if(name!=NULL){+fn_cache=xmalloc(sizeof(*fn_cache));++/* assuming the pointer to filename won't disappear */+fn_cache->name=name;+fn_cache->patch=patch;+fn_cache->next=fn_cache_top;++fn_cache_top=fn_cache;+}++/* skip normal diffs, creations and copies */+/*+*storeafailureonrename/deletioncasesbecause+*laterchunksshouldn'tpatcholdnames+*/+if((name==NULL)||(patch->is_rename)){+fn_cache=xmalloc(sizeof(*fn_cache));++/* assuming the pointer to filename won't disappear */+fn_cache->name=patch->old_name;+fn_cache->patch=(structpatch*)-1;+fn_cache->next=fn_cache_top;++fn_cache_top=fn_cache;+}+}+staticintapply_data(structpatch*patch,structstat*st,structcache_entry*ce){structstrbufbuf;
@@ -2188,7 +2245,16 @@ static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *if(read_file_or_gitlink(ce,&buf))returnerror("read of %s failed",patch->old_name);}elseif(patch->old_name){-if(S_ISGITLINK(patch->old_mode)){+structpatch*tpatch=in_fn_cache(patch->old_name);++if(tpatch!=NULL){+if(tpatch==(structpatch*)-1){+returnerror("patch %s has been renamed/deleted",+patch->old_name);+}+/* We have a patched copy in memory use that */+strbuf_add(&buf,tpatch->result,tpatch->resultsize);+}elseif(S_ISGITLINK(patch->old_mode)){if(ce){read_file_or_gitlink(ce,&buf);}else{
@@ -2211,6 +2277,7 @@ static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *return-1;/* note with --reject this succeeds. */patch->result=image.buf;patch->resultsize=image.len;+add_to_fn_cache(patch->new_name,patch);free(image.line_allocated);if(0<patch->is_delete&&patch->resultsize)
@@ -0,0 +1,40 @@+#!/bin/sh++test_description='apply same filename'++../test-lib.sh++test_expect_successsetup'+foriinabcdefghijklm+do+echo$i+done>same_fn&&+gitaddsame_fn&&+gitcommit-minitial+'+test_expect_success'apply same filename with independent changes''+sed-i-e"s/^d/z/"same_fn&&+gitdiff>patch0&&+gitaddsame_fn&&+sed-i-e"s/^i/y/"same_fn&&+gitdiff>>patch0&&+cpsame_fnsame_fn2&&+gitreset--hard&&+git-applypatch0&&+diffsame_fnsame_fn2+'++test_expect_success'apply same filename with overlapping changes''+gitreset--hard+sed-i-e"s/^d/z/"same_fn&&+gitdiff>patch0&&+gitaddsame_fn&&+sed-i-e"s/^e/y/"same_fn&&+gitdiff>>patch0&&+cpsame_fnsame_fn2&&+gitreset--hard&&+git-applypatch0&&+diffsame_fnsame_fn2+'++test_done
From: Johannes Schindelin <hidden> Date: 2016-06-15 22:44:44
Hi,
On Fri, 13 Jun 2008, Don Zickus wrote:
When working with a lot of people who backport patches all day long,
every once in a while I get a patch that modifies the same file more
than once inside the same patch. git-apply either fails if the second
change relies on the first change or silently drops the first change if
the second change is independent.
The silent part is the scary scenario for us. Also this behaviour is
different from the patch-utils.
I have modified git-apply to cache the filenames of files it modifies
such that if a later patch chunk modifies a file in the cache it will
buffer the previously changed file instead of reading the original file
from disk.
Logic has been put in to handle creations/deletions/renames/copies. All the
relevant tests of git-apply succeed.
A new test has been added to cover the two cases I addressed.
The fix is relatively straight-forward. But I'm not sure if this new
behaviour is something the git community wants.
The scary part is about adding a linked list for file names you want to
look up.
Not that performance matters here, I guess, but we _already_ have
something much more efficient in Git, namely path-lists.
You could use that, and end up with a substantially smaller patch.
Ciao,
Dscho
From: Don Zickus <hidden> Date: 2016-06-15 22:44:44
On Fri, Jun 13, 2008 at 09:32:52PM +0100, Johannes Schindelin wrote:
Hi,
On Fri, 13 Jun 2008, Don Zickus wrote:
quoted
When working with a lot of people who backport patches all day long,
every once in a while I get a patch that modifies the same file more
than once inside the same patch. git-apply either fails if the second
change relies on the first change or silently drops the first change if
the second change is independent.
The silent part is the scary scenario for us. Also this behaviour is
different from the patch-utils.
I have modified git-apply to cache the filenames of files it modifies
such that if a later patch chunk modifies a file in the cache it will
buffer the previously changed file instead of reading the original file
from disk.
Logic has been put in to handle creations/deletions/renames/copies. All the
relevant tests of git-apply succeed.
A new test has been added to cover the two cases I addressed.
The fix is relatively straight-forward. But I'm not sure if this new
behaviour is something the git community wants.
The scary part is about adding a linked list for file names you want to
look up.
Not that performance matters here, I guess, but we _already_ have
something much more efficient in Git, namely path-lists.
You could use that, and end up with a substantially smaller patch.
Thanks for the feedback. I was unaware of path-lists. I'll try to find
an example and implement it if it works.
Cheers,
Don
Signed-off-by: Miklos Vajna <redacted>
---
On Fri, Jun 13, 2008 at 09:32:52PM +0100, Johannes Schindelin [off-list ref] wrote:
Not that performance matters here, I guess, but we _already_ have
something much more efficient in Git, namely path-lists.
You could use that, and end up with a substantially smaller patch.
I just noticed that Documentation/technical/api-path-list.txt is almost
empty. Here is an attempt to document the path-list API.
Documentation/technical/api-path-list.txt | 92 +++++++++++++++++++++++++++-
1 files changed, 88 insertions(+), 4 deletions(-)
@@ -1,9 +1,93 @@ path-list API =============-Talk about <path-list.h>, things like+The path_list API offers a data structure and functions to handle sorted+and unsorted string lists.-* it is not just paths but strings in general;-* the calling sequence.+The name is a bit misleading, a path_list may store not only paths but+strings in general.-(Dscho)+The caller:++. Allocates and clears (`memset(&list, '0', sizeof(path_list));`) a+ `struct path_list` variable.++. Initializes the members. You can manually set the `items` member, but+ then you have to set `nr`, accordingly. Also don't forget to set+ `strdup_paths` if you need it.++. Adds new items to the list, using `path_list_append` or `path_list_insert`.++. Can check if a string is in the list using `path_list_has_path` or+ `unsorted_path_list_has_path` and get it from the list using+ `path_list_lookup` for sorted lists.++. Can sort an unsorted list using `sort_path_list`.++. Finally it should free the list using `path_list_clear`.++Functions+---------++* General ones (works with sorted and unsorted lists as well)++`print_path_list`::++ Dump a path_list to stdout, useful mainly for debugging purposes. It+ can take an optional header argument and it writes out the+ string-pointer pairs of the path_list, each one in its own line.++`path_list_clear`::++ Free a path_list. The `path` pointer of the items will be freed in case+ the `strdup_paths` member of the path_list is set. The second parameter+ controls if the `util` pointer of the items should be freed or not.++* Functions for sorted lists only++`path_list_has_path`::++ Determine if the path_list has a given string or not.++`path_list_insert`::++ Insert a new element to the path_list. The returned pointer can be handy+ if you want to write something to the `util` pointer of the+ path_list_item containing the just added string.++`path_list_lookup`::++ Look up a given string in the path_list, returning the containing+ path_list_item. If the string is not found, NULL is returned.++* Functions for unsorted lists only++`path_list_append`::++ Append a new string to the end of the path_list.++`sort_path_list`::++ Make an unsorted list sorted.++`unsorted_path_list_has_path`::++ It's like `path_list_has_path()` but for unsorted lists.++Data structures+---------------++* `struct path_list_item`++Represent an item of the list. The `path` member is a pointer to the+string, and you may use the `util` member for any purpose, if you want.++* `struct path_list`++Represents the list itself.++. The array of items are available via the `items` member.+. The `nr` member contains the number of items stored in the list.+. The `alloc` member is used for `ALLOC_GROW()`.+. Setting the `strdup_paths` member to 1 means that the added paths are+ copied to the path list and not just a pointer to them is stored.
Signed-off-by: Miklos Vajna <redacted>
---
On Sat, Jun 14, 2008 at 01:30:57AM +0200, Olivier Marin [off-list ref] wrote:
quoted
+. Allocates and clears (`memset(&list, '0', sizeof(path_list));`) a
+ `struct path_list` variable.
Don't you mean sizeof(list) here?
Right, it was a typo. Thanks for the correction.
Also I just noticed that the '0' did not render properly in asciidoc,
using \'0' fixes the issue.
Updated patch below.
Documentation/technical/api-path-list.txt | 92 +++++++++++++++++++++++++++-
1 files changed, 88 insertions(+), 4 deletions(-)
@@ -1,9 +1,93 @@ path-list API =============-Talk about <path-list.h>, things like+The path_list API offers a data structure and functions to handle sorted+and unsorted string lists.-* it is not just paths but strings in general;-* the calling sequence.+The name is a bit misleading, a path_list may store not only paths but+strings in general.-(Dscho)+The caller:++. Allocates and clears (`memset(&list, \'0', sizeof(list));`) a+ `struct path_list` variable.++. Initializes the members. You can manually set the `items` member, but+ then you have to set `nr`, accordingly. Also don't forget to set+ `strdup_paths` if you need it.++. Adds new items to the list, using `path_list_append` or `path_list_insert`.++. Can check if a string is in the list using `path_list_has_path` or+ `unsorted_path_list_has_path` and get it from the list using+ `path_list_lookup` for sorted lists.++. Can sort an unsorted list using `sort_path_list`.++. Finally it should free the list using `path_list_clear`.++Functions+---------++* General ones (works with sorted and unsorted lists as well)++`print_path_list`::++ Dump a path_list to stdout, useful mainly for debugging purposes. It+ can take an optional header argument and it writes out the+ string-pointer pairs of the path_list, each one in its own line.++`path_list_clear`::++ Free a path_list. The `path` pointer of the items will be freed in case+ the `strdup_paths` member of the path_list is set. The second parameter+ controls if the `util` pointer of the items should be freed or not.++* Functions for sorted lists only++`path_list_has_path`::++ Determine if the path_list has a given string or not.++`path_list_insert`::++ Insert a new element to the path_list. The returned pointer can be handy+ if you want to write something to the `util` pointer of the+ path_list_item containing the just added string.++`path_list_lookup`::++ Look up a given string in the path_list, returning the containing+ path_list_item. If the string is not found, NULL is returned.++* Functions for unsorted lists only++`path_list_append`::++ Append a new string to the end of the path_list.++`sort_path_list`::++ Make an unsorted list sorted.++`unsorted_path_list_has_path`::++ It's like `path_list_has_path()` but for unsorted lists.++Data structures+---------------++* `struct path_list_item`++Represent an item of the list. The `path` member is a pointer to the+string, and you may use the `util` member for any purpose, if you want.++* `struct path_list`++Represents the list itself.++. The array of items are available via the `items` member.+. The `nr` member contains the number of items stored in the list.+. The `alloc` member is used for `ALLOC_GROW()`.+. Setting the `strdup_paths` member to 1 means that the added paths are+ copied to the path list and not just a pointer to them is stored.
@@ -1,9 +1,93 @@ path-list API =============-Talk about <path-list.h>, things like+The path_list API offers a data structure and functions to handle sorted+and unsorted string lists.-* it is not just paths but strings in general;-* the calling sequence.+The name is a bit misleading, a path_list may store not only paths but+strings in general.-(Dscho)+The caller:++. Allocates and clears (`memset(&list, \'\0', sizeof(list));`) a+ `struct path_list` variable.++. Initializes the members. You can manually set the `items` member, but+ then you have to set `nr`, accordingly. Also don't forget to set+ `strdup_paths` if you need it.++. Adds new items to the list, using `path_list_append` or `path_list_insert`.++. Can check if a string is in the list using `path_list_has_path` or+ `unsorted_path_list_has_path` and get it from the list using+ `path_list_lookup` for sorted lists.++. Can sort an unsorted list using `sort_path_list`.++. Finally it should free the list using `path_list_clear`.++Functions+---------++* General ones (works with sorted and unsorted lists as well)++`print_path_list`::++ Dump a path_list to stdout, useful mainly for debugging purposes. It+ can take an optional header argument and it writes out the+ string-pointer pairs of the path_list, each one in its own line.++`path_list_clear`::++ Free a path_list. The `path` pointer of the items will be freed in case+ the `strdup_paths` member of the path_list is set. The second parameter+ controls if the `util` pointer of the items should be freed or not.++* Functions for sorted lists only++`path_list_has_path`::++ Determine if the path_list has a given string or not.++`path_list_insert`::++ Insert a new element to the path_list. The returned pointer can be handy+ if you want to write something to the `util` pointer of the+ path_list_item containing the just added string.++`path_list_lookup`::++ Look up a given string in the path_list, returning the containing+ path_list_item. If the string is not found, NULL is returned.++* Functions for unsorted lists only++`path_list_append`::++ Append a new string to the end of the path_list.++`sort_path_list`::++ Make an unsorted list sorted.++`unsorted_path_list_has_path`::++ It's like `path_list_has_path()` but for unsorted lists.++Data structures+---------------++* `struct path_list_item`++Represent an item of the list. The `path` member is a pointer to the+string, and you may use the `util` member for any purpose, if you want.++* `struct path_list`++Represents the list itself.++. The array of items are available via the `items` member.+. The `nr` member contains the number of items stored in the list.+. The `alloc` member is used for `ALLOC_GROW()`.+. Setting the `strdup_paths` member to 1 means that the added paths are+ copied to the path list and not just a pointer to them is stored.
+. Allocates and clears (`memset(&list, \'\0', sizeof(list));`) a
+ `struct path_list` variable.
What about just `memset(&list, 0, sizeof(list))` instead?
It's readable in the text format, clean in html and this is the way
memset() is used.
Olivier.
From: Johannes Schindelin <hidden> Date: 2016-06-15 22:44:45
Hi,
On Sat, 14 Jun 2008, Miklos Vajna wrote:
Signed-off-by: Miklos Vajna <redacted>
Thanks for doing this... I meant to document it after pushing the
path_list -> string_list patch.
Speaking of which: Junio, could you give me any clue how you would like to
proceed with that patch?
quoted hunk
@@ -1,9 +1,93 @@ path-list API =============-Talk about <path-list.h>, things like+The path_list API offers a data structure and functions to handle sorted+and unsorted string lists.-* it is not just paths but strings in general;-* the calling sequence.+The name is a bit misleading, a path_list may store not only paths but+strings in general.-(Dscho)+The caller:++. Allocates and clears (`memset(&list, \'\0', sizeof(list));`) a+ `struct path_list` variable.
Some callers use global variables; these are already initialized. Also, I
would not write code here, but later in a concise example.
+. Initializes the members. You can manually set the `items` member, but
+ then you have to set `nr`, accordingly. Also don't forget to set
+ `strdup_paths` if you need it.
I would not promote the manual setting of the items member, until later.
This is advanced usage, and you have to malloc() the list if you add
things later, and you should set the `alloc` member in that case, too.
Further, I would like to have an explanation of the variable
"strdup_paths" first.
Something like: "You might want to set the flag `strdup_paths` if the
strings should be strdup()ed. For example, this is necessary when you add
something like git_path("..."), since that function returns a static
buffer that will change with the next call to git_path()."
+. Adds new items to the list, using `path_list_append` or `path_list_insert`.
+
+. Can check if a string is in the list using `path_list_has_path` or
+ `unsorted_path_list_has_path` and get it from the list using
+ `path_list_lookup` for sorted lists.
+
+. Can sort an unsorted list using `sort_path_list`.
+
+. Finally it should free the list using `path_list_clear`.
Here, you should add a note that it is more efficient to build an unsorted
list and sort it afterwards, instead of building a sorted list (O(n log n)
instead of O(n^2)).
However, if you use the list to check if a certain string was added
already, you should not do that (using unsorted_path_list_has_path()),
because the complexity would be quadratic again (but with a worse factor).
+`path_list_insert`::
+
+ Insert a new element to the path_list. The returned pointer can be handy
+ if you want to write something to the `util` pointer of the
+ path_list_item containing the just added string.
Since this function uses xrealloc() (which die()s if it fails) if
the list needs to grow, it is safe not to check the pointer. I.e.
you may write "path_list_insert(...)->util = ...;"
+`unsorted_path_list_has_path`::
+
+ It's like `path_list_has_path()` but for unsorted lists.
Obviously, this function needs to look through all items, as
opposed to its counterpart for sorted lists, which performs a
binary search.
+Data structures
+---------------
+
+* `struct path_list_item`
+
+Represent an item of the list. The `path` member is a pointer to the
s/sent/&s/
+string, and you may use the `util` member for any purpose, if you want.
+
+* `struct path_list`
+
+Represents the list itself.
+
+. The array of items are available via the `items` member.
+. The `nr` member contains the number of items stored in the list.
+. The `alloc` member is used for `ALLOC_GROW()`.
Maybe "The `alloc` member is used to avoid reallocating at every
insertion. You should not tamper with it."
+. Setting the `strdup_paths` member to 1 means that the added paths are
+ copied to the path list and not just a pointer to them is stored.
Like I said, I would say that it will strdup() the strings before adding
them, and then motivate it (by presenting a case where it helps, e.g.
git_path()).
Of course, a short and concise example how to use path_lists would be
nice... ;-)
Ciao,
Dscho
Signed-off-by: Miklos Vajna <redacted>
---
On Sat, Jun 14, 2008 at 07:08:19PM +0100, Johannes Schindelin [off-list ref] wrote:
Thanks for doing this... I meant to document it after pushing the
path_list -> string_list patch.
Here is an updated version, hopefully I added all your suggestion, and
appended a short example as well.
(Sending to the list only, as I accidently removed the list from Cc, sorry for that.)
Documentation/technical/api-path-list.txt | 125 ++++++++++++++++++++++++++++-
1 files changed, 121 insertions(+), 4 deletions(-)
@@ -1,9 +1,126 @@ path-list API =============-Talk about <path-list.h>, things like+The path_list API offers a data structure and functions to handle sorted+and unsorted string lists.-* it is not just paths but strings in general;-* the calling sequence.+The name is a bit misleading, a path_list may store not only paths but+strings in general.-(Dscho)+The caller:++. Allocates and clears a `struct path_list` variable.++. Initializes the members. You might want to set the flag `strdup_paths`+ if the strings should be strdup()ed. For example, this is necessary+ when you add something like git_path("..."), since that function returns+ a static buffer that will change with the next call to git_path().+++If you need something advanced, you can manually malloc() the `items`+member (you need this if you add things later) and you should set the+`nr` and `alloc` members in that case, too.++. Adds new items to the list, using `path_list_append` or `path_list_insert`.++. Can check if a string is in the list using `path_list_has_path` or+ `unsorted_path_list_has_path` and get it from the list using+ `path_list_lookup` for sorted lists.++. Can sort an unsorted list using `sort_path_list`.++. Finally it should free the list using `path_list_clear`.++Example:++----+struct path_list list;+int i;++memset(&list, 0, sizeof(struct path_list));+path_list_append("foo", &list);+path_list_append("bar", &list);+for (i = 0; i < list.nr; i++)+ printf("%s\n", list.items[i].path)+----++NOTE: It is more efficient to build an unsorted list and sort it+afterwards, instead of building a sorted list `(O(n log n)` instead of+`O(n^2))`.+++However, if you use the list to check if a certain string was added+already, you should not do that (using unsorted_path_list_has_path()),+because the complexity would be quadratic again (but with a worse factor).++Functions+---------++* General ones (works with sorted and unsorted lists as well)++`print_path_list`::++ Dump a path_list to stdout, useful mainly for debugging purposes. It+ can take an optional header argument and it writes out the+ string-pointer pairs of the path_list, each one in its own line.++`path_list_clear`::++ Free a path_list. The `path` pointer of the items will be freed in case+ the `strdup_paths` member of the path_list is set. The second parameter+ controls if the `util` pointer of the items should be freed or not.++* Functions for sorted lists only++`path_list_has_path`::++ Determine if the path_list has a given string or not.++`path_list_insert`::++ Insert a new element to the path_list. The returned pointer can be handy+ if you want to write something to the `util` pointer of the+ path_list_item containing the just added string.+++Since this function uses xrealloc() (which die()s if it fails) if the+list needs to grow, it is safe not to check the pointer. I.e. you may+write `path_list_insert(...)->util = ...;`.++`path_list_lookup`::++ Look up a given string in the path_list, returning the containing+ path_list_item. If the string is not found, NULL is returned.++* Functions for unsorted lists only++`path_list_append`::++ Append a new string to the end of the path_list.++`sort_path_list`::++ Make an unsorted list sorted.++`unsorted_path_list_has_path`::++ It's like `path_list_has_path()` but for unsorted lists.+++This function needs to look through all items, as opposed to its+counterpart for sorted lists, which performs a binary search.++Data structures+---------------++* `struct path_list_item`++Represents an item of the list. The `path` member is a pointer to the+string, and you may use the `util` member for any purpose, if you want.++* `struct path_list`++Represents the list itself.++. The array of items are available via the `items` member.+. The `nr` member contains the number of items stored in the list.+. The `alloc` member is used to avoid reallocating at every insertion.+ You should not tamper with it.+. Setting the `strdup_paths` member to 1 will strdup() the strings+ before adding them, see above.
From: Junio C Hamano <hidden> Date: 2016-06-15 22:44:46
Johannes Schindelin [off-list ref] writes:
Speaking of which: Junio, could you give me any clue how you would like to
proceed with that patch?
It would be most convenient to do so when
git diff master pu | grep path.list
shrinks to the minimum. I think very early after 1.5.6 would be the best,
as there is nothing outstanding that adds new use or removes existing use
of path_list.
From: Jakub Narebski <hidden> Date: 2016-06-15 22:44:46
Miklos Vajna [off-list ref] writes:
+NOTE: It is more efficient to build an unsorted list and sort it
+afterwards, instead of building a sorted list `(O(n log n)` instead of
+`O(n^2))`.
I think there is typo here (misplaced backticks '`' on the wrong side
of enclosing parentheses), and this fragment should read:
+afterwards, instead of building a sorted list (`O(n log n)` instead of
+`O(n^2)`).
--
Jakub Narebski
Poland
ShadeHawk on #git
Signed-off-by: Miklos Vajna <redacted>
---
On Sun, Jun 15, 2008 at 02:01:19AM -0700, Jakub Narebski [off-list ref] wrote:
quoted
+NOTE: It is more efficient to build an unsorted list and sort it
+afterwards, instead of building a sorted list `(O(n log n)` instead
of
+`O(n^2))`.
I think there is typo here (misplaced backticks '`' on the wrong side
of enclosing parentheses), and this fragment should read:
+afterwards, instead of building a sorted list (`O(n log n)` instead
of
+`O(n^2)`).
@@ -1,9 +1,126 @@ path-list API =============-Talk about <path-list.h>, things like+The path_list API offers a data structure and functions to handle sorted+and unsorted string lists.-* it is not just paths but strings in general;-* the calling sequence.+The name is a bit misleading, a path_list may store not only paths but+strings in general.-(Dscho)+The caller:++. Allocates and clears a `struct path_list` variable.++. Initializes the members. You might want to set the flag `strdup_paths`+ if the strings should be strdup()ed. For example, this is necessary+ when you add something like git_path("..."), since that function returns+ a static buffer that will change with the next call to git_path().+++If you need something advanced, you can manually malloc() the `items`+member (you need this if you add things later) and you should set the+`nr` and `alloc` members in that case, too.++. Adds new items to the list, using `path_list_append` or `path_list_insert`.++. Can check if a string is in the list using `path_list_has_path` or+ `unsorted_path_list_has_path` and get it from the list using+ `path_list_lookup` for sorted lists.++. Can sort an unsorted list using `sort_path_list`.++. Finally it should free the list using `path_list_clear`.++Example:++----+struct path_list list;+int i;++memset(&list, 0, sizeof(struct path_list));+path_list_append("foo", &list);+path_list_append("bar", &list);+for (i = 0; i < list.nr; i++)+ printf("%s\n", list.items[i].path)+----++NOTE: It is more efficient to build an unsorted list and sort it+afterwards, instead of building a sorted list (`O(n log n)` instead of+`O(n^2)`).+++However, if you use the list to check if a certain string was added+already, you should not do that (using unsorted_path_list_has_path()),+because the complexity would be quadratic again (but with a worse factor).++Functions+---------++* General ones (works with sorted and unsorted lists as well)++`print_path_list`::++ Dump a path_list to stdout, useful mainly for debugging purposes. It+ can take an optional header argument and it writes out the+ string-pointer pairs of the path_list, each one in its own line.++`path_list_clear`::++ Free a path_list. The `path` pointer of the items will be freed in case+ the `strdup_paths` member of the path_list is set. The second parameter+ controls if the `util` pointer of the items should be freed or not.++* Functions for sorted lists only++`path_list_has_path`::++ Determine if the path_list has a given string or not.++`path_list_insert`::++ Insert a new element to the path_list. The returned pointer can be handy+ if you want to write something to the `util` pointer of the+ path_list_item containing the just added string.+++Since this function uses xrealloc() (which die()s if it fails) if the+list needs to grow, it is safe not to check the pointer. I.e. you may+write `path_list_insert(...)->util = ...;`.++`path_list_lookup`::++ Look up a given string in the path_list, returning the containing+ path_list_item. If the string is not found, NULL is returned.++* Functions for unsorted lists only++`path_list_append`::++ Append a new string to the end of the path_list.++`sort_path_list`::++ Make an unsorted list sorted.++`unsorted_path_list_has_path`::++ It's like `path_list_has_path()` but for unsorted lists.+++This function needs to look through all items, as opposed to its+counterpart for sorted lists, which performs a binary search.++Data structures+---------------++* `struct path_list_item`++Represents an item of the list. The `path` member is a pointer to the+string, and you may use the `util` member for any purpose, if you want.++* `struct path_list`++Represents the list itself.++. The array of items are available via the `items` member.+. The `nr` member contains the number of items stored in the list.+. The `alloc` member is used to avoid reallocating at every insertion.+ You should not tamper with it.+. Setting the `strdup_paths` member to 1 will strdup() the strings+ before adding them, see above.
From: Mike Ralphson <hidden> Date: 2016-06-15 22:44:46
2008/6/13 Don Zickus [off-list ref]:
When working with a lot of people who backport patches all day long, every
once in a while I get a patch that modifies the same file more than once
inside the same patch...
I have modified git-apply to cache the filenames of files it modifies such
that if a later patch chunk modifies a file in the cache it will buffer the
previously changed file instead of reading the original file from disk.
Excellent spot. A couple of things you might want to add to your new
test cases would be examples where the first patch renames or removes
a file (or two files are swapped) and a subsequent patch then touches
the same path(s).
Mike