22namespace Platformsh \Cli \Command \Environment ;
33
44use Platformsh \Cli \Command \CommandBase ;
5- use Platformsh \Cli \Util \OsUtil ;
65use Symfony \Component \Console \Input \InputArgument ;
76use Symfony \Component \Console \Input \InputInterface ;
87use Symfony \Component \Console \Input \InputOption ;
@@ -21,6 +20,7 @@ protected function configure()
2120 ->addOption ('title ' , null , InputOption::VALUE_REQUIRED , 'The title of the new environment ' )
2221 ->addOption ('type ' , null , InputOption::VALUE_REQUIRED , 'The type of the new environment ' )
2322 ->addOption ('no-clone-parent ' , null , InputOption::VALUE_NONE , "Do not clone the parent environment's data " )
23+ ->addOption ('no-checkout ' , null , InputOption::VALUE_NONE , 'Do not check out the branch locally ' )
2424 ->addHiddenOption ('dry-run ' , null , InputOption::VALUE_NONE , 'Dry run: do not create a new environment ' );
2525 $ this ->addProjectOption ()
2626 ->addEnvironmentOption ()
@@ -62,8 +62,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
6262 return 1 ;
6363 }
6464
65+ $ projectRoot = $ this ->getProjectRoot ();
66+ $ dryRun = $ input ->getOption ('dry-run ' );
67+ $ checkoutLocally = $ projectRoot && !$ input ->getOption ('no-checkout ' );
68+
6569 if ($ environment = $ this ->api ()->getEnvironment ($ branchName , $ selectedProject )) {
66- if (!$ this -> getProjectRoot () ) {
70+ if (!$ checkoutLocally || $ dryRun ) {
6771 $ this ->stdErr ->writeln ("The environment <comment> $ branchName</comment> already exists. " );
6872
6973 return 1 ;
@@ -115,10 +119,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
115119 : 'Settings will be copied and data cloned from the parent environment: %s ' ;
116120 $ this ->stdErr ->writeln (sprintf ($ parentMessage , $ this ->api ()->getEnvironmentLabel ($ parentEnvironment , 'info ' , false )));
117121
118- $ dryRun = $ input ->getOption ('dry-run ' );
119122 if ($ dryRun ) {
120123 $ this ->stdErr ->writeln ('' );
121- $ this ->stdErr ->writeln ('<comment>Dry-run mode:</comment> skipping branch operation. ' );
124+ if ($ checkoutLocally ) {
125+ $ this ->stdErr ->writeln ('<comment>Dry-run mode:</comment> skipping branching and local checkout. ' );
126+ $ checkoutLocally = false ;
127+ } else {
128+ $ this ->stdErr ->writeln ('<comment>Dry-run mode:</comment> skipping branch operation. ' );
129+ }
122130
123131 $ activities = [];
124132 } else {
@@ -141,11 +149,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
141149 $ git = $ this ->getService ('git ' );
142150
143151 $ createdNew = false ;
144- $ projectRoot = $ this ->getProjectRoot ();
145- if ($ projectRoot && !$ dryRun ) {
146- // If the Git branch already exists locally, just check it out.
147- $ existsLocally = $ git ->branchExists ($ branchName , $ projectRoot );
148- if ($ existsLocally ) {
152+ if ($ checkoutLocally ) {
153+ if ($ git ->branchExists ($ branchName , $ projectRoot )) {
149154 $ this ->stdErr ->writeln ("Checking out <info> $ branchName</info> locally " );
150155 if (!$ git ->checkOut ($ branchName , $ projectRoot )) {
151156 $ this ->stdErr ->writeln ('Failed to check out branch locally: <error> ' . $ branchName . '</error> ' );
@@ -160,16 +165,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
160165 }
161166 $ createdNew = true ;
162167 }
163- } elseif (!$ projectRoot ) {
164- $ this ->stdErr ->writeln ([
165- '' ,
166- 'This command was run from outside a local project root, so the new branch cannot be checked out automatically. ' ,
167- sprintf (
168- 'To switch to the branch when inside a repository run: <comment>%s checkout %s</comment> ' ,
169- $ this ->config ()->get ('application.executable ' ),
170- OsUtil::escapeShellArg ($ branchName )
171- ),
172- ]);
173168 }
174169
175170 $ remoteSuccess = true ;
@@ -181,7 +176,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
181176 // If a new local branch has been created, set it to track the
182177 // remote branch. This requires first fetching the new branch from
183178 // the remote.
184- if ($ remoteSuccess && $ projectRoot && $ createdNew ) {
179+ if ($ remoteSuccess && $ checkoutLocally && $ createdNew ) {
185180 $ upstreamRemote = $ this ->config ()->get ('detection.git_remote_name ' );
186181 $ git ->fetch ($ upstreamRemote , $ branchName , $ projectRoot );
187182 $ git ->setUpstream ($ upstreamRemote . '/ ' . $ branchName , $ branchName , $ projectRoot );
0 commit comments