<commit_before>/*
 * ====================================================================
 * Copyright (c) 2008 JavaGit Project.  All rights reserved.
 *
 * This software is licensed using the GNU LGPL v2.1 license.  A copy
 * of the license is included with the distribution of this source
 * code in the LICENSE.txt file.  The text of the license can also
 * be obtained at:
 *
 *   http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
 *
 * For more information on the JavaGit project, see:
 *
 *   http://www.javagit.com
 * ====================================================================
 */
package edu.nyu.cs.javagit.api;

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

import edu.nyu.cs.javagit.api.WorkingTree;
import edu.nyu.cs.javagit.api.commands.GitAdd;
import edu.nyu.cs.javagit.api.commands.GitAddResponse;
import edu.nyu.cs.javagit.api.commands.GitCommit;
import edu.nyu.cs.javagit.api.commands.GitCommitResponse;
import edu.nyu.cs.javagit.api.commands.GitMv;
import edu.nyu.cs.javagit.api.commands.GitMvResponse;
import edu.nyu.cs.javagit.api.commands.GitRm;
import edu.nyu.cs.javagit.api.commands.GitRmResponse;
import edu.nyu.cs.javagit.utilities.CheckUtilities;

/**
 * <code>GitFileSystemObject</code> provides some implementation shared by files and directories
 */

// TODO (rs2705): Alphabetize the methods in this class
public abstract class GitFileSystemObject {

  public static enum Status {
    // untracked (created but not added to the repository)
    UNTRACKED,
    // new, waiting to commit
    NEW_TO_COMMIT,
    // in repository and deleted locally
    DELETED,
    // deleted, waiting to commit
    DELETED_TO_COMMIT,
    // changed locally, but not updated
    MODIFIED,
    // changed and added to the index
    MODIFIED_TO_COMMIT,
    // in repository
    IN_REPOSITORY
  }

  /**
   * file path, as specified by the user
   */
  protected File file;
  /**
   * path, relative to working directory; used to pass around data to command API
   */
  protected File relativePath;

  protected WorkingTree workingTree;

  /**
   * The constructor.
   * 
   * @param file
   *          underlying <code>java.io.File</code> object
   */
  protected GitFileSystemObject(File file, WorkingTree workingTree) throws JavaGitException {
    this.workingTree = workingTree;
    this.file = file;
    this.relativePath = getRelativePath(file, workingTree.getPath());
  }

  /**
   * Returns a file, with path relative to git working tree
   * 
   * @param in
   *          input <code>File</code> object
   *        repositoryPath
   *          path to git repository
   * @return
   *        <code>File</code> object with relative path 
   * @throws JavaGitException
   *         input file does not belong to git working tree/ repo
   */
  public static File getRelativePath(File in, File repositoryPath) throws JavaGitException {
    String path = in.getPath();
    String absolutePath = in.getAbsolutePath();

    //check if the path is relative or absolute
    if(path.equals(absolutePath)) {
      //if absolute, make sure it belongs to working tree
      String workingTreePath = repositoryPath.getAbsolutePath();
      if(!path.startsWith(workingTreePath)) {
        throw new JavaGitException(999, "Invalid path :" + path 
            + ". Does not belong to the git working tree/ repository: " + workingTreePath);
      }

      //make path relative
      if(!path.equals(workingTreePath)) {
        path = path.substring(workingTreePath.length()+1);
      }
    }
    
    return new File(path);
  }
  

  @Override
  public boolean equals(Object obj) {
    if (!(obj instanceof GitFileSystemObject)) {
      return false;
    }

    GitFileSystemObject gitObj = (GitFileSystemObject) obj;
    return CheckUtilities.checkObjectsEqual(gitObj.getFile(), file);
  }

  /**
   * Gets the name of the file system object
   * 
   * @return The name
   */
  public String getName() {
    return file.getName();
  }

  /**
   * Gets the underlying <code>java.io.File</code> object
   * 
   * @return <code>java.io.File</code> object
   */
  public File getFile() {
    return file;
  }

  /**
   * Gets parent directory of this <code>GitFileSystemObject</code> object
   * 
   * @return parent directory (null if invalid)
   * 
   */
  public GitDirectory getParent() {
    // NOTE: file.getParentFile() returns null if there is no parent.
    if(file.getParentFile() == null) {
      return null;
    }
    
    try {
      return new GitDirectory(file.getParentFile(), workingTree);
    }
    catch(JavaGitException e) {
      //invalid git parent
      return null;
    }
  }

  /**
   * Adds the object to the git index
   * 
   * @return response from git add
   */
  public GitAddResponse add() throws IOException, JavaGitException {
    GitAdd gitAdd = new GitAdd();

    // create a list of filenames and add yourself to it
    List<File> list = new ArrayList<File>();
    for(File f : relativePath.listFiles()){
    	list.add(f);
    }
    

    // run git-add command
    return gitAdd.add(workingTree.getPath(), null, list);
  }

