Skip to content

Commit

Permalink
Merge pull request tomster#8 from collective/master
Browse files Browse the repository at this point in the history
fix svn switch handling.
  • Loading branch information
tomster committed Mar 11, 2012
2 parents d83bc9e + 8c1cd7f commit 894eb41
Show file tree
Hide file tree
Showing 48 changed files with 109 additions and 930 deletions.
4 changes: 4 additions & 0 deletions HISTORY.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
0.9 - Unreleased
----------------

* Fix the handling when switching to a svn branch that git already has
a local branch for.
[rossp]

0.8 - 2010-03-10
----------------

Expand Down
File renamed without changes.
9 changes: 6 additions & 3 deletions gitsvnhelpers/gitify.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ def __call__(self):
# perform all index updates in the cache to avoid conflicts
os.chdir(config.GIT_CACHE + package_name)

dummy, existing_branches = popen('git b', False, False)
existing_branches = [b.strip() for b in existing_branches]
dummy, existing_branches = popen('git branch', False, False)
existing_branches = [b.strip('* ') for b in existing_branches]
if local_branch in existing_branches:
popen('git checkout -f %s' % local_branch, False, False)
else:
Expand All @@ -83,10 +83,13 @@ def __call__(self):
# if the working copy is on another branch, switch:
if local_branch != git_branch():
if local_branch in existing_branches:
popen('git checkout %s' % local_branch)
popen('git checkout -f %s' % local_branch)
else:
popen('git checkout -b %s' % local_branch)

assert git_branch() == local_branch, (
"Changing branches failed, is on %r but should be on %r"
% (git_branch(), local_branch))
print "Git branch '%s' is now following svn branch '%s':" % (
local_branch, remote_branch)
popen('svn status')
Expand Down
64 changes: 51 additions & 13 deletions gitsvnhelpers/testing.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import shutil
import sys
import os
import StringIO
from os.path import join, dirname
from os.path import join
import subprocess

from jarn.mkrelease.testing import SubversionSetup, JailSetup, GitSetup
from jarn.mkrelease.process import Process
from gitsvnhelpers import config


class BaseTestCase(SubversionSetup):

name = 'svn'
source = 'testrepo.svn'
packagename = 'testpackage'

def setUp(self):
Expand All @@ -20,26 +19,66 @@ def setUp(self):
config.GIT_CACHE = join(self.tempdir, '.gitcache/')
# copy the test repo to temp, we perform all checkouts from there:
try:
original_repo = join(dirname(__file__), 'tests', self.source)
# the target folder needs to be the packagename, so that the
# file:/// urls used throughout testing match the pacakge name
# normally, the filesystem name doesn't matter, when it's being
# served via http
os.mkdir("%s/repos/" % self.tempdir)
os.mkdir(join(self.tempdir, "repos"))
self.repo = join(self.tempdir, 'repos', self.packagename)
shutil.copytree(original_repo, self.repo)
subprocess.check_call(["svnadmin", "create", self.repo])
subprocess.check_call(
["svn", "mkdir", "-m", "repo layout"]
+["file://%s/%s" % (self.repo, folder)
for folder in ('trunk', 'branches', 'tags')],
stdout=subprocess.PIPE)

self.checkout(target="setUp")
open('README.txt', 'w').write("Package documentation\n")
subprocess.check_call(["svn", "add", "README.txt"],
stdout=subprocess.PIPE)
subprocess.check_call(["svn", "commit", "-m", "Begin docs"],
stdout=subprocess.PIPE)

open('foo.py', 'w').write('"""fooberizing"""\n')
subprocess.check_call(["svn", "add", "foo.py"],
stdout=subprocess.PIPE)
subprocess.check_call(
["svn", "commit", "-m", "First attempt at fooberizing"],
stdout=subprocess.PIPE)
subprocess.check_call(["svn", "copy", "-m", "Release 0.1",
"file://%s/trunk" % self.repo,
"file://%s/tags/0.1" % self.repo],
stdout=subprocess.PIPE)

