Skip to content

Commit 0c0d6e8

Browse files
mhaggergitster
authored andcommitted
trim_last_path_component(): replace last_path_elm()
Rewrite last_path_elm() to take a strbuf parameter and to trim off the last path name element in place rather than returning a pointer to the beginning of the last path name element. This simplifies the function a bit and makes it integrate better with its caller, which is now also strbuf-based. Rename the function accordingly and a bit less tersely. Signed-off-by: Michael Haggerty <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6cad805 commit 0c0d6e8

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

lockfile.c

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -76,32 +76,28 @@ static void remove_lock_file_on_signal(int signo)
7676
}
7777

7878
/*
79-
* p = absolute or relative path name
79+
* path = absolute or relative path name
8080
*
81-
* Return a pointer into p showing the beginning of the last path name
82-
* element. If p is empty or the root directory ("/"), just return p.
81+
* Remove the last path name element from path (leaving the preceding
82+
* "/", if any). If path is empty or the root directory ("/"), set
83+
* path to the empty string.
8384
*/
84-
static char *last_path_elm(char *p)
85+
static void trim_last_path_component(struct strbuf *path)
8586
{
86-
/* r starts pointing to null at the end of the string */
87-
char *r = strchr(p, '\0');
88-
89-
if (r == p)
90-
return p; /* just return empty string */
91-
92-
r--; /* back up to last non-null character */
87+
int i = path->len;
9388

9489
/* back up past trailing slashes, if any */
95-
while (r > p && *r == '/')
96-
r--;
90+
while (i && path->buf[i - 1] == '/')
91+
i--;
9792

9893
/*
99-
* then go backwards until I hit a slash, or the beginning of
100-
* the string
94+
* then go backwards until a slash, or the beginning of the
95+
* string
10196
*/
102-
while (r > p && *(r-1) != '/')
103-
r--;
104-
return r;
97+
while (i && path->buf[i - 1] != '/')
98+
i--;
99+
100+
strbuf_setlen(path, i);
105101
}
106102

107103

@@ -131,14 +127,12 @@ static void resolve_symlink(struct strbuf *path)
131127
if (is_absolute_path(link.buf))
132128
/* absolute path simply replaces p */
133129
strbuf_reset(path);
134-
else {
130+
else
135131
/*
136132
* link is a relative path, so replace the
137133
* last element of p with it.
138134
*/
139-
char *r = last_path_elm(path->buf);
140-
strbuf_setlen(path, r - path->buf);
141-
}
135+
trim_last_path_component(path);
142136

143137
strbuf_addbuf(path, &link);
144138
}

0 commit comments

Comments
 (0)