  /**
   * Commits the file system object
   * 
   * @param comment
   *          Developer's comment
   * 
   * @return response from git commit
   */<commit_msg>fixed build - need to rework logic in adding files and repositories<commit_after>/*
 * ====================================================================
 * Copyright (c) 2008 JavaGit Project.  All rights reserved.
 *
 * This software is licensed using the GNU LGPL v2.1 license.  A copy
 * of the license is included with the distribution of this source
 * code in the LICENSE.txt file.  The text of the license can also
 * be obtained at:
 *
 *   http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
 *
 * For more information on the JavaGit project, see:
 *
 *   http://www.javagit.com
 * ====================================================================
 */
package edu.nyu.cs.javagit.api;

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

import edu.nyu.cs.javagit.api.WorkingTree;
import edu.nyu.cs.javagit.api.commands.GitAdd;
import edu.nyu.cs.javagit.api.commands.GitAddResponse;
import edu.nyu.cs.javagit.api.commands.GitCommit;
import edu.nyu.cs.javagit.api.commands.GitCommitResponse;
import edu.nyu.cs.javagit.api.commands.GitMv;
import edu.nyu.cs.javagit.api.commands.GitMvResponse;
import edu.nyu.cs.javagit.api.commands.GitRm;
import edu.nyu.cs.javagit.api.commands.GitRmResponse;
import edu.nyu.cs.javagit.utilities.CheckUtilities;

/**
 * <code>GitFileSystemObject</code> provides some implementation shared by files and directories
 */

// TODO (rs2705): Alphabetize the methods in this class
public abstract class GitFileSystemObject {

  public static enum Status {
    // untracked (created but not added to the repository)
    UNTRACKED,
    // new, waiting to commit
    NEW_TO_COMMIT,
    // in repository and deleted locally
    DELETED,
    // deleted, waiting to commit
    DELETED_TO_COMMIT,
    // changed locally, but not updated
    MODIFIED,
    // changed and added to the index
    MODIFIED_TO_COMMIT,
    // in repository
    IN_REPOSITORY
  }

  /**
   * file path, as specified by the user
   */
  protected File file;
  /**
   * path, relative to working directory; used to pass around data to command API
   */
  protected File relativePath;

  protected WorkingTree workingTree;

  /**
   * The constructor.
   * 
   * @param file
   *          underlying <code>java.io.File</code> object
   */
  protected GitFileSystemObject(File file, WorkingTree workingTree) throws JavaGitException {
    this.workingTree = workingTree;
    this.file = file;
    this.relativePath = getRelativePath(file, workingTree.getPath());
  }

  /**
   * Returns a file, with path relative to git working tree
   * 
   * @param in
   *          input <code>File</code> object
   *        repositoryPath
   *          path to git repository
   * @return
   *        <code>File</code> object with relative path 
   * @throws JavaGitException
   *         input file does not belong to git working tree/ repo
   */
  public static File getRelativePath(File in, File repositoryPath) throws JavaGitException {
    String path = in.getPath();
    String absolutePath = in.getAbsolutePath();

    //check if the path is relative or absolute
    if(path.equals(absolutePath)) {
      //if absolute, make sure it belongs to working tree
      String workingTreePath = repositoryPath.getAbsolutePath();
      if(!path.startsWith(workingTreePath)) {
        throw new JavaGitException(999, "Invalid path :" + path 
            + ". Does not belong to the git working tree/ repository: " + workingTreePath);
      }

      //make path relative
      if(!path.equals(workingTreePath)) {
        path = path.substring(workingTreePath.length()+1);
      }
    }
    
    return new File(path);
  }
  

  @Override
  public boolean equals(Object obj) {
    if (!(obj instanceof GitFileSystemObject)) {
      return false;
    }

    GitFileSystemObject gitObj = (GitFileSystemObject) obj;
    return CheckUtilities.checkObjectsEqual(gitObj.getFile(), file);
  }

  /**
   * Gets the name of the file system object
   * 
   * @return The name
   */
  public String getName() {
    return file.getName();
  }

  /**
   * Gets the underlying <code>java.io.File</code> object
   * 
   * @return <code>java.io.File</code> object
   */
  public File getFile() {
    return file;
  }

  /**
   * Gets parent directory of this <code>GitFileSystemObject</code> object
   * 
   * @return parent directory (null if invalid)
   * 
   */
  public GitDirectory getParent() {
    // NOTE: file.getParentFile() returns null if there is no parent.
    if(file.getParentFile() == null) {
      return null;
    }
    
    try {
      return new GitDirectory(file.getParentFile(), workingTree);
    }
    catch(JavaGitException e) {
      //invalid git parent
      return null;
    }
  }

  /**
   * Adds the object to the git index
   * 
   * @return response from git add
   */
  public GitAddResponse add() throws IOException, JavaGitException {
    GitAdd gitAdd = new GitAdd();

    // create a list of filenames and add yourself to it
    List<File> list = new ArrayList<File>();
    list.add(relativePath);

    // run git-add command
    return gitAdd.add(workingTree.getPath(), null, list);
  }

  /**
   * Commits the file system object
   * 
   * @param comment
   *          Developer's comment
   * 
   * @return response from git commit
   */
{'fraction_non_alphanumeric': 0.1252950618449627, 'fraction_numerical': 0.0030214332924180906, 'mean_word_length': 2.8841217455078842, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2016036', 'n_tokens_mistral': 3260, 'n_tokens_neox': 3137, 'n_words': 911}