t / t1306-xdg-files.shon commit Merge tag 'v2.23.0-rc2' of git://git.kernel.org/pub/scm/git/git (d6d5cbf)
   1#!/bin/sh
   2#
   3# Copyright (c) 2012 Valentin Duperray, Lucien Kong, Franck Jonas,
   4#                    Thomas Nguy, Khoi Nguyen
   5#                    Grenoble INP Ensimag
   6#
   7
   8test_description='Compatibility with $XDG_CONFIG_HOME/git/ files'
   9
  10. ./test-lib.sh
  11
  12test_expect_success 'read config: xdg file exists and ~/.gitconfig doesn'\''t' '
  13        mkdir -p .config/git &&
  14        echo "[alias]" >.config/git/config &&
  15        echo "  myalias = !echo in_config" >>.config/git/config &&
  16        echo in_config >expected &&
  17        git myalias >actual &&
  18        test_cmp expected actual
  19'
  20
  21
  22test_expect_success 'read config: xdg file exists and ~/.gitconfig exists' '
  23        >.gitconfig &&
  24        echo "[alias]" >.gitconfig &&
  25        echo "  myalias = !echo in_gitconfig" >>.gitconfig &&
  26        echo in_gitconfig >expected &&
  27        git myalias >actual &&
  28        test_cmp expected actual
  29'
  30
  31
  32test_expect_success 'read with --get: xdg file exists and ~/.gitconfig doesn'\''t' '
  33        rm .gitconfig &&
  34        echo "[user]" >.config/git/config &&
  35        echo "  name = read_config" >>.config/git/config &&
  36        echo read_config >expected &&
  37        git config --get user.name >actual &&
  38        test_cmp expected actual
  39'
  40
  41test_expect_success '"$XDG_CONFIG_HOME overrides $HOME/.config/git' '
  42        mkdir -p "$HOME"/xdg/git &&
  43        echo "[user]name = in_xdg" >"$HOME"/xdg/git/config &&
  44        echo in_xdg >expected &&
  45        XDG_CONFIG_HOME="$HOME"/xdg git config --get-all user.name >actual &&
  46        test_cmp expected actual
  47'
  48
  49test_expect_success 'read with --get: xdg file exists and ~/.gitconfig exists' '
  50        >.gitconfig &&
  51        echo "[user]" >.gitconfig &&
  52        echo "  name = read_gitconfig" >>.gitconfig &&
  53        echo read_gitconfig >expected &&
  54        git config --get user.name >actual &&
  55        test_cmp expected actual
  56'
  57
  58
  59test_expect_success 'read with --list: xdg file exists and ~/.gitconfig doesn'\''t' '
  60        rm .gitconfig &&
  61        echo user.name=read_config >expected &&
  62        git config --global --list >actual &&
  63        test_cmp expected actual
  64'
  65
  66
  67test_expect_success 'read with --list: xdg file exists and ~/.gitconfig exists' '
  68        >.gitconfig &&
  69        echo "[user]" >.gitconfig &&
  70        echo "  name = read_gitconfig" >>.gitconfig &&
  71        echo user.name=read_gitconfig >expected &&
  72        git config --global --list >actual &&
  73        test_cmp expected actual
  74'
  75
  76
  77test_expect_success 'Setup' '
  78        git init git &&
  79        cd git &&
  80        echo foo >to_be_excluded
  81'
  82
  83
  84test_expect_success 'Exclusion of a file in the XDG ignore file' '
  85        mkdir -p "$HOME"/.config/git/ &&
  86        echo to_be_excluded >"$HOME"/.config/git/ignore &&
  87        test_must_fail git add to_be_excluded
  88'
  89
  90test_expect_success '$XDG_CONFIG_HOME overrides $HOME/.config/git/ignore' '
  91        mkdir -p "$HOME"/xdg/git &&
  92        echo content >excluded_by_xdg_only &&
  93        echo excluded_by_xdg_only >"$HOME"/xdg/git/ignore &&
  94        test_when_finished "git read-tree --empty" &&
  95        (XDG_CONFIG_HOME="$HOME/xdg" &&
  96         export XDG_CONFIG_HOME &&
  97         git add to_be_excluded &&
  98         test_must_fail git add excluded_by_xdg_only
  99        )
 100'
 101
 102test_expect_success 'Exclusion in both XDG and local ignore files' '
 103        echo to_be_excluded >.gitignore &&
 104        test_must_fail git add to_be_excluded
 105'
 106
 107
 108test_expect_success 'Exclusion in a non-XDG global ignore file' '
 109        rm .gitignore &&
 110        echo >"$HOME"/.config/git/ignore &&
 111        echo to_be_excluded >"$HOME"/my_gitignore &&
 112        git config core.excludesfile "$HOME"/my_gitignore &&
 113        test_must_fail git add to_be_excluded
 114'
 115
 116test_expect_success 'Checking XDG ignore file when HOME is unset' '
 117        (sane_unset HOME &&
 118         git config --unset core.excludesfile &&
 119         git ls-files --exclude-standard --ignored >actual) &&
 120        test_must_be_empty actual
 121'
 122
 123test_expect_success 'Checking attributes in the XDG attributes file' '
 124        echo foo >f &&
 125        git check-attr -a f >actual &&
 126        test_line_count -eq 0 actual &&
 127        echo "f attr_f" >"$HOME"/.config/git/attributes &&
 128        echo "f: attr_f: set" >expected &&
 129        git check-attr -a f >actual &&
 130        test_cmp expected actual
 131'
 132
 133test_expect_success 'Checking XDG attributes when HOME is unset' '
 134        (sane_unset HOME &&
 135         git check-attr -a f >actual) &&
 136        test_must_be_empty actual
 137'
 138
 139test_expect_success '$XDG_CONFIG_HOME overrides $HOME/.config/git/attributes' '
 140        mkdir -p "$HOME"/xdg/git &&
 141        echo "f attr_f=xdg" >"$HOME"/xdg/git/attributes &&
 142        echo "f: attr_f: xdg" >expected &&
 143        XDG_CONFIG_HOME="$HOME/xdg" git check-attr -a f >actual &&
 144        test_cmp expected actual
 145'
 146
 147test_expect_success 'Checking attributes in both XDG and local attributes files' '
 148        echo "f -attr_f" >.gitattributes &&
 149        echo "f: attr_f: unset" >expected &&
 150        git check-attr -a f >actual &&
 151        test_cmp expected actual
 152'
 153
 154
 155test_expect_success 'Checking attributes in a non-XDG global attributes file' '
 156        test_might_fail rm .gitattributes &&
 157        echo "f attr_f=test" >"$HOME"/my_gitattributes &&
 158        git config core.attributesfile "$HOME"/my_gitattributes &&
 159        echo "f: attr_f: test" >expected &&
 160        git check-attr -a f >actual &&
 161        test_cmp expected actual
 162'
 163
 164
 165test_expect_success 'write: xdg file exists and ~/.gitconfig doesn'\''t' '
 166        mkdir -p "$HOME"/.config/git &&
 167        >"$HOME"/.config/git/config &&
 168        test_might_fail rm "$HOME"/.gitconfig &&
 169        git config --global user.name "write_config" &&
 170        echo "[user]" >expected &&
 171        echo "  name = write_config" >>expected &&
 172        test_cmp expected "$HOME"/.config/git/config
 173'
 174
 175
 176test_expect_success 'write: xdg file exists and ~/.gitconfig exists' '
 177        >"$HOME"/.gitconfig &&
 178        git config --global user.name "write_gitconfig" &&
 179        echo "[user]" >expected &&
 180        echo "  name = write_gitconfig" >>expected &&
 181        test_cmp expected "$HOME"/.gitconfig
 182'
 183
 184
 185test_expect_success 'write: ~/.config/git/ exists and config file doesn'\''t' '
 186        test_might_fail rm "$HOME"/.gitconfig &&
 187        test_might_fail rm "$HOME"/.config/git/config &&
 188        git config --global user.name "write_gitconfig" &&
 189        echo "[user]" >expected &&
 190        echo "  name = write_gitconfig" >>expected &&
 191        test_cmp expected "$HOME"/.gitconfig
 192'
 193
 194
 195test_done