Exemplo n.º 1
0
  public void renameRole(String roleName, String newRoleName, User currentUser) throws IOException {
    Assert.hasLength(roleName);
    Assert.hasLength(newRoleName);
    Assert.notNull(currentUser);
    // check that role exists by trying to load it
    getRole(roleName);
    // check that new role does not exist by trying to load it
    try {
      getRole(newRoleName);
      throw new IllegalArgumentException("role already exists: " + newRoleName); // $NON-NLS-1$
    } catch (RoleNotFoundException e) {
      // okay
    }

    log.info("renaming role: {} -> {}", roleName, newRoleName); // $NON-NLS-1$

    ILockedRepository repo = null;
    try {
      repo = globalRepositoryManager.getProjectCentralRepository(REPOSITORY_NAME, false);

      File workingDir = RepositoryUtil.getWorkingDir(repo.r());

      File file = new File(workingDir, roleName + ROLE_SUFFIX);
      File newFile = new File(workingDir, newRoleName + ROLE_SUFFIX);
      FileUtils.copyFile(file, newFile);
      Git git = Git.wrap(repo.r());
      git.rm().addFilepattern(roleName + ROLE_SUFFIX).call();
      git.add().addFilepattern(newRoleName + ROLE_SUFFIX).call();

      List<String> users = listUsers(repo);
      users.add(ANONYMOUS_USER_LOGIN_NAME);
      for (String user : users) {
        List<RoleGrantedAuthority> authorities = getUserAuthorities(user, repo);
        Set<RoleGrantedAuthority> newAuthorities = Sets.newHashSet();
        for (Iterator<RoleGrantedAuthority> iter = authorities.iterator(); iter.hasNext(); ) {
          RoleGrantedAuthority rga = iter.next();
          if (rga.getRoleName().equals(roleName)) {
            RoleGrantedAuthority newRga = new RoleGrantedAuthority(rga.getTarget(), newRoleName);
            newAuthorities.add(newRga);
            iter.remove();
          }
        }
        if (!newAuthorities.isEmpty()) {
          authorities.addAll(newAuthorities);
          saveUserAuthorities(user, Sets.newHashSet(authorities), repo, currentUser, false);
        }
      }

      PersonIdent ident = new PersonIdent(currentUser.getLoginName(), currentUser.getEmail());
      git.commit()
          .setAuthor(ident)
          .setCommitter(ident)
          .setMessage("rename role " + roleName + " to " + newRoleName) // $NON-NLS-1$ //$NON-NLS-2$
          .call();
    } catch (GitAPIException e) {
      throw new IOException(e);
    } finally {
      Util.closeQuietly(repo);
    }
  }
  private void doWriteGitViews(
      @Nonnull String datasourceName, @Nonnull Iterable<View> views, @Nullable String comment) {
    File localRepo = null;

    try {
      // Fetch or clone a tmp working directory
      localRepo = cloneDatasourceViewsGit(datasourceName);

      // Serialize all view files in the repo
      List<String> varFilesToRemove = Lists.newArrayList();
      StringBuilder message = new StringBuilder();
      for (View view : views) {
        doWriteGitView(localRepo, view, varFilesToRemove);
        if (message.length() > 0) {
          message.append(", ");
        }
        message.append(view.getName());
      }

      // Push changes
      Git git = new Git(new FileRepository(new File(localRepo, ".git")));
      for (String toRemove : varFilesToRemove) {
        git.rm().addFilepattern(toRemove).call();
      }
      git.add().addFilepattern(".").call();
      doCommitPush(git, Strings.isNullOrEmpty(comment) ? "Update " + message : comment);

    } catch (Exception e) {
      throw new RuntimeException(
          "Failed writing views in git for datasource: " + datasourceName, e);
    } finally {
      if (localRepo != null) localRepo.delete();
    }
  }
 /*
  * (non-Javadoc)
  *
  * @see nl.minicom.gitolite.manager.git.GitManager#remove(java.lang.String)
  */
 @Override
 public void remove(String filePattern) throws IOException {
   RmCommand rm = git.rm();
   rm.addFilepattern(filePattern);
   try {
     rm.call();
   } catch (NoFilepatternException e) {
     throw new IOException(e);
   }
 }
