Running Git command from Expect script fails

I am writing an Expect script to pull the latest code from our dev server each time a new server instance is spawned on AWS. The idea is that when the new server is created, the script is run at boot before being placed behind the load balancer. It works perfectly unless Git aborts because of a conflict. Here is the code so far:

#!/usr/bin/expect -f
set timeout 240

spawn /xyz/pullfromdev
expect "*?assphrase*"
send -- "<mypass>\r"

expect {
        "Aborting" {
                set response $expect_out(buffer)
                set format [split $response "\n"]
                cd /var/www/html
                foreach line $format {
                        if { !([string range $line 0 8] == "Identity " || [string range $line 0 8] == "Updating " || [string range $line 0 5] == "error:" || [string range $line 0 6] == "Please," || [string range $line 0 7] == "Aborting" || [string length $line] == 1 ) } {
                                open "| git checkout -- [string trim $line]"
                spawn /xyz/pullfromdev
                expect "*?assphrase*"
                send -- "<mypass>\r"

expect eof

Basically I am splitting the output buffer and trying to run git checkout -- <filename> for each file that has a conflict. Then trying to pull again to hopefully complete successfully.

  • Jenkins not waiting on call to a bash script with expect
  • How to use expect and git clone?
  • Why doesn't my git auto-update Expect script work?
  • expect utility is not working when executing from jenkins
  • The issue is with the line:

    open "| git checkout -- [string trim $line]"

    It simply does not perform any action apparently and i do not get any error. The script tries to pull again from the dev server and aborts because the git checkout did not occur and the conflicts remain. TCL is a bit foreign to me so perhaps its a syntax issue? I have tried spawn as well as exec but they do not seem to work either. Conflicts remain and git aborts.

    However, if I run git checkout -- <filename> via the command line, then that particular file is no longer conflicting (because the checkout actually occurred). So the issue really is getting the git checkout command to run from the Expect script which i am failing to do somehow.

    Does anyone have any suggestions?

    If it helps at all, this is on CentOS 5 and the ‘pullfromdev’ script referenced in the code is simply:

    cd /var/www/html
    ssh-agent bash -c 'ssh-add /xyz/<identity>; git pull'

  • Git commit from python
  • merge to master with sbt release-plugin
  • Redo part of an already committed merge
  • Repository other than GitStack?
  • Branch keeps tracking master
  • Merging all the commits centrally
  • 2 Solutions collect form web for “Running Git command from Expect script fails”

    When opening a pipeline, everything apart from the first character is a Tcl list of arguments (the first | is special). This means you should use this:

    open |[list git checkout -- [string trim $line]]

    You can’t use == to compare strings in TCL. Try the string compare or regexp commands.

    Git Baby is a git and github fan, let's start git clone.