21 * questions.
22 */
23 package org.openjdk.skara.cli;
24
25 import org.openjdk.skara.args.*;
26 import org.openjdk.skara.vcs.*;
27
28 import java.io.*;
29 import java.net.URI;
30 import java.nio.file.*;
31 import java.util.*;
32 import java.util.logging.*;
33
34 public class GitSync {
35 private static IOException die(String message) {
36 System.err.println(message);
37 System.exit(1);
38 return new IOException("will never reach here");
39 }
40
41 private static int fetch() throws IOException, InterruptedException {
42 var pb = new ProcessBuilder("git", "fetch");
43 pb.inheritIO();
44 return pb.start().waitFor();
45 }
46
47 private static int pull() throws IOException, InterruptedException {
48 var pb = new ProcessBuilder("git", "pull");
49 pb.inheritIO();
50 return pb.start().waitFor();
51 }
52
53 public static void main(String[] args) throws IOException, InterruptedException {
54 var flags = List.of(
55 Option.shortcut("")
56 .fullname("from")
57 .describe("REMOTE")
58 .helptext("Fetch changes from this remote")
59 .optional(),
60 Option.shortcut("")
61 .fullname("to")
62 .describe("REMOTE")
63 .helptext("Push changes to this remote")
64 .optional(),
65 Option.shortcut("")
66 .fullname("branches")
67 .describe("BRANCHES")
68 .helptext("Comma separated list of branches to sync")
69 .optional(),
70 Switch.shortcut("")
71 .fullname("pull")
72 .helptext("Pull current branch from origin after successful sync")
73 .optional(),
74 Switch.shortcut("")
75 .fullname("fetch")
76 .helptext("Fetch current branch from origin after successful sync")
77 .optional(),
78 Switch.shortcut("m")
79 .fullname("mercurial")
80 .helptext("Force use of mercurial")
81 .optional(),
82 Switch.shortcut("")
83 .fullname("verbose")
84 .helptext("Turn on verbose output")
85 .optional(),
86 Switch.shortcut("")
87 .fullname("debug")
88 .helptext("Turn on debugging output")
89 .optional(),
90 Switch.shortcut("v")
91 .fullname("version")
92 .helptext("Print the version of this tool")
93 .optional()
94 );
95
96 var parser = new ArgumentParser("git sync", flags);
97 var arguments = parser.parse(args);
148 if (arguments.contains("branches")) {
149 var requested = arguments.get("branches").asString().split(",");
150 for (var branch : requested) {
151 branches.add(branch.trim());
152 }
153 }
154
155 for (var branch : repo.remoteBranches(upstream)) {
156 var name = branch.name();
157 if (!branches.isEmpty() && !branches.contains(name)) {
158 System.out.println("Skipping branch " + name);
159 continue;
160 }
161 System.out.print("Syncing " + upstream + "/" + name + " to " + origin + "/" + name + "... ");
162 System.out.flush();
163 var fetchHead = repo.fetch(upstreamPullPath, branch.hash().hex());
164 repo.push(fetchHead, originPushPath, name);
165 System.out.println("done");
166 }
167
168 if (arguments.contains("fetch")) {
169 int err = fetch();
170 if (err != 0) {
171 System.exit(err);
172 }
173 }
174
175 if (arguments.contains("pull")) {
176 int err = pull();
177 if (err != 0) {
178 System.exit(err);
179 }
180 }
181 }
182 }
|
21 * questions.
22 */
23 package org.openjdk.skara.cli;
24
25 import org.openjdk.skara.args.*;
26 import org.openjdk.skara.vcs.*;
27
28 import java.io.*;
29 import java.net.URI;
30 import java.nio.file.*;
31 import java.util.*;
32 import java.util.logging.*;
33
34 public class GitSync {
35 private static IOException die(String message) {
36 System.err.println(message);
37 System.exit(1);
38 return new IOException("will never reach here");
39 }
40
41 private static int pull() throws IOException, InterruptedException {
42 var pb = new ProcessBuilder("git", "pull");
43 pb.inheritIO();
44 return pb.start().waitFor();
45 }
46
47 public static void main(String[] args) throws IOException, InterruptedException {
48 var flags = List.of(
49 Option.shortcut("")
50 .fullname("from")
51 .describe("REMOTE")
52 .helptext("Fetch changes from this remote")
53 .optional(),
54 Option.shortcut("")
55 .fullname("to")
56 .describe("REMOTE")
57 .helptext("Push changes to this remote")
58 .optional(),
59 Option.shortcut("")
60 .fullname("branches")
61 .describe("BRANCHES")
62 .helptext("Comma separated list of branches to sync")
63 .optional(),
64 Switch.shortcut("")
65 .fullname("pull")
66 .helptext("Pull current branch from origin after successful sync")
67 .optional(),
68 Switch.shortcut("m")
69 .fullname("mercurial")
70 .helptext("Force use of mercurial")
71 .optional(),
72 Switch.shortcut("")
73 .fullname("verbose")
74 .helptext("Turn on verbose output")
75 .optional(),
76 Switch.shortcut("")
77 .fullname("debug")
78 .helptext("Turn on debugging output")
79 .optional(),
80 Switch.shortcut("v")
81 .fullname("version")
82 .helptext("Print the version of this tool")
83 .optional()
84 );
85
86 var parser = new ArgumentParser("git sync", flags);
87 var arguments = parser.parse(args);
138 if (arguments.contains("branches")) {
139 var requested = arguments.get("branches").asString().split(",");
140 for (var branch : requested) {
141 branches.add(branch.trim());
142 }
143 }
144
145 for (var branch : repo.remoteBranches(upstream)) {
146 var name = branch.name();
147 if (!branches.isEmpty() && !branches.contains(name)) {
148 System.out.println("Skipping branch " + name);
149 continue;
150 }
151 System.out.print("Syncing " + upstream + "/" + name + " to " + origin + "/" + name + "... ");
152 System.out.flush();
153 var fetchHead = repo.fetch(upstreamPullPath, branch.hash().hex());
154 repo.push(fetchHead, originPushPath, name);
155 System.out.println("done");
156 }
157
158 }
159
160 if (arguments.contains("pull")) {
161 int err = pull();
162 if (err != 0) {
163 System.exit(err);
164 }
165 }
166 }
167 }
|