// ĠCopyright Ġ 2 0 0 0 - 2 0 1 9 ĠJetBrains Ġs . r . o . ĠUse Ġof Ġthis Ġsource Ġcode Ġis Ġgoverned Ġby Ġthe ĠApache Ġ 2 . 0 Ġlicense Ġthat Ġcan Ġbe Ġfound Ġin Ġthe ĠLICENSE Ġfile . Ċ @ file : Jvm Name (" Git Executor ") Ċ Ċ package Ġgit 4 idea . test Ċ Ċ import Ġcom . intellij . openapi . project . Project Ċ import Ġcom . intellij . openapi . util . io . File Util Ċ import Ġcom . intellij . openapi . util . text . StringUtil Ċ import Ġcom . intellij . openapi . vcs . Executor .* Ċ import Ġcom . intellij . openapi . vfs . V fs Util Ċ import Ġcom . intellij . test Framework . vcs . Executable Helper Ċ import Ġcom . intellij . vcs . log . util . V cs Log Util Ċ import Ġgit 4 idea . commands . Git Ċ import Ġgit 4 idea . commands . Git Binary Handler Ċ import Ġgit 4 idea . commands . Git Line Handler Ċ import Ġgit 4 idea . commands . get Git Command Instance Ċ import Ġgit 4 idea . repo . Git Repository Ċ import Ġorg . junit . Assert . assertFalse Ċ import Ġorg . junit . Assert . assertTrue Ċ import Ġjava . io . File Ċ Ċ fun Ġgit Executable () Ġ= ĠGit Executor Holder . Path Holder . GIT _ EXECUT ABLE Ċ Ċ @ Jvm Over loads Ċ fun ĠGit Repository . git ( command : ĠString , Ġignore Non Zero ExitCode : ĠBoolean Ġ= Ġfalse ) Ġ= Ġcd Ġ{ Ġgit ( project , Ġcommand , Ġignore Non Zero ExitCode ) Ġ} Ċ fun ĠGit Platform Test . git ( command : ĠString , Ġignore Non Zero ExitCode : ĠBoolean Ġ= Ġfalse ) Ġ= Ġgit ( project , Ġcommand , Ġignore Non Zero ExitCode ) Ċ @ Jvm Over loads Ċ fun Ġgit ( project : ĠProject , Ġcommand : ĠString , Ġignore Non Zero ExitCode : ĠBoolean Ġ= Ġfalse ): ĠString Ġ{ ĊĠ Ġval Ġworking Dir Ġ= Ġour Current Dir () ĊĠ Ġval Ġsplit Ġ= Ġsplit Command In Parameters ( command ) ĊĠ Ġval Ġhandler Ġ= ĠGit Line Handler ( project , Ġworking Dir , Ġget Git Command Instance ( split [ 0 ])) ĊĠ Ġhandler . set With Mediator ( false ) ĊĠ Ġhandler . add Parameters ( split . sub List ( 1 , Ġsplit . size )) ĊĊĠ Ġval Ġresult Ġ= ĠGit . getInstance (). run Command ( handler ) ĊĠ Ġif Ġ( result . exit Code Ġ!= Ġ 0 Ġ&& Ġ! ignore Non Zero ExitCode ) Ġ{ ĊĠĠĠ Ġthrow ĠIllegalStateException (" Command Ġ[$ command ] Ġfailed Ġwith Ġexit Ġcode Ġ${ result . exit Code }\ n ${ result . output }\ n ${ result . error Output }") ĊĠ Ġ} ĊĠ Ġreturn Ġresult . error Output As Joined String Ġ+ Ġresult . output As Joined String Ċ } Ċ Ċ fun ĠGit Repository . git As Bytes ( command : ĠString ): ĠByteArray Ġ{ ĊĠ Ġcd ( this ) ĊĠ Ġreturn Ġgit As Bytes ( project , Ġcommand ) Ċ } Ċ fun Ġgit As Bytes ( project : ĠProject , Ġcommand : ĠString ): ĠByteArray Ġ{ ĊĠ Ġval Ġworking Dir Ġ= ĠV fs Util . find File By Io File ( our Current Dir (), Ġtrue ) !! ĊĠ Ġval Ġsplit Ġ= Ġsplit Command In Parameters ( command ) ĊĠ Ġval Ġhandler Ġ= ĠGit Binary Handler ( project , Ġworking Dir , Ġget Git Command Instance ( split [ 0 ])) ĊĠ Ġhandler . add Parameters ( split . sub List ( 1 , Ġsplit . size )) ĊĠ Ġreturn Ġhandler . run () Ċ } Ċ Ċ fun Ġcd ( repository : ĠGit Repository ) Ġ= Ġcd ( repository . root . path ) Ċ Ċ @ Jvm Over loads Ċ fun ĠGit Repository . add ( path : ĠString Ġ= Ġ". ") Ġ= Ġcd Ġ{ Ġadd ( project , Ġpath ) Ġ} Ċ Ċ fun ĠGit Platform Test . add ( path : ĠString Ġ= Ġ". ") Ġ= Ġadd ( project , Ġpath ) Ċ private Ġfun Ġadd ( project : ĠProject , Ġpath : ĠString Ġ= Ġ". ") Ġ= Ġgit ( project , Ġ" add Ġ-- verbose Ġ" Ġ+ Ġpath ) Ċ Ċ fun ĠGit Repository . add Commit ( message : ĠString ) Ġ= Ġcd Ġ{ Ġadd Commit ( project , Ġmessage ) Ġ} Ċ fun ĠGit Platform Test . add Commit ( message : ĠString ) Ġ= Ġadd Commit ( project , Ġmessage ) Ċ private Ġfun Ġadd Commit ( project : ĠProject , Ġmessage : ĠString ): ĠString Ġ{ ĊĠ Ġadd ( project ) ĊĠ Ġreturn Ġcommit ( project , Ġmessage ) Ċ } Ċ Ċ fun ĠGit Repository . branch ( name : ĠString ) Ġ= Ġcd Ġ{ Ġbranch ( project , Ġname ) Ġ} Ċ fun ĠGit Platform Test . branch ( name : ĠString ) Ġ= Ġbranch ( project , Ġname ) Ċ private Ġfun Ġbranch ( project : ĠProject , Ġname : ĠString ) Ġ= Ġgit ( project , Ġ" branch Ġ$ name ") Ċ Ċ fun ĠGit Repository . checkout ( var arg Ġparams : ĠString ) Ġ= Ġcd Ġ{ Ġcheckout ( project , Ġ* params ) Ġ} Ċ fun ĠGit Platform Test . checkout ( var arg Ġparams : ĠString ) Ġ= Ġcheckout ( project , Ġ* params ) Ċ private Ġfun Ġcheckout ( project : ĠProject , Ġvar arg Ġparams : ĠString ) Ġ= Ġgit ( project , Ġ" checkout Ġ${ params . join ToString (" Ġ" )} ") Ċ Ċ fun ĠGit Repository . checkout New ( branch Name : ĠString , Ġstart Point : ĠString Ġ= Ġ"") Ġ= Ġcd Ġ{ Ġcheckout New ( project , Ġbranch Name , Ġstart Point ) Ġ} Ċ private Ġfun Ġcheckout New ( project : ĠProject , Ġbranch Name : ĠString , Ġstart Point : ĠString ) Ġ= ĊĠ Ġgit ( project , Ġ" checkout Ġ- b Ġ$ branch Name Ġ$ start Point ") Ċ Ċ fun ĠGit Repository . commit ( message : ĠString ) Ġ= Ġcd Ġ{ Ġcommit ( project , Ġmessage ) Ġ} Ċ fun ĠGit Platform Test . commit ( message : ĠString ) Ġ= Ġcommit ( project , Ġmessage ) Ċ private Ġfun Ġcommit ( project : ĠProject , Ġmessage : ĠString ): ĠString Ġ{ ĊĠ Ġgit ( project , Ġ" commit Ġ- m Ġ'$ message '") ĊĠ Ġreturn Ġlast ( project ) Ċ } Ċ Ċ @ Jvm Over loads Ċ fun ĠGit Repository . tac ( file : ĠString , Ġcontent : ĠString Ġ= Ġ" content " Ġ+ ĠMath . random ()) Ġ= Ġcd Ġ{ Ġt ac ( project , Ġfile , Ġcontent ) Ġ} Ċ Ċ fun ĠGit Platform Test . tac ( file : ĠString , Ġcontent : ĠString Ġ= Ġ" content " Ġ+ ĠMath . random ()) Ġ= Ġt ac ( project , Ġfile , Ġcontent ) Ċ private Ġfun Ġt ac ( project : ĠProject , Ġfile : ĠString , Ġcontent : ĠString ): ĠString Ġ{ ĊĠ Ġtouch ( file , Ġcontent ) ĊĠ Ġreturn Ġadd Commit ( project , Ġ" Tou ched Ġ$ file ") Ċ } Ċ Ċ fun ĠGit Repository . tac p ( file : ĠString ) Ġ= Ġcd Ġ{ Ġt ac p ( project , Ġfile ) Ġ} Ċ fun ĠGit Platform Test . tac p ( file : ĠString ) Ġ= Ġt ac p ( project , Ġfile ) Ċ private Ġfun Ġt ac p ( project : ĠProject , Ġfile : ĠString ): ĠString Ġ{ ĊĠ Ġtouch ( file ) ĊĠ Ġadd Commit ( project , Ġ" Tou ched Ġ$ file ") ĊĠ Ġreturn Ġgit ( project , Ġ" push ") Ċ } Ċ Ċ fun ĠGit Repository . append And Commit ( file : ĠString , Ġadditional Content : ĠString ) Ġ= Ġcd Ġ{ Ġappend And Commit ( project , Ġfile , Ġadditional Content ) Ġ} Ċ private Ġfun Ġappend And Commit ( project : ĠProject , Ġfile : ĠString , Ġadditional Content : ĠString ): ĠString Ġ{ ĊĠ Ġappend ( file , Ġadditional Content ) ĊĠ Ġadd ( project , Ġfile ) ĊĠ Ġreturn Ġcommit ( project , Ġ" Add Ġmore Ġcontent ") Ċ } Ċ Ċ fun ĠGit Repository . modify ( file : ĠString ): ĠString Ġ= Ġcd Ġ{ Ġmodify ( project , Ġfile ) Ġ} Ċ fun ĠGit Platform Test . modify ( file : ĠString ): ĠString Ġ= Ġmodify ( project , Ġfile ) Ċ private Ġfun Ġmodify ( project : ĠProject , Ġfile : ĠString ): ĠString Ġ{ ĊĠ Ġoverwrite ( file , Ġ" content " Ġ+ ĠMath . random ()) ĊĠ Ġreturn Ġadd Commit ( project , Ġ" modified Ġ" Ġ+ Ġfile ) Ċ } Ċ Ċ fun ĠGit Repository . last () Ġ= Ġcd Ġ{ Ġlast ( project ) Ġ} Ċ fun ĠGit Platform Test . last () Ġ= Ġlast ( project ) Ċ private Ġfun Ġlast ( project : ĠProject ) Ġ= Ġgit ( project , Ġ" log Ġ- 1 Ġ-- pretty =% H ") Ċ Ċ fun ĠGit Repository . last Message () Ġ= Ġcd Ġ{ Ġlast Message ( project ) Ġ} Ċ fun ĠGit Platform Test . last Message () Ġ= Ġlast Message ( project ) Ċ private Ġfun Ġlast Message ( project : ĠProject ) Ġ= Ġmessage ( project , Ġ" HEAD ") Ċ Ċ fun ĠGit Repository . last Author Time () Ġ= Ġcd Ġ{ Ġlast Author Time ( project ) Ġ} Ċ fun ĠGit Platform Test . last Author Time () Ġ= Ġlast Author Time ( project ) Ċ private Ġfun Ġlast Author Time ( project : ĠProject ) Ġ= Ġgit ( project , Ġ" log Ġ- 1 Ġ-- pretty =% at ") Ċ Ċ fun ĠGit Repository . message ( revision : ĠString ) Ġ= Ġcd Ġ{ Ġmessage ( project , Ġrevision )} Ċ private Ġfun Ġmessage ( project : ĠProject , Ġrevision : ĠString ) Ġ= ĊĠ Ġgit ( project , Ġ" log Ġ$ revision Ġ-- no - walk Ġ-- pretty =${ get Pretty Format Tag For Full Commit Message ( project )} ") Ċ Ċ fun ĠGit Repository . log ( var arg Ġparams : ĠString ) Ġ= Ġcd Ġ{ Ġlog ( project , Ġ* params ) Ġ} Ċ fun ĠGit Platform Test . log ( var arg Ġparams : ĠString ) Ġ= Ġlog ( project , Ġ* params ) Ċ private Ġfun Ġlog ( project : ĠProject , Ġvar arg Ġparams : ĠString ) Ġ= Ġgit ( project , Ġ" log Ġ" Ġ+ ĠString Util . join ( params , Ġ" Ġ")) Ċ Ċ fun ĠGit Repository . mv ( from Path : ĠString , Ġto Path : ĠString ) Ġ= Ġcd Ġ{ Ġmv ( project , Ġfrom Path , Ġto Path ) Ġ} Ċ fun ĠGit Platform Test . mv ( from Path : ĠString , Ġto Path : ĠString ) Ġ= Ġmv ( project , Ġfrom Path , Ġto Path ) Ċ private Ġfun Ġmv ( project : ĠProject , Ġfrom Path : ĠString , Ġto Path : ĠString ) Ġ= Ġgit ( project , Ġ" mv Ġ$ from Path Ġ$ to Path ") Ċ Ċ fun ĠGit Repository . mv ( from : ĠFile , Ġto : ĠFile ) Ġ{ ĊĠ Ġmv ( from . path , Ġto . path ) Ċ } Ċ Ċ fun ĠGit Repository . prepare Conflict ( initial Branch : ĠString Ġ= Ġ" master ", ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġfeature Branch : ĠString Ġ= Ġ" feature ", ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġconflicting File : ĠString Ġ= Ġ" c . txt "): ĠString Ġ{ ĊĠ Ġcheckout ( initial Branch ) ĊĠ Ġval Ġfile Ġ= Ġfile ( conflict ing File ) ĊĠ Ġfile . create (" initial \ n "). add Commit (" initial ") ĊĠ Ġbranch ( feature Branch ) ĊĠ Ġval Ġcommit Ġ= Ġfile . append (" master \ n "). add Commit (" on _ master "). hash () ĊĠ Ġcheckout ( feature Branch ) ĊĠ Ġfile . append (" feature \ n "). add Commit (" on _ feature ") ĊĠ Ġreturn Ġcommit Ċ } Ċ Ċ private Ġfun ĠGit Repository . cd ( command : Ġ() Ġ-> ĠString ): ĠString Ġ{ ĊĠ Ġcd ( this ) ĊĠ Ġreturn Ġcommand () Ċ } Ċ Ċ internal Ġfun ĠGit Repository . file ( fileName : ĠString ): ĠTest File Ġ{ ĊĠ Ġval Ġf Ġ= Ġchild ( fileName ) ĊĠ Ġreturn ĠTest File ( this , Ġf ) Ċ } Ċ Ċ private Ġclass ĠGit Executor Holder Ġ{ ĊĠ Ġ// using Ġinner Ġclass Ġto Ġavoid Ġextra Ġwork Ġduring Ġclass Ġloading Ġof Ġunrelated Ġtests ĊĠ Ġinternal Ġobject ĠPath Holder Ġ{ ĊĠĠĠ Ġinternal Ġval ĠGIT _ EXECUT ABLE Ġ= ĠExec utable Helper . find Git Executable () !! ĊĠ Ġ} Ċ } Ċ Ċ internal Ġclass ĠTest File Ġinternal Ġconstructor ( val Ġrepo : ĠGit Repository , Ġval Ġfile : ĠFile ) Ġ{ ĊĊĠ Ġfun Ġappend ( content : ĠString ): ĠTest File Ġ{ ĊĠĠĠ ĠFile Util . write ToFile ( file , Ġcontent . toByteArray (), Ġtrue ) ĊĠĠĠ Ġreturn Ġthis ĊĠ Ġ} ĊĊĠ Ġfun Ġwrite ( content : ĠString ): ĠTest File Ġ{ ĊĠĠĠ ĠFile Util . write ToFile ( file , Ġcontent . toByteArray (), Ġfalse ) ĊĠĠĠ Ġreturn Ġthis ĊĠ Ġ} ĊĊĠ Ġfun Ġcreate ( content : ĠString Ġ= Ġ"") : ĠTest File Ġ{ ĊĠĠĠ Ġassert Not Exists () ĊĠĠĠ ĠFile Util . write ToFile ( file , Ġcontent . toByteArray (), Ġfalse ) ĊĠĠĠ Ġreturn Ġthis ĊĠ Ġ} ĊĊĠ Ġfun Ġdelete (): ĠTest File Ġ{ ĊĠĠĠ ĠassertTrue ( file . exists ()) ĊĠĠĠ ĠFile Util . delete ( file ) ĊĠĠĠ Ġreturn Ġthis ĊĠ Ġ} ĊĊĠ Ġfun Ġassert Not Exists (): ĠTest File Ġ{ ĊĠĠĠ ĠassertFalse ( file . exists ()) ĊĠĠĠ Ġreturn Ġthis ĊĠ Ġ} ĊĊĠ Ġfun Ġadd (): ĠTest File Ġ{ ĊĠĠĠ Ġrepo . add ( file . path ) ĊĠĠĠ Ġreturn Ġthis ĊĠ Ġ} ĊĊĠ Ġfun Ġadd Commit ( message : ĠString ): ĠTest File Ġ{ ĊĠĠĠ Ġadd () ĊĠĠĠ Ġrepo . commit ( message ) ĊĠĠĠ Ġreturn Ġthis ĊĠ Ġ} ĊĊĠ Ġfun Ġhash () Ġ= Ġrepo . last () ĊĊĠ Ġfun Ġdetails () Ġ= ĠV cs Log Util . get Details ( find Git Log Provider ( repo . project ), Ġrepo . root , ĠlistOf ( hash ())). first () !! ĊĊĠ Ġfun Ġexists () Ġ= Ġfile . exists () ĊĊĠ Ġfun Ġread () Ġ= ĠFile Util . load File ( file ) ĊĊĠ Ġfun Ġcat (): ĠString Ġ= ĠFile Util . load File ( file ) ĊĊĠ Ġfun Ġprepend ( content : ĠString ): ĠTest File Ġ{ ĊĠĠĠ Ġval Ġprevious Content Ġ= Ġcat () ĊĠĠĠ ĠFile Util . write ToFile ( file , Ġcontent Ġ+ Ġprevious Content ) ĊĠĠĠ Ġreturn Ġthis ĊĠ Ġ} ĊĊĠ Ġoverride Ġfun ĠtoString (): ĠString Ġ{ ĊĠĠĠ Ġreturn Ġfile . toString () ĊĠ Ġ} Ċ } Ċ