Skip to content

Commit bcbb99e

Browse files
author
zhenwei-li
committed
the branch upgrade with set upstream to
1 parent b41e8d2 commit bcbb99e

2 files changed

Lines changed: 74 additions & 19 deletions

File tree

src/com/dvsnier/git/branch/branch.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
# -*- coding:utf-8 -*-
22

3+
import logging
4+
35
from com.dvsnier.git.git import Git
46
from com.dvsnier.process.execute import execute
5-
import logging
67

78

89
class Branch(Git, object):
910
'the git branch command class'
1011

12+
_current_branch_name = None
13+
1114
def __init__(self):
1215
super(Branch, self).__init__()
1316

1417
def get_branch(self):
1518
'the get current branch name'
1619
current_branch_list_str = execute(["git branch"])
1720
if len(current_branch_list_str) > 0:
18-
branch_name_list = ' '.join(current_branch_list_str.split())
21+
branch_name_list = ' '.join(str(current_branch_list_str).split())
1922
if len(branch_name_list) > 0:
2023
branch_s = branch_name_list.split(' ')
2124
for index in range(len(branch_s)):
@@ -46,3 +49,25 @@ def branch_to_file_and_commit_list_with_more(self):
4649
return execute([
4750
'git branch --list --all --sort=committerdate --format=\'%(HEAD) %(color:yellow)%(refname:short)%(color:reset)|%(color:red)%(objectname:short)%(color:reset)|%(color:reset)(%(color:green)%(committerdate:relative)%(color:reset))\''
4851
])
52+
53+
def branch_set_upstream_to(self, branch_name):
54+
''' the track remote and local branch associations '''
55+
if not branch_name or not len(branch_name.strip()) > 0:
56+
logging.exception('the current branch name is invalid.' )
57+
raise KeyError('the current branch name is invalid.')
58+
return execute([
59+
'git branch -u origin/{0} {0}'.format(branch_name)
60+
])
61+
62+
def get_current_branch_name(self):
63+
''' the obtain current branch name '''
64+
return self._current_branch_name
65+
66+
def set_current_branch_name(self, branch_name):
67+
''' the set current branch name '''
68+
if not branch_name or not len(branch_name.strip()) > 0:
69+
logging.exception('the current branch name is invalid.' )
70+
raise KeyError('the current branch name is invalid.')
71+
self._current_branch_name = branch_name
72+
73+

tests/com/dvsnier/git/branch/test_branch.py

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,40 +11,69 @@ class Test_Branch(unittest.TestCase):
1111
''' the test branch '''
1212

1313
_git = None
14+
_branch = None
1415

1516
@classmethod
1617
def setUpClass(cls):
1718
print("...the set up...")
1819
print('')
1920
cls._git = Git()
2021
cls._git.config()
22+
cls._branch = Branch()
2123

2224
def setUp(self):
2325
super(Test_Branch, self).setUp()
2426

25-
def test_get_branch(self):
26-
branch = Branch()
27-
branch_name = branch.get_branch()
28-
logging.info(branch_name)
29-
self.assertIsNotNone(branch_name, 'test_get_branch is error.')
30-
print("the test get_branch is succeed.")
27+
def test_0_get_branch(self):
28+
branch_name = Test_Branch._branch.get_branch()
29+
logging.info('the current branch name is {}'.format(branch_name))
30+
self.assertIsNotNone(branch_name, 'test_0_get_branch is error.')
31+
# print("the test get_branch is succeed.")
3132

32-
def test_branch_to_file_and_commit_list(self):
33-
branch = Branch()
34-
branch_commit_list = branch.branch_to_file_and_commit_list()
33+
def test_1_branch_to_file_and_commit_list(self):
34+
branch_commit_list = Test_Branch._branch.branch_to_file_and_commit_list()
3535
logging.info(branch_commit_list)
3636
self.assertIsNotNone(branch_commit_list,
37-
'test_branch_to_file_and_commit_list is error.')
38-
print("the test branch_to_file_and_commit_list is succeed.")
37+
'test_0_branch_to_file_and_commit_list is error.')
38+
# print("the test branch_to_file_and_commit_list is succeed.")
3939

40-
def test_branch_to_file_and_commit_list_with_more(self):
41-
branch = Branch()
42-
branch_commit_list_more = branch.branch_to_file_and_commit_list_with_more()
40+
def test_2_branch_to_file_and_commit_list_with_more(self):
41+
branch_commit_list_more = Test_Branch._branch.branch_to_file_and_commit_list_with_more()
4342
logging.info(branch_commit_list_more)
4443
self.assertIsNotNone(
4544
branch_commit_list_more,
46-
'test_branch_to_file_and_commit_list_with_more is error.')
47-
print("the test branch_to_file_and_commit_list_with_more is succeed.")
45+
'test_0_branch_to_file_and_commit_list_with_more is error.')
46+
# print("the test branch_to_file_and_commit_list_with_more is succeed.")
47+
48+
def test_3_branch_set_upstream_to(self):
49+
branch_name = Test_Branch._branch.get_branch()
50+
# branch_name = None
51+
# branch_name = ''
52+
# branch_name = ' '
53+
# branch_name = '0'
54+
branch_set_upstream_to = Test_Branch._branch.branch_set_upstream_to(branch_name)
55+
if branch_set_upstream_to:
56+
logging.info(branch_set_upstream_to)
57+
self.assertIsNotNone(branch_set_upstream_to, 'test_3_branch_set_upstream_to is error.')
58+
59+
def test_5_get_current_branch_name(self):
60+
branch_name = Test_Branch._branch.get_branch()
61+
# branch_name = None
62+
# branch_name = ''
63+
# branch_name = ' '
64+
# branch_name = '0'
65+
current_branch_name = Test_Branch._branch.get_current_branch_name()
66+
if current_branch_name:
67+
logging.info('the current branch name is {0}.'.format(current_branch_name))
68+
self.assertIsNotNone(current_branch_name, 'test_5_get_current_branch_name is error.')
69+
70+
def test_4_set_current_branch_name(self):
71+
branch_name = Test_Branch._branch.get_branch()
72+
# branch_name = None
73+
# branch_name = ''
74+
# branch_name = ' '
75+
# branch_name = '0'
76+
Test_Branch._branch.set_current_branch_name(branch_name)
4877

4978
def tearDown(self):
5079
super(Test_Branch, self).tearDown()
@@ -56,4 +85,5 @@ def tearDownClass(cls):
5685

5786

5887
if __name__ == '__main__':
59-
unittest.main()
88+
suite = unittest.TestLoader().loadTestsFromTestCase(Test_Branch)
89+
unittest.TextTestRunner(verbosity=2).run(suite)

0 commit comments

Comments
 (0)