Setting up a local working copy with SVN, MAMP and rsync Agentic - 2009
Get MAMP You can download MAMP for MAC at this address : http://www.mamp.info/en/downloads/index.html Install MAMP in your APPLICATION folder.
Get a copy of the site on your local Once you have MAMP installed, you can prepare the copy of the site you have to work on. To do so you need to have two things (for a drupal site) : The Drupal files (containing drupal core, themes, modules, etc...) The corresponding database of the site We are going to start by getting the Drupal files. To do so, we are going to use SVN. SVN is going to keep track of the files you will work with, and check if the modification you re doing can be added to the live site, as well as all the modifications other team member could commit to the live site. This allow a group of people to work on the same files without deleting/overlapping any code. It could be compared to editing a google document at the same time as another person on internet.
SVN In order to use SVN to retrieve the files you need, the most common way to do so is to use the Terminal application. You can find it in the Utility folder of your applications folder (Application/Utilities/Terminal). Start terminal :
Going to the right folder using terminal First you need to tell WHERE you want the site s files to be downloaded. To do so, you are going to point Terminal to the right folder. A very simple way is to use your FINDER to create the path. First, type in terminal cd followed by a space: Then go and create the folder you want to download the files to. It should be an empty folder in the htdocs folder located inside of the MAMP folder (here I use MyLocalSite as an example) : /Applications/MAMP/htdocs/MyLocalSite You could either type this path after cd in Terminal, or drag the folder into the Terminal window, it ll create the path automaticly. Then press ENTER
SVN checkout Now you should retrieve the link and password to the SVN repository from your developer. It should be something like this : svn co svn+ssh:// yourname@svn.livesitename.ca/var/svn/ pathtoproject You should also have a password along with this. Start first by copy/paste the code in Terminal. It should prompt you for the password, enter it (password are not shown in Terminal, so even if it looks like you re not typing, you are), and then you should see some activity. It s the list of all the files that are being downloaded from the live site to your computer.
Checkout using TEXTMATE You can checkout, and pretty much do all SVN action directly from TEXTMATE, the best code editor :). This offer a more friendly interface if you are not very comfortable with Terminal s interface. First start Textmate. To access to the SVN command, you need to have a file open.. I know it s weird, but open any text file and you should be good to go. Once you have a file open, go to BUNDLE > SUBVERSION > CHECKOUT or use the shortcut : CONTROL+SHIFT+A and click on CHECKOUT You should see a new pop up window asking you the URL of the repository like this one :
Same as before, type in the link your developer gave you, starting after the //. Press CHECKOUT. You should see a new window asking you where to drop the file. Remember to specify a folder within htdocs in the MAMP folder. Press SAVE. Wait few minutes, and all your files should be ready to go.
Checking the files You can view all the downloaded files if you navigate to the folder in FINDER. Now it s time to get the DATABASE
Database DUMP You now need to get a database dump that you will later connect to your Drupal install that is now on your local. You can get it yourself by login in on the live site you are working on. If you have the right permissions, you can access the export DB page : /admin/content/backup_migrate Make sure you selected EXPORT. Then Scroll down the page and click on BACKUP DATABASE. You should then be able to download a file. Save it, this is your database.
Import Database in MAMP Now that you have a database, you have to import it in your local environment. You can do so through Terminal, but there is a much more user-friendly way, using PHPMyAdmin, a PHP interface to interact with your databases. Open MAMP, and click on OPEN START PAGE Once the page is open, in the main menu, click on " phpmyadmin
PhpMyadmin First you need to create an empty database, to import the one you downloaded into it. Juste enter a name for the database and click CREATE. Your database should be created, but empty (no tables in it)
Import tables Now you need to import the database. Just click on IMPORT in the main menu Then use the browse input to select the database you downloaded earlier. Don t change anything else, and click GO on the bottom right of the page. Wait few minutes, the page should reload once it s done, and list all the tables that have been imported. If there is an error, contact your developper to try to do it through Terminal.
Connect the database with Drupal As this is not a first instal of Drupal, you need to manually connect the Drupal site to the database. Fist go to your drupal site and find the settings.php file located in sites/default/settings.php Open the file, and on line 93, you should see something like this : $db_url = 'mysql://username:password@localhost/databsename'; replace username and password by root and the databasename by the name of the database you entered earlier. $db_url = 'mysql://root:root@localhost/mydbname'; Save the file
View your site Now you can view your site through Firefox. Just open the Start page of MAMP, and delete the part of the URL that is after localhost/ or 8888/ You should see the folders in htdocs now, navigate to your site s folder to view it.
Adding a new theme with SVN and Terminal You might need to install a new theme (like BASIC for example :) ). This will give us the opportunity to see how can we add new files to the site using SVN with Terminal. First, download BASIC : http://drupal.org/project/basic Drop it in the folder sites/all/themes/ Open Terminal, and navigate to the theme folder. Remember that you can drag and drop a file into terminal to get its path : cd /Applications/MAMP/htdocs/drupal6/sites/all/themes In Terminal, type svn add basic, This will tell SVN to manage the newly added Basic folder and its content To commit the added files, type : svn commit -m type a message to explain your commit
Editing / Committing using Textmate After installing your theme in your local, you might want to edit it a bit :). Using Textmate, not only you can edit entire folders, but you can also commit the changes to SVN through a very nice and simple user interface. First to open a file in Textmate, just drag it to the icon in your dock. You can open an entire folder (like the Basic theme folder) in Textmate by just drag and drop on the Textmate logo. The folder should be exposed in the Textmate file drawer. When editing, each opened file is displayed in a tab, and unsaved files have a little circle instead of the closing cross before the name.
Editing / Committing using Textmate Once you re happy with your modifications, you can commit them to SVN using Textmate. First, select which file to commit. If you want to only commit one file, select it in the file drawer. If you want to commit all the modifications inside one folder, select the folder in the file drawer. To commit, use the following keyboard shortcut : Ctrl + Shift + A A menu should appear beside your mouse. All the SVN actions available to you are listed. To commit, you can either click on Commit or press the corresponding menu number 5.
Commenting your commit Once you selected Commit, the list of the modified files you are going to commit appear, and a text area on top allow you to type a comment about your committing. Make sure that all the files are checked. If you already type a comment previously that you d like to repeat, you can select one of the last 5 comments you typed to be repeated in the text area.
Adding/deleting files If you added or deleted some files, SVN will notice it, and ask you if you want to remove or add the files from the version control. Deleted files will show up with a! icon, and the new files will show up with a? icon. For both files, make sure that the corresponding checkbox is checked, and that you specified, using the link on the right, what is the action to do. Once all the files are configured correctly and that you entered your comment, click on Commit.
Failed commit If after committing you have an error message telling you that the commit failed, you might want to make sure that your files have the latest version. To do so, choose the Update action in your SVN menu (Ctrl + Shift + A). This action will retrieve the latest version of the files, and merge them with yours. If no conflict is reported, you can then commit your files.
Resolving Conflicts In some rare cases, you might have some conflicts between your code and the latest version of the code. A conflict means that SVN cannot find a way to merge your files with the latest version of the code. This can mean that you forgot to update your local files before working, and someone else committed some major changes, or in exactly the same areas as you. SVN will create some extra files in case of conflict. For example, if style.css has a conflict, SVN will create some duplicate of style.css of the latest revisions, and call them by their revision number like : r210.style.css, meaning that this is the file on revision #210. It will also create a copy of it named mine.style.css, that correspond to your version. Finally, SVN will comment the style.css file with the conflicted areas like this : >>>> revision 210 or >>>>> mine This can help you to manually figure out a way to merge the conflicting versions.
Resolving Conflicts When you manually resolved the conflicts, make sure you delete all unnecessary files that SVN added before trying to commit again. If you can figure out how to resolve a conflict, make a copy of your code (mine.style.css) somewhere on your hard drive (on your desktop for example), and delete all the conflicting files from the folder. Then, run a SVN update to retrieve the latest version of the files. You can now add your code to the valid style.css, and commit safely the modifications once it s working properly.
SVN Bugs Sometimes it is possible that SVN doesn t work properly. It could come from various reason, but the most common one is that you might have duplicated a folder that was under SVN, so both folder have the same SVN ID. To resolve this, delete all the.svn files in the duplicated folder (you can view hidden files using programs like Transmit) It is also possible that SVN through Textmate won t allow you to delete a file. If this happen, you can use Terminal instead, by navigating to the file and typing : svn delete filename.ext svn commit -m deleting file You ll also notice that Textmate won t allow you to do any SVN action unless you open at least one file.
Sync ing files with rsync Often, we deploy a site either to staging or production and clients will be uploading files that will not be under version control. In this case we need to pull the staging (or live) files back into our local working copy (so we can see all images when we re doing dev, for example). Example: your local working copy is at /Applications/MAMP/htdocs/givegreen/trunk; staging site is on our dev server at 76.77.86.133 Open terminal and type cd /Applications/MAMP/htdocs/givegreen/trunk/sites/all/files Enter Copy and paste the following and press Enter rsync --verbose --progress --stats --compress --rsh=/usr/bin/ssh --recursive givegreen@76.77.86.133:/home/givegreen/public_html/trunk/ sites/all/files/./ Supply the givegreen SFTP password (or get it from your sysadmin)