Example #1
0
  public void testSwitchBranchClosesOpenProjectsThatDontExistOnDestinationBranch()
      throws Throwable {
    testAddBranch();

    assertCurrentBranch("master");
    assertSwitchBranch("my_new_branch");

    GitIndex index = getRepo().index();
    assertTrue("Expected changed file listing to be empty", index.changedFiles().isEmpty());

    // Create a new project on this branch!
    String projectName = "project_on_branch" + System.currentTimeMillis();

    File projectDir = getRepo().workingDirectory().append(projectName).toFile();
    projectDir.mkdirs();

    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    IProject project = workspace.getRoot().getProject(projectName);

    IProjectDescription description = workspace.newProjectDescription(projectName);
    description.setLocation(getRepo().workingDirectory().append(projectName));
    project.create(description, new NullProgressMonitor());

    // Commit the project on this branch!
    index.refresh(new NullProgressMonitor());

    // Now there should be a single file that's been changed!
    List<ChangedFile> changed = index.changedFiles();
    assertEquals(
        "repository changed file listing should contain one entry for new .project file, but does not",
        1,
        changed.size());

    // Make sure it's shown as having unstaged changes only and is NEW
    assertNewUnstagedFile(changed.get(0));

    // Stage the new file
    assertStageFiles(index, changed);
    assertNewStagedFile(changed.get(0));

    assertCommit(index, "Initial commit");

    assertSwitchBranch("master");

    // Assert that the new project is closed!
    project = workspace.getRoot().getProject(projectName);
    assertFalse(project.isOpen());

    // assert that there's no .project file stranded there
    File dotProject = new File(projectDir, IProjectDescription.DESCRIPTION_FILE_NAME);
    assertFalse(dotProject.exists());
  }
  public void testRefresh() throws Exception {
    GitRepository repo = getRepo();
    // Write 1000 small files to the repo
    writeFiles(repo.workingDirectory(), 1000);

    GitIndex index = repo.index();
    for (int i = 0; i < 1200; i++) {
      startMeasuring();
      index.refresh(null);
      stopMeasuring();
    }
    commitMeasurements();
    assertPerformance();
  }
Example #3
0
  public void testDeleteUnMergedBranch() throws Throwable {
    testAddBranch();

    assertSwitchBranch("my_new_branch");

    // Now we need to make changes, commit and then switch back to master
    GitIndex index = getRepo().index();

    // TODO Refactor out common code with testAddFileStageUnstageCommit
    // Actually add a file to the location
    String txtFile = getRepo().workingDirectory() + File.separator + "file_on_branch.txt";
    FileWriter writer = new FileWriter(txtFile);
    writer.write("Hello Branched World!");
    writer.close();
    // refresh the index
    index.refresh(new NullProgressMonitor());

    // Now there should be a single file that's been changed!
    List<ChangedFile> changedFiles = index.changedFiles();
    assertEquals(
        "repository changed file listing should contain one entry for a new file_on_branch.txt file, but does not",
        1,
        changedFiles.size());

    // Make sure it's shown as having unstaged changes only and is NEW
    assertNewUnstagedFile(changedFiles.get(0));

    // Stage the new file
    assertStageFiles(index, changedFiles);
    assertNewStagedFile(changedFiles.get(0));

    assertCommit(index, "Initial commit");

    // Now switch to master
    assertSwitchBranch("master");

    IStatus status = getRepo().deleteBranch("my_new_branch");
    assertFalse(
        "Deleting an umerged branch didn't return an error status (as it should)", status.isOK());
    assertEquals(1, status.getCode());
    // Can't rely on the unmerged failure message from git to remain the same across versions.
    // assertEquals(
    // "error: The branch 'my_new_branch' is not an ancestor of your current HEAD.\nIf you are sure
    // you want to delete it, run 'git branch -D my_new_branch'.",
    // status.getMessage());
  }
