<commit_before>package com.google.copybara.folder;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.copybara.Author;
import com.google.copybara.Authoring;
import com.google.copybara.Change;
import com.google.copybara.Origin;
import com.google.copybara.RepoException;
import com.google.copybara.ValidationException;
import com.google.copybara.util.FileUtil;
import com.google.copybara.util.Glob;
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import javax.annotation.Nullable;

/**
 * Use a folder as the input for the migration.
 */
public class FolderOrigin implements Origin<FolderReference> {

  private static final String LABEL_NAME = "FolderOrigin-RevId";
  private final FileSystem fs;
  private final Author author;
  private final String message;

  FolderOrigin(FileSystem fs, Author author, String message) {
    this.fs = Preconditions.checkNotNull(fs);
    this.author = author;
    this.message = message;
  }

  @Override
  public FolderReference resolve(@Nullable String reference) throws RepoException {
    if (reference == null) {
      throw new RepoException("A path is expected as reference in the command line");
    }
    Path path = fs.getPath(reference);
    if (!Files.exists(path)) {
      throw new RepoException(path + " folder doesn't exist");
    } else if (!Files.isDirectory(path)) {
      throw new RepoException(path + " is not a folder");
    } else if (!Files.isReadable(path) || !Files.isExecutable(path)) {
      throw new RepoException(path + " is not readable/executable");
    }

    return new FolderReference(path, Instant.now(), LABEL_NAME);
  }

  @Override
  public Reader<FolderReference> newReader(Glob originFiles, Authoring authoring)
      throws ValidationException {
    return new Reader<FolderReference>() {
      @Override
      public void checkout(FolderReference ref, Path workdir) throws RepoException {
        try {
          FileUtil.copyFilesRecursively(ref.path, workdir);
        } catch (IOException e) {
          throw new RepoException(String.format("Cannot copy files into the workdir:\n"
                      + "  origin folder: %s\n"
                      + "  workdir: %s",
                  ref.path, workdir), e);
        }
      }

      @Override
      public ImmutableList<Change<FolderReference>> changes(@Nullable FolderReference fromRef,
          FolderReference toRef) throws RepoException {
        // Ignore fromRef since a folder doesn't have history of changes
        return ImmutableList.of(change(toRef));
      }

      @Override
      public Change<FolderReference> change(FolderReference ref) throws RepoException {
        ZonedDateTime time = ZonedDateTime.ofInstant(
            Preconditions.checkNotNull(ref.readTimestamp()), ZoneId.systemDefault());
        return new Change<>(ref, author, message, time, ImmutableMap.of());
      }

      @Override
      public void visitChanges(FolderReference start, ChangesVisitor visitor) throws RepoException {
        visitor.visit(change(start));
      }
    };
  }

  @Override
  public String getLabelName() {
    return LABEL_NAME;
  }
}
<commit_msg>Add more information on folder.origin reference error<commit_after>package com.google.copybara.folder;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.copybara.Author;
import com.google.copybara.Authoring;
import com.google.copybara.Change;
import com.google.copybara.Origin;
import com.google.copybara.RepoException;
import com.google.copybara.ValidationException;
import com.google.copybara.util.FileUtil;
import com.google.copybara.util.Glob;
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import javax.annotation.Nullable;

/**
 * Use a folder as the input for the migration.
 */
public class FolderOrigin implements Origin<FolderReference> {

  private static final String LABEL_NAME = "FolderOrigin-RevId";
  private final FileSystem fs;
  private final Author author;
  private final String message;

  FolderOrigin(FileSystem fs, Author author, String message) {
    this.fs = Preconditions.checkNotNull(fs);
    this.author = author;
    this.message = message;
  }

  @Override
  public FolderReference resolve(@Nullable String reference) throws RepoException {
    if (reference == null) {
      throw new RepoException(""
          + "A path is expected as reference in the command line. Invoke copybara as:\n"
          + "    copybara copy.bara.sky workflow_name ORIGIN_FOLDER");
    }
    Path path = fs.getPath(reference);
    if (!Files.exists(path)) {
      throw new RepoException(path + " folder doesn't exist");
    } else if (!Files.isDirectory(path)) {
      throw new RepoException(path + " is not a folder");
    } else if (!Files.isReadable(path) || !Files.isExecutable(path)) {
      throw new RepoException(path + " is not readable/executable");
    }

    return new FolderReference(path, Instant.now(), LABEL_NAME);
  }

  @Override
  public Reader<FolderReference> newReader(Glob originFiles, Authoring authoring)
      throws ValidationException {
    return new Reader<FolderReference>() {
      @Override
      public void checkout(FolderReference ref, Path workdir) throws RepoException {
        try {
          FileUtil.copyFilesRecursively(ref.path, workdir);
        } catch (IOException e) {
          throw new RepoException(String.format("Cannot copy files into the workdir:\n"
                      + "  origin folder: %s\n"
                      + "  workdir: %s",
                  ref.path, workdir), e);
        }
      }

      @Override
      public ImmutableList<Change<FolderReference>> changes(@Nullable FolderReference fromRef,
          FolderReference toRef) throws RepoException {
        // Ignore fromRef since a folder doesn't have history of changes
        return ImmutableList.of(change(toRef));
      }

      @Override
      public Change<FolderReference> change(FolderReference ref) throws RepoException {
        ZonedDateTime time = ZonedDateTime.ofInstant(
            Preconditions.checkNotNull(ref.readTimestamp()), ZoneId.systemDefault());
        return new Change<>(ref, author, message, time, ImmutableMap.of());
      }

      @Override
      public void visitChanges(FolderReference start, ChangesVisitor visitor) throws RepoException {
        visitor.visit(change(start));
      }
    };
  }

  @Override
  public String getLabelName() {
    return LABEL_NAME;
  }
}

{'fraction_non_alphanumeric': 0.09783083418255932, 'fraction_numerical': 0.0, 'mean_word_length': 3.3982074263764406, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '2267842', 'n_tokens_mistral': 1932, 'n_tokens_neox': 1886, 'n_words': 500}