< prev index next >

vcs/src/main/java/org/openjdk/skara/vcs/openjdk/convert/HgToGitConverter.java

Print this page

 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 package org.openjdk.skara.vcs.openjdk.convert;
 24 
 25 import org.openjdk.skara.vcs.*;
 26 import org.openjdk.skara.vcs.openjdk.*;
 27 
 28 import java.io.*;
 29 import java.net.URI;
 30 import java.nio.charset.StandardCharsets;
 31 import java.nio.file.*;
 32 import java.time.ZonedDateTime;
 33 import java.time.format.DateTimeFormatter;
 34 import java.util.*;

 35 import java.util.function.Function;
 36 import java.util.logging.Logger;
 37 import java.util.stream.Collectors;
 38 
 39 import static java.lang.Integer.parseInt;
 40 
 41 public class HgToGitConverter implements Converter {
 42     private static class ProcessInfo implements AutoCloseable {
 43         private final java.lang.Process process;
 44         private final List<String> command;
 45         private final Path stdout;
 46         private final Path stderr;
 47         private final CloseAction closeAction;
 48 
 49         @FunctionalInterface
 50         interface CloseAction {
 51             void close() throws IOException;
 52         }
 53 
 54         ProcessInfo(java.lang.Process process, List<String> command, Path stdout, Path stderr, CloseAction closeAction) {

 62         ProcessInfo(java.lang.Process process, List<String> command, Path stdout, Path stderr) {
 63             this(process, command, stdout, stderr, () -> {});
 64         }
 65 
 66         java.lang.Process process() {
 67             return process;
 68         }
 69 
 70         List<String> command() {
 71             return command;
 72         }
 73 
 74         Path stdout() {
 75             return stdout;
 76         }
 77 
 78         Path stderr() {
 79             return stderr;
 80         }
 81 









 82         @Override
 83         public void close() throws IOException {
 84             if (stdout != null) {
 85                 Files.delete(stdout);
 86             }
 87             if (stderr != null) {
 88                 Files.delete(stderr);
 89             }
 90             closeAction.close();
 91         }
 92     }
 93 
 94     private final byte[] fileBuffer = new byte[2048];
 95     private final Logger log = Logger.getLogger("org.openjdk.skara.vcs.openjdk.convert");
 96 
 97     private final Map<Hash, List<String>> replacements;
 98     private final Map<Hash, Map<String, String>> corrections;
 99     private final Set<Hash> lowercase;
100     private final Set<Hash> punctuated;
101 

588         return new ProcessInfo(pb.start(), command, null, stderr, () -> Files.delete(ext));
589     }
590 
591     private ProcessInfo fastImport(ReadOnlyRepository repo) throws IOException {
592         var command = List.of("git", "fast-import");
593         var pb = new ProcessBuilder(command);
594         pb.directory(repo.root().toFile());
595 
596         var stdout = Files.createTempFile("fast-import", ".stdout.txt");
597         pb.redirectOutput(stdout.toFile());
598 
599         var stderr = Files.createTempFile("fast-import", ".stderr.txt");
600         pb.redirectError(stderr.toFile());
601 
602         log.finer("Starting '" + String.join(" ", command) + "'");
603         return new ProcessInfo(pb.start(), command, stdout, stderr);
604     }
605 
606     private void await(ProcessInfo p) throws IOException {
607         try {
608             int res = p.process().waitFor();
609             if (res != 0) {
610                 var msg = String.join(" ", p.command()) + " exited with status " + res;
611                 log.severe(msg);
612                 throw new IOException(msg);
613             }
614         } catch (InterruptedException e) {
615             throw new IOException(e);
616         }
617     }
618 
619     private void convert(ProcessInfo hg, ProcessInfo git, ReadOnlyRepository hgRepo, Path marks) throws IOException {
620         var pipe = new Pipe(hg.process().getInputStream(), git.process().getOutputStream(), 512);
621 
622         pipe.println("feature done");
623         pipe.println("feature import-marks-if-exists=" + marks.toAbsolutePath().toString());
624         pipe.println("feature export-marks=" + marks.toAbsolutePath().toString());
625 
626         var tagCommits = convertCommits(pipe);
627         convertTags(pipe, tagCommits, hgRepo);
628 

 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 package org.openjdk.skara.vcs.openjdk.convert;
 24 
 25 import org.openjdk.skara.vcs.*;
 26 import org.openjdk.skara.vcs.openjdk.*;
 27 
 28 import java.io.*;
 29 import java.net.URI;
 30 import java.nio.charset.StandardCharsets;
 31 import java.nio.file.*;
 32 import java.time.ZonedDateTime;
 33 import java.time.format.DateTimeFormatter;
 34 import java.util.*;
 35 import java.util.concurrent.TimeUnit;
 36 import java.util.function.Function;
 37 import java.util.logging.Logger;
 38 import java.util.stream.Collectors;
 39 
 40 import static java.lang.Integer.parseInt;
 41 
 42 public class HgToGitConverter implements Converter {
 43     private static class ProcessInfo implements AutoCloseable {
 44         private final java.lang.Process process;
 45         private final List<String> command;
 46         private final Path stdout;
 47         private final Path stderr;
 48         private final CloseAction closeAction;
 49 
 50         @FunctionalInterface
 51         interface CloseAction {
 52             void close() throws IOException;
 53         }
 54 
 55         ProcessInfo(java.lang.Process process, List<String> command, Path stdout, Path stderr, CloseAction closeAction) {

 63         ProcessInfo(java.lang.Process process, List<String> command, Path stdout, Path stderr) {
 64             this(process, command, stdout, stderr, () -> {});
 65         }
 66 
 67         java.lang.Process process() {
 68             return process;
 69         }
 70 
 71         List<String> command() {
 72             return command;
 73         }
 74 
 75         Path stdout() {
 76             return stdout;
 77         }
 78 
 79         Path stderr() {
 80             return stderr;
 81         }
 82 
 83         int waitForProcess() throws InterruptedException {
 84             var finished = process.waitFor(12, TimeUnit.HOURS);
 85             if (!finished) {
 86                 process.destroyForcibly().waitFor();
 87                 throw new RuntimeException("Command '" + String.join(" ", command) + "' did not finish in 12 hours");
 88             }
 89             return process.exitValue();
 90         }
 91 
 92         @Override
 93         public void close() throws IOException {
 94             if (stdout != null) {
 95                 Files.delete(stdout);
 96             }
 97             if (stderr != null) {
 98                 Files.delete(stderr);
 99             }
100             closeAction.close();
101         }
102     }
103 
104     private final byte[] fileBuffer = new byte[2048];
105     private final Logger log = Logger.getLogger("org.openjdk.skara.vcs.openjdk.convert");
106 
107     private final Map<Hash, List<String>> replacements;
108     private final Map<Hash, Map<String, String>> corrections;
109     private final Set<Hash> lowercase;
110     private final Set<Hash> punctuated;
111 

598         return new ProcessInfo(pb.start(), command, null, stderr, () -> Files.delete(ext));
599     }
600 
601     private ProcessInfo fastImport(ReadOnlyRepository repo) throws IOException {
602         var command = List.of("git", "fast-import");
603         var pb = new ProcessBuilder(command);
604         pb.directory(repo.root().toFile());
605 
606         var stdout = Files.createTempFile("fast-import", ".stdout.txt");
607         pb.redirectOutput(stdout.toFile());
608 
609         var stderr = Files.createTempFile("fast-import", ".stderr.txt");
610         pb.redirectError(stderr.toFile());
611 
612         log.finer("Starting '" + String.join(" ", command) + "'");
613         return new ProcessInfo(pb.start(), command, stdout, stderr);
614     }
615 
616     private void await(ProcessInfo p) throws IOException {
617         try {
618             int res = p.waitForProcess();
619             if (res != 0) {
620                 var msg = String.join(" ", p.command()) + " exited with status " + res;
621                 log.severe(msg);
622                 throw new IOException(msg);
623             }
624         } catch (InterruptedException e) {
625             throw new IOException(e);
626         }
627     }
628 
629     private void convert(ProcessInfo hg, ProcessInfo git, ReadOnlyRepository hgRepo, Path marks) throws IOException {
630         var pipe = new Pipe(hg.process().getInputStream(), git.process().getOutputStream(), 512);
631 
632         pipe.println("feature done");
633         pipe.println("feature import-marks-if-exists=" + marks.toAbsolutePath().toString());
634         pipe.println("feature export-marks=" + marks.toAbsolutePath().toString());
635 
636         var tagCommits = convertCommits(pipe);
637         convertTags(pipe, tagCommits, hgRepo);
638 
< prev index next >