subprocess.check_call(
["svn", "copy", "-m", "Begin work on feature bar",
"file://%s/trunk" % self.repo,
"file://%s/branches/feature-bar" % self.repo],
stdout=subprocess.PIPE)
subprocess.check_call(
["svn", "switch",
"file://%s/branches/feature-bar" % self.repo],
stdout=subprocess.PIPE)
open('README.txt', 'a').write("Now supports bar\n")
open('foo.py', 'a').write('import bar\n')
open('bar.py', 'w').write('"""barberizing"""\n')
subprocess.check_call(["svn", "add", "bar.py"],
stdout=subprocess.PIPE)
subprocess.check_call(
["svn", "commit", "-m", "Implement bar feature"],
stdout=subprocess.PIPE)
except:
self.cleanUp()
raise

def checkout(self, path='trunk', target=None):
process = Process(quiet=True)
if target is None:
self.checkoutdir = join(self.tempdir, self.packagename)
else:
self.checkoutdir = join(self.tempdir, target, self.packagename)
process.system('svn checkout file://%s/%s %s' % (self.repo,
path, self.checkoutdir))
args = ['svn', 'checkout', 'file://%s/%s' % (self.repo, path),
'%s' % self.checkoutdir]
subprocess.check_call(args, stdout=subprocess.PIPE)
os.chdir(self.checkoutdir)


Expand Down Expand Up @@ -81,15 +120,14 @@ def cleanUp(self):
class GitTestCase(GitSetup):
""" a test class that operates on a git repository
"""
source = 'testrepo.git'

def setUp(self):
JailSetup.setUp(self)
try:
package = join(dirname(__file__), 'tests', self.source)
self.packagedir = join(self.tempdir, 'testpackage')
shutil.copytree(package, self.packagedir)
os.mkdir(self.packagedir)
os.chdir(self.packagedir)
subprocess.check_call(['git', 'init'], stdout=subprocess.PIPE)
except:
self.cleanUp()
raise
34 changes: 18 additions & 16 deletions gitsvnhelpers/tests/test_doctests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,32 @@

class TestDoctests(BaseTestCase):

def runTest(self):
raise NotImplemented

def shell(self, cmd):
"""executes the shell command and prints its output"""
code, output = popen(cmd, False, False)
for line in output:
print line

def _test_doctest(self):
doctest.testfile("%s.txt" % self._testMethodName,
globs=dict(self=self, do=self.shell),
report=True,
optionflags=optionflags)

def test_gitify(self):
self._test_doctest()

def test_gitify_up(self):
self._test_doctest()
def setUp(test):
self = TestDoctests()
test.globs.update(self=self, do=self.shell)
self.setUp()

def test_gitify_fetch(self):
self._test_doctest()

def test_symlink_migration(self):
self._test_doctest()
def tearDown(test):
test.globs['self'].tearDown()

def test_svn_switch(self):
self._test_doctest()

def test_suite():
return doctest.DocFileSuite(
'test_gitify.txt',
'test_gitify_up.txt',
'test_gitify_fetch.txt',
'test_symlink_migration.txt',
'test_svn_switch.txt',
setUp=setUp, tearDown=tearDown,
optionflags=optionflags)
6 changes: 3 additions & 3 deletions gitsvnhelpers/tests/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_dirty_index(self):
popen('git add bar.txt')
self.failUnless(index_is_dirty())
# Once we've actually committed the change, we're clean again:
popen('git ci -m "added bar"', False, False)
popen('git commit -m "added bar"', False, False)
self.failIf(index_is_dirty())

