@@ -31,80 +31,82 @@ public class SCMManipulator {
3131
3232 private static final Logger LOGGER = Logger .getLogger (SCMManipulator .class .getName ());
3333
34- private ScmManager scmManager ;
35- private ScmRepository scmRepository = null ;
36- private String scmSpecificFilename = null ;
37-
38- public SCMManipulator (ScmManager _scmManager ) {
39- this .scmManager = _scmManager ;
40- }
41-
42- /**
43- * Will check if everything is settled up (useful before a scm manipulation)
44- * @param scmContext
45- * @param resetScmRepository
46- * @return
47- */
48- public boolean scmConfigurationSettledUp (ScmContext scmContext , boolean resetScmRepository ){
49- String scmRepositoryUrl = scmContext .getScmRepositoryUrl ();
50- SCM scm = scmContext .getScm ();
51- if (scmRepositoryUrl == null || scm == null ){
52- return false ;
53- }
54-
55- if (resetScmRepository ){
56- LOGGER .info ("Creating scmRepository connection data .." );
57- this .scmRepository = scm .getConfiguredRepository (this .scmManager , scmRepositoryUrl );
58- try {
59- this .scmSpecificFilename = this .scmManager .getProviderByRepository (this .scmRepository ).getScmSpecificFilename ();
60- }
61- catch (NoSuchScmProviderException e ) {
62- LOGGER .throwing (ScmManager .class .getName (), "getScmSpecificFilename" , e );
63- LOGGER .severe ("[getScmSpecificFilename] Error while getScmSpecificFilename : " +e .getMessage ());
64- return false ;
65- }
66- }
67-
68- return expectScmRepositoryInitiated ();
69- }
70-
71- private boolean expectScmRepositoryInitiated (){
72- boolean scmRepositoryInitiated = this .scmRepository != null ;
73- if (!scmRepositoryInitiated ) LOGGER .warning ("SCM Repository has not yet been initiated !" );
74- return scmRepositoryInitiated ;
75- }
76-
77- public UpdateScmResult update (File root ) throws ScmException {
78- return this .scmManager .update (scmRepository , new ScmFileSet (root ));
79- }
80- public boolean checkout (File checkoutDirectory ){
81- boolean checkoutOk = false ;
82-
83- if (!expectScmRepositoryInitiated ()){
84- return checkoutOk ;
85- }
86-
87- // Checkouting sources
88- LOGGER .fine ("Checking out SCM files into [" +checkoutDirectory .getAbsolutePath ()+"] ..." );
89- try {
90- CheckOutScmResult result = scmManager .checkOut (this .scmRepository , new ScmFileSet (checkoutDirectory ));
91- if (!result .isSuccess ()){
92- LOGGER .severe ("[checkout] Error during checkout : " +result .getProviderMessage ()+" || " +result .getCommandOutput ());
93- return checkoutOk ;
94- }
95- checkoutOk = true ;
96- } catch (ScmException e ) {
97- LOGGER .throwing (ScmManager .class .getName (), "checkOut" , e );
98- LOGGER .severe ("[checkout] Error during checkout : " +e .getMessage ());
99- return checkoutOk ;
100- }
101-
102- if (checkoutOk ){
103- LOGGER .fine ("Checked out SCM files into [" +checkoutDirectory .getAbsolutePath ()+"] !" );
104- }
105-
106- return checkoutOk ;
107- }
34+ private final ScmManager scmManager ;
35+ private ScmRepository scmRepository = null ;
36+ private String scmSpecificFilename = null ;
37+
38+ public SCMManipulator (ScmManager _scmManager ) {
39+ this .scmManager = _scmManager ;
40+ }
41+
42+ /**
43+ * Will check if everything is settled up (useful before a scm manipulation)
44+ * @param scmContext
45+ * @param resetScmRepository
46+ * @return
47+ */
48+ public boolean scmConfigurationSettledUp (ScmContext scmContext , boolean resetScmRepository ){
49+ String scmRepositoryUrl = scmContext .getScmRepositoryUrl ();
50+ SCM scm = scmContext .getScm ();
51+ if (scmRepositoryUrl == null || scm == null ){
52+ return false ;
53+ }
54+
55+ if (resetScmRepository ){
56+ LOGGER .info ("Creating scmRepository connection data .." );
57+ this .scmRepository = scm .getConfiguredRepository (this .scmManager , scmRepositoryUrl );
58+ try {
59+ this .scmSpecificFilename = this .scmManager .getProviderByRepository (this .scmRepository ).getScmSpecificFilename ();
60+ }
61+ catch (NoSuchScmProviderException e ) {
62+ LOGGER .throwing (ScmManager .class .getName (), "getScmSpecificFilename" , e );
63+ LOGGER .severe ("[getScmSpecificFilename] Error while getScmSpecificFilename : " +e .getMessage ());
64+ return false ;
65+ }
66+ }
67+
68+ return expectScmRepositoryInitiated ();
69+ }
70+
71+ private boolean expectScmRepositoryInitiated (){
72+ boolean scmRepositoryInitiated = this .scmRepository != null ;
73+ if (!scmRepositoryInitiated ) {
74+ LOGGER .warning ("SCM Repository has not yet been initiated !" );
75+ }
76+ return scmRepositoryInitiated ;
77+ }
78+
79+ public UpdateScmResult update (File root ) throws ScmException {
80+ return this .scmManager .update (scmRepository , new ScmFileSet (root ));
81+ }
82+ public boolean checkout (File checkoutDirectory ){
83+ boolean checkoutOk = false ;
84+
85+ if (!expectScmRepositoryInitiated ()){
86+ return checkoutOk ;
87+ }
88+
89+ // Checkouting sources
90+ LOGGER .fine ("Checking out SCM files into [" +checkoutDirectory .getAbsolutePath ()+"] ..." );
91+ try {
92+ CheckOutScmResult result = scmManager .checkOut (this .scmRepository , new ScmFileSet (checkoutDirectory ));
93+ if (!result .isSuccess ()){
94+ LOGGER .severe ("[checkout] Error during checkout : " +result .getProviderMessage ()+" || " +result .getCommandOutput ());
95+ return checkoutOk ;
96+ }
97+ checkoutOk = true ;
98+ } catch (ScmException e ) {
99+ LOGGER .throwing (ScmManager .class .getName (), "checkOut" , e );
100+ LOGGER .severe ("[checkout] Error during checkout : " +e .getMessage ());
101+ return checkoutOk ;
102+ }
103+
104+ if (checkoutOk ){
105+ LOGGER .fine ("Checked out SCM files into [" +checkoutDirectory .getAbsolutePath ()+"] !" );
106+ }
107+
108+ return checkoutOk ;
109+ }
108110
109111 public List <File > deleteHierarchy (File hierarchyToDelete ){
110112 if (!expectScmRepositoryInitiated ()){
@@ -140,32 +142,32 @@ public List<File> deleteHierarchy(File hierarchyToDelete){
140142 }
141143 }
142144
143- public List <File > addFile (File scmRoot , String filePathRelativeToScmRoot ){
144- List <File > synchronizedFiles = new ArrayList <File >();
145-
146- if (!expectScmRepositoryInitiated ()){
147- return synchronizedFiles ;
148- }
149-
150- LOGGER .fine ("Adding SCM file [" +filePathRelativeToScmRoot +"] ..." );
151-
152- try {
153- // Split every directory leading through modifiedFilePathRelativeToHudsonRoot
154- // and try add it in the scm
155- String [] pathChunks = filePathRelativeToScmRoot .split ("\\ \\ |/" );
156- StringBuilder currentPath = new StringBuilder ();
157- for (int i =0 ; i <pathChunks .length ; i ++){
158- currentPath .append (pathChunks [i ]);
159- if (i != pathChunks .length -1 ){
160- currentPath .append (File .separator );
161- }
162- File currentFile = new File (currentPath .toString ());
163-
164- // Trying to add current path to the scm ...
165- AddScmResult addResult = this .scmManager .add (this .scmRepository , new ScmFileSet (scmRoot , currentFile ));
166- // If current has not yet been synchronized, addResult.isSuccess() should be true
167- if (addResult .isSuccess ()){
168- synchronizedFiles .addAll (refineUpdatedFilesInScmResult (addResult .getAddedFiles ()));
145+ public List <File > addFile (File scmRoot , String filePathRelativeToScmRoot ){
146+ List <File > synchronizedFiles = new ArrayList <File >();
147+
148+ if (!expectScmRepositoryInitiated ()){
149+ return synchronizedFiles ;
150+ }
151+
152+ LOGGER .fine ("Adding SCM file [" +filePathRelativeToScmRoot +"] ..." );
153+
154+ try {
155+ // Split every directory leading through modifiedFilePathRelativeToHudsonRoot
156+ // and try add it in the scm
157+ String [] pathChunks = filePathRelativeToScmRoot .split ("\\ \\ |/" );
158+ StringBuilder currentPath = new StringBuilder ();
159+ for (int i =0 ; i <pathChunks .length ; i ++){
160+ currentPath .append (pathChunks [i ]);
161+ if (i != pathChunks .length -1 ){
162+ currentPath .append (File .separator );
163+ }
164+ File currentFile = new File (currentPath .toString ());
165+
166+ // Trying to add current path to the scm ...
167+ AddScmResult addResult = this .scmManager .add (this .scmRepository , new ScmFileSet (scmRoot , currentFile ));
168+ // If current has not yet been synchronized, addResult.isSuccess() should be true
169+ if (addResult .isSuccess ()){
170+ synchronizedFiles .addAll (refineUpdatedFilesInScmResult (addResult .getAddedFiles ()));
169171
170172 if (i == pathChunks .length -1 && new File (scmRoot .getAbsolutePath ()+File .separator +currentPath .toString ()).isDirectory ()){
171173 addResult = this .scmManager .add (this .scmRepository , new ScmFileSet (scmRoot , currentPath .toString ()+"/**/*" ));
@@ -175,30 +177,30 @@ public List<File> addFile(File scmRoot, String filePathRelativeToScmRoot){
175177 LOGGER .severe ("Error while adding SCM files in directory : " + addResult .getCommandOutput ());
176178 }
177179 }
178- } else {
180+ } else {
179181 // If addResult.isSuccess() is false, it isn't an error if it is related to path chunks (except for latest one) :
180182 // if pathChunk is already synchronized, addResult.isSuccess() will be false.
181183 Level logLevel = (i ==pathChunks .length -1 )?Level .SEVERE :Level .FINE ;
182184 LOGGER .log (logLevel , "Error while adding SCM file : " + addResult .getCommandOutput ());
183185 }
184- }
186+ }
185187 } catch (IOException e ) {
186188 LOGGER .throwing (ScmFileSet .class .getName (), "init<>" , e );
187189 LOGGER .warning ("[addFile] Error while creating ScmFileset : " +e .getMessage ());
188190 return synchronizedFiles ;
189- } catch (ScmException e ) {
190- LOGGER .throwing (ScmManager .class .getName (), "add" , e );
191- LOGGER .warning ("[addFile] Error while adding file : " +e .getMessage ());
192- return synchronizedFiles ;
193- }
191+ } catch (ScmException e ) {
192+ LOGGER .throwing (ScmManager .class .getName (), "add" , e );
193+ LOGGER .warning ("[addFile] Error while adding file : " +e .getMessage ());
194+ return synchronizedFiles ;
195+ }
196+
194197
198+ if (!synchronizedFiles .isEmpty ()){
199+ LOGGER .fine ("Added SCM files : " +Arrays .toString (synchronizedFiles .toArray (new File [0 ]))+" !" );
200+ }
195201
196- if (!synchronizedFiles .isEmpty ()){
197- LOGGER .fine ("Added SCM files : " +Arrays .toString (synchronizedFiles .toArray (new File [0 ]))+" !" );
198- }
199-
200- return synchronizedFiles ;
201- }
202+ return synchronizedFiles ;
203+ }
202204
203205 private List <File > refineUpdatedFilesInScmResult (List <?> updatedFiles ){
204206 List <File > refinedUpdatedFiles = new ArrayList <File >();
@@ -222,42 +224,42 @@ private List<File> refineUpdatedFilesInScmResult(List<?> updatedFiles){
222224
223225 return refinedUpdatedFiles ;
224226 }
225-
226- public boolean checkinFiles (File scmRoot , String commitMessage ){
227- boolean checkinOk = false ;
228-
229- if (!expectScmRepositoryInitiated ()){
230- return checkinOk ;
231- }
232-
233- LOGGER .fine ("Checking in SCM files ..." );
234-
235- ScmFileSet fileSet = new ScmFileSet (scmRoot );
236-
237- // Let's commit everything !
238- try {
239- CheckInScmResult result = this .scmManager .checkIn (this .scmRepository , fileSet , commitMessage );
240- if (!result .isSuccess ()){
241- LOGGER .severe ("[checkinFiles] Problem during SCM commit : " +result .getCommandOutput ());
242- return checkinOk ;
243- }
244- checkinOk = true ;
245- } catch (ScmException e ) {
246- LOGGER .throwing (ScmManager .class .getName (), "checkIn" , e );
247- LOGGER .severe ("[checkinFiles] Error while checkin : " +e .getMessage ());
248- return checkinOk ;
249- }
250-
251-
252- if (checkinOk ){
253- LOGGER .fine ("Checked in SCM files !" );
254- }
255-
256- return checkinOk ;
257- }
258-
259- public String getScmSpecificFilename () {
260- return scmSpecificFilename ;
261- }
262-
227+
228+ public boolean checkinFiles (File scmRoot , String commitMessage ){
229+ boolean checkinOk = false ;
230+
231+ if (!expectScmRepositoryInitiated ()){
232+ return checkinOk ;
233+ }
234+
235+ LOGGER .fine ("Checking in SCM files ..." );
236+
237+ ScmFileSet fileSet = new ScmFileSet (scmRoot );
238+
239+ // Let's commit everything !
240+ try {
241+ CheckInScmResult result = this .scmManager .checkIn (this .scmRepository , fileSet , commitMessage );
242+ if (!result .isSuccess ()){
243+ LOGGER .severe ("[checkinFiles] Problem during SCM commit : " +result .getCommandOutput ());
244+ return checkinOk ;
245+ }
246+ checkinOk = true ;
247+ } catch (ScmException e ) {
248+ LOGGER .throwing (ScmManager .class .getName (), "checkIn" , e );
249+ LOGGER .severe ("[checkinFiles] Error while checkin : " +e .getMessage ());
250+ return checkinOk ;
251+ }
252+
253+
254+ if (checkinOk ){
255+ LOGGER .fine ("Checked in SCM files !" );
256+ }
257+
258+ return checkinOk ;
259+ }
260+
261+ public String getScmSpecificFilename () {
262+ return scmSpecificFilename ;
263+ }
264+
263265}
0 commit comments