Phil for Humanity Phil for Humanity
A Guide for the Survival of Humankind and Helping the World, Society, and Yourself.



Git Pull Deletes Script


Here is an interesting issue with Git deleting files when running "git pull" in a script when it should not.

Assume that you want all users to run the latest version of a script that is in a Git repository, you would most likely start the script with the "git pull" command. Unfortunately, this will not work, at least not on Windows. You would assume that the script is first loaded into memory, then the script is executed. Again assuming that the script would pull a newer version of the script (if exists) into the local repository, so that the next time the script is run then the newer version of the script would run.


In actuality, you will get this error message when pulling.

remote: Counting objects: 5, done
remote: Finding sources: 100% (3/3)
remote: Total 3 (delta 1), reused 3 (delta 1)
Unpacking objects: 100% (3/3), done.
From ssh://server.com/directory/repository
5cc47e1..5952a12 master -> origin/master
Updating 5cc47e1..5952a12
error: unable to create file scriptname (Permission denied)

This will delete your script and not replace it with a newer version. This is because the script is open and executing; and therefore, the script cannot be replaced. Unfortunately, it can and will be deleted as if it is going to be replaced. Again, I think this is only a Windows issue with Git.

I have found this workaround as an acceptable alternative that greatly minimizes the occurrence of this issue and provides an immediate solution to the users. First, you have to split the script into two scripts. The first script basically mostly runs "git pull", and then runs the second script. Since the second script would be modified more often, there will be no issues updating the second script, since it is not running at the time of running the "git pull". The first script is so simple that it rarely needs modifying thus minimizing the occurrence of the issue. Finally, the first script will additionally monitor when it did not successfully pull and provide a solution to fix the deleted file.

Here is an example of the first script:

#!/bin/perl

`git pull origin > /dev/null 2>&1`;
if ($?)
{
die "\nERROR: The \"git pull\" command failed. You will have to manually run \"git pull\" manually in order to re-execute this script..\n\n";
}
system ("second_script.pl");
exit $?;

An added benefit of this script is that it guarantees that the latest version of the script is always run instead of the second time it runs.

by Phil for Humanity
on 20121001

Related Articles
 » Git Log and Cat-File
 » Git Executable Permissions
 » Git Tag