def test_local_changes(self):
Expand All @@ -42,11 +42,11 @@ def test_local_changes(self):
# Once we've actually committed the change, we're clean again:
popen('git add bar.txt')
self.failUnless(local_changes())
popen('git ci -m "added bar"', False, False)
popen('git commit -m "added bar"', False, False)
self.failIf(local_changes())
# Modifying an existing file will have the same effect:
popen('echo "modified" >> bar.txt')
self.failUnless(local_changes())
popen('git add bar.txt')
popen('git ci -m "modified bar"', False, False)
popen('git commit -m "modified bar"', False, False)
self.failIf(local_changes())
41 changes: 21 additions & 20 deletions gitsvnhelpers/tests/test_gitify.txt
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ As expected, git will report the modification:

>>> do('git status')
# On branch local/trunk
# Changed but not updated:
# Change...:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
Expand Down Expand Up @@ -142,25 +142,26 @@ Let's make another git commit before pushing our changes to svn:
Now we can push our commits to svn using the ``gitify push`` command:

>>> gitify(args=['push'])
Committing to file:///...testpackage/trunk ...
M README.txt
Committed r7
M README.txt
r7 = ... (refs/remotes/trunk)
No changes between current HEAD and refs/remotes/trunk
Resetting to the latest refs/remotes/trunk
Unstaged changes after reset:
M foo.py
M foo.py
Committed r8
M foo.py
r8 = ... (refs/remotes/trunk)
No changes between current HEAD and refs/remotes/trunk
Resetting to the latest refs/remotes/trunk
G foo.py
G README.txt
Updated to revision 8.
Pushed local changes to svn.
Committing to file:///...testpackage/trunk ...
M README.txt
Committed r7
M README.txt
r7 = ... (refs/remotes/trunk)
No changes between current HEAD and refs/remotes/trunk
Resetting to the latest refs/remotes/trunk
Unstaged changes after reset:
M foo.py
M foo.py
Committed r8
M foo.py
r8 = ... (refs/remotes/trunk)
No changes between current HEAD and refs/remotes/trunk
Resetting to the latest refs/remotes/trunk
Updating '.':
G foo.py
G README.txt
Updated to revision 8.
Pushed local changes to svn.

Now git and svn are 'in sync' IOW they both agree that there are no uncommitted
local changes:
Expand Down
8 changes: 4 additions & 4 deletions gitsvnhelpers/tests/test_gitify_up.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Starting easy, we simply add a new file to the svn repo:
>>> do('svn commit -m "Added from svn"')
Adding svn.txt
Transmitting file data .
Committed revision 7.
Committed revision ...

Then we re-visit our gitified checkout:

Expand Down Expand Up @@ -166,6 +166,7 @@ Subversion

>>> os.chdir(svnlocal)
>>> do("svn up")
Updating '.':
G svn.txt
Updated to revision 8.

Expand Down Expand Up @@ -195,7 +196,6 @@ The rebase command works as expected:
M svn.txt
r8 = ... (refs/remotes/trunk)
First, rewinding head to replay your work on top of it...
Nothing to do.

Now the slate is clean:

Expand All @@ -212,7 +212,7 @@ let's first check the status:
>>> os.chdir(gitified)
>>> do('git status')
# On branch local/trunk
# Changed but not updated:
# Change...:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
Expand All @@ -231,7 +231,7 @@ except that git now calls them 'unmerged' instead of 'modified':
# On branch local/trunk
# Unmerged paths:
# (use "git reset HEAD <file>..." to unstage)
# (use "git add <file>..." to mark resolution)
# (use "git add... to mark resolution)
#
# both modified: svn.txt
#
Expand Down
1 change: 0 additions & 1 deletion gitsvnhelpers/tests/testrepo.git
Submodule testrepo.git deleted from 152226
5 changes: 0 additions & 5 deletions gitsvnhelpers/tests/testrepo.svn/README.txt

This file was deleted.

32 changes: 0 additions & 32 deletions gitsvnhelpers/tests/testrepo.svn/conf/authz

This file was deleted.

8 changes: 0 additions & 8 deletions gitsvnhelpers/tests/testrepo.svn/conf/passwd

This file was deleted.

Loading

0 comments on commit 894eb41

Please sign in to comment.