Update git repository with apple script

applescriptgitterminal

I want to update my git Bitbucket online storage with apple script. This is the code that works in Terminal, but not when i run apple script.

cd /Users/mainuser/Desktop/Projects git add -A
git commit -m fromAppleScript
git push

crafted into apple script:

        say "updating backup"
        do shell script "cd /Users/mainuser/Desktop/Projects git add -A"
        do shell script "cd /Users/mainuser/Desktop/Projects git commit -m fromAppleScript"
        do shell script "cd /Users/mainuser/Desktop/Projects git push"

I am a bit puzzled why this code doesn't work. Can you help me out with this problem?

Best Answer

You appear to be using git as a file system back-up. Have you considered git-annex?

Learning git

git can be tricky to learn and debug. Consider mastering the steps you want via Terminal.app – with the help of the Git Book.

BitBucket support provides a wealth of examples and workflows to try and to learn from. Once you trust that process, automating via AppleScript will be much easier.

Fixing the Shell Script

To get you started, there are is a problem with the AppleScript. The script passed to do shell script will return an error:

do shell script "cd /Users/mainuser/Desktop/Projects git add -A"

Within these quotes you are issuing two commands: one to change directory, and the second to add files to git. These two commands need to be separated with a semi-colon:

do shell script "cd /Users/mainuser/Desktop/Projects; git add -A"

A better approach still, would be to tie the commands together with &&. This will mean that if the first command fails, the second command is not performed. This approach deals with the problem of a missing or renamed directory:

do shell script "cd /Users/mainuser/Desktop/Projects && git add -A"

Alternatively, to avoid working directory concerns try passing the full path to the git commands:

do shell script "git add -A '/Users/mainuser/Desktop/Projects'"