Skip to content

Commit 849f371

Browse files
authored
Merge pull request libgit2#5535 from libgit2/ethomson/strarray
strarray refactoring
2 parents 629515a + 5eb48a1 commit 849f371

File tree

28 files changed

+179
-132
lines changed

28 files changed

+179
-132
lines changed

examples/checkout.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ static int guess_refish(git_annotated_commit **out, git_repository *repo, const
239239

240240
out:
241241
git_reference_free(remote_ref);
242-
git_strarray_free(&remotes);
242+
git_strarray_dispose(&remotes);
243243
return error;
244244
}
245245

examples/general.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ static void reference_listing(git_repository *repo)
707707
git_reference_free(ref);
708708
}
709709

710-
git_strarray_free(&ref_list);
710+
git_strarray_dispose(&ref_list);
711711
}
712712

713713
/**

examples/remote.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ static int cmd_rename(git_repository *repo, struct remote_opts *o)
129129
puts(problems.strings[0]);
130130
}
131131

132-
git_strarray_free(&problems);
132+
git_strarray_dispose(&problems);
133133

134134
return retval;
135135
}
@@ -207,7 +207,7 @@ static int cmd_show(git_repository *repo, struct remote_opts *o)
207207
git_remote_free(remote);
208208
}
209209

210-
git_strarray_free(&remotes);
210+
git_strarray_dispose(&remotes);
211211

212212
return 0;
213213
}

examples/tag.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ static void action_list_tags(tag_state *state)
162162
each_tag(tag_names.strings[i], state);
163163
}
164164

165-
git_strarray_free(&tag_names);
165+
git_strarray_dispose(&tag_names);
166166
}
167167

168168
static void action_delete_tag(tag_state *state)

include/git2/deprecated.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,42 @@ typedef int GIT_CALLBACK(git_headlist_cb)(git_remote_head *rhead, void *payload)
524524

525525
/**@}*/
526526

527+
/** @name Deprecated String Array Functions
528+
*
529+
* These types are retained for backward compatibility. The newer
530+
* versions of these values should be preferred in all new code.
531+
*
532+
* There is no plan to remove these backward compatibility values at
533+
* this time.
534+
*/
535+
/**@{*/
536+
537+
/**
538+
* Copy a string array object from source to target.
539+
*
540+
* This function is deprecated, but there is no plan to remove this
541+
* function at this time.
542+
*
543+
* @param tgt target
544+
* @param src source
545+
* @return 0 on success, < 0 on allocation failure
546+
*/
547+
GIT_EXTERN(int) git_strarray_copy(git_strarray *tgt, const git_strarray *src);
548+
549+
/**
550+
* Free the memory referred to by the git_strarray. This is an alias of
551+
* `git_strarray_dispose` and is preserved for backward compatibility.
552+
*
553+
* This function is deprecated, but there is no plan to remove this
554+
* function at this time.
555+
*
556+
* @deprecated Use git_strarray_dispose
557+
* @see git_strarray_dispose
558+
*/
559+
GIT_EXTERN(void) git_strarray_free(git_strarray *array);
560+
561+
/**@}*/
562+
527563
/** @name Deprecated Options Initialization Functions
528564
*
529565
* These functions are retained for backward compatibility. The newer

include/git2/strarray.h

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,16 @@ typedef struct git_strarray {
2525
} git_strarray;
2626

2727
/**
28-
* Close a string array object
29-
*
30-
* This method should be called on `git_strarray` objects where the strings
31-
* array is allocated and contains allocated strings, such as what you
32-
* would get from `git_strarray_copy()`. Not doing so, will result in a
33-
* memory leak.
28+
* Free the strings contained in a string array. This method should
29+
* be called on `git_strarray` objects that were provided by the
30+
* library. Not doing so, will result in a memory leak.
3431
*
3532
* This does not free the `git_strarray` itself, since the library will
36-
* never allocate that object directly itself (it is more commonly embedded
37-
* inside another struct or created on the stack).
33+
* never allocate that object directly itself.
3834
*
39-
* @param array git_strarray from which to free string data
35+
* @param array The git_strarray that contains strings to free
4036
*/
41-
GIT_EXTERN(void) git_strarray_free(git_strarray *array);
37+
GIT_EXTERN(void) git_strarray_dispose(git_strarray *array);
4238

4339
/**
4440
* Copy a string array object from source to target.

src/branch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ int git_branch_remote_name(git_buf *buf, git_repository *repo, const char *refna
548548
if (error < 0)
549549
git_buf_dispose(buf);
550550

551-
git_strarray_free(&remote_list);
551+
git_strarray_dispose(&remote_list);
552552
return error;
553553
}
554554

src/remote.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1237,7 +1237,7 @@ static int prune_candidates(git_vector *candidates, git_remote *remote)
12371237
}
12381238

12391239
out:
1240-
git_strarray_free(&arr);
1240+
git_strarray_dispose(&arr);
12411241
return error;
12421242
}
12431243

src/repository.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2289,7 +2289,7 @@ int git_repository_foreach_head(git_repository *repo,
22892289

22902290
out:
22912291
git_buf_dispose(&path);
2292-
git_strarray_free(&worktrees);
2292+
git_strarray_dispose(&worktrees);
22932293
return error;
22942294
}
22952295

src/strarray.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (C) the libgit2 contributors. All rights reserved.
3+
*
4+
* This file is part of libgit2, distributed under the GNU GPL v2 with
5+
* a Linking Exception. For full terms see the included COPYING file.
6+
*/
7+
8+
#include "util.h"
9+
10+
#include "common.h"
11+
12+
int git_strarray_copy(git_strarray *tgt, const git_strarray *src)
13+
{
14+
size_t i;
15+
16+
assert(tgt && src);
17+
18+
memset(tgt, 0, sizeof(*tgt));
19+
20+
if (!src->count)
21+
return 0;
22+
23+
tgt->strings = git__calloc(src->count, sizeof(char *));
24+
GIT_ERROR_CHECK_ALLOC(tgt->strings);
25+
26+
for (i = 0; i < src->count; ++i) {
27+
if (!src->strings[i])
28+
continue;
29+
30+
tgt->strings[tgt->count] = git__strdup(src->strings[i]);
31+
if (!tgt->strings[tgt->count]) {
32+
git_strarray_dispose(tgt);
33+
memset(tgt, 0, sizeof(*tgt));
34+
return -1;
35+
}
36+
37+
tgt->count++;
38+
}
39+
40+
return 0;
41+
}
42+
43+
void git_strarray_dispose(git_strarray *array)
44+
{
45+
size_t i;
46+
47+
if (array == NULL)
48+
return;
49+
50+
for (i = 0; i < array->count; ++i)
51+
git__free(array->strings[i]);
52+
53+
git__free(array->strings);
54+
55+
memset(array, 0, sizeof(*array));
56+
}
57+
58+
void git_strarray_free(git_strarray *array)
59+
{
60+
git_strarray_dispose(array);
61+
}

0 commit comments

Comments
 (0)