My favorites
▼
|
Sign in
p4scripts
Perforce Utility Scripts
Project Home
Downloads
Wiki
Issues
Source
Checkout
Browse
Changes
Source path:
svn
/
trunk
/
src
/
p4revert.py
r15
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env python
#
# Reverts a specified changelist in perforce.
# Add it to the perforce p4win tool as a contex sensitive tool.
#
# Import the following to your p4win client:
#
# --- cut ---
# P4Win Tools File from jt
# >>Imported from jt 0 0 0 1 0 1
# >>RevertChangelist p4revert.py -c $c -p $p -u $u %c 1 0 1 0 0 1 0 1
# --- cut ---
#
# Jim Tilander (2007)
# Downloaded off http://www.tilander.org/aurora
#
# Improvements made by Matt Zimmer. Graceously contributed with the permission of Intuit.
# - Changed default from overwrite to require manual resolve if later
# edits made to a file (you want to roll 28 back to 27 but there is a 29
# in the depot).
# - Added force flag to force overwrite (27 would overwrite 29 in the above
# scenario).
#
#
#
import os
import sys
import getopt
import marshal
import logging
P4_PORT_AND_USER = ' '
def p4( command ):
"""
Run a perforce command line instance and marshal the
result as a list of dictionaries.
"""
commandline = 'p4 %s -G %s' % (P4_PORT_AND_USER, command)
logging.debug( '%s' % commandline )
stream = os.popen( commandline, 'rb' )
entries = []
try:
while 1:
entry = marshal.load(stream)
entries.append(entry)
except EOFError:
pass
code = stream.close()
if None != code:
raise IOError( "Failed to execute %s: %d" % (commandline, int(code)) )
return entries
def deleteFile(name):
"""
Deletes the file from the repository.
"""
p4( 'delete "%s"' % name )
def backRevision(name, revision, force):
"""
Given a file and a revision number, tries to go back
one revision in time.
"""
targetRevision = revision - 1
headAction = p4( 'fstat "%s#%d"' % (name, targetRevision))[0]['headAction']
if 'delete' == headAction:
if laterRevisionExists(name, revision) and not force:
logging.warn("%s has new edits since this change list was submitted. Skipping automated delete; You must delete manually." % (name))
else:
deleteFile(name)
else:
syncDoActionAndResolve(name, "edit", int(revision), force)
def laterRevisionExists(name, revision):
headRevision = p4( 'fstat "%s"' % (name))[0]['headRev']
return int(headRevision) > revision
def recoverFile(name, revision, force):
"""
Adds a deleted file back to the repository.
"""
syncDoActionAndResolve(name, "add", int(revision), force)
def syncDoActionAndResolve(name, action, revision, force):
p4( 'sync "%s#%d"' % (name, revision -1) )
p4( '%s "%s"' % (action, name) )
p4( 'sync "%s"' % name )
headRevision = int(p4( 'fstat "%s"' % (name))[0]['headRev'])
logging.debug("revision = %d" % (revision))
logging.debug("headRevision = %d" % (headRevision))
if laterRevisionExists(name, revision) and not force:
logging.warn("%s has new edits since this change list was submitted. Skipping automated resolve; You must resolve manually." % (name))
else:
p4( 'resolve -ay "%s"' % name )
def revertChangelist( changelistNumber, force ):
"""
Steps through the whole changelist file by file and looks at
the last action taken and then tries to go back one step.
"""
logging.debug( 'Trying to revert the changelist %d' % changelistNumber )
description = p4( 'describe -s %d' % changelistNumber )[0]
infos = []
counter = 0
try:
while 1:
name = description[ 'depotFile%d' % counter ]
action = description[ 'action%d' % counter ]
revision = int(description[ 'rev%d' % counter ])
infos.append( (name, action, revision) )
counter += 1
except KeyError:
pass
for name, action, revision in infos:
logging.debug( 'Processing %s#%d' % (name, revision) )
if 'add' == action:
deleteFile(name)
if 'edit' == action:
backRevision(name, revision, force)
if 'delete' == action:
recoverFile(name, revision, force)
if 'branch' == action or 'integrate' == action:
if 1 == revision:
deleteFile(name)
else:
backRevision(name, revision, force)
def main(argv):
"""
Usage: p4revert.py [options] <changelist>
Options:
-v : verbose
-f : force
-c client : perforce client
-p port : perforce port
-u user : perforce user
"""
try:
options, arguments = getopt.getopt(argv, 'c:p:u:vf')
except getopt.GetoptError:
print 'Error parsing arguments'
print main.__doc__
return 1
# Default tweakable values for the options.
verbose = False
force = False
client = ''
port = ''
user = ''
# Loop through all the options
for o,a in options:
if '-v' == o:
verbose = True
if '-c' == o:
client = a
if '-p' == o:
port = a
if '-u' == o:
user = a
if '-f' == o:
force = True
if len(arguments) != 1:
print 'Must give one changelist number'
print main.__doc__
return 1
try:
changelistNumber = int(arguments[0])
except ValueError:
print 'Changelist number must be a number!'
print main.__doc__
return 1
global P4_PORT_AND_USER
if len(client):
P4_PORT_AND_USER += ' -c %s ' % client
if len(port):
P4_PORT_AND_USER += ' -p %s ' % port
if len(user):
P4_PORT_AND_USER += ' -u %s ' % user
# At this point we're all done with the options! Now to the real code.
if verbose:
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s %(levelname)-7s: %(message)s' )
else:
logging.basicConfig(
level=logging.INFO, format='%(message)s' )
revertChangelist( changelistNumber, force )
logging.info( 'Revert of %d done.' % changelistNumber )
results = p4 ( "resolve -n" )
for result in results:
code = result['code']
if code == 'stat':
logging.warning("%s must be resolved." % (result['fromFile']))
elif code == 'error':
logging.info("Change list reverted and files are ready for submit.")
return 0
if __name__ == '__main__':
# This is just the main stub trick that makes the script act like a regular
# unix application. We return 1 for errors and 0 for success.
sys.exit( main(sys.argv[1:]) )
Show details
Hide details
Change log
r2
by jim.tilander on May 26, 2008
Diff
First import from webpage
Go to:
/trunk/LICENSE
/trunk/README
/trunk/old
/trunk/old/p4diff.py
/trunk/old/p4patch.py
/trunk/src
/trunk/src/p4offlinesync.py
/trunk/src/p4revert.py
/trunk/src/p4shelf.py
Project members,
sign in
to write a code review
Older revisions
All revisions of this file
File info
Size: 6119 bytes, 218 lines
View raw file
Powered by
Google Project Hosting