0

I make some script :

#!/bin/bash

#####
dest_branch="4.0.8_copy"
source="sprint24_copy"
#####

dest=$dest_branch
source=$source_branch
startDir=`pwd`
sshFile=$startDir"/git_ssh.ssh"
gitrepo="<some valid repo adress>"
git_key=$startDir"/key.ssh"

sshKey="-----BEGIN RSA PRIVATE KEY-----
MIIJJwIBAAKCAgEAs7Rfpca8s4.... and rest of key" 

echo $sshKey > $git_key 
echo "ssh -i "$git_key" \$@ "> $sshFile

export GIT_SSH=$sshFile
export GIT_TRACE=1

git clone git@$gitrepo -b $dest

I run this script in Git Bash (C:\Program Files\Git\bin\sh.exe), and this script failed with this out error:

Cloning into 'neos'...
10:44:26.575804 run-command.c:343       trace: run_command: 'C:/Users/<some user>/Desktop/<some dir>/git_ssh.ssh' 'git@<some valid adress>' 'git-upload-pack '\''<some project>'\'''
error: cannot spawn C:/Users/<some user>/Desktop/<some dir>/git_ssh.ssh: No such file or directory
fatal: unable to fork

I try to change command pwd to static path like C:/.... c:/... /c/... \c... c:... C:... but still faild. Can anybady help me to specific another private key to clone git repo?? Why this sh.exe cannot see my file? I'm sure that file exists. I use git version 2.6.4.windows.1. Thanks for your help, and sorry about my English.

javabrett
  • 7,020
  • 4
  • 51
  • 73
lgabryel
  • 109
  • 1
  • 9
  • 1
    Never tried git on windows. Normal debug for bash is to change the shebang to `#!/bin/bash -x` and look at the output to verify that everything is expanded as intended. For sshKey-assignment have a look at heredocs to store multiline strings, e.g. http://stackoverflow.com/questions/23929235/bash-multi-line-string-with-extra-space – Fredrik Pihl Jan 21 '16 at 13:45

1 Answers1

1

I try to understand problem. I have some observation.

I use git.exe. git.exe uses Windows path, but MinGW uses both, and ssh uses only linux :X

When I try use GIT_SSH='path to scrip', then git uses some like ConvertToWindowsPatch(GIT_SSH). Finally git finds script, but now SSH can not finds file with key !

When I try GIT_SSH='ssh -vvv' to debug I see

bash: ssh: command not found

First I think wtf... but I see that ssh is an alias ! So I use GIT_SSH='ssh.exe -vvv' and now it work. Unfortunately I can not resolve my problem that ssh do not see my file with key, but I know, if I write file to C:\, then in C this file doesn't exists ! This file is in C:\Users\\AppData\Local\VirtualStore . I don't know how can I save my ssh key in file and ssh can see it but I have other solve.

Before my main funcion of script, I backup .ssh folder. Then I write id_rsa in .ssh, and known_host. Then I want git clone, but ssh ask me about passphrase ... BUT THIS KEY HAVE NOT PASS !!. Ok calm down. When I try ssh.exe -vvv I see that sshKey haven't header, because I write this key without newlines ! I have this :

echo $sshKey > $sshKeyFile

but correct is

echo "$sshKey" > $sshKeyFile

Now all work correctly. I see 2 WTF - first ssh don't see files in MinGW, second if ssh don't see header of key without -vvv don't see this but ask about pass (wtfwtfwtfwtfwtf...)

This is correct script to manipulate git repo ( merge, push etc... ) in bash using MinGW, git and Windows, with specific rsa private key. I use it to automatic marge and create branches in Jenkins on Windows.

#!/bin/bash

#####
dest_branch="4.0.8_copy"
source="sprint24_copy"
#####

dest=$dest_branch
source=$source_branch
sshDir="/c/Users/<user>/.ssh"
gitrepo="<repo>"
sshKeyFile=$sshDir"/id_rsa"
knownHostsFile=$sshDir"/known_hosts"

sshKey="-----BEGIN RSA PRIVATE KEY-----
MIIJKAIBAAKCAgEApH0bkaXa0z7811Sd1ZEG87adPPNWyaya47T3GrCjjnRvVyEk
N8jGjh/..."

knownHosts="<some host> ecdsa-sha2-nistp256 AAAAE2VjZ..."

if [ -a $sshDir ] ; then
  echo "backup .ssh"
  cp -r $sshDir $sshDir"_back" 

  echo "usuwanie starego .ssh"
  rm -r $sshDir

  echo "tworzenie nowego .ssh"
  mkdir $sshDir

  echo "zapis klucza rsa"
  echo "$sshKey" > $sshKeyFile

  echo "zapis znanyn hostów"
  echo $knownHosts > $knownHostsFile

  echo "wlaczenie debugu gita"
  export GIT_TRACE=2

  echo "wlaczenie debugu 

  echo "clone repo"
  git clone git@$gitrepo -b $dest
  #other git operations

  echo "usuwanie tymczasowego .ssh" 
  rm -r $sshDir

  echo "odtwrzanie z backupu .ssh"
  cp -r $sshDir"_back" $sshDir

  echo "usuwanie backupu"
  rm -r $sshDir"_back"
fi 

Thanks for help and comment !

lgabryel
  • 109
  • 1
  • 9