-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrevert-day.py
83 lines (72 loc) · 2.2 KB
/
revert-day.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/python
import sys
import os
import commands
import re
import json
import datetime
import uuid
# input parameter : project path, revertdate
if len(sys.argv) < 3:
print 'num of parameter wrong ...'
exit(1)
rootdir = sys.argv[1] if (sys.argv[1][-1] == '/') else sys.argv[1]+'/'
revertdate = sys.argv[2]
datetarget = datetime.datetime.strptime(revertdate, '%Y-%m-%d').date()
jsonall = []
jsonfile = '/tmp/'+str(uuid.uuid4())+'.txt'
def init():
os.path.exists(jsonfile) and os.remove(jsonfile)
def record_git_log(path):
command_git = 'cd %s; git log --pretty=format:"%s" --date=short ' %(path, '%H|%cd|%s')
git_log_ret = commands.getoutput(command_git).split('\n')
subpath = path.replace(rootdir, '')
jsonret = [subpath, git_log_ret]
jsonall.append(jsonret)
def save_to_file(path):
file = open(path, 'w')
try:
json.dump(jsonall, file)
except:
print 'json dump failed'
finally:
file.close()
def git_reset_to_date(path):
jsondata=[]
try:
file = open(jsonfile, 'r')
jsondata = json.loads(file.read())
except:
print 'load json failed'
finally:
file.close()
hashret='HEAD'
for data in jsondata:
print 'project <'+data[0]+'>'
gitdata = data[1]
for line in gitdata:
if datetarget >= datetime.datetime.strptime((line.split('|'))[1], '%Y-%m-%d').date():
hashret=(line.split('|'))[0]
print "\treset to ["+line+']'
break
command_git = 'cd %s; git checkout %s' %(path, hashret)
commands.getoutput(command_git)
# reverse project manifest
init()
record_git_log(rootdir+'.repo/manifests');
save_to_file(jsonfile)
git_reset_to_date(rootdir+'.repo/manifests')
command_git = 'cd %s; repo sync' %(rootdir)
commands.getoutput(command_git)
print "==================== reset manifest done ======================"
# traverse root directory
init()
for subroot, dirs, files in os.walk(rootdir):
skipdir = ['.repo']
if subroot.replace(rootdir, '') in skipdir:
continue
if '.git' in dirs:
record_git_log(subroot)
save_to_file(jsonfile)
git_reset_to_date(subroot)
jsonall=[]