Skip to content

Commit 5e04738

Browse files
derrickstoleegitster
authored andcommitted
parse: add git_parse_maybe_pathname()
This extraction of logic from config.c's git_config_pathname() allows for parsing a fully-qualified path from a relative path along with validation of the existence of the path without failing with a die(). Signed-off-by: Derrick Stolee <stolee@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent b20f49d commit 5e04738

3 files changed

Lines changed: 27 additions & 13 deletions

File tree

config.c

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,24 +1278,12 @@ int git_config_string(char **dest, const char *var, const char *value)
12781278

12791279
int git_config_pathname(char **dest, const char *var, const char *value)
12801280
{
1281-
bool is_optional;
1282-
char *path;
1283-
12841281
if (!value)
12851282
return config_error_nonbool(var);
12861283

1287-
is_optional = skip_prefix(value, ":(optional)", &value);
1288-
path = interpolate_path(value, 0);
1289-
if (!path)
1284+
if (git_parse_maybe_pathname(value, dest) < 0)
12901285
die(_("failed to expand user dir in: '%s'"), value);
12911286

1292-
if (is_optional && is_missing_file(path)) {
1293-
free(path);
1294-
*dest = NULL;
1295-
return 0;
1296-
}
1297-
1298-
*dest = path;
12991287
return 0;
13001288
}
13011289

parse.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "git-compat-util.h"
22
#include "gettext.h"
33
#include "parse.h"
4+
#include "path.h"
45

56
static uintmax_t get_unit_factor(const char *end)
67
{
@@ -209,3 +210,26 @@ unsigned long git_env_ulong(const char *k, unsigned long val)
209210
die(_("failed to parse %s"), k);
210211
return val;
211212
}
213+
214+
int git_parse_maybe_pathname(const char *value, char **dest)
215+
{
216+
bool is_optional;
217+
char *path;
218+
219+
if (!value)
220+
return -1;
221+
222+
is_optional = skip_prefix(value, ":(optional)", &value);
223+
path = interpolate_path(value, 0);
224+
if (!path)
225+
return -1;
226+
227+
if (is_optional && is_missing_file(path)) {
228+
free(path);
229+
*dest = NULL;
230+
return 0;
231+
}
232+
233+
*dest = path;
234+
return 0;
235+
}

parse.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ int git_parse_maybe_bool_text(const char *value);
1919
int git_env_bool(const char *, int);
2020
unsigned long git_env_ulong(const char *, unsigned long);
2121

22+
int git_parse_maybe_pathname(const char *value, char **dest);
23+
2224
#endif /* PARSE_H */

0 commit comments

Comments
 (0)