Skip to content

Commit 9ff8cbe

Browse files
committed
Added simple test for starting and saving a branch
1 parent d0fd822 commit 9ff8cbe

File tree

4 files changed

+69
-10
lines changed

4 files changed

+69
-10
lines changed

app.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from flask import Flask, redirect, request, Response, render_template, session
99
from jekyll import load_jekyll_doc, dump_jekyll_doc
1010

11+
import bizarro
12+
1113
_default_branch = 'master'
1214
_repo_path = 'sample-site'
1315

@@ -109,9 +111,7 @@ def start_branch():
109111
r = get_repo()
110112
branch_desc = request.form.get('branch')
111113
branch_name = name_branch(branch_desc)
112-
branch = r.create_head(branch_name, commit=r.branches[_default_branch])
113-
114-
r.remotes.origin.push(branch.name)
114+
branch = bizarro.repo.start_branch(r, _default_branch, branch_name)
115115

116116
safe_branch = branch_name2path(branch.name)
117117

@@ -219,12 +219,13 @@ def branch_save(branch, path):
219219

220220
r = get_repo()
221221
b = r.branches[branch]
222-
b.checkout()
223-
c = r.commit()
222+
c = b.commit
224223

225224
if c.hexsha != request.form.get('hexsha'):
226225
raise Exception('Out of date SHA: %s' % request.form.get('hexsha'))
227226

227+
b.checkout()
228+
228229
with open(join(r.working_dir, path), 'w') as file:
229230
front = dict(title=request.form.get('title'))
230231
body = request.form.get('body').replace('\r\n', '\n')

bizarro/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import repo

bizarro/repo.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
def start_branch(clone, default_branch_name, new_branch_name):
2+
''' Start a new repository branch, push it to origin and return it.
3+
'''
4+
clone.remotes.origin.fetch()
5+
6+
if 'origin/' + new_branch_name in clone.refs:
7+
start_point = clone.refs['origin/' + new_branch_name]
8+
else:
9+
start_point = clone.branches[default_branch_name]
10+
11+
branch = clone.create_head(new_branch_name, commit=start_point)
12+
clone.remotes.origin.push(new_branch_name)
13+
14+
return branch
15+
16+
def save_working_file(clone, path, message, base_sha):
17+
''' Save a file in the working dir and push it to origin.
18+
'''
19+
if clone.active_branch.commit.hexsha != base_sha:
20+
raise Exception('Out of date SHA: %s' % base_sha)
21+
22+
clone.index.add([path])
23+
clone.index.commit(message)
24+
clone.remotes.origin.push(clone.active_branch.name)

test.py

+38-5
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
from subprocess import Popen, PIPE
33
from unittest import TestCase, main
44
from shutil import rmtree
5+
from os.path import join
6+
from uuid import uuid4
57

68
from git import Repo
9+
import bizarro
710

811
#
912
# Tarball of a single-commit Git repo with files index.md and sub/index.md
@@ -14,23 +17,53 @@
1417
class TestRepo (TestCase):
1518

1619
def setUp(self):
17-
dirname = mkdtemp()
20+
dirname = mkdtemp(prefix='bizarro-')
1821

1922
tar = Popen(('tar', '-C', dirname, '-xzf', '-'), stdin=PIPE)
2023
tar.stdin.write(_tarball)
2124
tar.stdin.close()
2225
tar.wait()
2326

24-
self.repo = Repo(dirname)
27+
self.origin = Repo(dirname)
28+
self.clone1 = self.origin.clone(mkdtemp(prefix='bizarro-'))
29+
self.clone2 = self.origin.clone(mkdtemp(prefix='bizarro-'))
2530

2631
def test_repo_features(self):
27-
self.assertTrue(self.repo.bare)
32+
self.assertTrue(self.origin.bare)
2833

29-
branch_names = [b.name for b in self.repo.branches]
34+
branch_names = [b.name for b in self.origin.branches]
3035
self.assertEqual(set(branch_names), set(['master', 'title', 'body']))
3136

37+
def test_start_branch(self):
38+
branch1 = bizarro.repo.start_branch(self.clone1, 'master', 'hello')
39+
40+
self.assertTrue('hello' in self.clone1.branches)
41+
self.assertTrue('hello' in self.origin.branches)
42+
43+
#
44+
# Make a change to the branch and push it.
45+
#
46+
branch1.checkout()
47+
message = str(uuid4())
48+
49+
with open(join(self.clone1.working_dir, 'index.md'), 'a') as file:
50+
file.write('\n\n...')
51+
52+
bizarro.repo.save_working_file(self.clone1, 'index.md', message, branch1.commit.hexsha)
53+
54+
#
55+
# See if the branch made it to clone 2
56+
#
57+
branch2 = bizarro.repo.start_branch(self.clone2, 'master', 'hello')
58+
59+
self.assertTrue('hello' in self.clone2.branches)
60+
self.assertEquals(branch2.commit.hexsha, branch1.commit.hexsha)
61+
self.assertEquals(branch2.commit.message, message)
62+
3263
def tearDown(self):
33-
rmtree(self.repo.git_dir)
64+
rmtree(self.origin.git_dir)
65+
rmtree(self.clone1.working_dir)
66+
rmtree(self.clone2.working_dir)
3467

3568
if __name__ == '__main__':
3669
main()

0 commit comments

Comments
 (0)