Calling Git Config from Code - Standard Out is Empty on Global Vars

107 views
Skip to first unread message

drsyn.a...@googlemail.com

unread,
Apr 7, 2009, 11:44:19 AM4/7/09
to msysGit
Hi,
I don't want to start any flame wars here, but I think I might
have found a bug (and yes, it probably is in my own code)... (and I'm
sorry for that cliched start to a post when everyone sighs and goes oh
no not another moron on the list). I'ld like to apologise for the
formatting of this message - I've tried my best to keep it short but
contain all the information thats possible.

Basically, I am writing a bit of code that interfaces with the Git
Repositiory (it's actually part of a bug tracking system and I want to
interrogate the Git for various reasons). For the most part its going
really well apart from git config variables:

I am writing in c# and the problem is if I call the code to retrieve a
variable which is a local variable (ie defined by saying git config
x.y true) then when I recall it by saying git config x.y, I get true
in the standard out response stream, which I can read and everythings
happy.

However, if I have a global variable (for instance, user.name or
user.email) I don't get anything in the standard out response stream,
but only when called from inside code. I'm perfectly happy for people
to find a bug in my example app (which I've included below) because I
can't see why these should make a difference. (I have tried several
combinations, and never been able to work out why it is so).

I guess I should try the same type of function in a different language
to see if I get the same problem, but I don't have any other
programming language environment installed on my laptop at the moment.

Details & Replication
===============

C:\test>git config --global testing.testGlobal true

C:\test>git config testing.testLocal false

C:\test>git config testing.testGlobal
true

C:\test>git config testing.testLocal
false

Demonstration that both are going to Standard Out and not standard
Error:

C:\test>git config testing.testGlobal > StdOut.txt 2> StdError.txt

C:\test>dir

<SNIP />

07/04/2009 16:30 0 StdError.txt
07/04/2009 16:30 5 StdOut.txt


Issue with Program:

C:\test>GitTestApp.exe
Global]
Local ]false


