<commit_before>/*
 * Copyright (c) 2015 JBYoshi
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package jbyoshi.gitupdate.processor;

import java.util.*;

import org.eclipse.jgit.api.*;
import org.eclipse.jgit.lib.*;
import org.eclipse.jgit.transport.*;

import com.google.common.collect.*;

import jbyoshi.gitupdate.*;

public final class Push extends Processor {

	@Override
	public void registerTasks(Repository repo, Git git, Task root) throws Exception {
		Task me = root.newChild(getClass().getSimpleName());
		// Group the branches by their remotes.
		Multimap<String, String> branchList = HashMultimap.create();
		for (String branch : Utils.getLocalBranches(repo).keySet()) {
			String remote = new BranchConfig(repo.getConfig(), branch).getRemote();
			if (remote != null) {
				branchList.put(remote, branch);
			}
		}
		for (Map.Entry<String, Collection<String>> remote : branchList.asMap().entrySet()) {
			me.newChild(remote.getKey(), report -> {
				try {
					process(repo, git, remote.getKey(), Constants.R_REMOTES + remote.getKey() + "/", remote.getValue(),
							report);
				} catch (Exception e) {
					report.newErrorChild(e);
				}
			});
		}
	}

	private static void process(Repository repo, Git git, String remote, String fullRemote, Collection<String> branches,
			Report report) throws Exception {
		// Figure out if anything needs to be pushed.
		Map<String, ObjectId> oldIds = new HashMap<>();
		boolean canPush = false;
		for (Iterator<String> it = branches.iterator(); it.hasNext();) {
			String branch = it.next();
			BranchConfig config = new BranchConfig(repo.getConfig(), branch);
			ObjectId target = repo.getRef(branch).getObjectId();

			Ref remoteRef = repo.getRef(config.getRemoteTrackingBranch());
			if (remoteRef == null || !target.equals(remoteRef.getObjectId())) {
				canPush = true;
			}
			oldIds.put(branch, remoteRef == null ? ObjectId.zeroId() : remoteRef.getObjectId());
		}

		if (!canPush) {
			return;
		}

		PushCommand push = git.push().setCredentialsProvider(Prompts.INSTANCE).setTimeout(5)
				.setRemote(remote);
		for (String branch : branches) {
			push.add(Constants.R_HEADS + branch);<commit_msg>Fix a few more warnings.<commit_after>/*
 * Copyright (c) 2015 JBYoshi
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package jbyoshi.gitupdate.processor;

import java.util.*;

import org.eclipse.jgit.api.*;
import org.eclipse.jgit.lib.*;
import org.eclipse.jgit.transport.*;

import com.google.common.collect.*;

import jbyoshi.gitupdate.*;

public final class Push extends Processor {

	@Override
	public void registerTasks(Repository repo, Git git, Task root) throws Exception {
		Task me = root.newChild(getClass().getSimpleName());
		// Group the branches by their remotes.
		Multimap<String, String> branchList = HashMultimap.create();
		for (String branch : Utils.getLocalBranches(repo).keySet()) {
			String remote = new BranchConfig(repo.getConfig(), branch).getRemote();
			if (remote != null) {
				branchList.put(remote, branch);
			}
		}
		for (Map.Entry<String, Collection<String>> remote : branchList.asMap().entrySet()) {
			me.newChild(remote.getKey(), report -> {
				try {
					process(repo, git, remote.getKey(), remote.getValue(), report);
				} catch (Exception e) {
					report.newErrorChild(e);
				}
			});
		}
	}

	private static void process(Repository repo, Git git, String remote, Collection<String> branches,
			Report report) throws Exception {
		// Figure out if anything needs to be pushed.
		Map<String, ObjectId> oldIds = new HashMap<>();
		boolean canPush = false;
		for (String branch : branches) {
			BranchConfig config = new BranchConfig(repo.getConfig(), branch);
			ObjectId target = repo.getRef(branch).getObjectId();

			Ref remoteRef = repo.getRef(config.getRemoteTrackingBranch());
			if (remoteRef == null || !target.equals(remoteRef.getObjectId())) {
				canPush = true;
			}
			oldIds.put(branch, remoteRef == null ? ObjectId.zeroId() : remoteRef.getObjectId());
		}

		if (!canPush) {
			return;
		}

		PushCommand push = git.push().setCredentialsProvider(Prompts.INSTANCE).setTimeout(5)
				.setRemote(remote);
		for (String branch : branches) {
			push.add(Constants.R_HEADS + branch);
{'fraction_non_alphanumeric': 0.12380405663987754, 'fraction_numerical': 0.003444316877152698, 'mean_word_length': 4.016314779270633, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5102128', 'n_tokens_mistral': 1672, 'n_tokens_neox': 1515, 'n_words': 467}