In my Cocoa application I'm trying to use NSTask
to run some basic Git commands. Whenever I run a command that requires permissions (SSH keys) to access a remote (e.g. git push
, git pull
), it fails with the following error:
Permission denied (publickey). The remote end hung up unexpectedly
Running the same commands from Terminal works just fine, so I'm thinking that this might be an issue with NSTask
not setting an environment variable that would be used somewhere in the process of accessing the ssh keys. I tried manually setting the HOME
and USER
environment variables like this:
[task setEnvironment:[NSDictionary dictionaryWithObjectsAndKeys:NSHomeDirectory(), @"HOME", NSUserName(), @"USER", nil]];
But this has no effect. Is there any particular environment variable I have to set in NSTask
for this to work properly?
EDIT: Thanks to Dustin's tip, I got a little bit further in figuring this out. I used the env
command to list the environment variables for my current session and I found this:
SSH_AUTH_SOCK=/tmp/launch-DMQopt/Listeners
To test, I copied that path and set it as an environment variable of NSTask
and ran the code again, and this time it worked! That said, I'm certain that SSH_AUTH_SOCK
changes for each session so I can't just hardcode it. How do I dynamically generate/retrieve this variable?
env -i
on the commandline and see how much you need to add back before it works. – Cogitable