config --get --path: check for unset $HOME
If $HOME is unset (as in some automated build situations), currently git config --path path.home "~" git config --path --get path.home segfaults. Error out with Failed to expand user dir in: '~/' instead. Reported-by: Julien Cristau <jcristau@debian.org> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jonathan Nieder committed
Jul 26, 2010 at 10:06 UTC
79bf149061fa29482512d903192a0d929e82872b
2 files changed
+25
-1
path.c
+2
@@ -316,6 +316,8 @@ char *expand_user_path(const char *path)
316
size_t username_len = first_slash - username;
317
if (username_len == 0) {
318
const char *home = getenv("HOME");
319
+ if (!home)
320
+ goto return_null;
321
strbuf_add(&user_path, home, strlen(home));
322
} else {
323
struct passwd *pw = getpw_str(username, username_len);
t/t1300-repo-config.sh
+23
-1
@@ -707,19 +707,41 @@ test_expect_success 'set --path' '
707
git config --path path.trailingtilde "foo~" &&
708
test_cmp expect .git/config'
709
710
+if test "${HOME+set}"
711
+then
712
+ test_set_prereq HOMEVAR
713
+fi
714
+
715
cat >expect <<EOF
716
$HOME/
717
/dev/null
718
foo~
719
EOF
720
716
-test_expect_success 'get --path' '
721
+test_expect_success HOMEVAR 'get --path' '
722
git config --get --path path.home > result &&
723
git config --get --path path.normal >> result &&
724
git config --get --path path.trailingtilde >> result &&
725
test_cmp expect result
726
'
727
728
+cat >expect <<\EOF
729
+/dev/null
730
+foo~
731
+EOF
732
+
733
+test_expect_success 'get --path copes with unset $HOME' '
734
+ (
735
+ unset HOME;
736
+ test_must_fail git config --get --path path.home \
737
+ >result 2>msg &&
738
+ git config --get --path path.normal >>result &&
739
+ git config --get --path path.trailingtilde >>result
740
+ ) &&
741
+ grep "[Ff]ailed to expand.*~/" msg &&
742
+ test_cmp expect result
743
+'
744
+
745
rm .git/config
746
747
git config quote.leading " test"