Exemplo n.º 4
0
 @Override
 public void deleteDirectory(File dir) {
   Repository repository = getRepository(dir);
   persistence.deleteDirectory(dir);
   Git git = new Git(repository);
   try {
     git.rm().addFilepattern(getPath(dir, repository)).call();
     commit(git, String.format("FitNesse directory %s deleted.", dir.getName()));
   } catch (GitAPIException e) {
     throw new RuntimeException(e);
   }
 }
Exemplo n.º 5
0
 @Override
 public void renameFile(File file, File oldFile) {
   Repository repository = getRepository(file);
   persistence.renameFile(file, oldFile);
   Git git = new Git(repository);
   try {
     git.add().addFilepattern(getPath(file, repository)).call();
     git.rm().addFilepattern(getPath(oldFile, repository)).call();
     commit(
         git, String.format("FitNesse file %s moved to %s.", oldFile.getName(), file.getName()));
   } catch (GitAPIException e) {
     throw new RuntimeException(e);
   }
 }
 private void doRemoveGitView(@Nonnull String datasourceName, @Nonnull String viewName) {
   File localRepo = null;
   try {
     localRepo = cloneDatasourceViewsGit(datasourceName);
     Git git = new Git(new FileRepository(new File(localRepo, ".git")));
     git.rm().addFilepattern(viewName);
     doCommitPush(git, "Remove " + viewName);
   } catch (Exception e) {
     throw new RuntimeException(
         "Failed removing view '" + viewName + "' from git for datasource: " + datasourceName, e);
   } finally {
     if (localRepo != null) localRepo.delete();
   }
 }
Exemplo n.º 7
0
  public void renameUser(String loginName, String newLoginName, User currentUser)
      throws IOException {
    Assert.hasLength(loginName);
    Assert.hasLength(newLoginName);
    Assert.notNull(currentUser);
    // check that user exists by trying to load it
    getUser(loginName);
    // check that new user does not exist by trying to load it
    try {
      getUser(newLoginName);
      throw new IllegalArgumentException("user already exists: " + newLoginName); // $NON-NLS-1$
    } catch (UserNotFoundException e) {
      // okay
    }

    ILockedRepository repo = null;
    try {
      repo = globalRepositoryManager.getProjectCentralRepository(REPOSITORY_NAME, false);

      File workingDir = RepositoryUtil.getWorkingDir(repo.r());
      File file = new File(workingDir, loginName + USER_SUFFIX);
      File newFile = new File(workingDir, newLoginName + USER_SUFFIX);
      FileUtils.copyFile(file, newFile);
      file = new File(workingDir, loginName + AUTHORITIES_SUFFIX);
      newFile = new File(workingDir, newLoginName + AUTHORITIES_SUFFIX);
      FileUtils.copyFile(file, newFile);
      Git git = Git.wrap(repo.r());
      git.rm().addFilepattern(loginName + USER_SUFFIX).call();
      git.rm().addFilepattern(loginName + AUTHORITIES_SUFFIX).call();
      git.add().addFilepattern(newLoginName + USER_SUFFIX).call();
      git.add().addFilepattern(newLoginName + AUTHORITIES_SUFFIX).call();
      PersonIdent ident = new PersonIdent(currentUser.getLoginName(), currentUser.getEmail());
      git.commit()
          .setAuthor(ident)
          .setCommitter(ident)
          .setMessage(
              "rename user " + loginName + " to " + newLoginName) // $NON-NLS-1$ //$NON-NLS-2$
          .call();
    } catch (GitAPIException e) {
      throw new IOException(e);
    } finally {
      Util.closeQuietly(repo);
    }
  }
