Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add git-sync command #153

Closed
wants to merge 7 commits into from
@@ -52,6 +52,16 @@ private static int pull() throws IOException, InterruptedException {

public static void main(String[] args) throws IOException, InterruptedException {
var flags = List.of(
Option.shortcut("")
.fullname("from")
.describe("REMOTE")
.helptext("Fetch changes from this remote")
.optional(),
Option.shortcut("")
.fullname("to")
.describe("REMOTE")
.helptext("Push changes to this remote")
.optional(),
Option.shortcut("")
.fullname("branches")
.describe("BRANCHES")
@@ -83,14 +93,7 @@ public static void main(String[] args) throws IOException, InterruptedException
.optional()
);

var inputs = List.of(
Input.position(0)
.describe("REMOTE")
.singular()
.optional()
);

var parser = new ArgumentParser("git sync", flags, inputs);
var parser = new ArgumentParser("git sync", flags);
var arguments = parser.parse(args);

if (arguments.contains("version")) {
@@ -111,20 +114,34 @@ public static void main(String[] args) throws IOException, InterruptedException
var remotes = repo.remotes();

String upstream = null;
if (arguments.at(0).isPresent()) {
upstream = arguments.at(0).asString();
if (arguments.contains("from")) {
upstream = arguments.get("from").asString();
} else {
var lines = repo.config("sync.remote");
var lines = repo.config("sync.from");
if (lines.size() == 1 && remotes.contains(lines.get(0))) {
upstream = lines.get(0);
} else {
die("No remote provided to sync with");
die("No remote provided to fetch from, please set the --from flag");
}
}

var upstreamPullPath = remotes.contains(upstream) ?
Remote.toURI(repo.pullPath(upstream)) : URI.create(upstream);
var origin = "origin";

String origin = null;
if (arguments.contains("to")) {
origin = arguments.get("to").asString();
} else {
var lines = repo.config("sync.to");
if (lines.size() == 1) {
if (!remotes.contains(lines.get(0))) {
die("The given remote to push to, " + lines.get(0) + ", does not exist");
} else {
origin = lines.get(0);
}
} else {
origin = "origin";
}
}
var originPushPath = Remote.toURI(repo.pushPath(origin));

var branches = new HashSet<String>();
@@ -142,6 +159,7 @@ public static void main(String[] args) throws IOException, InterruptedException
continue;
}
System.out.print("Syncing " + upstream + "/" + name + " to " + origin + "/" + name + "... ");

This comment has been minimized.

@rwestberg

rwestberg Sep 24, 2019 Member

Do you need to flush the PrintStream here?

This comment has been minimized.

@edvbld

edvbld Sep 24, 2019 Author Member

Probably, good idea, let me fix!

System.out.flush();
var fetchHead = repo.fetch(upstreamPullPath, branch.hash().hex());
repo.push(fetchHead, originPushPath, name);
System.out.println("done");
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.