Added support for git-p4 submit --direct (experimental)
Signed-off-by: Simon Hausmann <simon@lst.de>
Simon Hausmann committed
May 20, 2007 at 16:55 UTC
c1b296b9f19a62aa9e444cc454b3d62ffaa98da4
1 file changed
+47
-14
contrib/fast-import/git-p4
+47
-14
@@ -137,6 +137,7 @@ class P4Submit(Command):
137
optparse.make_option("--log-substitutions", dest="substFile"),
138
optparse.make_option("--noninteractive", action="store_false"),
139
optparse.make_option("--dry-run", action="store_true"),
140
+ optparse.make_option("--direct", dest="directSubmit", action="store_true"),
141
]
142
self.description = "Submit changes from git to the perforce depot."
143
self.usage += " [name of git branch to submit into perforce depot]"
@@ -147,6 +148,7 @@ class P4Submit(Command):
148
self.substFile = ""
149
self.firstTime = True
150
self.origin = ""
151
+ self.directSubmit = False
152
153
self.logSubstitutions = {}
154
self.logSubstitutions["<enter description here>"] = "%log%"
@@ -161,9 +163,12 @@ class P4Submit(Command):
163
die("Cannot start sync. Previous sync config found at %s\nIf you want to start submitting again from scratch maybe you want to call git-p4 submit --reset" % self.configFile)
164
165
commits = []
164
- for line in mypopen("git rev-list --no-merges %s..%s" % (self.origin, self.master)).readlines():
165
- commits.append(line[:-1])
166
- commits.reverse()
166
+ if self.directSubmit:
167
+ commits.append("0")
168
+ else:
169
+ for line in mypopen("git rev-list --no-merges %s..%s" % (self.origin, self.master)).readlines():
170
+ commits.append(line[:-1])
171
+ commits.reverse()
172
173
self.config["commits"] = commits
174
@@ -191,8 +196,12 @@ class P4Submit(Command):
196
return result
197
198
def apply(self, id):
194
- print "Applying %s" % (mypopen("git log --max-count=1 --pretty=oneline %s" % id).read())
195
- diff = mypopen("git diff-tree -r --name-status \"%s^\" \"%s\"" % (id, id)).readlines()
199
+ if self.directSubmit:
200
+ print "Applying local change in working directory/index"
201
+ diff = self.diffStatus
202
+ else:
203
+ print "Applying %s" % (mypopen("git log --max-count=1 --pretty=oneline %s" % id).read())
204
+ diff = mypopen("git diff-tree -r --name-status \"%s^\" \"%s\"" % (id, id)).readlines()
205
filesToAdd = set()
206
filesToDelete = set()
207
editedFiles = set()
@@ -213,11 +222,13 @@ class P4Submit(Command):
222
else:
223
die("unknown modifier %s for %s" % (modifier, path))
224
216
- diffcmd = "git format-patch -k --stdout \"%s^\"..\"%s\"" % (id, id)
225
+ if self.directSubmit:
226
+ diffcmd = "cat \"%s\"" % self.diffFile
227
+ else:
228
+ diffcmd = "git format-patch -k --stdout \"%s^\"..\"%s\"" % (id, id)
229
patchcmd = diffcmd + " | git apply "
218
- tryPatchCmd = diffcmd + "--check -"
219
- applyPatchCmd = diffcmd + "--check --apply -"
220
- print mypopen(diffcmd).read()
230
+ tryPatchCmd = patchcmd + "--check -"
231
+ applyPatchCmd = patchcmd + "--check --apply -"
232
233
if os.system(tryPatchCmd) != 0:
234
print "Unfortunately applying the change failed!"
@@ -250,9 +261,11 @@ class P4Submit(Command):
261
system("p4 revert %s" % f)
262
system("p4 delete %s" % f)
263
253
- logMessage = extractLogMessageFromGitCommit(id)
254
- logMessage = logMessage.replace("\n", "\n\t")
255
- logMessage = logMessage[:-1]
264
+ logMessage = ""
265
+ if not self.directSubmit:
266
+ logMessage = extractLogMessageFromGitCommit(id)
267
+ logMessage = logMessage.replace("\n", "\n\t")
268
+ logMessage = logMessage[:-1]
269
270
template = mypopen("p4 change -o").read()
271
@@ -356,6 +369,15 @@ class P4Submit(Command):
369
370
print "Perforce checkout for depot path %s located at %s" % (depotPath, self.clientPath)
371
oldWorkingDirectory = os.getcwd()
372
+
373
+ if self.directSubmit:
374
+ self.diffStatus = mypopen("git diff -r --name-status HEAD").readlines()
375
+ patch = mypopen("git diff -p --binary --diff-filter=ACMRTUXB HEAD").read()
376
+ self.diffFile = gitdir + "/p4-git-diff"
377
+ f = open(self.diffFile, "wb")
378
+ f.write(patch)
379
+ f.close();
380
+
381
os.chdir(self.clientPath)
382
response = raw_input("Do you want to sync %s with p4 sync? [y]es/[n]o " % self.clientPath)
383
if response == "y" or response == "yes":
@@ -395,14 +417,25 @@ class P4Submit(Command):
417
418
self.config.close()
419
420
+ if self.directSubmit:
421
+ os.remove(self.diffFile)
422
+
423
if len(commits) == 0:
424
if self.firstTime:
425
print "No changes found to apply between %s and current HEAD" % self.origin
426
else:
427
print "All changes applied!"
403
- response = raw_input("Do you want to sync from Perforce now using git-p4 rebase? [y]es/[n]o ")
428
+ response = ""
429
+ os.chdir(oldWorkingDirectory)
430
+
431
+ if self.directSubmit:
432
+ response = raw_input("Do you want to DISCARD your git WORKING DIRECTORY CHANGES and sync from Perforce now using git-p4 rebase? [y]es/[n]o ")
433
+ if response == "y" or response == "yes":
434
+ system("git reset --hard")
435
+
436
+ if len(response) == 0:
437
+ response = raw_input("Do you want to sync from Perforce now using git-p4 rebase? [y]es/[n]o ")
438
if response == "y" or response == "yes":
405
- os.chdir(oldWorkingDirectory)
439
rebase = P4Rebase()
440
rebase.run([])
441
os.remove(self.configFile)