Skip to content

Commit fe4a0a2

Browse files
peffgitster
authored andcommitted
argv-array: add pop function
Sometimes we build a set of similar command lines, differing only in the final arguments (e.g., "fetch --multiple"). To use argv_array for this, you have to either push the same set of elements repeatedly, or break the abstraction by manually manipulating the array's internal members. Instead, let's provide a sanctioned "pop" function to remove elements from the end. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9e234af commit fe4a0a2

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

Documentation/technical/api-argv-array.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ Functions
4646
Format a string and push it onto the end of the array. This is a
4747
convenience wrapper combining `strbuf_addf` and `argv_array_push`.
4848

49+
`argv_array_pop`::
50+
Remove the final element from the array. If there are no
51+
elements in the array, do nothing.
52+
4953
`argv_array_clear`::
5054
Free all memory associated with the array and return it to the
5155
initial, empty state.

argv-array.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ void argv_array_pushl(struct argv_array *array, ...)
4949
va_end(ap);
5050
}
5151

52+
void argv_array_pop(struct argv_array *array)
53+
{
54+
if (!array->argc)
55+
return;
56+
free((char *)array->argv[array->argc - 1]);
57+
array->argv[array->argc - 1] = NULL;
58+
array->argc--;
59+
}
60+
5261
void argv_array_clear(struct argv_array *array)
5362
{
5463
if (array->argv != empty_argv) {

argv-array.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ void argv_array_push(struct argv_array *, const char *);
1616
__attribute__((format (printf,2,3)))
1717
void argv_array_pushf(struct argv_array *, const char *fmt, ...);
1818
void argv_array_pushl(struct argv_array *, ...);
19+
void argv_array_pop(struct argv_array *);
1920
void argv_array_clear(struct argv_array *);
2021

2122
#endif /* ARGV_ARRAY_H */

0 commit comments

Comments
 (0)