Skip to content

Commit a1975c4

Browse files
committed
Merge branch 'ak/p4'
* ak/p4: Utilise our new p4_read_pipe and p4_write_pipe wrappers Add p4 read_pipe and write_pipe wrappers Put in the two other configuration elements found in the source Put some documentation in about the parameters that have been added Move git-p4.syncFromOrigin into a configuration parameters section Consistently use 'git-p4' for the configuration entries If the user has configured various parameters, use them. Switch to using 'p4_build_cmd' If we are in verbose mode, output what we are about to run (or return) Add a single command that will be used to construct the 'p4' command Utilise the new 'p4_system' function. Have a command that specifically invokes 'p4' (via system) Utilise the new 'p4_read_pipe_lines' command Create a specific version of the read_pipe_lines command for p4 invocations Conflicts: contrib/fast-import/git-p4
2 parents 053fd0c + a7d3ef9 commit a1975c4

File tree

2 files changed

+124
-28
lines changed

2 files changed

+124
-28
lines changed

contrib/fast-import/git-p4

Lines changed: 68 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,41 @@ from sets import Set;
1616

1717
verbose = False
1818

19+
20+
def p4_build_cmd(cmd):
21+
"""Build a suitable p4 command line.
22+
23+
This consolidates building and returning a p4 command line into one
24+
location. It means that hooking into the environment, or other configuration
25+
can be done more easily.
26+
"""
27+
real_cmd = "%s " % "p4"
28+
29+
user = gitConfig("git-p4.user")
30+
if len(user) > 0:
31+
real_cmd += "-u %s " % user
32+
33+
password = gitConfig("git-p4.password")
34+
if len(password) > 0:
35+
real_cmd += "-P %s " % password
36+
37+
port = gitConfig("git-p4.port")
38+
if len(port) > 0:
39+
real_cmd += "-p %s " % port
40+
41+
host = gitConfig("git-p4.host")
42+
if len(host) > 0:
43+
real_cmd += "-h %s " % host
44+
45+
client = gitConfig("git-p4.client")
46+
if len(client) > 0:
47+
real_cmd += "-c %s " % client
48+
49+
real_cmd += "%s" % (cmd)
50+
if verbose:
51+
print real_cmd
52+
return real_cmd
53+
1954
def chdir(dir):
2055
if os.name == 'nt':
2156
os.environ['PWD']=dir
@@ -39,6 +74,10 @@ def write_pipe(c, str):
3974

4075
return val
4176

77+
def p4_write_pipe(c, str):
78+
real_cmd = p4_build_cmd(c)
79+
return write_pipe(c, str)
80+
4281
def read_pipe(c, ignore_error=False):
4382
if verbose:
4483
sys.stderr.write('Reading pipe: %s\n' % c)
@@ -50,6 +89,9 @@ def read_pipe(c, ignore_error=False):
5089

5190
return val
5291

92+
def p4_read_pipe(c, ignore_error=False):
93+
real_cmd = p4_build_cmd(c)
94+
return read_pipe(real_cmd, ignore_error)
5395

5496
def read_pipe_lines(c):
5597
if verbose:
@@ -62,12 +104,22 @@ def read_pipe_lines(c):
62104

63105
return val
64106

107+
def p4_read_pipe_lines(c):
108+
"""Specifically invoke p4 on the command supplied. """
109+
real_cmd = p4_build_cmd(c)
110+
return read_pipe_lines(real_cmd)
111+
65112
def system(cmd):
66113
if verbose:
67114
sys.stderr.write("executing %s\n" % cmd)
68115
if os.system(cmd) != 0:
69116
die("command failed: %s" % cmd)
70117

