2013/14 Institut für Computergraphik, TU Braunschweig Pablo Bauszat [PRAKTISCHE ASPEKTE DER INFORMATIK WS 13/14] All elemental steps that will get you started for your new life as a computer science programmer.
Week G Versioning with svn What is Versioning? Different approaches Subversion (svn) workflow Basic svn commands http://subversion.tigris.org/ Group assignments: Check in your proposals Week G Versioning with svn Imagine you are working on a very important university project, and your deadline is tomorrow. Your program is running quite smoothly, so you decide to implement a new, very cool feature that would make your program even more awesome. Around midnight, you realize it s not going to work as you expect. At 3 am you realize that your program does not run properly any more. At 5 am you realize your program does not run at all. At 6 am you start searching for the USB stick with the backup. At 7 am you find it, but realize the backup is more than a week old. At 8 am you have to leave for university. Another similar situation: You work with a bunch of friends on a program. Each of you is modifying your initial prototype by implementing a cool new feature. At some point you want to merge your version, an USB stick starts making its way around the busy programmers. People start realizing that there are inconsistencies among the versions. Some changes are merged, some are not. When you continue the project next day, nobody can remember which version is the most complete one and you start again from scratch. Some of you might have encountered these situations. The good news is that there exists a technical solution to these problems: Versioning tools. Versioning tools let you sync your data with other machines (sometimes peer-to-peer, sometimes in a client-server relationship). You can access the complete file version history, so you can, e.g., access the state of the project from last night. You can 2 PADI Praktische Aspekte der Informatik
merge your modifications with modifications made by other people, and you can get (some) assistance, when there are conflicting versions of files. As always, there are several possibilities. The most naïve would be to simply put up an FTP server and to upload all changes to the server. There are also more mature solutions; today, we will get to know a program called subversion (or svn ), that works in a strict client-server model but has several advantages over the simple FTP solution, as we will see. 3 PADI Praktische Aspekte der Informatik
svn basics A simple server (e.g. ftp) SVN basics These are examples from the freely available book Version Control with Subversion: http://svnbook.red-bean.com/ I recommend to read Chapter 1 and Chapter 2. You can use svn via the console or using a GUI client. For Windows, a good GUI client is Tortoise svn (http://tortoisesvn.net/). Eclipse also has a svn plugin. When working with Linux, rapidsvn is an option. As mentioned before, the simplest approach would be to set up an FTP server. Every time a change is made to a file, the user has to upload the file to the server. This approach has several disadvantagess: If the users do not check if other users made changes, they may accidently overwrite previously submitted data. In addition, no history of the files is kept. 4 PADI Praktische Aspekte der Informatik
svn basics lock-modify-unlock One possible solution would be to use locks: If a user intends to work on a file, he locks it and unlocks it after the changes are submitted to the server. This prevents accidental overwrites. On the other hand, other users may have to wait for a very long time to get a lock on a file. It may also happen that one user has a lock on MyClass.h while another user has a lock on MyClass.cpp. It is quite probable that neither of them can continue to work if the other one unlocks one of the files. 5 PADI Praktische Aspekte der Informatik
svn basics copy-modify-merge (1/2) With svn, a copy-modify-merge scheme is used. Everyone is working on a local copy of the repository. Users may at any time commit changes to the repository. The repository keeps track of the files and their versions. If a user commits a file that has been changed previously by another user, the two versions are checked for conflicts. If the changes happened in different parts of the document, the files can be automatically changed. If the conflict cannot be resolved automatically, it has to be resolved by the user. The user receives the concurring versions and can resolve the conflict locally, before the file is submitted to the server. 6 PADI Praktische Aspekte der Informatik
svn basics copy-modify-merge (2/2) 7 PADI Praktische Aspekte der Informatik
svn basics benefits Global revision numbers No serialization (parallel editing) Offline editing Merging works most of the time But: Requires disciplined workflow and maintenance! One nice benefit of svn is the unique number each revision has. If your project has reached a certain state (e.g. a specific milestone), these revisions can be tagged. You can always roll back the code to a given revision. This comes in very handy when you try to track down a bug that somehow slipped into your code. You can also display the differences between the files of your current working copy and the ones on the svn. One word of warning: Your problems do not magically disappear when you use svn. You have to keep a disciplined workflow. That means that you should try to keep your local copy in sync with the server, that you resolve conflicts properly and do not commit files that other s will not need. 8 PADI Praktische Aspekte der Informatik
svn workflow 1. Update your working copy and make sure everything works properly svn update or svn checkout (initial checkout) 2. Make changes. svn add, svn delete, svn copy, svn move 3. Examine your changes. svn status, svn diff 4. Possibly undo some changes. svn revert Initially, 5. Resolve you check conflicts out a (merge project others' with the changes). svn checkout command: svn checkout svn://yourname@elara.cg.cs.tu-bs.de/public/padi/ svn update, svn resolve The current project is downloaded on your machine. You may now work on your project. E.g., you create 6. a Commit file called your main.cpp. changes. At the end of your work session you should type svn svn commit add main.cpp svn commit main.cpp A text dialog may now show up where you can type in a commit message, which is quite useful. If you are working with several people on the same files, you should type svn status svn update before resuming work. By doing this, you ensure that your working copy is in sync with the repository 9 PADI Praktische Aspekte der Informatik
svn commands - update $ svn update Basic Usage 19 U foo.c U bar.c Updated to revision 2. For bug hunting it is very useful to check out old revisions with the -revision [revision-number] option! The svn update command tells you which alterations have been (compared to your local copy). Files may have been added, deleted, updated or are in conflict. It also tells you the current revision number. 10 PADI Praktische Aspekte der Informatik
svn commands - modifying svn add foo svn delete foo svn copy foo bar svn move foo bar svn mkdir blort 11 PADI Praktische Aspekte der Informatik
svn commands - status $ svn status? scratch.c # file is not under version control A stuff/loot/bloo.h # file is scheduled for addition C stuff/loot/lump.c # file has conflicts from an update D stuff/fish.c # file is scheduled for deletion M bar.c # the content has local modifications With svn status you can check the status of all local files compared to the repository on the server. 12 PADI Praktische Aspekte der Informatik
svn commands log and diff $ svn log ----------------------------------------------------------- ----- r3 sally 2008-05-15 23:09:28-0500 (15 May 2008) 1 line Added include lines and corrected # of cheese slices. $ svn diff Index: bar.c =========================================================== ==== --- bar.c (revision 3) +++ bar.c (working copy) @@ -1,7 +1,12 @@ +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> + +#include <stdio.h> int main(void) { - printf("sixty-four slices of American Cheese...\n"); + printf("sixty-five slices of American Cheese...\n"); return 0; 13 PADI Praktische Aspekte der Informatik
svn commands resolving conflicts A conflicting file looks like this $ cat sandwich.txt Top piece of bread Mayonnaise Lettuce Tomato Provolone <<<<<<<.mine Salami Mortadella Prosciutto ======= Sauerkraut Grilled Chicken >>>>>>>.r2 Creole Mustard Bottom piece of bread Every time there is a conflict, the file is modified in a way that you see the conflicting text passages. The file sandwich.txt has been modified in revision 2: Sauerkraut and Grilled Cheese have been added. The local copy has local modifications at the same location (Salami, Mortadella and Prosciutto). You can remove the conflict by deciding how to merge the changes and deleting the extra symbols (<<<<<<<, ========, >>>>>>>>>) 14 PADI Praktische Aspekte der Informatik
svn commands resolving conflicts All relevant file versions are saved on your disk: $ ls -1 sandwich.txt sandwich.txt.mine sandwich.txt.r1 sandwich.txt.r2 In the case of a conflict, all relevant versions of the file are stored on your local copy. The conflicting one (sandwich.txt), your locally modified one (sandwich.txt.mine), the last revision that did not conflict (sandwich.txt.r1) and the current version on the server (sandwich.txt.r2). 15 PADI Praktische Aspekte der Informatik
svn commands resolving conflicts You have the following options: $ svn resolve --accept theirs-full sandwich.txt Choose this option, to replace your file with the one from the current revision completely $ svn resolve --accept working sandwich.txt Choose this if you corrected the conflict by hand $ svn resolve --accept mine-full sandwich.txt Choose this if your changes are correct $ svn resolve --accept base sandwich.txt Choose this to revert to the last modified revision you checked out (in his example revision 1). It is also a good idea to clean up the temporary files *.r* and *.mine afterwards (if not automatically done by your svn application) After you decided what to do, you can use the svn resolve command. Alternatively, you can erase all files except the original sandwich.txt and commit this one. 16 PADI Praktische Aspekte der Informatik
svn commands commit $ svn commit -m "Corrected number of cheese slices." Sending sandwich.txt Transmitting file data. Committed revision 3. 17 PADI Praktische Aspekte der Informatik
PADI announcement If you signed up for PADI, you do have a svn account and may check out your project: svn checkout svn://yourname@elara.cg.cs.tu-bs.de/public/padi/yourname/ If you make changes to your proposal, edited them directly in the svn! If you don t get your svn username and password yet, send me a mail! 18 PADI Praktische Aspekte der Informatik
Assignment G: SVN This week s assignment will be mandatory! So please do it. Task 1: Commit your project proposal [50 points] Check out your own project s directory: svn checkout svn://elara.cg.cs.tu-bs.de/public/padi/yourname/ Copy your proposal (named proposal.txt) into this directory. Make sure your proposal contains your name and the Matrikelnummer. Type svn add proposal.txt svn commit m proposal added Task 2: Modify the projects.txt list [50 points] Check out the directory svn checkout svn://elara.cg.cs.tu-bs.de/public/padi/common/ There should be a file called projects.txt. It contains a list with all projects and current states. Please add your project information to the file, so I know who is still working on his/her project. Please update the state of your project in this file regularly. If you cancel your project for some reason, please write cancelled as current state. Commit the file with your changes. 19 PADI Praktische Aspekte der Informatik