Practical SVN for PHP Developers

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

3 comments

Comments 1 - 3 of 3 previous next Post a comment

  • + weierophinney Matthew Weier O'Phinney 5 months ago
    @guest75d934f1: feature branches and/or developer branches are where unstable commits should go. However, as @guestc4db8b notes, this is also where using a distributed VCS alongside SVN can be incredibly useful -- and I cover that in my 'Git + SVN: A Match Made in ?' presentation (also up here on slideshare). I will often create a local branch in git for experimentation that I know may be unstable, and then merge only the completed feature to my main branch -- which gets pushed to SVN.

    BTW, merges in SVN are not difficult at all, and that’s one of the myths we were trying to dispel with this talk.
  • + guestc4db8b guestc4db8b 5 months ago
    @guest75d934f1:
    That’s where using something like bzr-svn or git-svn comes in handy. You can commit as much as you want within your feature branch, then commit the whole lot as a single commit to the svn repo. (could be done using just svn as well, but last I checked, merging is a pain with svn)
  • + guest75d934f1 guest75d934f1 5 months ago
    not sure how I feel about the suggestion to hold your commits until you have a discrete feature to check in. You lose the ability to roll back your code with SVN if you start to go down the wrong path. You also leave yourself vulnerable to hard drive crashes and data corruption.

    My suggestion is to commit as often as is feasible. And sure, each commit should only include changes related to a discrete feature, but don’t feel like you have to wait to make a commit. If you’re worried about your changes getting lost, you can enter a bug/ticket/issue number into the comment of each commit.
Post a comment
Embed Video
Edit your comment Cancel

13 Favorites & 1 Event

