2

I have the problem, that we login to the systems as our own users johndoe and then have to do a sudo su - root to be able to do administrative tasks. We have the system config files in a git repository (which makes everything better). But now I lose the settings in the user's (johndoe) /home/johndoe/.gitconfig file. I tried to set the $GIT_CONFIGvariable which works, but this is only for setting the repo configuration, not the global one and therefore the for me important variables won't be set:

$ sudo su - root
Hello johndoe. Switching your .gitconfig to /home/johndoe/.gitconfig
# git config --get user.name
John Doe
# git config --get user.email
johndoe@somefirm.somewhere
# git config --global --get user.name
# git config --global --get user.email
#

This is the trick I tried:

$ cat .bashrc
--- snip ---

# load git config of user
LOGINUSER=$(who am i|awk '{print $1}')
if [ "$LOGINUSER" != "root" ]; then
    GITCFGFILE="/home/$LOGINUSER/.gitconfig"
    if [ -f $GITCFGFILE ]; then
        echo "Hello $LOGINUSER. Switching your .gitconfig to $GITCFGFILE"
        export GIT_CONFIG=$GITCFGFILE
    fi
fi

Is there a way to set the path for the global git configuration?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
  • Using something like linking the .gitconfig file (as suggested in different other posts) is not a good solution because the possibility exists, that more than one user is logged in concurrently. – oXiVanisher Sep 19 '14 at 06:46
  • It seems to be strange to ask whether to use git or su. You should probably write down a problem statement, i.e. what exactly are you trying to achieve at the beginning of your question. Btw, sudo su - is just a more complicated way to do sudo -i? Also git config --global is actually local to the user, [sudo] git config --system handles the system-wide config. – Pavel Šimerda Sep 19 '14 at 07:33
  • 1
    Wouldn't it be better to create a bunch of "root users", like 'johndoeroot', who are members of the root group and have root privileges? Then everyone could su to his own rootuser and everyone can have his own git-config. – Campfire Sep 19 '14 at 09:05
  • +1 for @Campfire, that way you can also distinguish who did what and when. – Jan Sep 19 '14 at 10:57
  • @PavelŠimerda: Title corrected and theoretically yes it is just more complicated. But thats the way "they" want us to do this, so i have to follow this rule. – oXiVanisher Sep 19 '14 at 11:55
  • @Campfire: In a perfect world, this is all correct. But sadly, i have no control over these things. I am just searching a solution for "us users". – oXiVanisher Sep 19 '14 at 11:56

2 Answers2

1

To answer my Question: No

This is the solution i ended up with: I added a file /etc/profile.d/gitsetup.sh with the following content:

[ -z "$PS1" ] && return
[ -z "$BASH_VERSION" -o -z "$PS1" ] && return
CFGFILE="/home/$(who am i|awk '{ print $1 }')/.gitconfig"
[ $USER == "root" -a -f "$CFGFILE" -a -n "$CFGFILE" ] && cat $CFGFILE > $HOME/.gitconfig

It tests on line one and two for an interactive shell, reads the source .gitconfig path on line three and then replaces the roots config file if the source file exists.

0

I jumped through all the same hoops and found the same issue with user information stored in $HOME/.gitconfig not having environment variable counterparts for our version of git.

I came across a situation that may affect you -- the .gitconfig file is clobbered when a second user is invovled -- and my own solution is questionable enough for me (a newbie git user) that it can be found posted here:

Dynamic user config for git with wrapper script?