Exemplo n.º 1
0
Arquivo: gitdb.py Projeto: 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)
Exemplo n.º 2
0
Arquivo: gitdb.py Projeto: cb22/gitdb
def handle_pull(branch, database):
    logging.info("New patches from pull:")
    if branch != "master":
        logging.warn("gitdb does not currently support remote tracking branches.")
    else:
        patches = [patch for patch in list_file_patches(branch, database) if patch not in list_applied_patches(branch, database) and patch not in list_ignored_patches(branch, database)]
        logging.info(patches)
        
        try:
            apply_patch(database, patches, True)
            for patch in patches:
                add_patch_to_db(branch, database, patch)
            
        except MySqlException:
            logging.error("Patches from pull failed to apply.")
            logging.error("Feel free to do whatever you like to the database to make it consistent. Then run git db merge.")
Exemplo n.º 3
0
Arquivo: git-db.py Projeto: cb22/gitdb
def stash():
    branch = git.current_branch()
    if args.unstash or args.delete:
        if list_stashed_patches(branch, database):
            patches = dict([(i, patch) for i, patch in enumerate(list_stashed_patches(branch, database))])
            pprint(patches)
            try:
                input = raw_input("Please enter a space separated list of patch #s to unstash: " if args.unstash else "Please enter a space separated list of patch #s to delete: ")
                if input != "":
                    to_unstash = [patches[int(i)] for i in input.split(" ")]
                    
                    for unstashed in to_unstash:
                        if args.unstash:
                            try:
                                apply_patch(database, unstashed, True)
                                remove_stashed_from_db(branch, database, unstashed)
                            except MySqlException:
                                print "Error applying stashed patch %s, not continuing"
                        elif args.delete:
                            # Delete the patch here.
                            os.remove(unstashed)
                            remove_stashed_from_db(branch, database, unstashed)
                        
            except:
                print "Invalid input."
                return 
            
        else:
            print "No stashed patches available for unstashing." if args.unstash else "No stashed patches available for deleting." 
            
        if args.unstash:
            add_event_to_db(database, "unstashed patches, %s: ", time.time())
    else:
        if fast_check_sqlchanges(database, git.last_commit_time()):
            print "Stashing all schema changes made on branch %s to database %s" % (branch, database)
            
            with amalgamated_sql(branch, database) as amalgamated_sql_file:
                base_dir = os.path.join(".git", "gitdb", "stashed", branch, database)
                patch_name = os.path.join(base_dir, free_patch_name())
                makedirs(base_dir)
                
                sql_changes = calculate_difference(amalgamated_sql_file, database, [filters.filter_auto_increment, lambda input: filters.filter_renames(input, database, git.last_commit_time())])
                if not sql_changes:
                    print "No schema changes to stash."
                    return
                else:
                    print "Your SQL schema changes will be stashed in the file %s. You can use the git db stash command to manipulate them." % patch_name
                    with open(patch_name, "w") as sql_patch:
                        sql_patch.write(sql_changes)
        
                    # Rollback the database to the last commit.
                    temp_file = NamedTemporaryFile()
                    sql_difference = calculate_difference(database, amalgamated_sql_file, [filters.filter_auto_increment, lambda input: filters.filter_renames(input, database, git.last_commit_time())])
                    temp_file.write(sql_difference)
                    temp_file.flush()
                    
                    apply_patch(database, temp_file.name, True)
                    temp_file.close()
                    
                    add_stashed_to_db(branch, database, patch_name)
                    add_event_to_db(database, "stash", time.time())
        else:
            print "No schema changes to stash."