Exemplo n.º 8
0
  public void deleteRole(String roleName, User currentUser) throws IOException {
    Assert.hasLength(roleName);
    Assert.notNull(currentUser);
    // check that role exists by trying to load it
    getRole(roleName);

    ILockedRepository repo = null;
    try {
      repo = globalRepositoryManager.getProjectCentralRepository(REPOSITORY_NAME, false);
      Git git = Git.wrap(repo.r());

      git.rm().addFilepattern(roleName + ROLE_SUFFIX).call();

      // remove role from all users
      List<String> users = listUsers(repo);
      users.add(ANONYMOUS_USER_LOGIN_NAME);
      for (String user : users) {
        List<RoleGrantedAuthority> authorities = getUserAuthorities(user, repo);
        boolean changed = false;
        for (Iterator<RoleGrantedAuthority> iter = authorities.iterator(); iter.hasNext(); ) {
          RoleGrantedAuthority rga = iter.next();
          if (rga.getRoleName().equals(roleName)) {
            iter.remove();
            changed = true;
          }
        }
        if (changed) {
          saveUserAuthorities(user, Sets.newHashSet(authorities), repo, currentUser, false);
        }
      }

      PersonIdent ident = new PersonIdent(currentUser.getLoginName(), currentUser.getEmail());
      git.commit()
          .setAuthor(ident)
          .setCommitter(ident)
          .setMessage("delete role " + roleName) // $NON-NLS-1$
          .call();
    } catch (GitAPIException e) {
      throw new IOException(e);
    } finally {
      Util.closeQuietly(repo);
    }
  }
Exemplo n.º 9
0
 @Override
 public void delete(FileSystemPage page) {
   Repository repository = getRepository(page);
   Git git = new Git(repository);
   String fileSystemPath = getPath(page, repository);
   try {
     git.rm()
         .addFilepattern(fileSystemPath + "/" + contentFilename)
         .addFilepattern(fileSystemPath + "/" + propertiesFilename)
         .call();
     commit(
         git,
         String.format(
             "FitNesse page %s deleted.", PathParser.render(page.getPageCrawler().getFullPath())));
   } catch (Exception e) {
     throw new RuntimeException(e);
   }
   persistence.delete(page);
 }
Exemplo n.º 10
0
  /** @throws Exception */
  @Test
  public void testUntrackedFolders() throws Exception {
    try (Git git = new Git(db)) {
      IndexDiff diff = new IndexDiff(db, Constants.HEAD, new FileTreeIterator(db));
      diff.diff();
      assertEquals(Collections.EMPTY_SET, diff.getUntrackedFolders());

      writeTrashFile("readme", "");
      writeTrashFile("src/com/A.java", "");
      writeTrashFile("src/com/B.java", "");
      writeTrashFile("src/org/A.java", "");
      writeTrashFile("src/org/B.java", "");
      writeTrashFile("target/com/A.java", "");
      writeTrashFile("target/com/B.java", "");
      writeTrashFile("target/org/A.java", "");
      writeTrashFile("target/org/B.java", "");

      git.add().addFilepattern("src").addFilepattern("readme").call();
      git.commit().setMessage("initial").call();

      diff = new IndexDiff(db, Constants.HEAD, new FileTreeIterator(db));
      diff.diff();
      assertEquals(new HashSet<String>(Arrays.asList("target")), diff.getUntrackedFolders());

      writeTrashFile("src/tst/A.java", "");
      writeTrashFile("src/tst/B.java", "");

      diff = new IndexDiff(db, Constants.HEAD, new FileTreeIterator(db));
      diff.diff();
      assertEquals(
          new HashSet<String>(Arrays.asList("target", "src/tst")), diff.getUntrackedFolders());

      git.rm().addFilepattern("src/com/B.java").addFilepattern("src/org").call();
      git.commit().setMessage("second").call();
      writeTrashFile("src/org/C.java", "");

      diff = new IndexDiff(db, Constants.HEAD, new FileTreeIterator(db));
      diff.diff();
      assertEquals(
          new HashSet<String>(Arrays.asList("src/org", "src/tst", "target")),
          diff.getUntrackedFolders());
    }
  }
