<commit_before>package pl.project13.janbanery.entity2wiki;

import com.google.common.base.Charsets;
import com.google.common.base.Joiner;
import com.google.common.io.Files;
import org.eclipse.jgit.api.CreateBranchCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.*;
import org.eclipse.jgit.errors.UnmergedPathException;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.List;

/**
 * @author <NAME>
 */
public class GitHubWikiPublisher {

  Git git;

  public static void main(String... args) throws Exception {
    validateArgs(args);

    int i = 0;
    String cloneUrl = args[i++];
    String pageName = args[i++];
    String sourceRootLocationString = args[i++];

    File tempFile = File.createTempFile("gitHubWikiPublisher", ".md.tmp");
    PrintStream out = new PrintStream(tempFile);

    File sourceRootLocation = new File(sourceRootLocationString);
    Entity2WikiRunner entity2Wiki = new Entity2WikiRunner(sourceRootLocation);

    entity2Wiki.withClasses(DocumentedClasses.resources()).writeSection(out, "Entities", new NotReadOnlyClassPredicate());
    entity2Wiki.withClasses(DocumentedClasses.resources()).writeSection(out, "ReadOnly Entities", new ReadOnlyClassPredicate());
    entity2Wiki.withClasses(DocumentedClasses.enums()).writeSection(out, "Enums");

    String newContent = Joiner.on('\n').join(Files.readLines(tempFile, Charsets.UTF_8));

    new GitHubWikiPublisher(cloneUrl).pushToWiki(pageName, newContent);
  }

  private static void validateArgs(String[] args) {
    if (args.length != 3) {
      System.out.println("You must pass in:");
      System.out.println("1) cloneUrl");
      System.out.println("2) pageName");
      System.out.println("3) sourceRootLocation");
      System.exit(1);
    }
  }

  public GitHubWikiPublisher(String wikiRepositoryUrl) throws IOException, InvalidRemoteException, InvalidRefNameException, RefNotFoundException, RefAlreadyExistsException {
    cloneRepository(wikiRepositoryUrl);
    checkoutRemoteMaster();
  }

  private void cloneRepository(String wikiRepositoryUrl) {
    File tempDir = Files.createTempDir();

    git = Git.cloneRepository()
             .setDirectory(tempDir)
             .setURI(wikiRepositoryUrl)
             .call();
  }

  private void checkoutRemoteMaster() throws IOException, RefAlreadyExistsException, RefNotFoundException, InvalidRefNameException {
    ObjectId masterObjectId = git.getRepository().getRef("remotes/origin/master").getObjectId();
    RevWalk walk = new RevWalk(git.getRepository());
    RevCommit commit = walk.parseCommit(masterObjectId);

    git.checkout()
       .setCreateBranch(true)
        .setStartPoint(commit)
       .setName("master")
       .call();
  }

  public void pushToWiki(String pageName, String contents) throws IOException,
      NoFilepatternException, NoHeadException, NoMessageException,
      ConcurrentRefUpdateException, WrongRepositoryStateException, InvalidRemoteException {

    addNewContent(pageName, contents);
    commitNewContent();
    pushNewContent();
  }

  private void pushNewContent() throws InvalidRemoteException {
    git.push().call();
  }

  private void commitNewContent() throws NoHeadException, NoMessageException, UnmergedPathException, ConcurrentRefUpdateException, WrongRepositoryStateException {
    git.commit()
       .setAuthor("Git Hub Wiki Publisher", "<EMAIL>")
       .setCommitter("Git Hub Wiki Publisher", "<EMAIL>")
       .setMessage("Automatic documentation update")
       .call();
  }

  private void addNewContent(String pageName, String contents) throws IOException, NoFilepatternException {
    File workTree = git.getRepository().getWorkTree();
    String fileName = pageName + ".md";
    File pageFile = new File(workTree, fileName);

    Files.touch(pageFile);

    Files.write(contents, pageFile, Charsets.UTF_8);

    git.add().addFilepattern(fileName).call();
  }

}<commit_msg>improved github wiki publisher<commit_after>package pl.project13.janbanery.entity2wiki;

import com.google.common.base.Charsets;
import com.google.common.base.Joiner;
import com.google.common.io.Files;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.*;
import org.eclipse.jgit.errors.UnmergedPathException;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;

/**
 * @author <NAME>
 */
public class GitHubWikiPublisher {

  Logger log = LoggerFactory.getLogger(getClass());

  Git git;

