Robustness fixes for pipes
- add read_pipe(), read_pipe_lines(), write_pipe(), which check pipe.close() - use throughout Signed-off-by: Han-Wen Nienhuys <hanwen@google.com>
Han-Wen Nienhuys committed
May 23, 2007 at 17:10 UTC
b016d39756f7d82e51c35e281673ed30b95cc586
1 file changed
+58
-27
contrib/fast-import/git-p4
+58
-27
@@ -14,9 +14,43 @@ import re
14
from sets import Set;
15
16
gitdir = os.environ.get("GIT_DIR", "")
17
+silent = False
18
18
-def mypopen(command):
19
- return os.popen(command, "rb");
19
+def write_pipe (c, str):
20
+ if not silent:
21
+ sys.stderr.write ('writing pipe: %s\n' % c)
22
+
23
+ ## todo: check return status
24
+ pipe = os.popen (c, 'w')
25
+ val = pipe.write(str)
26
+ if pipe.close ():
27
+ sys.stderr.write ('Command failed')
28
+ sys.exit (1)
29
+
30
+ return val
31
+
32
+def read_pipe (c):
33
+ sys.stderr.write ('reading pipe: %s\n' % c)
34
+ ## todo: check return status
35
+ pipe = os.popen (c, 'rb')
36
+ val = pipe.read()
37
+ if pipe.close ():
38
+ sys.stderr.write ('Command failed')
39
+ sys.exit (1)
40
+
41
+ return val
42
+
43
+
44
+def read_pipe_lines (c):
45
+ sys.stderr.write ('reading pipe: %s\n' % c)
46
+ ## todo: check return status
47
+ pipe = os.popen (c, 'rb')
48
+ val = pipe.readlines()
49
+ if pipe.close ():
50
+ sys.stderr.write ('Command failed')
51
+ sys.exit (1)
52
+
53
+ return val
54
55
def p4CmdList(cmd):
56
cmd = "p4 -G %s" % cmd
@@ -67,7 +101,7 @@ def die(msg):
101
sys.exit(1)
102
103
def currentGitBranch():
70
- return mypopen("git name-rev HEAD").read().split(" ")[1][:-1]
104
+ return read_pipe("git name-rev HEAD").split(" ")[1][:-1]
105
106
def isValidGitDir(path):
107
if os.path.exists(path + "/HEAD") and os.path.exists(path + "/refs") and os.path.exists(path + "/objects"):
@@ -75,7 +109,7 @@ def isValidGitDir(path):
109
return False
110
111
def parseRevision(ref):
78
- return mypopen("git rev-parse %s" % ref).read()[:-1]
112
+ return read_pipe("git rev-parse %s" % ref)[:-1]
113
114
def system(cmd):
115
if os.system(cmd) != 0:
@@ -83,8 +117,10 @@ def system(cmd):
117
118
def extractLogMessageFromGitCommit(commit):
119
logMessage = ""
120
+
121
+ ## fixme: title is first line of commit, not 1st paragraph.
122
foundTitle = False
87
- for log in mypopen("git cat-file commit %s" % commit).readlines():
123
+ for log in read_pipe_lines("git cat-file commit %s" % commit):
124
if not foundTitle:
125
if len(log) == 1:
126
foundTitle = True
@@ -158,10 +194,10 @@ class P4RollBack(Command):
194
195
if self.rollbackLocalBranches:
196
refPrefix = "refs/heads/"
161
- lines = mypopen("git rev-parse --symbolic --branches").readlines()
197
+ lines = read_pipe_lines("git rev-parse --symbolic --branches")
198
else:
199
refPrefix = "refs/remotes/"
164
- lines = mypopen("git rev-parse --symbolic --remotes").readlines()
200
+ lines = read_pipe_lines("git rev-parse --symbolic --remotes")
201
202
for line in lines:
203
if self.rollbackLocalBranches or (line.startswith("p4/") and line != "p4/HEAD\n"):
@@ -230,7 +266,7 @@ class P4Submit(Command):
266
if self.directSubmit:
267
commits.append("0")
268
else:
233
- for line in mypopen("git rev-list --no-merges %s..%s" % (self.origin, self.master)).readlines():
269
+ for line in read_pipe_lines("git rev-list --no-merges %s..%s" % (self.origin, self.master)):
270
commits.append(line[:-1])
271
commits.reverse()
272
@@ -264,8 +300,8 @@ class P4Submit(Command):
300
print "Applying local change in working directory/index"
301
diff = self.diffStatus
302
else:
267
- print "Applying %s" % (mypopen("git log --max-count=1 --pretty=oneline %s" % id).read())
268
- diff = mypopen("git diff-tree -r --name-status \"%s^\" \"%s\"" % (id, id)).readlines()
303
+ print "Applying %s" % (read_pipe("git log --max-count=1 --pretty=oneline %s" % id))
304
+ diff = read_pipe_lines("git diff-tree -r --name-status \"%s^\" \"%s\"" % (id, id))
305
filesToAdd = set()
306
filesToDelete = set()
307
editedFiles = set()
@@ -334,11 +370,11 @@ class P4Submit(Command):
370
logMessage = logMessage.replace("\n", "\n\t")
371
logMessage = logMessage[:-1]
372
337
- template = mypopen("p4 change -o").read()
373
+ template = read_pipe("p4 change -o")
374
375
if self.interactive:
376
submitTemplate = self.prepareLogMessage(template, logMessage)
341
- diff = mypopen("p4 diff -du ...").read()
377
+ diff = read_pipe("p4 diff -du ...")
378
379
for newFile in filesToAdd:
380
diff += "==== new file ====\n"
@@ -387,14 +423,10 @@ class P4Submit(Command):
423
if self.directSubmit:
424
print "Submitting to git first"
425
os.chdir(self.oldWorkingDirectory)
390
- pipe = os.popen("git commit -a -F -", "wb")
391
- pipe.write(submitTemplate)
392
- pipe.close()
426
+ write_pipe("git commit -a -F -", submitTemplate)
427
os.chdir(self.clientPath)
428
395
- pipe = os.popen("p4 submit -i", "wb")
396
- pipe.write(submitTemplate)
397
- pipe.close()
429
+ write_pipe("p4 submit -i", submitTemplate)
430
elif response == "s":
431
for f in editedFiles:
432
system("p4 revert \"%s\"" % f);
@@ -451,11 +483,11 @@ class P4Submit(Command):
483
self.oldWorkingDirectory = os.getcwd()
484
485
if self.directSubmit:
454
- self.diffStatus = mypopen("git diff -r --name-status HEAD").readlines()
486
+ self.diffStatus = read_pipe_lines("git diff -r --name-status HEAD")
487
if len(self.diffStatus) == 0:
488
print "No changes in working directory to submit."
489
return True
458
- patch = mypopen("git diff -p --binary --diff-filter=ACMRTUXB HEAD").read()
490
+ patch = read_pipe("git diff -p --binary --diff-filter=ACMRTUXB HEAD")
491
self.diffFile = gitdir + "/p4-git-diff"
492
f = open(self.diffFile, "wb")
493
f.write(patch)
@@ -557,7 +589,7 @@ class P4Sync(Command):
589
self.syncWithOrigin = False
590
591
def p4File(self, depotPath):
560
- return os.popen("p4 print -q \"%s\"" % depotPath, "rb").read()
592
+ return read_pipe("p4 print -q \"%s\"" % depotPath)
593
594
def extractFilesFromCommit(self, commit):
595
files = []
@@ -799,7 +831,7 @@ class P4Sync(Command):
831
else:
832
cmdline += " --branches"
833
802
- for line in mypopen(cmdline).readlines():
834
+ for line in read_pipe_lines(cmdline):
835
if self.importIntoRemotes and ((not line.startswith("p4/")) or line == "p4/HEAD\n"):
836
continue
837
if self.importIntoRemotes:
@@ -1032,7 +1064,7 @@ class P4Sync(Command):
1064
else:
1065
if self.verbose:
1066
print "Getting p4 changes for %s...%s" % (self.depotPath, self.changeRange)
1035
- output = mypopen("p4 changes %s...%s" % (self.depotPath, self.changeRange)).readlines()
1067
+ output = read_pipe_lines("p4 changes %s...%s" % (self.depotPath, self.changeRange))
1068
1069
for line in output:
1070
changeNum = line.split(" ")[1]
@@ -1141,7 +1173,7 @@ class P4Rebase(Command):
1173
sync = P4Sync()
1174
sync.run([])
1175
print "Rebasing the current branch"
1144
- oldHead = mypopen("git rev-parse HEAD").read()[:-1]
1176
+ oldHead = read_pipe("git rev-parse HEAD")[:-1]
1177
system("git rebase p4")
1178
system("git diff-tree --stat --summary -M %s HEAD" % oldHead)
1179
return True
@@ -1252,9 +1284,9 @@ if cmd.needsGit:
1284
if len(gitdir) == 0:
1285
gitdir = ".git"
1286
if not isValidGitDir(gitdir):
1255
- gitdir = mypopen("git rev-parse --git-dir").read()[:-1]
1287
+ gitdir = read_pipe("git rev-parse --git-dir")[:-1]
1288
if os.path.exists(gitdir):
1257
- cdup = mypopen("git rev-parse --show-cdup").read()[:-1];
1289
+ cdup = read_pipe("git rev-parse --show-cdup")[:-1];
1290
if len(cdup) > 0:
1291
os.chdir(cdup);
1292
@@ -1268,4 +1300,3 @@ if cmd.needsGit:
1300
1301
if not cmd.run(args):
1302
parser.print_help()
1271
-