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