def test_1(self): clear.remove_test_dir("promiseTest/") os.makedirs("promiseTest") os.chdir("promiseTest") git.init() with open("first", "w") as text: text.write("first") git.add("first") with open("second", "w") as text: text.write("second") git.add("second") git.commit("first commit") current_hash = git.current_hash() args = promise.promise_parser( "newBranch -f first -f second -l 1 -b 5-23".split()) promise.promise_creator(args) new_hash = git.current_hash() self.assertNotEqual(current_hash, new_hash, "There isn't a new commit") self.assertTrue(git.branch_exists("newBranch")) with open(".promise") as promise_file: self.assertEqual( promise_file.read(), '[{"files": [{"lines": null, "linesInBetween": null, "fileName": ' '"first"}, {"lines": [1], "linesInBetween": ["5-23"], "fileName": ' '"second"}], "hash": "' + current_hash + '", "parent": "master", ' '"child": "newBranch"}]')
def save_patch(branch, database, sql_difference): clean_pending(branch, database) if branch == "master": # We are working on the master branch - this means that the patch # needs to go straight into the sql/development/database folder # ready to be applied. base_dir = os.path.join("sql", "development", database) patch_filename = free_patch_name() patch_name = os.path.join(base_dir, patch_filename) makedirs(base_dir) base_dir = os.path.join(".git", "gitdb", "pending", branch, database) indicator_file = os.path.join(base_dir, patch_filename) makedirs(base_dir) with open(indicator_file, "w") as indicator: indicator.write("") else: # We are working on a local branch (remote tracking, what's that :/) # Therefore we store the patch in a local directory that is not under VCS. # These patches will later get "merged" into one base_dir = os.path.join(".git", "gitdb", "pending", branch, database) patch_name = os.path.join(base_dir, free_patch_name()) makedirs(base_dir) with open(patch_name, "w") as sql_patch: sql_patch.write(sql_difference) if branch == "master": git.add(patch_name)
def promise_creator(parsed_args): import helpers.git as git if not git.branch_exists(parsed_args.newBranchName): import helpers.promise as promise promise.write_promise(parsed_args) git.add(".promise") git.commit("New promise for branch: " + parsed_args.newBranchName + " is created") git.branch(parsed_args.newBranchName) print "New promise for branch: " + parsed_args.newBranchName + " is created" else: # TODO: add a parser option to append to the promised branch print "Promised branch already exists!"
def post_merge(dst_branch, database, squash): merge_info = git.reflog(False)[0] res = re.search(r".*: merge (\w+):.*", merge_info) if res: src_branch = res.group(1) logging.info("Branch '%s' was merged with us, '%s'", src_branch, dst_branch) logging.debug("Amalgamating patches from branch '%s' into one SQL file, and importing.", src_branch) patches = [patch for patch in list_applied_patches(src_branch, database) if ( patch not in list_merged_patches(src_branch, dst_branch, database) and patch not in list_ignored_patches(src_branch, database) and patch not in list_patches_resulting_from_merge(dst_branch, src_branch, database))] if not patches: logging.info("No patches to apply.") return output_file = NamedTemporaryFile(suffix=".sql", delete=False) for patch in patches: with open(patch, "r") as patchfile: output_file.write(patchfile.read()) output_file.flush() try: apply_patch(database, output_file.name, True) if dst_branch == "master": base_dir = os.path.join("sql", "development", database) patch_filename = free_patch_name() else: base_dir = os.path.join(".git", "gitdb", "patches", dst_branch, database) patch_filename = free_patch_name() patch_name = os.path.join(base_dir, patch_filename) makedirs(base_dir) shutil.copy(output_file.name, patch_name) add_patch_to_db(dst_branch, database, patch_name) add_merged_patches(patches, patch_name, src_branch, dst_branch, database) if dst_branch == "master": # If we are on master, add the SQL file and amend the commit. git.add(patch_name) git.amend_commit() except MySqlException: # Rollback occured. logging.error("SQL patch did not apply cleanly. This means that while the files in branch %s will have been updated, the database %s is not. Please bring the database to a consistent state manually (you can use the SQL information stored in %s for help), then run:", dst_branch, database, output_file.name) logging.error("foobar") else: handle_pull(dst_branch, database)
def create_promise_repo(): clear.remove_test_dir("commitTest/") os.makedirs("commitTest") os.chdir("commitTest") git.init() with open("first", "w") as text: text.write("1\n2\n3\n4\n5\n6\n7\n") git.add("first") with open("second", "w") as text: text.write("1\n2\n3\n4\n5\n6\n7\n") git.add("second") git.commit("first commit") args = promise.promise_parser("newBranch -f first -l 2 -b 4-6".split()) promise.promise_creator(args)
def test_child3(self): self.create_promise_repo() git.checkout("newBranch") with open("first", "w") as text: text.write("1\n2\n3\n4\n5\n6\n7-edited\n8-added\n9-added\n") current_hash = git.current_hash() git.add("first") result = commit.checking_and_committing( ["-m", "\"this commit should not work\""]) new_hash = git.current_hash() self.assertEqual(False, result, "Child should not be able to commit.") self.assertEqual( current_hash, new_hash, "Child should not be able to commit, hashes should equal.")
def test_child2(self): self.create_promise_repo() git.checkout("newBranch") with open("first", "w") as text: text.write("1\n2-edited\n3\n4\n5-edited\n6\n7\n") current_hash = git.current_hash() git.add("first") result = commit.checking_and_committing( ["-m", "\"this commit should work\""]) new_hash = git.current_hash() self.assertEqual(True, result, "Child should be able to commit.") self.assertNotEqual( current_hash, new_hash, "Child should be able to commit, hashes should be different.")
def test_parent(self): self.create_promise_repo() # Edit the parent branch with the child's promised line with open("first", "w") as text: text.write("1\n2-edited\n3\n4\n5\n6\n7\n") current_hash = git.current_hash() git.add("first") result = commit.checking_and_committing( ["-m", "\"this commit should not work\""]) new_hash = git.current_hash() self.assertEqual(False, result, "Parent shouldn't be able to commit.") self.assertEqual( current_hash, new_hash, "Parent shouldn't commit, hashes should be the same.")
def fulfill(): parser = argparse.ArgumentParser(description='Fulfill a promise, merge it back to its parent.') parser.add_argument('branch', metavar="branch", help='Name of the promise branch, if promise branch exist, merge the branch.\n') parser.add_argument('-d', dest="delete", action="store_const", const=True, default=False, help='Delete the branch if this option is given after merge.\n') parsed_args = parser.parse_args() git.merge(parsed_args.branch) if parsed_args.delete: git.delete_branch(parsed_args.branch) all_promises = promise.read_promise() new_promises = [] for the_promise in all_promises: if the_promise["child"] != parsed_args.branch: new_promises.append(the_promise) if len(new_promises) == 0: import os os.remove(".promise") else: promise.write_promises(new_promises) git.add(".promise") git.commit("Promise updated, removed branch " + parsed_args.branch) print "Promise fulfilled."