118+
def p4_system(cmd):
119+
"""Specifically invoke p4 as the system command. """
120+
real_cmd = p4_build_cmd(cmd)
121+
return system(real_cmd)
122+
71123
def isP4Exec(kind):
72124
"""Determine if a Perforce 'kind' should have execute permission
73125
@@ -89,12 +141,12 @@ def setP4ExecBit(file, mode):
89141
if p4Type[-1] == "+":
90142
p4Type = p4Type[0:-1]
91143

92-
system("p4 reopen -t %s %s" % (p4Type, file))
144+
p4_system("reopen -t %s %s" % (p4Type, file))
93145

94146
def getP4OpenedType(file):
95147
# Returns the perforce file type for the given file.
96148

97-
result = read_pipe("p4 opened %s" % file)
149+
result = p4_read_pipe("opened %s" % file)
98150
match = re.match(".*\((.+)\)\r?$", result)
99151
if match:
100152
return match.group(1)
@@ -150,7 +202,7 @@ def isModeExecChanged(src_mode, dst_mode):
150202
return isModeExec(src_mode) != isModeExec(dst_mode)
151203

152204
def p4CmdList(cmd, stdin=None, stdin_mode='w+b'):
153-
cmd = "p4 -G %s" % cmd
205+
cmd = p4_build_cmd("-G %s" % (cmd))
154206
if verbose:
155207
sys.stderr.write("Opening pipe: %s\n" % cmd)
156208

@@ -369,7 +421,7 @@ def originP4BranchesExist():
369421

370422
def p4ChangesForPaths(depotPaths, changeRange):
371423
assert depotPaths
372-
output = read_pipe_lines("p4 changes " + ' '.join (["%s...%s" % (p, changeRange)
424+
output = p4_read_pipe_lines("changes " + ' '.join (["%s...%s" % (p, changeRange)
373425
for p in depotPaths]))
374426

375427
changes = []
@@ -517,7 +569,7 @@ class P4Submit(Command):
517569
# remove lines in the Files section that show changes to files outside the depot path we're committing into
518570
template = ""
519571
inFilesSection = False
520-
for line in read_pipe_lines("p4 change -o"):
572+
for line in p4_read_pipe_lines("change -o"):
521573
if line.endswith("\r\n"):
522574
line = line[:-2] + "\n"
523575
if inFilesSection:
@@ -552,7 +604,7 @@ class P4Submit(Command):
552604
modifier = diff['status']
553605
path = diff['src']
554606
if modifier == "M":
555-
system("p4 edit \"%s\"" % path)
607+
p4_system("edit \"%s\"" % path)
556608
if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
557609
filesToChangeExecBit[path] = diff['dst_mode']
558610
editedFiles.add(path)
@@ -567,8 +619,8 @@ class P4Submit(Command):
567619
filesToAdd.remove(path)
568620
elif modifier == "R":
569621
src, dest = diff['src'], diff['dst']
570-
system("p4 integrate -Dt \"%s\" \"%s\"" % (src, dest))
571-
system("p4 edit \"%s\"" % (dest))
622+
p4_system("integrate -Dt \"%s\" \"%s\"" % (src, dest))
623+
p4_system("edit \"%s\"" % (dest))
572624
if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
573625
filesToChangeExecBit[dest] = diff['dst_mode']
574626
os.unlink(dest)
@@ -592,7 +644,7 @@ class P4Submit(Command):
592644
if response == "s":
593645
print "Skipping! Good luck with the next patches..."
594646
for f in editedFiles:
595-
system("p4 revert \"%s\"" % f);
647+
p4_system("revert \"%s\"" % f);
596648
for f in filesToAdd:
597649
system("rm %s" %f)
598650
return
@@ -615,10 +667,10 @@ class P4Submit(Command):
615667
system(applyPatchCmd)
616668

617669
for f in filesToAdd:
618-
system("p4 add \"%s\"" % f)
670+
p4_system("add \"%s\"" % f)
619671
for f in filesToDelete:
620-
system("p4 revert \"%s\"" % f)
621-
system("p4 delete \"%s\"" % f)
672+
p4_system("revert \"%s\"" % f)
673+
p4_system("delete \"%s\"" % f)
622674

623675
# Set/clear executable bits
624676
for f in filesToChangeExecBit.keys():
@@ -634,7 +686,7 @@ class P4Submit(Command):
634686
submitTemplate = self.prepareLogMessage(template, logMessage)
635687
if os.environ.has_key("P4DIFF"):
636688
del(os.environ["P4DIFF"])
637-
diff = read_pipe("p4 diff -du ...")
689+
diff = p4_read_pipe("diff -du ...")
638690

639691
newdiff = ""
640692
for newFile in filesToAdd:
@@ -672,7 +724,7 @@ class P4Submit(Command):
672724
if self.isWindows:
673725
submitTemplate = submitTemplate.replace("\r\n", "\n")
674726

675-
write_pipe("p4 submit -i", submitTemplate)
727+
p4_write_pipe("submit -i", submitTemplate)
676728
else:
677729
fileName = "submit.txt"
678730
file = open(fileName, "w+")
@@ -719,7 +771,7 @@ class P4Submit(Command):
719771

720772
chdir(self.clientPath)
721773
print "Syncronizing p4 checkout..."
722-
system("p4 sync ...")
774+
p4_system("sync ...")
723775

724776
self.check()
725777

@@ -1404,7 +1456,7 @@ class P4Sync(Command):
14041456
if not gitBranchExists(self.refPrefix + "HEAD") and self.importIntoRemotes and gitBranchExists(self.branch):
14051457
system("git symbolic-ref %sHEAD %s" % (self.refPrefix, self.branch))
14061458

1407-
if self.useClientSpec or gitConfig("p4.useclientspec") == "true":
1459+
if self.useClientSpec or gitConfig("git-p4.useclientspec") == "true":
14081460
self.getClientSpec()
14091461

14101462
# TODO: should always look at previous commits,

contrib/fast-import/git-p4.txt

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,6 @@ It is recommended to run 'git repack -a -d -f' from time to time when using
6363
incremental imports to optimally combine the individual git packs that each
6464
incremental import creates through the use of git-fast-import.
6565

66-
67-
A useful setup may be that you have a periodically updated git repository
68-
somewhere that contains a complete import of a Perforce project. That git
69-
repository can be used to clone the working repository from and one would
70-
import from Perforce directly after cloning using git-p4. If the connection to
71-
the Perforce server is slow and the working repository hasn't been synced for a
72-
while it may be desirable to fetch changes from the origin git repository using
73-
the efficient git protocol. git-p4 supports this setup by calling "git fetch origin"
74-
by default if there is an origin branch. You can disable this using
75-
76-
git config git-p4.syncFromOrigin false
77-
7866
Updating
7967
========
8068

@@ -140,6 +128,62 @@ Example
140128
git-p4 rebase
141129

142130

131+
Configuration parameters
132+
========================
133+
134+
git-p4.user ($P4USER)
135+
136+
Allows you to specify the username to use to connect to the Perforce repository.
137+
138+
git config [--global] git-p4.user public
139+
140+
git-p4.password ($P4PASS)
141+
142+
Allows you to specify the password to use to connect to the Perforce repository.
143+
Warning this password will be visible on the command-line invocation of the p4 binary.
144+
145+
git config [--global] git-p4.password public1234
146+
147+
git-p4.port ($P4PORT)
148+
149+
Specify the port to be used to contact the Perforce server. As this will be passed
150+
directly to the p4 binary, it may be in the format host:port as well.
151+
152+
git config [--global] git-p4.port codes.zimbra.com:2666
153+
154+
git-p4.host ($P4HOST)
155+
156+
Specify the host to contact for a Perforce repository.
157+
158+
git config [--global] git-p4.host perforce.example.com
159+
160+
git-p4.client ($P4CLIENT)
161+
162+
Specify the client name to use
163+
164+
git config [--global] git-p4.client public-view
165+
166+
git-p4.allowSubmit
167+
168+
git config [--global] git-p4.allowSubmit false
169+
170+
git-p4.syncFromOrigin
171+
172+
A useful setup may be that you have a periodically updated git repository
173+
somewhere that contains a complete import of a Perforce project. That git
174+
repository can be used to clone the working repository from and one would
175+
import from Perforce directly after cloning using git-p4. If the connection to
176+
the Perforce server is slow and the working repository hasn't been synced for a
177+
while it may be desirable to fetch changes from the origin git repository using
178+
the efficient git protocol. git-p4 supports this setup by calling "git fetch origin"
179+
by default if there is an origin branch. You can disable this using:
180+
181+
git config [--global] git-p4.syncFromOrigin false
182+
183+
git-p4.useclientspec
184+
185+
git config [--global] git-p4.useclientspec false
186+
143187
Implementation Details...
144188
=========================
145189

0 commit comments

Comments
 (0)