Source Smallest Complete Program Demonstrating Behaviour:

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace GitTestApp {
class Program {
static void Main(string[] args) {
Console.WriteLine("Global]" + runGitCommand("config
testing.testGlobal").Trim());
Console.WriteLine("Local ]" + runGitCommand("config
testing.testLocal").Trim());
}

// Making sure that I run the same command for both
private static String runGitCommand(string args) {
Process git = new Process();

// Hard coded for Demonstration
git.StartInfo.FileName = "c:\\Program Files\\git\\bin\
\git.exe";
git.StartInfo.Arguments = args;

// if ytou comment out the next line it should execute in
the "current" working directory
// force it for demonstration purposes
git.StartInfo.WorkingDirectory = "c:\\test\\";
git.StartInfo.CreateNoWindow = true;
git.StartInfo.RedirectStandardOutput = true;

git.StartInfo.UseShellExecute = false;

git.Start();

git.WaitForExit();

string ret = git.StandardOutput.ReadToEnd();

git.Close();

return ret;
}
}
}

Markus Heidelberg

unread,
Apr 7, 2009, 3:13:39 PM4/7/09
to msy...@googlegroups.com, drsyn.a...@googlemail.com
drsyn.a...@googlemail.com, 07.04.2009:

> I am writing in c# and the problem is if I call the code to retrieve a
> variable which is a local variable (ie defined by saying git config
> x.y true) then when I recall it by saying git config x.y, I get true
> in the standard out response stream, which I can read and everythings
> happy.
>
> However, if I have a global variable (for instance, user.name or
> user.email) I don't get anything in the standard out response stream,
> but only when called from inside code. I'm perfectly happy for people
> to find a bug in my example app (which I've included below) because I
> can't see why these should make a difference. (I have tried several
> combinations, and never been able to work out why it is so).

I guess the HOME environment variable is not set in the scope of your
program and hence git cannot find $HOME/.gitconfig

Markus

drsyn.a...@googlemail.com

unread,
Apr 9, 2009, 9:42:13 AM4/9/09
to msysGit
> > I am writing in c# and the problem is if I call the code to retrieve a
> > variable which is a local variable (ie defined by saying git config
> > x.y true) then when I recall it by saying git config x.y, I get true
> > in the standard out response stream, which I can read and everythings
> > happy.
>
> > However, if I have a global variable (for instance, user.name or
> > user.email) I don't get anything in the standard out response stream,
> > but only when called from inside code.

> I guess the HOME environment variable is not set in the scope of your
> program and hence git cannot find $HOME/.gitconfig

Thanks for your quick reply, I don't have a %HOME% variable in either
my cmd or my program - there is a %HOMEPATH% in both which I think is
the window's equivalent. I've started looking into this in more depth,
but it looks to me at the moment that this is set.

I've also got a suspicion about a parameter called "useshellexecute"
which I have to set to false to be able to read the std out stream. I
think that if I set this to true, then the git config global lookup
would work, so I'm wondering if its something else in the shell which
is causing the difference (or if having this stops the environment
variables to be loaded into the sub-process).

I'll investigate some more but I think you've put me on the right
track.

Many Thanks
Syn

Markus Heidelberg

unread,
Apr 9, 2009, 6:10:09 PM4/9/09
to msy...@googlegroups.com, drsyn.a...@googlemail.com
drsyn.a...@googlemail.com, 09.04.2009:

>
> > I guess the HOME environment variable is not set in the scope of your
> > program and hence git cannot find $HOME/.gitconfig
>
> Thanks for your quick reply, I don't have a %HOME% variable in either
> my cmd or my program

Then I wonder how you can set and query global git config options, it
doesn't work for me without %HOME% on cmd.exe, which is no surprise
after looking into the sources.

Could you please set %HOME% and test again?

> - there is a %HOMEPATH% in both which I think is
> the window's equivalent.

And there is %HOMEDRIVE% and these concatenated together seem to form
%USERPROFILE%, at least for me.

Markus

drsyn.a...@googlemail.com

unread,
Apr 10, 2009, 1:06:56 PM4/10/09
to msysGit

> > > I guess the HOME environment variable is not set in the scope of your
> > > program and hence git cannot find $HOME/.gitconfig
>
> > Thanks for your quick reply, I don't have a %HOME% variable in either
> > my cmd or my program
>
> Then I wonder how you can set and query global git config options, it
> doesn't work for me without %HOME% on cmd.exe, which is no surprise
> after looking into the sources.
>
> Could you please set %HOME% and test again?

Bingo! I'm now wondering why you think it shouldn't work without the
%HOME% being set in normal cmd.exe ... Is git (when called) cause
itself to form a new shell or similar which wraps the environmental
variables it needs?

What I'll do is try to get a test sequence put together tonight /
tomorrow morning to work out why loading a global variable works in
cmd.exe without %HOME% being set (I know it wasnt set from the simple
sequence of

> echo %HOME%
%HOME%
> set HOME c:\users\Syn\
> echo %HOME%
c:\users\Syn\

But I will to put a proper test together and put it somewhere.

Best Regards
Syn

Markus Heidelberg

unread,
Apr 10, 2009, 2:06:52 PM4/10/09
to msy...@googlegroups.com, drsyn.a...@googlemail.com
drsyn.a...@googlemail.com, 10.04.2009:

>
> > > > I guess the HOME environment variable is not set in the scope of your
> > > > program and hence git cannot find $HOME/.gitconfig
> >
> > > Thanks for your quick reply, I don't have a %HOME% variable in either
> > > my cmd or my program
> >
> > Then I wonder how you can set and query global git config options, it
> > doesn't work for me without %HOME% on cmd.exe, which is no surprise
> > after looking into the sources.
> >
> > Could you please set %HOME% and test again?
>
> Bingo! I'm now wondering why you think it shouldn't work without the
> %HOME% being set in normal cmd.exe

Well, from the source and because it doesn't work for me.

Can you investigate, where (in which path in the filesystem) your global
.gitconfig exactly is, when you do your "git config --global
testing.testGlobal true", which works for you without %HOME%?

> ... Is git (when called) cause
> itself to form a new shell or similar which wraps the environmental
> variables it needs?

The parts that are written in C, i.e. the built-ins like git-config,
shouldn't need a shell.

> What I'll do is try to get a test sequence put together tonight /
> tomorrow morning to work out why loading a global variable works in
> cmd.exe without %HOME% being set

I just tried some things under Linux.

1) default, $HOME set to /home/markus
$ git config --global section.key value
worked as expected, of course

2)
$ export HOME=
$ git config --global section.key value
error: could not lock config file /.gitconfig

So here git wants to use /.gitconfig, where only root has access to.

3)
$ unset HOME
$ git config --global section.key value
$ fatal: $HOME not set

The relevant part of the source with 2 comments from me:

char *home = getenv("HOME");
if (home) {
/* 1) and 2) go here */
char *user_config = xstrdup(mkpath("%s/.gitconfig", home));
config_exclusive_filename = user_config;
} else {
/* 3) goes here */
die("$HOME not set");
}

Maybe, although %HOME% is not set, cmd.exe (sometimes?) treats it as an
empty string and writes to /.gitconfig, which then will become C:\.gitconfig?

I think I can remember having seen C:\.gitconfig under Windows at
work, but now under Windows in Qemu I get the expected error "fatal:
$HOME not set". Maybe you have C:\.gitconfig as well on your drive?

Markus

Dr Syn

unread,
Apr 10, 2009, 2:42:51 PM4/10/09
to msy...@googlegroups.com
Ok I'm struggling a bit with all the questions at once, so I'm just
going to take this opportunity to sumerise and answer.

The issue seems to be that when %HOME% was not set when git is called
as a process (through a start process command) then you cant get at
git global variables.

This is because in builtin-config.c there is some code which says
literally "get the HOME parameter".

However when I am in the cmd shell (cmd.exe) on both my XP computer,
my Vista 64 and my Vista 32 computers, this seems to work fine without
the %HOME% variable being set.

My .gitconfig file is (on my vista 32 computer) stored in my HOMEPATH
location (c:\users\syn\), and there is no .gitconfig file in the root
of C: (which would happen if %HOME% was interpreted as being empty and
"/.gitconfig" gets translated to c:\gitconfig)

Finally that this behaviour is different to Markus's on his Windows
test environment.


I streamlined my "git test program" so that what it does now is 1)
print out the environmental variable of HOME in *its* process,
followed by the environmental variable that is set in the start.info
of its child process (the git process) and finally, the results of the
"git config section.key".

Hope that helps

Syn.

Demonstration


C:\test>echo %HOME%
%HOME%

C:\test>git config --global section.key value

C:\test>git config section.key
value

C:\test>GitTestApp.exe
env. HOME value ]
StartInfo Home     ]
section.key   ]

