不安装git工具应用GIT binary patch

从网上得到一GIT binary patch文件, 却没办法把它恢复出来. google后发现可以用mercurial的hg工具还apply.

以下是在ubuntu下实际操作记录.

1) 先安装mercurial

sudo apt-get install mercurial

 

2) 用hg创建一个临时的代码库

hg init

 

3) vi .hg/hgrc 增加下面的内容,不然在后面的操作中会报"abort: no username supplied (see "hg help config")"的错误

[ui]
username = j 


4) 预先建一空文件, 否则会报"unable to find 'xxx' for patching"的错误

touch rt2860.bin
hg add rt2860.bin


5) 打补丁,得到想要的文件

hg import -f a.patch



通过分析hg的代码(python写的)发现"GIT binary patch"文件就是一经过base85编码的zip文件.

将其中的代码抽出来做一个小脚本, 就不需要上面那么麻烦了.

 

通过查阅网上资料,用python写了个小程序显示解码的过程.

附python2.4写的例程

# vim: set ts=8 sw=4 sts=4 et ai:
# Example base85 decoder, Walter Doekes 2012
import sys
import zlib

en85 = [
 	'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
 	'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
 	'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
 	'U', 'V', 'W', 'X', 'Y', 'Z',
 	'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
 	'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
 	'u', 'v', 'w', 'x', 'y', 'z',
 	'!', '#', '$', '%', '&', '(', ')', '*', '+', '-',
 	';', '<', '=', '>', '?', '@', '^', '_',	'`', '{',
 	'|', '}', '~'
]

def _chunk(data):
    # TODO: remove white space
    idx = 0
    while len(data) > idx:
        next = data[idx:idx+5]
        idx += len(next)
        padding = 5 - len(next)
        if padding:
            next = next.ljust(5, 'u')
        #if next >= '|NsC1':
        #    print >> sys.stderr, 'Garbage in input: %s' % (next,)
        #    sys.exit(0)
        yield next, padding

def _decode(chunk, padding):
    assert len(chunk) == 5

    num = 0
    for c in chunk[:5] :
      try :
        num = num * 85
        num += int(en85.index(c))
      except ValueError:
        print >> sys.stderr, 'Invalid base85 alphabet: "%c"' % (c)
        sys.exit(0)

    if num >= 2**32:
        print >> sys.stderr, 'Garbage in input: %s' % (chunk)
        sys.exit(0)
    return ('%c%c%c%c' % (
        chr(num >> 24),
        chr((num >> 16) & 0xff),
        chr((num >> 8) & 0xff),
        chr(num & 0xff)
    ))[0:4-padding]

b85decode = (lambda x: ''.join(_decode(*i) for i in _chunk(x)))

def _b85decode(data):
    dec = []
    for line in data.split():
      l = line[0]
      if l <= 'Z' and l >= 'A':
          l = ord(l) - ord('A') + 1
      else:
          l = ord(l) - ord('a') + 27
      dec.append(b85decode(line[1:])[:l])
    return ''.join(dec)


