Skip to content

Commit c455c87

Browse files
dschogitster
authored andcommitted
Rename path_list to string_list
The name path_list was correct for the first usage of that data structure, but it really is a general-purpose string list. $ perl -i -pe 's/path-list/string-list/g' $(git grep -l path-list) $ perl -i -pe 's/path_list/string_list/g' $(git grep -l path_list) $ git mv path-list.h string-list.h $ git mv path-list.c string-list.c $ perl -i -pe 's/has_path/has_string/g' $(git grep -l has_path) $ perl -i -pe 's/path/string/g' string-list.[ch] $ git mv Documentation/technical/api-path-list.txt \ Documentation/technical/api-string-list.txt $ perl -i -pe 's/strdup_paths/strdup_strings/g' $(git grep -l strdup_paths) ... and then fix all users of string-list to access the member "string" instead of "path". Documentation/technical/api-string-list.txt needed some rewrapping, too. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 51ef1da commit c455c87

28 files changed

+556
-553
lines changed

Documentation/CodingGuidelines

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ For C programs:
105105

106106
- Use the API. No, really. We have a strbuf (variable length
107107
string), several arrays with the ALLOC_GROW() macro, a
108-
path_list for sorted string lists, a hash map (mapping struct
108+
string_list for sorted string lists, a hash map (mapping struct
109109
objects) named "struct decorate", amongst other things.
110110

111111
- When you come up with an API, document it.

Documentation/technical/api-path-list.txt

Lines changed: 0 additions & 126 deletions
This file was deleted.
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
string-list API
2+
===============
3+
4+
The string_list API offers a data structure and functions to handle sorted
5+
and unsorted string lists.
6+
7+
The 'string_list' struct used to be called 'path_list', but was renamed
8+
because it is not specific to paths.
9+
10+
The caller:
11+
12+
. Allocates and clears a `struct string_list` variable.
13+
14+
. Initializes the members. You might want to set the flag `strdup_strings`
15+
if the strings should be strdup()ed. For example, this is necessary
16+
when you add something like git_path("..."), since that function returns
17+
a static buffer that will change with the next call to git_path().
18+
+
19+
If you need something advanced, you can manually malloc() the `items`
20+
member (you need this if you add things later) and you should set the
21+
`nr` and `alloc` members in that case, too.
22+
23+
. Adds new items to the list, using `string_list_append` or
24+
`string_list_insert`.
25+
26+
. Can check if a string is in the list using `string_list_has_string` or
27+
`unsorted_string_list_has_string` and get it from the list using
28+
`string_list_lookup` for sorted lists.
29+
30+
. Can sort an unsorted list using `sort_string_list`.
31+
32+
. Finally it should free the list using `string_list_clear`.
33+
34+
Example:
35+
36+
----
37+
struct string_list list;
38+
int i;
39+
40+
memset(&list, 0, sizeof(struct string_list));
41+
string_list_append("foo", &list);
42+
string_list_append("bar", &list);
43+
for (i = 0; i < list.nr; i++)
44+
printf("%s\n", list.items[i].path)
45+
----
46+
47+
NOTE: It is more efficient to build an unsorted list and sort it
48+
afterwards, instead of building a sorted list (`O(n log n)` instead of
49+
`O(n^2)`).
50+
+
51+
However, if you use the list to check if a certain string was added
52+
already, you should not do that (using unsorted_string_list_has_string()),
53+
because the complexity would be quadratic again (but with a worse factor).
54+
55+
Functions
56+
---------
57+
58+
* General ones (works with sorted and unsorted lists as well)
59+
60+
`print_string_list`::
61+
62+
Dump a string_list to stdout, useful mainly for debugging purposes. It
63+
can take an optional header argument and it writes out the
64+
string-pointer pairs of the string_list, each one in its own line.
65+
66+
`string_list_clear`::
67+
68+
Free a string_list. The `string` pointer of the items will be freed in
69+
case the `strdup_strings` member of the string_list is set. The second
70+
parameter controls if the `util` pointer of the items should be freed
71+
or not.
72+
73+
* Functions for sorted lists only
74+
75+
`string_list_has_string`::
76+
77+
Determine if the string_list has a given string or not.
78+
79+
`string_list_insert`::
80+
81+
Insert a new element to the string_list. The returned pointer can be
82+
handy if you want to write something to the `util` pointer of the
83+
string_list_item containing the just added string.
84+
+
85+
Since this function uses xrealloc() (which die()s if it fails) if the
86+
list needs to grow, it is safe not to check the pointer. I.e. you may
87+
write `string_list_insert(...)->util = ...;`.
88+
89+
`string_list_lookup`::
90+
91+
Look up a given string in the string_list, returning the containing
92+
string_list_item. If the string is not found, NULL is returned.
93+
94+
* Functions for unsorted lists only
95+
96+
`string_list_append`::
97+
98+
Append a new string to the end of the string_list.
99+
100+
`sort_string_list`::
101+
102+
Make an unsorted list sorted.
103+
104+
`unsorted_string_list_has_string`::
105+
106+
It's like `string_list_has_string()` but for unsorted lists.
107+
+
108+
This function needs to look through all items, as opposed to its
109+
counterpart for sorted lists, which performs a binary search.
110+
111+
Data structures
112+
---------------
113+
114+
* `struct string_list_item`
115+
116+
Represents an item of the list. The `path` member is a pointer to the
117+
string, and you may use the `util` member for any purpose, if you want.
118+
119+
* `struct string_list`
120+
121+
Represents the list itself.
122+
123+
. The array of items are available via the `items` member.
124+
. The `nr` member contains the number of items stored in the list.
125+
. The `alloc` member is used to avoid reallocating at every insertion.
126+
You should not tamper with it.
127+
. Setting the `strdup_strings` member to 1 will strdup() the strings
128+
before adding them, see above.

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ LIB_H += pack-refs.h
356356
LIB_H += pack-revindex.h
357357
LIB_H += parse-options.h
358358
LIB_H += patch-ids.h
359-
LIB_H += path-list.h
359+
LIB_H += string-list.h
360360
LIB_H += pkt-line.h
361361
LIB_H += progress.h
362362
LIB_H += quote.h
@@ -437,7 +437,7 @@ LIB_OBJS += pager.o
437437
LIB_OBJS += parse-options.o
438438
LIB_OBJS += patch-delta.o
439439
LIB_OBJS += patch-ids.o
440-
LIB_OBJS += path-list.o
440+
LIB_OBJS += string-list.o
441441
LIB_OBJS += path.o
442442
LIB_OBJS += pkt-line.o
443443
LIB_OBJS += pretty.o

builtin-apply.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "blob.h"
1313
#include "delta.h"
1414
#include "builtin.h"
15-
#include "path-list.h"
15+
#include "string-list.h"
1616

1717
/*
1818
* --check turns on checking that the working tree matches the
@@ -194,7 +194,7 @@ struct image {
194194
* the case where more than one patches touch the same file.
195195
*/
196196

197-
static struct path_list fn_table;
197+
static struct string_list fn_table;
198198

199199
static uint32_t hash_line(const char *cp, size_t len)
200200
{
@@ -2250,12 +2250,12 @@ static int read_file_or_gitlink(struct cache_entry *ce, struct strbuf *buf)
22502250

22512251
static struct patch *in_fn_table(const char *name)
22522252
{
2253-
struct path_list_item *item;
2253+
struct string_list_item *item;
22542254

22552255
if (name == NULL)
22562256
return NULL;
22572257

2258-
item = path_list_lookup(name, &fn_table);
2258+
item = string_list_lookup(name, &fn_table);
22592259
if (item != NULL)
22602260
return (struct patch *)item->util;
22612261

@@ -2264,15 +2264,15 @@ static struct patch *in_fn_table(const char *name)
22642264

22652265
static void add_to_fn_table(struct patch *patch)
22662266
{
2267-
struct path_list_item *item;
2267+
struct string_list_item *item;
22682268

22692269
/*
22702270
* Always add new_name unless patch is a deletion
22712271
* This should cover the cases for normal diffs,
22722272
* file creations and copies
22732273
*/
22742274
if (patch->new_name != NULL) {
2275-
item = path_list_insert(patch->new_name, &fn_table);
2275+
item = string_list_insert(patch->new_name, &fn_table);
22762276
item->util = patch;
22772277
}
22782278

@@ -2281,7 +2281,7 @@ static void add_to_fn_table(struct patch *patch)
22812281
* later chunks shouldn't patch old names
22822282
*/
22832283
if ((patch->new_name == NULL) || (patch->is_rename)) {
2284-
item = path_list_insert(patch->old_name, &fn_table);
2284+
item = string_list_insert(patch->old_name, &fn_table);
22852285
item->util = (struct patch *) -1;
22862286
}
22872287
}
@@ -3051,7 +3051,7 @@ static int apply_patch(int fd, const char *filename, int options)
30513051
int skipped_patch = 0;
30523052

30533053
/* FIXME - memory leak when using multiple patch files as inputs */
3054-
memset(&fn_table, 0, sizeof(struct path_list));
3054+
memset(&fn_table, 0, sizeof(struct string_list));
30553055
strbuf_init(&buf, 0);
30563056
patch_input_file = filename;
30573057
read_patch_file(&buf, fd);

builtin-blame.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include "quote.h"
1717
#include "xdiff-interface.h"
1818
#include "cache-tree.h"
19-
#include "path-list.h"
19+
#include "string-list.h"
2020
#include "mailmap.h"
2121
#include "parse-options.h"
2222

@@ -40,7 +40,7 @@ static int blank_boundary;
4040
static int incremental;
4141
static int cmd_is_annotate;
4242
static int xdl_opts = XDF_NEED_MINIMAL;
43-
static struct path_list mailmap;
43+
static struct string_list mailmap;
4444

4545
#ifndef DEBUG
4646
#define DEBUG 0
@@ -1926,7 +1926,7 @@ static void sanity_check_refcnt(struct scoreboard *sb)
19261926
* Used for the command line parsing; check if the path exists
19271927
* in the working tree.
19281928
*/
1929-
static int has_path_in_work_tree(const char *path)
1929+
static int has_string_in_work_tree(const char *path)
19301930
{
19311931
struct stat st;
19321932
return !lstat(path, &st);
@@ -2390,14 +2390,14 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
23902390
if (argc < 2)
23912391
usage_with_options(blame_opt_usage, options);
23922392
path = add_prefix(prefix, argv[argc - 1]);
2393-
if (argc == 3 && !has_path_in_work_tree(path)) { /* (2b) */
2393+
if (argc == 3 && !has_string_in_work_tree(path)) { /* (2b) */
23942394
path = add_prefix(prefix, argv[1]);
23952395
argv[1] = argv[2];
23962396
}
23972397
argv[argc - 1] = "--";
23982398

23992399
setup_work_tree();
2400-
if (!has_path_in_work_tree(path))
2400+
if (!has_string_in_work_tree(path))
24012401
die("cannot stat path %s: %s", path, strerror(errno));
24022402
}
24032403

0 commit comments

Comments
 (0)