@@ -596,6 +596,46 @@ def p4PathStartsWith(path, prefix):
596596 return path .lower ().startswith (prefix .lower ())
597597 return path .startswith (prefix )
598598
599+ def getClientSpec ():
600+ """Look at the p4 client spec, create a View() object that contains
601+ all the mappings, and return it."""
602+
603+ specList = p4CmdList ("client -o" )
604+ if len (specList ) != 1 :
605+ die ('Output from "client -o" is %d lines, expecting 1' %
606+ len (specList ))
607+
608+ # dictionary of all client parameters
609+ entry = specList [0 ]
610+
611+ # just the keys that start with "View"
612+ view_keys = [ k for k in entry .keys () if k .startswith ("View" ) ]
613+
614+ # hold this new View
615+ view = View ()
616+
617+ # append the lines, in order, to the view
618+ for view_num in range (len (view_keys )):
619+ k = "View%d" % view_num
620+ if k not in view_keys :
621+ die ("Expected view key %s missing" % k )
622+ view .append (entry [k ])
623+
624+ return view
625+
626+ def getClientRoot ():
627+ """Grab the client directory."""
628+
629+ output = p4CmdList ("client -o" )
630+ if len (output ) != 1 :
631+ die ('Output from "client -o" is %d lines, expecting 1' % len (output ))
632+
633+ entry = output [0 ]
634+ if "Root" not in entry :
635+ die ('Client has no "Root"' )
636+
637+ return entry ["Root" ]
638+
599639class Command :
600640 def __init__ (self ):
601641 self .usage = "usage: %prog [options]"
@@ -1220,11 +1260,20 @@ class P4Submit(Command, P4UserMap):
12201260 print "Internal error: cannot locate perforce depot path from existing branches"
12211261 sys .exit (128 )
12221262
1223- self .clientPath = p4Where (self .depotPath )
1263+ self .useClientSpec = False
1264+ if gitConfig ("git-p4.useclientspec" , "--bool" ) == "true" :
1265+ self .useClientSpec = True
1266+ if self .useClientSpec :
1267+ self .clientSpecDirs = getClientSpec ()
12241268
1225- if len (self .clientPath ) == 0 :
1226- print "Error: Cannot locate perforce checkout of %s in client view" % self .depotPath
1227- sys .exit (128 )
1269+ if self .useClientSpec :
1270+ # all files are relative to the client spec
1271+ self .clientPath = getClientRoot ()
1272+ else :
1273+ self .clientPath = p4Where (self .depotPath )
1274+
1275+ if self .clientPath == "" :
1276+ die ("Error: Cannot locate perforce checkout of %s in client view" % self .depotPath )
12281277
12291278 print "Perforce checkout for depot path %s located at %s" % (self .depotPath , self .clientPath )
12301279 self .oldWorkingDirectory = os .getcwd ()
@@ -1530,6 +1579,7 @@ class P4Sync(Command, P4UserMap):
15301579 self .p4BranchesInGit = []
15311580 self .cloneExclude = []
15321581 self .useClientSpec = False
1582+ self .useClientSpec_from_options = False
15331583 self .clientSpecDirs = None
15341584 self .tempBranches = []
15351585 self .tempBranchLocation = "git-p4-tmp"
@@ -2223,33 +2273,6 @@ class P4Sync(Command, P4UserMap):
22232273 print self .gitError .read ()
22242274
22252275
2226- def getClientSpec (self ):
2227- specList = p4CmdList ("client -o" )
2228- if len (specList ) != 1 :
2229- die ('Output from "client -o" is %d lines, expecting 1' %
2230- len (specList ))
2231-
2232- # dictionary of all client parameters
2233- entry = specList [0 ]
2234-
2235- # just the keys that start with "View"
2236- view_keys = [ k for k in entry .keys () if k .startswith ("View" ) ]
2237-
2238- # hold this new View
2239- view = View ()
2240-
2241- # append the lines, in order, to the view
2242- for view_num in range (len (view_keys )):
2243- k = "View%d" % view_num
2244- if k not in view_keys :
2245- die ("Expected view key %s missing" % k )
2246- view .append (entry [k ])
2247-
2248- self .clientSpecDirs = view
2249- if self .verbose :
2250- for i , m in enumerate (self .clientSpecDirs .mappings ):
2251- print "clientSpecDirs %d: %s" % (i , str (m ))
2252-
22532276 def run (self , args ):
22542277 self .depotPaths = []
22552278 self .changeRange = ""
@@ -2282,11 +2305,15 @@ class P4Sync(Command, P4UserMap):
22822305 if not gitBranchExists (self .refPrefix + "HEAD" ) and self .importIntoRemotes and gitBranchExists (self .branch ):
22832306 system ("git symbolic-ref %sHEAD %s" % (self .refPrefix , self .branch ))
22842307
2285- if not self .useClientSpec :
2308+ # accept either the command-line option, or the configuration variable
2309+ if self .useClientSpec :
2310+ # will use this after clone to set the variable
2311+ self .useClientSpec_from_options = True
2312+ else :
22862313 if gitConfig ("git-p4.useclientspec" , "--bool" ) == "true" :
22872314 self .useClientSpec = True
22882315 if self .useClientSpec :
2289- self .getClientSpec ()
2316+ self .clientSpecDirs = getClientSpec ()
22902317
22912318 # TODO: should always look at previous commits,
22922319 # merge with previous imports, if possible.
@@ -2607,6 +2634,10 @@ class P4Clone(P4Sync):
26072634 else :
26082635 print "Could not detect main branch. No checkout/master branch created."
26092636
2637+ # auto-set this variable if invoked with --use-client-spec
2638+ if self .useClientSpec_from_options :
2639+ system ("git config --bool git-p4.useclientspec true" )
2640+
26102641 return True
26112642
26122643class P4Branches (Command ):
0 commit comments