Skip to content

Method: cloneFrom(URI)

1: /*
2: * #%L
3: * *********************************************************************************************************************
4: *
5: * NorthernWind - lightweight CMS
6: * http://northernwind.tidalwave.it - git clone https://bitbucket.org/tidalwave/northernwind-src.git
7: * %%
8: * Copyright (C) 2011 - 2023 Tidalwave s.a.s. (http://tidalwave.it)
9: * %%
10: * *********************************************************************************************************************
11: *
12: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
13: * the License. You may obtain a copy of the License at
14: *
15: * http://www.apache.org/licenses/LICENSE-2.0
16: *
17: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
18: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
19: * specific language governing permissions and limitations under the License.
20: *
21: * *********************************************************************************************************************
22: *
23: *
24: * *********************************************************************************************************************
25: * #L%
26: */
27: package it.tidalwave.northernwind.frontend.filesystem.hg.impl;
28:
29: import javax.annotation.Nonnull;
30: import java.util.List;
31: import java.util.NoSuchElementException;
32: import java.util.Optional;
33: import java.io.IOException;
34: import java.nio.file.Files;
35: import java.nio.file.Path;
36: import java.net.URI;
37: import it.tidalwave.util.ProcessExecutor;
38: import it.tidalwave.util.ProcessExecutorException;
39: import it.tidalwave.northernwind.frontend.filesystem.scm.spi.ScmWorkingDirectorySupport;
40: import it.tidalwave.northernwind.frontend.filesystem.scm.spi.Tag;
41: import lombok.extern.slf4j.Slf4j;
42: import static java.util.stream.Collectors.*;
43:
44: /***********************************************************************************************************************
45: *
46: * A Mercurial implementation of {@link it.tidalwave.northernwind.frontend.filesystem.scm.spi.ScmWorkingDirectory}.
47: *
48: * @author Fabrizio Giudici
49: *
50: **********************************************************************************************************************/
51: @Slf4j
52: public class MercurialWorkingDirectory extends ScmWorkingDirectorySupport
53: {
54: private static final String HG = "hg";
55:
56: /*******************************************************************************************************************
57: *
58: * Creates a new instance for the given folder.
59: *
60: * @param folder the folder
61: *
62: ******************************************************************************************************************/
63: public MercurialWorkingDirectory (@Nonnull final Path folder)
64: {
65: super(".hg", folder);
66: }
67:
68: /*******************************************************************************************************************
69: *
70: * {@inheritDoc}
71: *
72: ******************************************************************************************************************/
73: @Override @Nonnull
74: public Optional<Tag> getCurrentTag()
75: throws InterruptedException, IOException
76: {
77: try
78: {
79: final var executor = hgCommand().withArgument("id").start().waitForCompletion();
80: final var scanner = executor.getStdout().waitForCompleted().filteredAndSplitBy("(.*)", " ");
81: scanner.next();
82: return Optional.of(new Tag(scanner.next()));
83: }
84: catch (NoSuchElementException e)
85: {
86: return Optional.empty();
87: }
88: }
89:
90: /*******************************************************************************************************************
91: *
92: * {@inheritDoc}
93: *
94: ******************************************************************************************************************/
95: @Nonnull
96: public List<String> listTags()
97: throws InterruptedException, IOException
98: {
99: return hgCommand().withArgument("tags")
100: .start()
101: .waitForCompletion()
102: .getStdout()
103: .waitForCompleted()
104: .filteredBy("([^ ]*) *.*$")
105: .stream()
106: .filter(s -> !"tip".equals(s))
107: .collect(collectingAndThen(toList(), ScmWorkingDirectorySupport::reversed));
108: }
109:
110: /*******************************************************************************************************************
111: *
112: * {@inheritDoc}
113: *
114: ******************************************************************************************************************/
115: public void cloneFrom (@Nonnull final URI uri)
116: throws InterruptedException, IOException
117: {
118: Files.createDirectories(folder);
119: hgCommand().withArguments("clone", "--noupdate", uri.toASCIIString(), ".").start().waitForCompletion();
120: }
121:
122: /*******************************************************************************************************************
123: *
124: * {@inheritDoc}
125: *
126: ******************************************************************************************************************/
127: @Override
128: public void checkOut (@Nonnull final Tag tag)
129: throws InterruptedException, IOException
130: {
131: try
132: {
133: hgCommand().withArguments("update", "-C", tag.getName()).start().waitForCompletion();
134: }
135: catch (ProcessExecutorException e)
136: {
137: if ((e.getExitCode() == 255) &&
138: (e.getStderr().stream().anyMatch(s -> s.contains("abort: unknown revision"))))
139: {
140: throw new IllegalArgumentException("Invalid tag: " + tag.getName());
141: }
142:
143: throw e;
144: }
145: }
146:
147: /*******************************************************************************************************************
148: *
149: * {@inheritDoc}
150: *
151: ******************************************************************************************************************/
152: @Override
153: public void fetchChangesets()
154: throws InterruptedException, IOException
155: {
156: hgCommand().withArgument("pull").start().waitForCompletion();
157: }
158:
159: /*******************************************************************************************************************
160: *
161: * Creates an executor for Mercurial.
162: *
163: * @return the executor
164: * @throws IOException in case of error
165: *
166: ******************************************************************************************************************/
167: @Nonnull
168: private ProcessExecutor hgCommand()
169: throws IOException
170: {
171: return ProcessExecutor.forExecutable(HG).withWorkingDirectory(folder);
172: }
173: }