C:\test>type c:\Users\syn\.gitconfig
[user]
        name = Syn
       <snip snip snip>
[testing]
        testglobal = true
[section]
        key = value

C:\test>set HOME=c:\users\Syn

C:\test>echo %HOME%
c:\users\Syn

C:\test>git config section.key
value

C:\test>GitTestApp.exe
env. HOME value ] c:\users\Syn
StartInfo Home     ]c:\users\Syn
section.key ] value

/// Just confirming it a second time...

C:\test>set HOME=

C:\test>echo %HOME%
%HOME%

C:\test>GitTestApp.exe
env. HOME value ]
StartInfo Home     ]
section.key ]

C:\test>git config section.key
value

Johannes Schindelin

unread,
Apr 14, 2009, 11:47:47 AM4/14/09
to Dr Syn, msy...@googlegroups.com
Hi,

On Fri, 10 Apr 2009, Dr Syn wrote:

> The issue seems to be that when %HOME% was not set when git is called as
> a process (through a start process command) then you cant get at git
> global variables.

If HOME is unset, it is set to the value of $USERPROFILE (or in cmd speak:
%USERPROFILE%).

Hth,
Dscho

Markus Heidelberg

unread,
Apr 18, 2009, 5:57:32 AM4/18/09
to msy...@googlegroups.com, Johannes....@gmx.de, Dr Syn
Johannes Schindelin, 14.04.2009:

For msys git bash, but not for cmd.exe
I was just reminded of this message when fetching the latest msysgit
with the commit "Add /git-cmd.bat, a shortcut to launch cmd with the
PATH set up properly".

Markus

Johannes Schindelin

unread,
Apr 18, 2009, 8:26:14 AM4/18/09
to Markus Heidelberg, msy...@googlegroups.com, Dr Syn
Hi,

On Sat, 18 Apr 2009, Markus Heidelberg wrote:

> Johannes Schindelin, 14.04.2009:


>
> > On Fri, 10 Apr 2009, Dr Syn wrote:
> >
> > > The issue seems to be that when %HOME% was not set when git is
> > > called as a process (through a start process command) then you cant
> > > get at git global variables.
> >
> > If HOME is unset, it is set to the value of $USERPROFILE (or in cmd
> > speak: %USERPROFILE%).
>
> For msys git bash, but not for cmd.exe

But from cmd.exe, you should call git.cmd and gitk.cmd from /cmd/, not
what is in /bin/...

Ciao,
Dscho

Markus Heidelberg

unread,
Apr 18, 2009, 9:38:05 AM4/18/09
to Johannes Schindelin, msy...@googlegroups.com, Dr Syn
Johannes Schindelin, 18.04.2009:

> But from cmd.exe, you should call git.cmd and gitk.cmd from /cmd/, not
> what is in /bin/...

Oh, thanks, didn't know that. I installed without touching PATH and
manually added bin/ to it. Now I know, why I had to set HOME manually, too.

And I wondered how the installer would accomplish "Only Git will be
added to your PATH", now that's clear :)
In fact, just this week it occured the first time that I needed to start
gitk from cmd.exe and I wondered why it's not in bin/ but cmd/. Now
that's clear as well.

Markus

Reply all
Reply to author
Forward
0 new messages