background image

Content tagged with: diff

Eric's picture

Back in December 2008 I posted a blog entry on how to upgrade a subversion integrated Drupal site. This procedure was based on the process of unpacking the new Drupal installation, copying the sites folder into the new Drupal installation, and checking everything back into subversion. Although this is the recommended and cleanest way to upgrade Drupal, it may be a hassle if you have a huge repository and lots of files. So why not just overwrite the old directory with the new Drupal files? Well first of all, it would be good to take into consideration file deletions; and second, I've always been the kind of person who believes in fresh installs. I'd rather reinstall my operation system versus upgrading it. If you insist on overwriting the files, here's how you can do it...

Go to /admin/reports/updates to checkout which version your currently running and which is recommended. Download the new version of Drupal, along with the current version of your Drupal installation. If you can't find the current version, look for the "View all releases" link here: http://drupal.org/project/drupal

$ wget http://ftp.drupal.org/files/projects/drupal-6.8.tar.gz
$ wget http://ftp.drupal.org/files/projects/drupal-6.9.tar.gz

Next, unpack both of the files:

$ tar -xzf drupal-6.8.tar.gz
$ tar -xzf drupal-6.9.tar.gz

Now, use the diff command to compare the directories:

$ diff -r -q drupal-6.8 drupal-6.9

You'll see a lines of output that resemble the following:

Files drupal-6.8/CHANGELOG.txt and drupal-6.9/CHANGELOG.txt differ

Files that differ are OK, we can easily copy them into our current installation and check them in as modifications. To ignore those lines, we can run the previous command and tack on a grep statement to ignore them:

$ diff -r -q drupal-6.8 drupal-6.9 | grep -iv differ$

Hopefully, the previous command will return no output. If the diff command returned any output, it's probably telling you a file exists in one version and not the other. You'll have to manually resolve these subversion changes.

If the previous diff command returned no output, the new Drupal installation can be simply be copied on top of your current installation. But first, let's keep track of how many changes there were. NOTE: the next command assumes your current Drupal environment is up to date (the HEAD revision) and every file has been checked into the repository prior to the upgrade.

# check how many file modifications there are:
$ diff -r -q drupal-6.8 drupal-6.9 | wc -l
83

# copy the files into your current Drupal installation:
$ cp -r drupal-6.9/* /path/to/your/drupal/installation/httpdocs/

# verfiy the same number of file modifications:
$ svn stat /path/to/your/drupal/installation/httpdocs/ | grep ^M | wc -l
83

If the numbers match, run the update.php script. And if all went well, commit the changes:

$ svn commit /path/to/your/drupal/installation/httpdocs/ -m "Upgraded Drupal from 6.8 to 6.9"

Eric's picture

I recently had to deploy some new code to an old production environment. Like a good doobie, I made all my changes in a local checked out copy of the subversion file system. I did not want to break the production environment, so I copied the entire vhost into my home directory on the server. I tried to execute an svn update command, but it terminated with the message: object of the same name already exists. This means that a file was creating in my development environment (later revision) that was also created in the production environment. When I ran a svn stat command, there were too many additions, deletions, and modifications, so I decided to write a PHP script to compare the directory structure and files of the 2 environments. I used this script to analyze the file system and create a deployment plan...

<?php
// define where all my files are
$path_httpdocs = '/my/first/path/httpdocs';
$path_httpdocs_new = '/my/second/path/httpdocs';

// get a list of files from the 1st location, ignoring subversion folders
chdir($path_httpdocs);
$files_httpdocs = `find . | grep -v \.svn | sort`;
$files_httpdocs = explode("\n", $files_httpdocs);

// get a list of files from the 2nd location, ignoring subversion folders
chdir($path_httpdocs_new);
$files_httpdocs_new = `find . | grep -v \.svn | sort`;
$files_httpdocs_new = explode("\n", $files_httpdocs_new);

// check for file list diffs
$diffs = array_diff($files_httpdocs, $files_httpdocs_new);
sort($diffs);
echo
"### Additions to httpdocs:\n";
print_r($diffs);

// loop through files and check if they are additions
foreach ($diffs as $f) {
  if (
file_exists($path_httpdocs . '/' . $f) && !file_exists($path_httpdocs_new . '/' . $f)) {
   
// copy new addition to new folder
   
copy($path_httpdocs . '/' . $f, $path_httpdocs_new .'/' . $f);
  }
}

// check for file list diffs
$diffs = array_diff($files_httpdocs_new, $files_httpdocs);
sort($diffs);
echo
"### Additions to httpdocs_new:\n";
print_r($diffs);

// do not continue if there are file differences in the 2 directories
if (count(array_diff($files_httpdocs, $files_httpdocs_new)) || count(array_diff($files_httpdocs_new, $files_httpdocs))) {
  echo
"### Clean up file differences before continuing...\n";
  die;
}

// loop through files and check file properties
$diffs = array();
foreach (
$files_httpdocs as $f) {
  if (!
is_file($path_httpdocs . '/' . $f)) continue;

 
// do a diff on the files 
 
$command = "diff \"$path_httpdocs/$f\" \"$path_httpdocs_new/$f\"";
 
$t = `$command`;

 
// if the diff command generated output, store it
 
if (strlen($t)) {
   
$diffs[$f] = $t;
  }

}

// print all the file diffs
print_r($diffs);
?>