<commit_before>/*
 * Copyright 2016 Red Hat, Inc.
 *
 * Red Hat licenses this file to you 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 io.fabric8.updatebot.repository;

import io.fabric8.updatebot.Configuration;
import io.fabric8.updatebot.github.GitHubHelpers;
import io.fabric8.updatebot.model.GitHubProjects;
import io.fabric8.updatebot.model.GitRepository;
import io.fabric8.updatebot.model.GitRepositoryConfig;
import io.fabric8.updatebot.model.GithubOrganisation;
import io.fabric8.updatebot.model.GithubRepository;
import io.fabric8.updatebot.model.RepositoryConfig;
import io.fabric8.updatebot.support.FileHelper;
import io.fabric8.updatebot.support.Strings;
import io.fabric8.utils.Filter;
import org.kohsuke.github.GHPerson;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

/**
 */
public class Repositories {
    private static final transient Logger LOG = LoggerFactory.getLogger(Repositories.class);


    public static List<LocalRepository> cloneOrPullRepositories(Configuration configuration, RepositoryConfig repositoryConfig) throws IOException {
        List<LocalRepository> repositories = findRepositories(configuration, repositoryConfig);
        for (LocalRepository repository : repositories) {
            cloneOrPullRepository(configuration, repository);
        }
        return repositories;
    }

    public static void cloneOrPullRepository(Configuration configuration, LocalRepository repository) {
        File dir = repository.getDir();
        String secureCloneUrl = repository.getRepo().secureCloneUrl(configuration);
        File gitDir = new File(dir, ".git");
        if (gitDir.exists()) {
            if (configuration.getGit().stashAndCheckoutMaster(dir)) {
                if (!configuration.isPullDisabled()) {
                    configuration.getGit().pull(dir, repository.getCloneUrl());
                }
            }
        } else {
            File parentDir = dir.getParentFile();
            parentDir.mkdirs();

            configuration.info(LOG, "Cloning: " + repository.getFullName() + " to " + FileHelper.getRelativePathToCurrentDir(dir));
            configuration.getGit().clone(parentDir, secureCloneUrl, dir.getName());

            configuration.getGit().configUserNameAndEmail(dir);
        }
    }

    protected static List<LocalRepository> findRepositories(Configuration configuration, RepositoryConfig repositoryConfig) throws IOException {
        String workDirPath = configuration.getWorkDir();
        File workDir = new File(workDirPath);<commit_msg>lets always set the git user/email every time just in case its changed<commit_after>/*
 * Copyright 2016 Red Hat, Inc.
 *
 * Red Hat licenses this file to you 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 io.fabric8.updatebot.repository;

import io.fabric8.updatebot.Configuration;
import io.fabric8.updatebot.github.GitHubHelpers;
import io.fabric8.updatebot.model.GitHubProjects;
import io.fabric8.updatebot.model.GitRepository;
import io.fabric8.updatebot.model.GitRepositoryConfig;
import io.fabric8.updatebot.model.GithubOrganisation;
import io.fabric8.updatebot.model.GithubRepository;
import io.fabric8.updatebot.model.RepositoryConfig;
import io.fabric8.updatebot.support.FileHelper;
import io.fabric8.updatebot.support.Strings;
import io.fabric8.utils.Filter;
import org.kohsuke.github.GHPerson;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

/**
 */
public class Repositories {
    private static final transient Logger LOG = LoggerFactory.getLogger(Repositories.class);


    public static List<LocalRepository> cloneOrPullRepositories(Configuration configuration, RepositoryConfig repositoryConfig) throws IOException {
        List<LocalRepository> repositories = findRepositories(configuration, repositoryConfig);
        for (LocalRepository repository : repositories) {
            cloneOrPullRepository(configuration, repository);
        }
        return repositories;
    }

    public static void cloneOrPullRepository(Configuration configuration, LocalRepository repository) {
        File dir = repository.getDir();
        String secureCloneUrl = repository.getRepo().secureCloneUrl(configuration);
        File gitDir = new File(dir, ".git");
        if (gitDir.exists()) {
            if (configuration.getGit().stashAndCheckoutMaster(dir)) {
                if (!configuration.isPullDisabled()) {
                    configuration.getGit().pull(dir, repository.getCloneUrl());
                }
            }
            configuration.getGit().configUserNameAndEmail(dir);
        } else {
            File parentDir = dir.getParentFile();
            parentDir.mkdirs();

            configuration.info(LOG, "Cloning: " + repository.getFullName() + " to " + FileHelper.getRelativePathToCurrentDir(dir));
            configuration.getGit().clone(parentDir, secureCloneUrl, dir.getName());

            configuration.getGit().configUserNameAndEmail(dir);
        }
    }

    protected static List<LocalRepository> findRepositories(Configuration configuration, RepositoryConfig repositoryConfig) throws IOException {
        String workDirPath = configuration.getWorkDir();
        File workDir = new File(workDirPath);
{'fraction_non_alphanumeric': 0.09194382852919439, 'fraction_numerical': 0.0065040650406504065, 'mean_word_length': 3.8294075660242686, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2279939', 'n_tokens_mistral': 1847, 'n_tokens_neox': 1761, 'n_words': 466}