Example #1
0
File: gitdb.py Project: cb22/gitdb
def check_branch_rename(current_branch, database):
    if not exists_database(database + current_branch):
        for line in git.reflog(True):
            res = re.search(line, r".* Branch: renamed refs/heads/(\w+) to refs/heads/%s" % current_branch)
            if res:
                old_branch_name = res.group(1)
                logging.info("Detected that branch we are working on, '%s', used to be called '%s'.", current_branch, old_branch_name)
                
                # Rename the database, as well as any SQL patch files, etc, and rewrite our sqlite data.
                rename_database(database + old_branch_name, database + current_branch)
                
                break
Example #2
0
File: gitdb.py Project: cb22/gitdb
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)