根据文件路径、commit id、 批量获取patch

  根据已知文件路径,和commit ID,遍历仓库,获取patch。文件路径和commit id以如下文件格式给出:

system / core / ecf5fd58a8f50362ce9e8d4245a33d56f29f142b
frameworks / base / e739d9ca5469ed30129d0fa228e3d0f2878671ac
frameworks / av / 119a012b2a9a186655da4bef3ed4ed8dd9b94c26
frameworks / av / 1e9801783770917728b7edbdeff3d0ec09c621ac

  思路:以行的方式读取文件,获取字符串后分别解析出路径和commit id ;  通过python os库操作shell命令,切换文件目录,使用git format -patch生成patch


完整code如下:

#!/usr/bin/python
# coding:utf-8


import os

# 分割字符串
def deal_string(string):
    str_list = []
    for i in string.split("/"):
        str_list.append(i.strip())
    return str_list


# 生成patch
def product_patch(str_list):
    length = str_list.__len__()
    directory = "/tmp/patches/2016_09/"     # patch打包路径
    code_dir = "/work/android/"  # code 路径
    commit_id = str_list[length-1]
    for i in range(length-1):                      # len-1前为路径,len-1commit id
        directory = directory + str_list[i] + "/"
        code_dir = code_dir + str_list[i] + "/"

    if os.path.exists(directory) is False:
        os.makedirs(directory)                     # 生成patch目录

    os.chdir(code_dir)                # 切换到code工作区
    command = 'git format-patch ' + commit_id + ' -1 '   # 生成patch
    os.system(command)
    os.system('mv *.patch ' + directory)            # 移动patch


if __name__ == '__main__':
    filename = '2016_09.txt'
    open_file = open(filename, 'r')
    for line in open_file:
        line_list = deal_string(line)
        product_patch(line_list)
    open_file.close()

猜你喜欢

转载自blog.csdn.net/King0217/article/details/78352203