/
Squash.java
77 lines (65 loc) · 2.85 KB
/
Squash.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* Copyright (c) 2013-2016 Boundless and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/edl-v10.html
*
* Contributors:
* Victor Olaya (Boundless) - initial implementation
*/
package org.locationtech.geogig.cli.porcelain;
import java.util.List;
import org.locationtech.geogig.cli.AbstractCommand;
import org.locationtech.geogig.cli.CLICommand;
import org.locationtech.geogig.cli.GeogigCLI;
import org.locationtech.geogig.model.ObjectId;
import org.locationtech.geogig.model.RevCommit;
import org.locationtech.geogig.plumbing.RevParse;
import org.locationtech.geogig.porcelain.SquashOp;
import org.locationtech.geogig.repository.impl.GeoGIG;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import java.util.Optional;
import com.google.common.collect.Lists;
/**
* Squashes a set of commits into a single one.
* <p>
* CLI proxy for {@link org.locationtech.geogig.porcelain.SquashOp}
* <p>
* Usage:
* <ul>
* <li>{@code geogig squash [<message>] <since_commit> <until_commit>
*
</ul>
*
* @see org.locationtech.geogig.porcelain.LogOp
*/
@Parameters(commandNames = "squash", commandDescription = "Squash commits")
public class Squash extends AbstractCommand implements CLICommand {
@Parameter(description = "<since_commit> <until_commit>", arity = 2)
private List<String> commits = Lists.newArrayList();
@Parameter(names = "-m", description = "Commit message")
private String message;
/**
* Executes the squash command using the provided options.
*/
@Override
public void runInternal(GeogigCLI cli) {
checkParameter(commits.size() == 2, "2 commit references must be supplied");
final GeoGIG geogig = cli.getGeogig();
Optional<ObjectId> sinceId = geogig.command(RevParse.class).setRefSpec(commits.get(0))
.call();
checkParameter(sinceId.isPresent(), "'since' reference cannot be found");
checkParameter(geogig.getRepository().commitExists(sinceId.get()),
"'since' reference does not resolve to a commit");
RevCommit sinceCommit = geogig.getRepository().getCommit(sinceId.get());
Optional<ObjectId> untilId = geogig.command(RevParse.class).setRefSpec(commits.get(1))
.call();
checkParameter(untilId.isPresent(), "'until' reference cannot be found");
checkParameter(geogig.getRepository().commitExists(untilId.get()),
"'until' reference does not resolve to a commit");
RevCommit untilCommit = geogig.getRepository().getCommit(untilId.get());
geogig.command(SquashOp.class).setSince(sinceCommit).setUntil(untilCommit)
.setMessage(message).call();
}
}