Modifications command between two revisions (#1761)

Adds the option to compute the modifications between two revisions unsing the modifications command.
This commit is contained in:
René Pfeuffer
2021-08-09 12:13:41 +02:00
committed by GitHub
parent ddd2fc1055
commit 8558572c99
22 changed files with 696 additions and 100 deletions

View File

@@ -27,7 +27,9 @@ package sonia.scm.repository.spi;
import sonia.scm.repository.Modification;
import sonia.scm.repository.Modifications;
import sonia.scm.repository.spi.javahg.HgLogChangesetCommand;
import sonia.scm.repository.spi.javahg.StateCommand;
import java.io.IOException;
import java.util.Collection;
public class HgModificationsCommand extends AbstractCommand implements ModificationsCommand {
@@ -36,7 +38,6 @@ public class HgModificationsCommand extends AbstractCommand implements Modificat
super(context);
}
@Override
public Modifications getModifications(String revision) {
com.aragost.javahg.Repository repository = open();
@@ -46,9 +47,9 @@ public class HgModificationsCommand extends AbstractCommand implements Modificat
}
@Override
public Modifications getModifications(ModificationsCommandRequest request) {
return getModifications(request.getRevision());
public Modifications getModifications(String baseRevision, String revision) throws IOException {
com.aragost.javahg.Repository repository = open();
StateCommand stateCommand = new StateCommand(repository);
return new Modifications(baseRevision, revision, stateCommand.call(baseRevision, revision));
}
}

View File

@@ -63,7 +63,10 @@ public class HgRepositoryServiceProvider extends RepositoryServiceProvider {
Command.FULL_HEALTH_CHECK
);
public static final Set<Feature> FEATURES = EnumSet.of(Feature.COMBINED_DEFAULT_BRANCH);
public static final Set<Feature> FEATURES = EnumSet.of(
Feature.COMBINED_DEFAULT_BRANCH,
Feature.MODIFICATIONS_BETWEEN_REVISIONS
);
private final HgRepositoryHandler handler;
private final HgCommandContext context;

View File

@@ -33,28 +33,40 @@ import sonia.scm.repository.Renamed;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Locale;
class HgModificationParser {
private final Collection<Modification> modifications = new LinkedHashSet<>();
void addLine(String line) {
if (line.startsWith("a ")) {
modifications.add(new Added(line.substring(2)));
} else if (line.startsWith("m ")) {
modifications.add(new Modified(line.substring(2)));
} else if (line.startsWith("d ")) {
modifications.add(new Removed(line.substring(2)));
} else if (line.startsWith("c ")) {
String sourceTarget = line.substring(2);
int divider = sourceTarget.indexOf('\0');
String source = sourceTarget.substring(0, divider);
String target = sourceTarget.substring(divider + 1);
modifications.remove(new Added(target));
if (modifications.remove(new Removed(source))) {
modifications.add(new Renamed(source, target));
} else {
modifications.add(new Copied(source, target));
}
if (line.length() < 2) {
return;
}
String linePrefix = line.substring(0, 2).toLowerCase(Locale.ROOT);
switch (linePrefix) {
case "a ":
modifications.add(new Added(line.substring(2)));
break;
case "m ":
modifications.add(new Modified(line.substring(2)));
break;
case "r ":
modifications.add(new Removed(line.substring(2)));
break;
case "c ":
String sourceTarget = line.substring(2);
int divider = sourceTarget.indexOf('\0');
String source = sourceTarget.substring(0, divider);
String target = sourceTarget.substring(divider + 1);
modifications.remove(new Added(target));
if (modifications.remove(new Removed(source))) {
modifications.add(new Renamed(source, target));
} else {
modifications.add(new Copied(source, target));
}
break;
default:
// nothing to do
}
}

View File

@@ -0,0 +1,55 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.repository.spi.javahg;
import com.aragost.javahg.Repository;
import com.aragost.javahg.internals.HgInputStream;
import sonia.scm.repository.Modification;
import java.io.IOException;
import java.util.Collection;
public class StateCommand extends com.aragost.javahg.internals.AbstractCommand {
public StateCommand(Repository repository) {
super(repository);
}
@Override
public String getCommandName() {
return "status";
}
public Collection<Modification> call(String from, String to) throws IOException {
cmdAppend("--rev", from + ":" + to);
HgInputStream in = launchStream();
HgModificationParser hgModificationParser = new HgModificationParser();
String line = in.textUpTo('\n');
while (line != null && line.length() > 0) {
hgModificationParser.addLine(line);
line = in.textUpTo('\n');
}
return hgModificationParser.getModifications();
}
}