Practical SVN for PHP Developers - Presentation Transcript

  1. Practical SVN for PHP Developers Matthew Weier O'Phinney Project Lead Zend Framework Lorna Jane Mitchell Software Engineer Ibuildings
  2. What is Version Control?
    • What did you change from the previous revision?
    • What changes have others recorded since your last local update?
    Change Management
  3. Types of Version Control
    • Each checkout is a fully functional repository
    • Anybody may accept patches from anyone else
    • Anybody may send patches to anyone else
    • Ideal for projects with parallel feature developement
    Distributed
    • One canonical repository
    • All changes are submitted to the repository
    • All changes are retrieved from the repository
    • Ideal when:
      • a canonical version is required,
      • access to the repository should be controlled
    Non-Distributed
  4. What is Subversion?
    • Non-Distributed Version Control System
    • Successor to CVS
    • Stores and versions:
      • source code
      • documentation
      • anything, really
    Subversion is...
  5. Installing Subversion
  6. Choose a Protocol
    • give repo access to local users
    • give users and/or groups read/write on the repo to grant access
    • file:///home/lorna/svn-repos/main/trunk/
    Local filesystem
    • give repo access to users with ssh credentials
    • give users and/or groups read/write on the repo to grant access
    • svn+ssh svn://rivendell/home/lorna/svn-repos/main/trunk
    svn+ssh
    • make repo web-browseable and have apache handle credentials
    • basic or digest HTTP authentication (e.g., .htpasswd)
    • mod_authz_svn - an Apache module to give finer access control
    • http://svn.example.com/main/trunk
    WebDAV via HTTP/S
  7. Installing on Linux
    • Debian/Ubuntu: aptitude install subversion
    • Fedora/RedHat: yum install subversion
    Distribution packages
    • download from http://subversion.tigris.org
    • check dependencies (install apache, mod_dav, etc., first)
    • compile
    From source
  8. Setting up a repository
  9. What is a repository ?
    • The place all changesets are stored
    • A black box; you may only interact with it using svn tools
    A repository is...
  10. Creating the repository
  11. Use “svnadmin create”
  12. Create initial content
  13. Commit initial structure
  14. Alternate way: import into the repo
  15. About repository structure
    • Trunk – the main code version
    • Branches – copies of code which can be modified
    • Tags – copies of code which are never changed
    • All are “cheap” copies
    Repository Structure
  16. Using Subversion
  17. Checkout: svn checkout (co)
  18. .svn directory
  19. Status: svn status (st)
    • ' ' no modifications
    • 'A' Added
    • 'C' Conflicted
    • 'D' Deleted
    • 'M' Modified
    • '?' item is not under version control
    • '!' item is missing (removed by non-svn command) or incomplete
    • 'X' external resource managed by svn (svn:externals)
    Common status codes
  20. Add to repo: svn add
  21. Commit to repo: svn commit (ci)
  22. Changelog: svn log
  23. Dealing with missing files
    • “ !” in the status listing
    • Usually because something was moved or deleted without using the svn tools
    • Always use svn rm and svn mv
    • To fix: update (svn up) and try again
    Missing files - ! in the status
  24. What and When to commit
    • hourly, semi-daily, daily checkins
    • code only, tests only, docs only
    Bad practices
  25. Each commit should include all code, tests, and docs related to a discrete behavior or set of functionality. The Best Practice:
    • Easy to “cherry-pick” changesets between branches
    • Trivial to identify changesets to rollback
    Why?
  26. Examples
  27. Examples
    • PLAN your project; break it into distinct units of functionality
    • Use an issue tracker; issues should include expectations, actual results, and reproduce code (these become your tests!)
    • No unit of functionality should take more than 3-4 hours to implement; longer, and it likely should be broken into sub-tasks.
    How?
  28. Conflicts
    • svn blame / annotate / praise
    • shows who last edited which line
    svn blame
  29. Example
    • Two people change same line of same file
    • svn will ask user to choose
    Conflicts
    • index.php.r4 version from before either your changes or the commit that has conflicted
    • index.php.r6 current repo version
    • index.php.mine working copy before server showed conflict
    • index.php whole file with some markup to show just the conflict(s) in place
    Example: conflict adds files
    • edit index.php until correct
    • may copy one of the "extra" files into place of this one
    • let SVN know you're all sorted
    svn resolved
    • Good team communication
    • Update often
    • Commit first!
    Avoiding Conflicts
  30. Branching and Tagging
  31. Patterns
  32. Feature Branches Graph by Eli White
    • Pros:
      • Features may be developed in isolation
      • Trunk always contains “finished” features
    • Cons:
      • Features may be developed in isolation
      • Hard to cleanly merge at times if multiple features touch the same areas of code
      • Harder to rollback (trunk doesn't always have discrete featuresets)
    Feature Branches
  33. Long-lived Branches Graph by Eli White
    • Pros:
      • Production is always stable
      • Rollback by pointing to production tags
    • Cons:
      • Long-lived feature development often lags trunk features
        • Difficult to determine what to merge
      • Difficult to do parallel feature development
      • More difficult to rollback specific features
    Long-lived Branches
  34. Release Branches Graph by Eli White
    • Pros:
      • Easy to isolate features by version (parallel development)
      • Possibility of keeping multiple active versions of a project
    • Cons:
      • Harder to merge patches between branches (files may differ widely)
    Release Branches
  35. Merging
    • Moving changes between branches (including trunk)
    • Merge single changes
    • Merging as part of a branch lifecycle
    Merging
    • Check out the target branch
    • From the target directory, run svn diff until the output illustrates the operation you wanted
    • Replace "diff" with "merge"
    • Review changes to working copy and/or test
    How to merge
    • Use “svn log” to identify discrete changesets you want to merge.
    • Use the “cherry-pick” flag of “svn merge” (-c) to merge that single changeset.
    • Works well if you're following the best practices outlined earlier!
    How to merge: the easy way
  36. Administering Repositories
  37. Backing up your Repository
    • Copying files directly can lead to corruption in the copy
    • Use “svnadmin hotcopy” or “svnadmin dump”
    svnadmin hotcopy
  38. Authentication
    • Dependent on how the OS does authentication:
      • Local accounts
      • “ Virtual” accounts
      • etc.
    • SSH public keys are a common solution
    svnserve or svn+ssh
    • Typically HTTP basic or digest authentication
      • Use htpasswd to generate user/pass pairs
      • Can still allow anonymous access
      • Configure your server to require it
    WebDAV (http/https)
  39. Example HTTP Auth configuration
  40. Authorization
    • Typically conf/authz in your repo, or “access.conf” accessible to web server
    • INI-style file with [sections]
      • [groups] section to define users and groups
      • [/path] specifies path on repo
      • [repo:/path] specifies path on specific repo (if more than one repo defined)
    ACL File
    • ACLs:
      • [user|group] = [r|rw]
      • “ *” as group/user means any user
      • Groups are prefixed with “@”
    ACL File
  41. ACL File: example
    • Svnserve, file access, or svn+ssh: conf/authz in your repository
    • WebDAV: anywhere.
      • Tell your vhost where to find it
      • Must setup authorization prior to authentication
    ACL File: placement
  42. ACL File in WebDAV: vhost setup
  43. Hooks
    • Allow extending SVN's capabilities via userland scripts
    • “ Hook” into specific processes:
      • pre/post-commit, start-commit
      • pre/post-revprop-change
      • pre/post-lock
      • pre/post-unlock
    What are hooks?
  44. Running a linter REPOS = "$1" TXN = "$2" PHP = "/path/to/php" SVNLOOK = "/path/to/svnlook" AWK = "/path/to/awk" GREP = "/path/to/egrep" SED = "/path/to/sed" CHANGED = `$SVNLOOK changed -t "$TXN" "$REPOS" | $AWK '{print $2}' | $GREP \.php$` for FILE in $CHANGED do MESSAGE = `$SVNLOOK cat -t "$TXN" "$REPOS" "$FILE" | $PHP -l` if [ $? - ne 0 ] then echo 1 >& 2 echo "***********************************" 1 >& 2 echo "PHP error in: $FILE:" 1 >& 2 echo `echo "$MESSAGE" | $SED "s| -| $FILE|g"` 1 >& 2 echo "***********************************" 1 >& 2 exit 1 fi done
  45. *********************************** PHP error in: test.php echo $foobar *********************************** Sample linter output
    • Run post-commit – prevents blocking issues
    • Send email notification when tests fail
    • Two approaches:
      • Run entire suite
      • Grep committed files for classes, and test just those classes
    • Typically best to do this from a Continuous Integration server, and not via subversion hooks
    Running unit tests
    • Email, Nabaztag, TwitterSVN
    • Email notifications post-commit
    • SVN::Notify:
      • http://search.cpan.org/~dwheeler/SVN-Notify-2.79/lib/SVN/Notify.pm
      • Can selectively determine which people/lists get notifications based on commit path
      • Plain text and/or HTML emails
    Sending notifications
  46. Example notification:
    • svn2feed.py: http://svn.collab.net/repos/svn/trunk/tools/hook-scripts/svn2feed.py
    • Add svn2feed.py to your hooks/ directory
    Publishing RSS Feeds
  47. Adding svn2feed.py to your post-commit hook: path / to / python path / to / hooks / svn2feed.py -- svn - path / usr / bin / -- max - items = 100 -- format = atom -- revision "$REV" – item - url "http://localhost/svn/" -- feed - url = "http://localhost/rss/svn.rss" -- feed - file "path/to/rss/svn.rss" "$REPOS" &
    • Trigger docbook build process, or PhpDocumentor
    • Run post-commit, so as not to block
    • Typically best done from a CI server, and not subversion hooks
    Generating documentation
    • PHP_CodeSniffer includes a script, bin/scripts/phpcs-svn-pre-commit
      • Edit the script to provide the path to svnlook:
      • define('PHP_CODESNIFFER_SVNLOOK', '/usr/bin/svnlook');
    • Edit the pre-commit hook script to invoke the script:
      • /path/to/bin/scripts/phpcs-svn-pre-commit "$REPOS" -t "$TXN" >&2 || exit 1
    Running PHP_CodeSniffer
        • Transmitting file data .svn: Commit failed (details follow):
        • svn: 'pre-commit' hook failed with error output:
        • FILE: temp.php
        • ---------------------------------------------------------------
        • FOUND 1 ERROR(S) AND 0 WARNING(S) AFFECTING 1 LINE(S)
        • ---------------------------------------------------------------
        • 2 | ERROR | Missing file doc comment
        • --------------------------------------------------------------
    PHP_CodeSniffer hook output
        • http://pear.php.net/manual/en/package.php.php-codesniffer.svn-pre-commit.php
    For more info on PHP_CodeSniffer:
  48. Deploying from SVN
    • What else needs to happen at deploy time?
    • Preserve uploads
    • Set permissions
    • Database patches
    Deployment Considerations
    • Checkout retains link to repo
    • Meta data present on server
    • Inconsistent state during update
    • Export is clean copy, no dependencies
    • Other ways to keep track of version
    Checkout or Export
    • Tag and export from svn – include some tag info
    • Use symlinks to enable seamless switching
    • Wrap in a script
    • Have a rollback plan/script
    • Consider database strategies
    Best Practices
  49. Thank you.

+ Matthew Weier O'PhinneyMatthew Weier O'Phinney, 5 months ago

custom

2499 views, 13 favs, 1 embeds more stats

Tutorial showing how to create and administer svn r more

More info about this document

© All Rights Reserved

Go to text version

  • Total Views 2499
    • 2498 on SlideShare
    • 1 from embeds
  • Comments 3
  • Favorites 13
  • Downloads 122
Most viewed embeds
  • 1 views on http://www.plaxo.com

more

All embeds
  • 1 views on http://www.plaxo.com

less

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

Cancel
File a copyright complaint
Having problems? Go to our helpdesk?

Categories

Groups / Events