Exemplo n.º 11
0
  protected RevCommit doRemove(
      Git git,
      File rootDir,
      String branch,
      String path,
      String commitMessage,
      PersonIdent personIdent)
      throws Exception {
    File file = getFile(rootDir, path);
    if (file.exists()) {
      file.delete();

      String filePattern = getFilePattern(path);
      git.rm().addFilepattern(filePattern).call();
      CommitCommand commit =
          git.commit().setAll(true).setAuthor(personIdent).setMessage(commitMessage);
      return commitThenPush(git, branch, commit);
    } else {
      return null;
    }
  }
Exemplo n.º 12
0
  public void deleteUser(String loginName, User currentUser) throws IOException {
    Assert.hasLength(loginName);
    Assert.notNull(currentUser);

    ILockedRepository repo = null;
    try {
      repo = globalRepositoryManager.getProjectCentralRepository(REPOSITORY_NAME, false);
      Git git = Git.wrap(repo.r());
      git.rm().addFilepattern(loginName + USER_SUFFIX).call();
      git.rm().addFilepattern(loginName + AUTHORITIES_SUFFIX).call();
      PersonIdent ident = new PersonIdent(currentUser.getLoginName(), currentUser.getEmail());
      git.commit()
          .setAuthor(ident)
          .setCommitter(ident)
          .setMessage("delete user " + loginName) // $NON-NLS-1$
          .call();
    } catch (GitAPIException e) {
      throw new IOException(e);
    } finally {
      Util.closeQuietly(repo);
    }
  }
Exemplo n.º 13
0
  @Test
  public void testConflictingDeletedAndModified() throws Exception {
    try (Git git = new Git(db)) {
      writeTrashFile("a", "1\na\n3\n");
      writeTrashFile("b", "1\nb\n3\n");
      git.add().addFilepattern("a").addFilepattern("b").call();
      RevCommit initialCommit = git.commit().setMessage("initial").call();

      // create side branch and delete "a"
      createBranch(initialCommit, "refs/heads/side");
      checkoutBranch("refs/heads/side");
      git.rm().addFilepattern("a").call();
      RevCommit secondCommit = git.commit().setMessage("side").call();

      // update a on master to generate conflict
      checkoutBranch("refs/heads/master");
      writeTrashFile("a", "1\na(main)\n3\n");
      git.add().addFilepattern("a").call();
      git.commit().setMessage("main").call();

      // merge side with master
      MergeResult result =
          git.merge().include(secondCommit.getId()).setStrategy(MergeStrategy.RESOLVE).call();
      assertEquals(MergeStatus.CONFLICTING, result.getMergeStatus());
    }

    FileTreeIterator iterator = new FileTreeIterator(db);
    IndexDiff diff = new IndexDiff(db, Constants.HEAD, iterator);
    diff.diff();

    assertEquals("[]", new TreeSet<String>(diff.getChanged()).toString());
    assertEquals("[]", diff.getAdded().toString());
    assertEquals("[]", diff.getRemoved().toString());
    assertEquals("[]", diff.getMissing().toString());
    assertEquals("[]", diff.getModified().toString());
    assertEquals("[a]", diff.getConflicting().toString());
    assertEquals(StageState.DELETED_BY_THEM, diff.getConflictingStageStates().get("a"));
    assertEquals(Collections.EMPTY_SET, diff.getUntrackedFolders());
  }
Exemplo n.º 14
0
 private RevCommit commitRm(String path) throws Exception {
   git.rm().addFilepattern(path).call();
   return git.commit().setMessage("commit").call();
 }
Exemplo n.º 15
0
 private void stageMissingFilesAsDeleted(final Git git) throws GitAPIException {
   final Status status = git.status().call();
   for (final String missingFile : status.getMissing()) {
     git.rm().addFilepattern(missingFile).call();
   }
 }