@Test
 public void untrackedWhileDontIncludeUntrackedIsNotDirty() throws Exception {
   assertFalse(EGitUtils.isDirty(testRepository.getRepository(), new NullProgressMonitor()));
   testRepository.createFile("a.txt", "protoculture");
   assertFalse(
       EGitUtils.isDirty(testRepository.getRepository(), false, new NullProgressMonitor()));
 }
 @Test
 public void addedButNotCommittedIsDirty() throws Exception {
   assertFalse(EGitUtils.isDirty(testRepository.getRepository(), new NullProgressMonitor()));
   File file = testRepository.createFile("a.txt", "protoculture");
   testRepository.add(file);
   assertTrue(EGitUtils.isDirty(testRepository.getRepository(), new NullProgressMonitor()));
 }
 @Test
 public void newUntrackedFileIsNotCountedIfIgnoreUntracked() throws Exception {
   assertFalse(EGitUtils.isDirty(testRepository.getRepository(), new NullProgressMonitor()));
   testRepository.createFile(testProject.getProject(), "a.txt", "42");
   int numOfChanges =
       EGitUtils.countChanges(testRepository.getRepository(), false, new NullProgressMonitor());
   assertEquals(0, numOfChanges);
 }
 @Test
 public void removedButNotCommittedIsDirty() throws Exception {
   assertFalse(EGitUtils.isDirty(testRepository.getRepository(), new NullProgressMonitor()));
   File file = testRepository.createFile("a.txt", "protonica");
   testRepository.addAndCommit(file, "commit-by-junit-tests");
   assertFalse(EGitUtils.isDirty(testRepository.getRepository(), new NullProgressMonitor()));
   file.delete();
   assertTrue(EGitUtils.isDirty(testRepository.getRepository(), new NullProgressMonitor()));
 }
  @Test
  public void canGetSingleRemoteConfig()
      throws CoreException, MalformedURLException, URISyntaxException, IOException {
    String remoteName = "repo2";

    testRepository.addRemoteTo(remoteName, testRepository2.getRepository());
    List<RemoteConfig> allRemoteConfigs =
        EGitUtils.getAllRemoteConfigs(testRepository.getRepository());
    assertNotNull(allRemoteConfigs);
    assertEquals(1, allRemoteConfigs.size());
    RemoteConfig repo2Config = EGitUtils.getRemoteConfig(remoteName, allRemoteConfigs);
    assertNotNull(repo2Config);
  }
  @Test
  public void shouldNotBeAheadAfterPush() throws Exception {
    String fileName = "a.txt";
    String fileContent = "*****@*****.**";
    File file = testRepository.createFile(fileName, fileContent);
    testRepository.addAndCommit(file, "adding a file");
    testRepository.addRemoteTo(REPO2_REMOTE_NAME, testRepository2.getRepository());

    assertTrue(EGitUtils.isAhead(testRepository.getRepository(), REPO2_REMOTE_NAME, null));

    EGitUtils.pushForce(REPO2_REMOTE_NAME, testRepository.getRepository(), null);

    assertFalse(EGitUtils.isAhead(testRepository.getRepository(), REPO2_REMOTE_NAME, null));
  }
  @Test
  public void canGetFromSeveralRemoteConfig()
      throws CoreException, MalformedURLException, URISyntaxException, IOException {
    String repo2RemoteName = "repo2";

    testRepositoryClone.addRemoteTo(repo2RemoteName, testRepository2.getRepository());
    List<RemoteConfig> allRemoteConfigs =
        EGitUtils.getAllRemoteConfigs(testRepositoryClone.getRepository());
    assertNotNull(allRemoteConfigs);
    // clone already has repo1 as origin
    assertEquals(2, allRemoteConfigs.size());
    RemoteConfig repo2Config = EGitUtils.getRemoteConfig(repo2RemoteName, allRemoteConfigs);
    assertNotNull(repo2Config);
  }
  @Test
  public void shouldReturnThatCloneIsAhead() throws Exception {
    assertFalse(
        EGitUtils.isAhead(
            testRepositoryClone.getRepository(), Constants.DEFAULT_REMOTE_NAME, null));

    String fileName = "c.txt";
    String fileContent = "*****@*****.**";
    File file = testRepositoryClone.createFile(fileName, fileContent);
    testRepositoryClone.addAndCommit(file, "adding a file");

    assertTrue(
        EGitUtils.isAhead(
            testRepositoryClone.getRepository(), Constants.DEFAULT_REMOTE_NAME, null));
  }
 @Test
 public void shouldConsiderSCPGitUrlValid() {
   // pre-conditions
   // operation
   assertTrue(EGitUtils.isValidGitUrl("[email protected]:openshift/eap2.git/"));
   // verification
 }
 public void shouldReturnHostFromHostWithoutPathGitUrl() {
   // pre-conditions
   // operation
   String host = EGitUtils.getGitHost("ssh://[email protected]");
   // verification
   assertNotNull(host);
   assertEquals("eap2-honkabonka2.rhcloud.com", host);
 }
 public void shouldReturnHostFromHostOnlyGitUrl() {
   // pre-conditions
   // operation
   String host = EGitUtils.getGitHost("ssh://eap2-honkabonka2.rhcloud.com/~/git/eap2.git/");
   // verification
   assertNotNull(host);
   assertEquals("eap2-honkabonka2.rhcloud.com", host);
 }
 public void shouldReturnNullFromInvalidGitUrl() {
   // pre-conditions
   // operation
   String host =
       EGitUtils.getGitHost(
           "://[email protected]/~/git/eap2.git/");
   // verification
   assertNull(host);
 }
 @Test
 public void shouldConsiderSSHGitUrlValid() {
   // pre-conditions
   // operation
   assertTrue(
       EGitUtils.isValidGitUrl(
           "ssh://[email protected]/~/git/eap2.git/"));
   // verification
 }
  @Test
  public void shouldReturnThatCloneIsAheadOfRemote() throws Exception {
    testRepositoryClone.addRemoteTo(REPO2_REMOTE_NAME, testRepository2.getRepository());
    new Git(testRepositoryClone.getRepository())
        .push()
        .setRemote(REPO2_REMOTE_NAME)
        .setForce(true)
        .call();
    assertFalse(EGitUtils.isAhead(testRepositoryClone.getRepository(), REPO2_REMOTE_NAME, null));

    String fileName = "c.txt";
    String fileContent = "*****@*****.**";
    File file = testRepositoryClone.createFile(fileName, fileContent);
    testRepositoryClone.addAndCommit(file, "adding a file");
    assertTrue(EGitUtils.isAhead(testRepositoryClone.getRepository(), REPO2_REMOTE_NAME, null));

    new Git(testRepositoryClone.getRepository()).push().setForce(true).call();
    assertTrue(EGitUtils.isAhead(testRepositoryClone.getRepository(), REPO2_REMOTE_NAME, null));
  }
 @Test
 public void shouldReturnUserFromFullGitUrl() {
   // pre-conditions
   // operation
   String user =
       EGitUtils.getGitUsername(
           "ssh://[email protected]/~/git/eap2.git/");
   // verification
   assertNotNull(user);
   assertEquals("516e82ca4382ec2174000098", user);
 }
 @Test
 public void shouldReturnHostFromFullGitUrl() {
   // pre-conditions
   // operation
   String host =
       EGitUtils.getGitHost(
           "ssh://[email protected]/~/git/eap2.git/");
   // verification
   assertNotNull(host);
   assertEquals("eap2-honkabonka2.rhcloud.com", host);
 }
  @Test
  public void originShouldBeDefaultRemoteRepo() throws Exception {
    // pre-conditions
    testRepository.addRemoteTo("git", "git://git.stuff/");
    testRepository.addRemoteTo("foo", "https://foo.bar/");
    testRepository.addRemoteTo("bar", "http://bar.foo/");

    // Get 1st remote after remotes were ordered alphabetically
    // operation
    String defaultRepo = EGitUtils.getDefaultRemoteRepo(testProject.getProject());
    // verification
    assertEquals("http://bar.foo/", defaultRepo);

    // Check origin is always default
    testRepository.addRemoteTo("origin", "http://origin/");
    // operation
    defaultRepo = EGitUtils.getDefaultRemoteRepo(testProject.getProject());
    // verification
    assertEquals("http://origin/", defaultRepo);
  }
  @Test
  public void canCheckIfHasRemote()
      throws CoreException, MalformedURLException, URISyntaxException, IOException {
    String repo2RemoteName = "repo2";

    testRepositoryClone.addRemoteTo(repo2RemoteName, testRepository2.getRepository());
    assertTrue(
        EGitUtils.hasRemote(
            repo2RemoteName,
            testRepository2.getUri().toString(),
            testRepositoryClone.getRepository()));
  }
  @Test(expected = CoreException.class)
  public void pushFailsOnNonFastForward() throws Exception {
    String fileName = "a.txt";
    String fileContent = "*****@*****.**";
    File file = testRepository.createFile(fileName, fileContent);
    testRepository.addAndCommit(file, "adding a file");

    File file2 = testRepository2.createFile("b.txt", "bingobongo");
    testRepository2.addAndCommit(file2, "adding a file");

    testRepository.addRemoteTo(REPO2_REMOTE_NAME, testRepository2.getRepository());
    EGitUtils.push(REPO2_REMOTE_NAME, testRepository.getRepository(), null);
  }
  @Test
  public void fileAddedToCloneIsInRemoteAfterPush() throws Exception {
    String fileName = "c.txt";
    String fileContent = "*****@*****.**";
    File file = testRepositoryClone.createFile(fileName, fileContent);
    testRepositoryClone.addAndCommit(file, "adding a file");

    testRepositoryClone.addRemoteTo(REPO2_REMOTE_NAME, testRepository2.getRepository());
    EGitUtils.pushForce(REPO2_REMOTE_NAME, testRepositoryClone.getRepository(), null);

    // repo2 must contain file added to clone
    testUtils.assertRepositoryContainsFilesWithContent(
        testRepository2.getRepository(), fileName, fileContent);
  }
  @Test
  public void fileAddedToCloneIsInOriginAfterPush() throws Exception {
    String fileName = "b.txt";
    String fileContent = "*****@*****.**";

    File file = testRepositoryClone.createFile(fileName, fileContent);
    testRepositoryClone.addAndCommit(file, "adding a file");

    EGitUtils.push(testRepositoryClone.getRepository(), null);

    // does origin contain file added to clone?
    testUtils.assertRepositoryContainsFilesWithContent(
        testRepository.getRepository(), fileName, fileContent);
  }
  @Test
  public void canCommitFileInProject() throws Exception {
    String fileName = "a.txt";
    String fileContent = "*****@*****.**";

    IFile file = testUtils.addFileToProject(testProject.getProject(), fileName, fileContent);
    testRepository.add(file);

    EGitUtils.commit(testProject.getProject(), null);

    testUtils.assertRepositoryContainsFilesWithContent(
        testRepository.getRepository(),
        new String[] {testUtils.getPathInRepository(file), fileContent});
  }
  @Test
  public void canAddRemoteRepo() throws Exception {
    Repository repository = testRepository.getRepository();
    String remoteName = "redhat";
    String gitUri = "www.redhat.com";
    EGitUtils.addRemoteTo(remoteName, gitUri, repository);

    StoredConfig config = repository.getConfig();
    Set<String> subsections = config.getSubsections(ConfigConstants.CONFIG_REMOTE_SECTION);
    assertEquals(1, subsections.size());
    assertTrue(subsections.contains(remoteName));
    assertEquals(
        gitUri,
        config.getString(
            ConfigConstants.CONFIG_REMOTE_SECTION, remoteName, ConfigConstants.CONFIG_KEY_URL));
  }
  @Test
  public void getRemoteHttpRepos() throws Exception {
    // pre-conditions
    testRepository.addRemoteTo("git", "git://git.stuff/");
    testRepository.addRemoteTo("foo", "https://foo.bar/");
    testRepository.addRemoteTo("bar", "http://bar.foo/");
    testRepository.addRemoteTo("origin", "http://origin/");

    // operation
    List<String> repos = EGitUtils.getRemoteGitRepos(testProject.getProject());

    // verification
    assertEquals(3, repos.size());
    assertEquals("http://origin/", repos.get(0));
    assertEquals("http://bar.foo/", repos.get(1));
    assertEquals("https://foo.bar/", repos.get(2));
  }
  @Test
  public void shouldUserFetchSpecInConfig() throws Exception {
    testRepository.addRemoteTo(REPO2_REMOTE_NAME, testRepository2.getRepository());

    // add custom fetch-spec in config (fetch = refs/heads/master:refs/remotes/bingo/master)
    StoredConfig config = testRepository.getRepository().getConfig();
    config.getString(ConfigConstants.CONFIG_KEY_REMOTE, REPO2_REMOTE_NAME, "fetch");
    String remoteTrackingBranchRef = "refs/remotes/bingo/master";
    String fetchSpec = "refs/heads/master:" + remoteTrackingBranchRef;
    config.setString(ConfigConstants.CONFIG_KEY_REMOTE, REPO2_REMOTE_NAME, "fetch", fetchSpec);
    config.save();

    EGitUtils.isAhead(testRepository.getRepository(), REPO2_REMOTE_NAME, null);

    // was remote tracking branch created?
    assertTrue(testRepository.getRepository().getAllRefs().containsKey(remoteTrackingBranchRef));
  }
  @Test
  public void forcedPushRemovesFileInRemote() throws Exception {
    String fileName = "a.txt";
    String fileContent = "*****@*****.**";
    File file = testRepository.createFile(fileName, fileContent);
    testRepository.addAndCommit(file, "adding a file");

    File file2 = testRepository2.createFile("b.txt", "bingobongo");
    testRepository2.addAndCommit(file2, "adding a file");

    testRepository.addRemoteTo(REPO2_REMOTE_NAME, testRepository2.getRepository());
    EGitUtils.pushForce(REPO2_REMOTE_NAME, testRepository.getRepository(), null);

    // repo2 mustn't contain "b.txt"
    testUtils.assertRepositoryMisses(testRepository2.getRepository(), file2.getName());
    // repo2 must contain "a.txt"
    testUtils.assertRepositoryContainsFilesWithContent(
        testRepository2.getRepository(), fileName, fileContent);
  }
 @Test
 public void canGetRepoForProject() throws Exception {
   Repository repository = EGitUtils.getRepository(testProject.getProject());
   assertNotNull(repository);
   assertEquals(testRepository.getRepository(), repository);
 }
 @Test
 public void untrackedWhileIncludeUntrackedIsDirty() throws Exception {
   assertFalse(EGitUtils.isDirty(testRepository.getRepository(), new NullProgressMonitor()));
   testRepository.createFile(testProject.getProject(), "a.txt", "42");
   assertTrue(EGitUtils.isDirty(testRepository.getRepository(), true, new NullProgressMonitor()));
 }