Example #4
0
  public void testCommitMessageWithDoubleQuotes() throws Throwable {
    GitRepository repo = createRepo();
    GitIndex index = repo.index();
    assertTrue(index.changedFiles().isEmpty());

    // Actually add a file to the location
    FileWriter writer = new FileWriter(fileToAdd());
    writer.write("Hello World!");
    writer.close();
    // refresh the index
    index.refresh(new NullProgressMonitor());

    // Now there should be a single file that's been changed!
    List<ChangedFile> changed = index.changedFiles();
    assertEquals(
        "Repository changed file listing should contain one entry for the new file, but does not",
        1,
        changed.size());

    // Make sure it's shown as having unstaged changes only and is NEW
    assertNewUnstagedFile(changed.get(0));

    // stage
    assertStageFiles(index, changed);
    assertNewStagedFile(changed.get(0));

    // commit
    final String commitMessage = "Initial commit with \"double quotes\" inside the message!";
    assertCommit(index, commitMessage);

    // now grab the resulting log to see if the message escaped the quotes too many times!
    File file = repo.gitFile(GitRepository.COMMIT_EDITMSG);
    FileInputStream stream = null;
    try {
      stream = new FileInputStream(file);
      String result = IOUtil.read(stream);
      assertEquals(commitMessage + "\n", result);
    } finally {
      if (stream != null) {
        stream.close();
      }
    }
  }
Example #5
0
  public void testDeleteFile() throws Throwable {
    testAddFileStageUnstageAndCommit();
    // Now delete the file we committed!
    File addedFile = new File(fileToAdd());
    // make sure it's there first
    assertTrue("File we want to delete through git repo doesn't exist", addedFile.exists());
    // delete it
    IStatus status = getRepo().deleteFile(addedFile.getName());
    assertTrue(
        MessageFormat.format("Deleting file in git repo returned an error status: {0}", status),
        status.isOK());
    // make sure its deleted from filesystem
    assertFalse("Deleted file through git, file still exists", addedFile.exists());

    // Check the changed files and make sure it shows up as changed: DELETED, unstaged
    GitIndex index = getRepo().index();
    assertRefresh(index);

    // Now there should be a single file that's been changed!
    List<ChangedFile> changedFiles = index.changedFiles();
    assertEquals(
        "Repository changed file listing should contain one entry for the deleted file, but does not",
        1,
        changedFiles.size());

    // Make sure it's shown as having staged changes only and is DELETED
    assertDeletedStagedFile(changedFiles.get(0));

    // unstage
    assertUnstageFiles(index, changedFiles);
    assertDeletedUnstagedFile(changedFiles.get(0));

    // stage
    assertStageFiles(index, changedFiles);
    assertDeletedStagedFile(changedFiles.get(0));

    // commit
    assertCommit(index, "Delete files");
  }
Example #6
0
  public void testAddFileStageUnstageAndCommit() throws Exception {
    GitRepository repo = createRepo();
    GitIndex index = repo.index();
    assertTrue(index.changedFiles().isEmpty());

    // Actually add a file to the location
    FileWriter writer = new FileWriter(fileToAdd());
    writer.write("Hello World!");
    writer.close();
    // refresh the index
    assertRefresh();

    // Now there should be a single file that's been changed!
    List<ChangedFile> changed = index.changedFiles();
    assertEquals(
        "Repository changed file listing should contain one entry for the new file, but does not",
        1,
        changed.size());

    // Make sure it's shown as having unstaged changes only and is NEW
    assertNewUnstagedFile(changed.get(0));

    // Stage the new file
    assertStageFiles(index, changed);
    assertNewStagedFile(changed.get(0));

    // Unstage the file
    assertUnstageFiles(index, changed);
    assertNewUnstagedFile(changed.get(0));

    // Stage
    assertStageFiles(index, changed);
    assertNewStagedFile(changed.get(0));

    // Commit
    assertCommit(index, "Initial commit");
  }
Example #7
0
  // Test modifying file that isn't new (already checked in)
  public void testModifyCheckedInFile() throws Throwable {
    testAddFileStageUnstageAndCommit();

    GitIndex index = getRepo().index();

    // Actually add a file to the location
    FileWriter writer = new FileWriter(fileToAdd(), true);
    writer.write("\nHello second line!");
    writer.close();
    // refresh the index
    assertRefresh();

    // Now there should be a single file that's been changed!
    List<ChangedFile> changed = index.changedFiles();
    assertEquals(
        "Repository changed file listing should contain one entry for a new file, but does not",
        1,
        changed.size());

    // Make sure it's shown as having unstaged changes only and is MODIFIED
    assertModifiedUnstagedFile(changed.get(0));

    // stage
    assertStageFiles(index, changed);
    assertModifiedStagedFile(changed.get(0));

    // unstage
    assertUnstageFiles(index, changed);
    assertModifiedUnstagedFile(changed.get(0));

    // stage
    assertStageFiles(index, changed);
    assertModifiedStagedFile(changed.get(0));

    // commit
    assertCommit(index, "Add second line");
  }