  public static void main(String... args) throws Exception {
    validateArgs(args);

    int i = 0;
    String cloneUrl = args[i++];
    String pageName = args[i++];
    String sourceRootLocationString = args[i++];

    File tempFile = File.createTempFile("gitHubWikiPublisher", ".md.tmp");
    PrintStream out = new PrintStream(tempFile);

    File sourceRootLocation = new File(sourceRootLocationString);
    Entity2WikiRunner entity2Wiki = new Entity2WikiRunner(sourceRootLocation);

    entity2Wiki.withClasses(DocumentedClasses.resources()).writeSection(out, "Entities", new NotReadOnlyClassPredicate());
    entity2Wiki.withClasses(DocumentedClasses.resources()).writeSection(out, "ReadOnly Entities", new ReadOnlyClassPredicate());
    entity2Wiki.withClasses(DocumentedClasses.enums()).writeSection(out, "Enums");

    String newContent = Joiner.on('\n').join(Files.readLines(tempFile, Charsets.UTF_8));

    new GitHubWikiPublisher(cloneUrl).pushToWiki(pageName, newContent);
  }

  private static void validateArgs(String[] args) {
    if (args.length != 3) {
      System.out.println("You must pass in:");
      System.out.println("1) cloneUrl");
      System.out.println("2) pageName");
      System.out.println("3) sourceRootLocation");
      System.exit(1);
    }
  }

  public GitHubWikiPublisher(String wikiRepositoryUrl) throws IOException, InvalidRemoteException, InvalidRefNameException, RefNotFoundException, RefAlreadyExistsException {
    cloneRepository(wikiRepositoryUrl);
    checkoutRemoteMaster();
  }

  private void cloneRepository(String wikiRepositoryUrl) {
    log.info("Cloning repository: {}", wikiRepositoryUrl);
    File tempDir = Files.createTempDir();

    git = Git.cloneRepository()
             .setDirectory(tempDir)
             .setURI(wikiRepositoryUrl)
             .call();

    log.info("Done cloning repository.");
  }

  private void checkoutRemoteMaster() throws IOException, RefAlreadyExistsException, RefNotFoundException, InvalidRefNameException {
    log.info("Checking out remotes/origin/master -> master branch");
    ObjectId masterObjectId = git.getRepository().getRef("remotes/origin/master").getObjectId();
    RevWalk walk = new RevWalk(git.getRepository());
    RevCommit commit = walk.parseCommit(masterObjectId);

    git.checkout()
       .setCreateBranch(true)
       .setStartPoint(commit)
       .setName("master")
       .call();

    log.info("Done checking out branch.");
  }

  public void pushToWiki(String pageName, String contents) throws IOException,
      NoFilepatternException, NoHeadException, NoMessageException,
      ConcurrentRefUpdateException, WrongRepositoryStateException, InvalidRemoteException {

    addNewContent(pageName, contents);
    commitNewContent();
    pushNewContent();
    cleanupWorkDir();

    log.info("Done pushing new contents to github wiki.");
  }

  private void cleanupWorkDir() throws IOException {
    log.info("Cleaning up work directory...");

    File workTree = git.getRepository().getWorkTree();
    Files.deleteRecursively(workTree);
  }

  private void pushNewContent() throws InvalidRemoteException {
    log.info("Pushing changes to remote...");

    git.push().call();
  }

  private void commitNewContent() throws NoHeadException, NoMessageException, UnmergedPathException, ConcurrentRefUpdateException, WrongRepositoryStateException {
    log.info("Committing new data...");

    String name = "Git Hub Wiki Publisher";
    String email = "<EMAIL>";
    git.commit()
       .setAuthor(name, email)
       .setCommitter(name, email)
       .setMessage("Automatic documentation update")
       .call();
  }

  private void addNewContent(String pageName, String contents) throws IOException, NoFilepatternException {
    log.info("Adding new content to files...");

    File workTree = git.getRepository().getWorkTree();
    String fileName = pageName + ".md";
    File pageFile = new File(workTree, fileName);

    Files.touch(pageFile);

    Files.write(contents, pageFile, Charsets.UTF_8);

    log.info("Running git add {}", fileName);
    git.add().addFilepattern(fileName).call();
  }

}
{'fraction_non_alphanumeric': 0.11586253218403672, 'fraction_numerical': 0.004030001119444755, 'mean_word_length': 4.411265899454876, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6649433', 'n_tokens_mistral': 2602, 'n_tokens_neox': 2518, 'n_words': 523}