def do_merge(self, args): branches = [] command = [G.GIT, '-C', self.repodir, 'show-ref', '--heads'] process = cw.run_process(command) output = process.stdout output_lines = output.split('\n') if '' in output_lines: output_lines.pop() for line in output_lines: branches.append(re.sub(r'[0-9a-f]{40} refs/heads/', '', line)) args = args.split() if len(args) == 1 and args[0] in branches: command = [ G.GIT, '-C', self.repodir, 'merge', '--no-ff', '--log', '-m', 'merge branch ' + args[0], args[0] ] process = cw.run_process(command) self.output = process.stdout self.err = process.stderr elif len(args) == 0: self.output = "No branch names provided" elif len(args) > 1: self.output = "Git Crystals does not support merging mulitple branches" self.display_output() return self.is_player_alive()
def test_branch(self): git = GitCmd() git.do_branch('newbranch') command = [G.GIT, '-C', G.repodir, 'branch'] process = cw.run_process(command) output = process.stdout command = [G.GIT, '-C', G.repodir, 'branch', '-D', 'newbranch'] process = cw.run_process(command) expected = "* data\n newbranch\n" self.assertEqual(output, expected)
def test_stage(self): git = GitCmd() G.change_location_file("Git Crystal") git.do_stage('location.json') command = [G.GIT, '-C', G.repodir, 'status', '--short'] process1 = cw.run_process(command) G.change_location_file("Mountain Gate") command = [G.GIT, '-C', G.repodir, 'reset', 'HEAD', 'location.json'] process2 = cw.run_process(command) expected = 'M location.json\n' # location.json is staged self.assertEqual(process1.stdout, expected)
def test_git_diffbranch(self): git = GitCmd() git.do_branch('trial') G.change_location_file("Git Crystal") git.do_stage('location.json') git.do_commit('Player in Git Crystal') git.do_diffbranch('') expected = "only " + "data,trial are legal branch names\n" expected += "usage: diffbranch branch1 branch2\n" self.assertEqual(git.output, expected) git.do_diffbranch('trial data') expected = """diff --git a/location.json b/location.json index 86b52b7..64e45dc 100644 --- a/location.json +++ b/location.json @@ -1,4 +1,4 @@ { - "location":"Mountain Gate" + "location":"Git Crystal" } """ self.assertEqual(git.output, expected) command = [G.GIT, '-C', G.repodir, 'branch', '-D', 'trial'] process = cw.run_process(command)
def test_repo_with_extra_commits(self): with open(G.repodir + '/location.json', 'a') as f: f.write('# Test Addition') command = [G.GIT, '-C',G.repodir,'add','location.json'] process = cw.run_process(command) command = [G.GIT, '-C',G.repodir,'commit','-m', 'Test commit'] process = cw.run_process(command) with self.assertRaises(Exception) as cm: G.test_clean_repo() exception_message = str(cm.exception) expected = "Extra commits found in test repository, please reset" self.assertEqual(exception_message, expected)
def test_no_conflict_recursive_merge(self): git = GitCmd() git.do_merge('') expected = "No branch names provided" self.assertEqual(git.output, expected) git.do_merge('octupus merge') expected = "Git Crystals does not support merging mulitple branches" self.assertEqual(git.output, expected) git.do_branch('trial') git.do_checkout('trial') G.change_location_file("Git Crystal") git.do_stage('location.json') git.do_commit('Player in Git Crystal') git.do_checkout('data') git.do_merge('trial') command = [G.GIT, '-C', G.repodir, 'branch', '-D', 'trial'] process = cw.run_process(command) expected = """Merge made by the 'recursive' strategy. location.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) """ self.assertEqual(git.output, expected)
def do_checkoutforce(self, args): first_arg = args.split()[0] command = [G.GIT, '-C', self.repodir, 'checkout', '-f', first_arg] process = cw.run_process(command) self.output = process.stdout self.error = process.stderr return self.is_player_alive()
def test_checkout_same_ref(self): git = GitCmd() git.do_branch('newbranch') command = [G.GIT, '-C', G.repodir, 'checkout', 'newbranch'] process = cw.run_process(command) git.do_checkout('newbranch') git.do_listbranches('') expected = " data\n* newbranch\n" actual = git.output command = [G.GIT, '-C', G.repodir, 'checkout', G.current_branch] process = cw.run_process(command) command = [G.GIT, '-C', G.repodir, 'branch', '-d', 'newbranch'] process = cw.run_process(command) self.assertEqual(actual, expected)
def test_repo_with_extra_branches(self): command = [G.GIT, '-C',G.repodir,'branch','gittest'] process = cw.run_process(command) with self.assertRaises(Exception) as cm: G.test_clean_repo() exception_message = str(cm.exception) expected = "Unnecessary Branches, Please Delete" self.assertEqual(exception_message, expected)
def do_resolveright(self, args): args.split() for arg in args: command = [G.GIT, '-C', self.repodir, 'checkout', '--theirs', arg] process = cw.run_process(command) self.output = process.stdout self.err = process.stderr self.display_output() return self.is_player_alive()
def do_stage(self, args): first_arg = args.split()[0] if first_arg.endswith('.json'): command = [G.GIT, '-C', self.repodir, 'add', first_arg] process = cw.run_process(command) self.output = process.stdout self.error = process.stderr else: print("Type 1 file name exactly") print("Example: 'stage princess/location.json'")
def do_branch(self, args): first_arg = args.split()[0] # do not allow branches with space names command = [G.GIT, '-C', self.repodir, 'branch', first_arg] process = cw.run_process(command) self.output = process.stdout self.error = process.stderr if len(args.split()) > 1: print('branch names cannot have spaces ' + first_arg + ' used as branch name')
def reset_repo(): command = [GIT, '-C', repodir, 'checkout', current_branch] process = cw.run_process(command) command = [GIT, '-C', repodir, 'reset', '--hard', current_commit_sha] process = cw.run_process(command) branches = [] command = [GIT, '-C', repodir, '--show-ref', '--heads'] process = cw.run_process(command) output = process.stdout output_lines = output.split('\n') if '' in output_lines: output_lines.pop() for line in output_lines: branches.append(re.sub(r'[0-9a-f]{40} refs/heads/', '', line)) for branch in branches: if branch != 'data': command = [G.GIT, '-C', repodir, 'branch', '-D', branch] process = cw.run_process(command)
def test_commit(self): git = GitCmd() G.change_location_file("Git Crystal") git.do_stage('location.json') git.do_commit('Player in Git Crystal') command = [G.GIT, '-C', G.repodir, 'show-ref', '--heads'] process = cw.run_process(command) self.assertNotEqual( process.stdout, G.current_commit_sha + " refs/heads/" + G.current_branch + '\n')
def test_change_location_file(self): with open(G.repodir + '/location.json', 'w') as f: f.write('{\n "location":"Git Crystal"\n}\n\n') G.change_location_file('Mountain Gate') command = [G.GIT, '-C',G.repodir,'status','--short'] process = cw.run_process(command) output = process.stdout expected = "" self.assertEqual(output, expected)
def setUp(self): command = [G.GIT, '-C',G.repodir,'checkout','--force', G.current_branch] process = cw.run_process(command) command = [G.GIT, '-C',G.repodir,'reset','--hard', G.current_commit_sha] process = cw.run_process(command) # delete all branches but G.current_branch command = [G.GIT, '-C',G.repodir,'show-ref','--heads'] process = cw.run_process(command) output = process.stdout branches = output.split('\n') if len(branches) > 1: sub_string = "/" + G.current_branch for branch in branches: if branch.find(sub_string) == -1: index = branch.rfind('/') branch_name = branch[index+1:] command = [G.GIT, '-C',G.repodir,'branch','-D', branch_name] process = cw.run_process(command)
def test_clean_repo(): command = [GIT, '-C', repodir, 'rev-list', current_branch] process = cw.run_process(command) output = process.stdout if not output.split('\n')[0] == current_commit_sha: raise Exception("Extra commits found in test repository, please reset") command = [GIT, '-C', repodir, 'status', '--short'] process = cw.run_process(command) output = process.stdout if not output == '': raise Exception("Working Directory or Index Not Clean. Please clear") command = [GIT, '-C', repodir, 'show-ref', '--heads'] process = cw.run_process(command) output = process.stdout lines = output.split('\n') if len(lines) > 2: # only one ref raise Exception("Unnecessary Branches, Please Delete") return True
def test_reset_repo(self): command = [G.GIT, '-C',G.repodir,'rev-list','HEAD'] process = cw.run_process(command) expected = process.stdout with open(G.repodir + '/location.json', 'a') as f: f.write('# Test Addition') command = [G.GIT, '-C',G.repodir,'add','location.json'] process = cw.run_process(command) command = [G.GIT, '-C',G.repodir,'commit','-m', 'Test commit'] process = cw.run_process(command) G.reset_repo() command = [G.GIT, '-C',G.repodir,'rev-list','HEAD'] process = cw.run_process(command) new_rev_list = process.stdout self.assertEqual(new_rev_list, expected)
def test_unstage(self): git = GitCmd() G.change_location_file("Git Crystal") git.do_stage('location.json') git.do_unstage('location.json') command = [G.GIT, '-C', G.repodir, 'status', '--short'] process = cw.run_process(command) G.change_location_file("Mountain Gate") expected = ' M location.json\n' # location.json has unstaged changes self.assertEqual(process.stdout, expected)
def test_checkout_file(self): git = GitCmd() with open(G.repodir + '/README.md', 'a') as f: f.write("##Test Header") git.do_checkoutfile('README.md') command = [G.GIT, '-C', G.repodir, 'status', '--short'] process = cw.run_process(command) expected = '' self.assertEqual(process.stdout, expected)
def test_repo_with_staged_changes(self): with open(G.repodir + '/location.json', 'a') as f: f.write('# Test Addition') command = [G.GIT, '-C',G.repodir,'add','location.json'] process = cw.run_process(command) with self.assertRaises(Exception) as cm: G.test_clean_repo() exception_message = str(cm.exception) expected = "Working Directory or Index Not Clean. Please clear" self.assertEqual(exception_message, expected)
def do_diffbranch(self, args): branches = [] args = args.split() while (len(args) < 2): args.append('') command = [G.GIT, '-C', self.repodir, 'show-ref', '--heads'] process = cw.run_process(command) output = process.stdout output_lines = output.split('\n') if '' in output_lines: output_lines.pop() for line in output_lines: branches.append(re.sub(r'[0-9a-f]{40} refs/heads/', '', line)) if args[0] in branches and args[1] in branches: command = [G.GIT, '-C', self.repodir, 'diff', args[0], args[1]] process = cw.run_process(command) self.output = process.stdout else: self.output = "only " + ",".join( branches) + " are legal branch names\n" self.output += "usage: diffbranch branch1 branch2\n" self.display_output()
def do_graph(self, args): if args.split() != []: arg = args.split()[0] else: arg = 20 try: entries = int(arg) except ValueError: entries = 20 entries = '-' + str(entries) command = [ G.GIT, '--no-pager', '-C', self.repodir, 'log', entries, '--oneline', '--decorate', '--graph', '--all' ] process = cw.run_process(command) self.output = process.stdout self.error = process.stderr print(self.output)
def test_merge_with_conflicts(self): git = GitCmd() git.do_branch('trial') G.change_location_file("Git Crystal") git.do_stage('location.json') git.do_commit('Player in Git Crystal') git.do_checkout('data') G.change_location_file("Stalagmite Central") git.do_stage('location.json') git.do_merge('trial') git.do_resolveleft('location.json') git.do_resolveright('location.json') git.do_stage('location.json') git.do_commit('Merge Branch Trial') git.do_status( '') # Get status message after successful merge resolution. command = [G.GIT, '-C', G.repodir, 'branch', '-D', 'trial'] process = cw.run_process(command) expected = 'No changes since last commit\n' self.assertEqual(git.output, expected)
def test_command_wrapper(self): process = run_process(['echo', 'yoyoyo']) self.assertEqual(process.stdout, 'yoyoyo\n')
def do_revlist(self, args): command = [G.GIT, '-C', self.repodir, 'rev-list', 'HEAD'] process = cw.run_process(command) self.output = process.stdout self.error = process.stderr
def do_diffchanges(self, args): command = [G.GIT, '-C', self.repodir, 'diff', 'HEAD'] process = cw.run_process(command) self.output = self.format_status(process.stdout) self.error = process.stderr self.display_output()
def do_status(self, args): command = [G.GIT, '-C', self.repodir, 'status', '--short'] process = cw.run_process(command) self.output = self.format_status(process.stdout) self.error = process.stderr self.display_output()
def do_commit(self, args): message = args command = [G.GIT, '-C', self.repodir, 'commit', '-m', message] process = cw.run_process(command) self.output = process.stdout self.error = process.stderr
def do_listbranches(self, args): command = [G.GIT, '-C', self.repodir, 'branch'] process = cw.run_process(command) self.output = process.stdout self.error = process.stderr self.display_output()