Skip to content

Commit fc32293

Browse files
committed
Merge branch 'rs/strbuf-add-real-path'
An helper function to make it easier to append the result from real_path() to a strbuf has been added. * rs/strbuf-add-real-path: strbuf: add strbuf_add_real_path() cocci: use ALLOC_ARRAY
2 parents 82682e2 + 33ad9dd commit fc32293

File tree

6 files changed

+49
-2
lines changed

6 files changed

+49
-2
lines changed

contrib/coccinelle/array.cocci

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,19 @@ expression n;
2424
@@
2525
- memcpy(dst, src, n * sizeof(T));
2626
+ COPY_ARRAY(dst, src, n);
27+
28+
@@
29+
type T;
30+
T *ptr;
31+
expression n;
32+
@@
33+
- ptr = xmalloc(n * sizeof(*ptr));
34+
+ ALLOC_ARRAY(ptr, n);
35+
36+
@@
37+
type T;
38+
T *ptr;
39+
expression n;
40+
@@
41+
- ptr = xmalloc(n * sizeof(T));
42+
+ ALLOC_ARRAY(ptr, n);

contrib/coccinelle/strbuf.cocci

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,9 @@ expression E1, E2, E3;
3838
@@
3939
- strbuf_addstr(E1, find_unique_abbrev(E2, E3));
4040
+ strbuf_add_unique_abbrev(E1, E2, E3);
41+
42+
@@
43+
expression E1, E2;
44+
@@
45+
- strbuf_addstr(E1, real_path(E2));
46+
+ strbuf_add_real_path(E1, E2);

setup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ int get_common_dir_noenv(struct strbuf *sb, const char *gitdir)
254254
if (!is_absolute_path(data.buf))
255255
strbuf_addf(&path, "%s/", gitdir);
256256
strbuf_addbuf(&path, &data);
257-
strbuf_addstr(sb, real_path(path.buf));
257+
strbuf_add_real_path(sb, path.buf);
258258
ret = 1;
259259
} else {
260260
strbuf_addstr(sb, gitdir);

strbuf.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,17 @@ void strbuf_add_absolute_path(struct strbuf *sb, const char *path)
707707
strbuf_addstr(sb, path);
708708
}
709709

710+
void strbuf_add_real_path(struct strbuf *sb, const char *path)
711+
{
712+
if (sb->len) {
713+
struct strbuf resolved = STRBUF_INIT;
714+
strbuf_realpath(&resolved, path, 1);
715+
strbuf_addbuf(sb, &resolved);
716+
strbuf_release(&resolved);
717+
} else
718+
strbuf_realpath(sb, path, 1);
719+
}
720+
710721
int printf_ln(const char *fmt, ...)
711722
{
712723
int ret;

strbuf.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,20 @@ extern int strbuf_getcwd(struct strbuf *sb);
441441
*/
442442
extern void strbuf_add_absolute_path(struct strbuf *sb, const char *path);
443443

444+
/**
445+
* Canonize `path` (make it absolute, resolve symlinks, remove extra
446+
* slashes) and append it to `sb`. Die with an informative error
447+
* message if there is a problem.
448+
*
449+
* The directory part of `path` (i.e., everything up to the last
450+
* dir_sep) must denote a valid, existing directory, but the last
451+
* component need not exist.
452+
*
453+
* Callers that don't mind links should use the more lightweight
454+
* strbuf_add_absolute_path() instead.
455+
*/
456+
extern void strbuf_add_real_path(struct strbuf *sb, const char *path);
457+
444458

445459
/**
446460
* Normalize in-place the path contained in the strbuf. See

worktree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ struct worktree **get_worktrees(unsigned flags)
175175
struct dirent *d;
176176
int counter = 0, alloc = 2;
177177

178-
list = xmalloc(alloc * sizeof(struct worktree *));
178+
ALLOC_ARRAY(list, alloc);
179179

180180
list[counter++] = get_main_worktree();
181181

0 commit comments

Comments
 (0)