gitbp = (r'zcmeGh{cjY<^>%h|eg43{+uMWl*}$?r2V*d7z+juA#PMhNw83X=t0^oJQAq;o0=J<`'+'\n'
         r'z<9KVZN-GjpHh(~BM{T1hvP7>^B--S(PEo2v>1k1<{t|V78=5u&uNpQXfPH9cN+PADwf11W=Cw($KlFsOvs5PZ@'+'\n'
         r'zutKod*LXCIO8YlE>O*'+'\n'
         r'zY9Z{l3&9$L_YfV~S&wdWrWVeE0#MjfnM`IRb28&(@cIYpf0o97OXI(%@#ks$MH*jE'+'\n'
         r'z;~Qx_lg5QP3D6DaGI%b7XEXS22H(lx+Zp^#2H*O(;`B_g(6dY#C?N@zizEzW4Jd;p'+'\n'
         r'zqz4*3&=~xrw@3lF)8;(8Ramb+q@1bMVdZk0HDzkG*bG*K{;Qx~3UwXS%N)RAw-F#L'+'\n'
         r'zUH+0bfDw>Opjt^Dh3}_D~Oj$09dZDyj1D|950mtAWPw5'+'\n'
         r'zDa4c29cxtflEF-my(zHO#(P$q-H~-AcZJ*QcDk$GHCC&;+uh^#nOu~5my3bJt(Xh3k<%x$$CGIMkUBFtIcfZ5=BBzlJ14L|z^R-7)r^y@Uh9rz8#MRibw@6Lm%A!I'+'\n'
         r'zaHO6I6fDm_WKC;I@bLmF7SQFaE4y7L=L4<5Hu}#{);;6?+&%5S<9?=_QTvSkYrj0B'+'\n'
         r'zvdfLzaxVQ?k_G7_>67|nCGz}MRQ7BdA(F;QK~@Q|Z9iq?k7quACls|DakV?`{Qww^&KyXdfRNFSg^o?'+'\n'
         r'z1r{u@V1We-ELdQ{0t*&cu)u-^7A){Tvj7U#qTqK?a0?3m4uyY@#A8T2J`EWGRJjes'+'\n'
         r'zZlcIxB>qbA^U@+#U|v#qUMgpW>b0_b@AE%=!Ta{#OTYaE^i1%M60{!=j&BJ5q9K^t'+'\n'
         r'z7<_9}sJJJzYj>!BPpH2))Sn2A_l2CjLYpQ$cT^ZV7JlM*_<#{UeKP#|DW5NV!H(Jo'+'\n'
         r'z^o@1;`ci$vJF*ou84)LD9ohT*2PJ)q>nM@gON6sW}=bX{KkY4cQ-PBkVzJOgt&a)MfK9yo9W@o;'+'\n'
         r'z^<9kKed-<>2^4)WpmI1R0gctWrL4Qs6B*Bqbv-O?D>Y0Fj(^|nrvh@xuA2T^7iTU~'+'\n'
         r'z8M)-s`3B@N3KMupldYudV3E2M%7D5A%3?JHWr1l}liSDI)Q2G&YE'+'\n'
         r'zH?t35N9p3GRvH{xc{69C+8O6x=L(|dsJPkdx30PU|6x{CfdZF1^4du}kI@YJpqX_C'+'\n'
         r'z+(9$Q;#93)A7G3v9;^V248?;t0L(!aiSleLGlK}HjsYrQgz~5o4>iu}13_3Tmn%(w'+'\n'
         r'z0Jk&Ftm*gT#2p8g!S%!(Af2qc%k9=T=km!7x54zkkqkML$sGJypej}+tTWu+;^Vpsatk#(sUcyXQ070|HpAh{uf<)_cFElcQr5Qw6dD{5(Swx-jC'+'\n'
         r'zlvCF2CB3ke#iNS4;`~l!XLX)AM48KgIgn&2r-A&P7Du`}cBui<9qHCkWS0`#I=p`9'+'\n'
         r'zotH}mw6TXbxnQ!z8QwUwUSOZ>DK>)~PJHdyJlS~ZTz!k8RMlHm6Vb7l6pNc&F;096'+'\n'
         r'zxhW2}L$c_A&B|GKBPA-aB1&wRL2E^XiB`hOgH;v9-T!@sS)y!hw|0Y'+'\n'
         r'zq&E=R5EMU^#91{`{<_Z>8617OZFF1f=#G7`F?g7GWYDN0PrncQqK#~$RV&#+E03-n'+'\n'
         r'z=xwVoo*CWRHoCoabY~v76FO|oci5iqu=7C&g|sCq+JkPJ@fZxic{|U~x9#Kf)?(Od'+'\n'
         r'ze9)sSKvqoUBARd$5H'+'\n'
         r'z)O8wkF1pu`9xNhhm#&tQ%?A<89nKhzN}-2Lm>x2D0dU@Eo?Pjq+1qxk8'+'\n'
         r'z=bx93Q0NLee^_7JB0a}V95OnTLpXEckTzJVqFM%N_Odvug{>x|!frB3NvCoppXF^*'+'\n'
         r'zy8KfsQDp@^xM@Y>3M~UFEMSN$EQ-G7NxSQi&7vxcYK^GY$M33JT1xX19TBUmu>TPF;>~l#^!m#jrwoVq#1U>eXBG>qJF*('+'\n'
         r'zg^1RbDh+z$f9{Z#iO)MaAhu@#;RU}A2!0*+g5TL5dr&@`um|9zYqX}wZc*JPsxQP5'+'\n'
         r'z*faKtJ*f9FQmooU0qP}4X(kbn}J@WN8|NI+TRp>'+'\n'
         r'zIKgTWk4J(tl*+jSY$(qLJQ*n#p=fxKL9vfat`Q%U>DV8c0+%dtAoN|0WKF*hf>{9m'+'\n'
         r'zi~})km=RdT48ya77IVxnJUeJrP_KqshPoCf9JnY!L2-$XJRwU+gBB)RZkpjb6$0p&'+'\n'
         r'z2HZLBIEGk2fnNXU*y0!v9Bw;QQ;xyyK_?&m;4p&oSqi}f$JRQ5g<-*RJwPQ'+'\n'
         r'z!|vmG_BCF(Wq=@EEh7yn*5jmoP)K6SDHVpM7dpV{jf~z0DgNC?G1$!yc5`==Mm1_N'+'\n'
         r'zfU^drSozzHQ!PT(ZPJ)xkN*p&@4KDX@dWB8yr9-d*ama}ORS;Q6KULF>_1m``K~-LVsNg-PVF$<<=FX%Zf&'+'\n'
         r'z+~%xXLz;ERcq8u15vReJBaP2CYyP`KwFdXHZ^$=OpS!VTPN;sfr-6*|k~9Jog0}Ih#oJXfshK{<_f(#xx5ZSh$X}Cn'+'\n'
         r'zb&Do_AUGtEb#wkfmrY9e*#h>e{lc+')

decoded = _b85decode(gitbp)
fo = open("out.bin","wb")
fo.write(zlib.decompress(decoded))
fo.close()

print "done!"



 

你可能感兴趣的:(不安装git工具应用GIT binary patch)