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 updateCmd = new String[] {
66 HgCommandConstants.PULL_CMD,
67 HgCommandConstants.REVISION_OPTION,
68 tag != null && !StringUtils.isEmpty( tag.getName() ) ? tag.getName() : "tip" };
69 } else {
70 updateCmd = new String[] {
71 HgCommandConstants.UPDATE_CMD,
72 tag != null && !StringUtils.isEmpty( tag.getName() ) ? tag.getName() : "tip",
73 HgCommandConstants.CLEAN_OPTION };
74 }
75 ScmResult updateResult = HgUtils.execute( new HgConsumer( getLogger() ), getLogger(), workingDir, updateCmd );
76
77 if ( !updateResult.isSuccess() )
78 {
79 return new UpdateScmResult( null, null, updateResult );
80 }
81
82
83 int currentRevision = HgUtils.getCurrentRevisionNumber( getLogger(), workingDir );
84 int previousRevision = currentRevision - 1;
85 String[] diffCmd = new String[] {
86 HgCommandConstants.DIFF_CMD,
87 HgCommandConstants.REVISION_OPTION,
88 "" + previousRevision };
89 HgDiffConsumer diffConsumer = new HgDiffConsumer( getLogger(), workingDir );
90 ScmResult diffResult = HgUtils.execute( diffConsumer, getLogger(), workingDir, diffCmd );
91
92
93 List<ScmFile> updatedFiles = new ArrayList<ScmFile>();
94 List<CharSequence> changes = new ArrayList<CharSequence>();
95 List<ScmFile> diffFiles = diffConsumer.getChangedFiles();
96 Map<String, CharSequence> diffChanges = diffConsumer.getDifferences();
97 for ( ScmFile file : diffFiles )
98 {
99 changes.add( diffChanges.get( file.getPath() ) );
100 if ( file.getStatus() == ScmFileStatus.MODIFIED )
101 {
102 updatedFiles.add( new ScmFile( file.getPath(), ScmFileStatus.PATCHED ) );
103 }
104 else
105 {
106 updatedFiles.add( file );
107 }
108 }
109
110 if ( repo.isPushChanges() ) {
111 String[] hgUpdateCmd = new String[] { HgCommandConstants.UPDATE_CMD };
112 HgUtils.execute( new HgConsumer( getLogger() ), getLogger(), workingDir, hgUpdateCmd );
113 }
114
115 return new UpdateScmResultWithRevision( updatedFiles, new ArrayList<ChangeSet>(0), String.valueOf( currentRevision ), diffResult );
116 }
117
118 protected ChangeLogCommand getChangeLogCommand()
119 {
120 HgChangeLogCommand command = new HgChangeLogCommand();
121 command.setLogger( getLogger() );
122 return command;
123 }
124 }