1 package org.apache.maven.scm.provider.hg.command.update;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Map;
26
27 import org.apache.maven.scm.ChangeSet;
28 import org.apache.maven.scm.ScmException;
29 import org.apache.maven.scm.ScmFile;
30 import org.apache.maven.scm.ScmFileSet;
31 import org.apache.maven.scm.ScmFileStatus;
32 import org.apache.maven.scm.ScmResult;
33 import org.apache.maven.scm.ScmVersion;
34 import org.apache.maven.scm.command.Command;
35 import org.apache.maven.scm.command.changelog.ChangeLogCommand;
36 import org.apache.maven.scm.command.update.AbstractUpdateCommand;
37 import org.apache.maven.scm.command.update.UpdateScmResult;
38 import org.apache.maven.scm.command.update.UpdateScmResultWithRevision;
39 import org.apache.maven.scm.provider.ScmProviderRepository;
40 import org.apache.maven.scm.provider.hg.HgUtils;
41 import org.apache.maven.scm.provider.hg.command.HgCommandConstants;
42 import org.apache.maven.scm.provider.hg.command.HgConsumer;
43 import org.apache.maven.scm.provider.hg.command.changelog.HgChangeLogCommand;
44 import org.apache.maven.scm.provider.hg.command.diff.HgDiffConsumer;
45 import org.codehaus.plexus.util.StringUtils;
46
47
48
49
50
51
52 public class HgUpdateCommand
53 extends AbstractUpdateCommand
54 implements Command
55 {
56
57 protected UpdateScmResult executeUpdateCommand( ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion tag )
58 throws ScmException
59 {
60 File workingDir = fileSet.getBasedir();
61
62 String[] updateCmd;
63
64 if ( repo.isPushChanges() )
65 {
66 updateCmd =
67 new String[] { HgCommandConstants.PULL_CMD, HgCommandConstants.REVISION_OPTION,
68 tag != null && !StringUtils.isEmpty( tag.getName() ) ? tag.getName() : "tip" };
69 }
70 else
71 {
72 updateCmd =
73 new String[] { HgCommandConstants.UPDATE_CMD,
74 tag != null && !StringUtils.isEmpty( tag.getName() ) ? tag.getName() : "tip",
75 HgCommandConstants.CLEAN_OPTION };
76 }
77 ScmResult updateResult = HgUtils.execute( new HgConsumer( getLogger() ), getLogger(), workingDir, updateCmd );
78
79 if ( !updateResult.isSuccess() )
80 {
81 return new UpdateScmResult( null, null, updateResult );
82 }
83
84
85 int currentRevision = HgUtils.getCurrentRevisionNumber( getLogger(), workingDir );
86 int previousRevision = currentRevision - 1;
87 String[] diffCmd = new String[] {
88 HgCommandConstants.DIFF_CMD,
89 HgCommandConstants.REVISION_OPTION,
90 "" + previousRevision };
91 HgDiffConsumer diffConsumer = new HgDiffConsumer( getLogger(), workingDir );
92 ScmResult diffResult = HgUtils.execute( diffConsumer, getLogger(), workingDir, diffCmd );
93
94
95 List<ScmFile> updatedFiles = new ArrayList<ScmFile>();
96 List<CharSequence> changes = new ArrayList<CharSequence>();
97 List<ScmFile> diffFiles = diffConsumer.getChangedFiles();
98 Map<String, CharSequence> diffChanges = diffConsumer.getDifferences();
99 for ( ScmFile file : diffFiles )
100 {
101 changes.add( diffChanges.get( file.getPath() ) );
102 if ( file.getStatus() == ScmFileStatus.MODIFIED )
103 {
104 updatedFiles.add( new ScmFile( file.getPath(), ScmFileStatus.PATCHED ) );
105 }
106 else
107 {
108 updatedFiles.add( file );
109 }
110 }
111
112 if ( repo.isPushChanges() )
113 {
114 String[] hgUpdateCmd = new String[] { HgCommandConstants.UPDATE_CMD };
115 HgUtils.execute( new HgConsumer( getLogger() ), getLogger(), workingDir, hgUpdateCmd );
116 }
117
118 return new UpdateScmResultWithRevision( updatedFiles, new ArrayList<ChangeSet>( 0 ),
119 String.valueOf( currentRevision ), diffResult );
120 }
121
122 protected ChangeLogCommand getChangeLogCommand()
123 {
124 HgChangeLogCommand command = new HgChangeLogCommand();
125 command.setLogger( getLogger() );
126 return command;
127 }
128 }