List of usage examples for org.eclipse.jgit.api Git wrap
public static Git wrap(Repository repo)
From source file:de.blizzy.documentr.access.UserStore.java
License:Open Source License
/** * Saves a user./*w w w.j a va 2s . co m*/ * * @param user the user to save * @param currentUser the user performing the save operation */ public void saveUser(User user, User currentUser) throws IOException { Assert.notNull(user); Assert.notNull(currentUser); ILockedRepository repo = null; try { repo = globalRepositoryManager.getProjectCentralRepository(REPOSITORY_NAME, false); Map<String, Object> userMap = new HashMap<String, Object>(); userMap.put("loginName", user.getLoginName()); //$NON-NLS-1$ userMap.put("password", user.getPassword()); //$NON-NLS-1$ userMap.put("email", user.getEmail()); //$NON-NLS-1$ userMap.put("disabled", Boolean.valueOf(user.isDisabled())); //$NON-NLS-1$ if (!user.getOpenIds().isEmpty()) { userMap.put("openIds", user.getOpenIds()); //$NON-NLS-1$ } Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create(); String json = gson.toJson(userMap); File workingDir = RepositoryUtil.getWorkingDir(repo.r()); File workingFile = new File(workingDir, user.getLoginName() + USER_SUFFIX); FileUtils.write(workingFile, json, Charsets.UTF_8); Git git = Git.wrap(repo.r()); git.add().addFilepattern(user.getLoginName() + USER_SUFFIX).call(); PersonIdent ident = new PersonIdent(currentUser.getLoginName(), currentUser.getEmail()); git.commit().setAuthor(ident).setCommitter(ident).setMessage(user.getLoginName()).call(); } catch (GitAPIException e) { throw new IOException(e); } finally { Closeables.closeQuietly(repo); } }
From source file:de.blizzy.documentr.access.UserStore.java
License:Open Source License
/** * Saves a role./*from w w w .ja v a 2 s.co m*/ * * @param role the role to save * @param currentUser the user performing the save operation */ public void saveRole(Role role, User currentUser) throws IOException { Assert.notNull(role); Assert.notNull(currentUser); ILockedRepository repo = null; try { repo = globalRepositoryManager.getProjectCentralRepository(REPOSITORY_NAME, false); Map<String, Object> roleMap = new HashMap<String, Object>(); roleMap.put("name", role.getName()); //$NON-NLS-1$ Set<String> permissions = Sets.newHashSet(); for (Permission permission : role.getPermissions()) { permissions.add(permission.name()); } roleMap.put("permissions", permissions); //$NON-NLS-1$ Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create(); String json = gson.toJson(roleMap); File workingDir = RepositoryUtil.getWorkingDir(repo.r()); File workingFile = new File(workingDir, role.getName() + ROLE_SUFFIX); FileUtils.write(workingFile, json, Charsets.UTF_8); Git git = Git.wrap(repo.r()); git.add().addFilepattern(role.getName() + ROLE_SUFFIX).call(); PersonIdent ident = new PersonIdent(currentUser.getLoginName(), currentUser.getEmail()); git.commit().setAuthor(ident).setCommitter(ident).setMessage(role.getName()).call(); } catch (GitAPIException e) { throw new IOException(e); } finally { Closeables.closeQuietly(repo); } }
From source file:de.blizzy.documentr.access.UserStore.java
License:Open Source License
private void saveUserAuthorities(String loginName, Set<RoleGrantedAuthority> authorities, ILockedRepository repo, User currentUser, boolean commit) throws IOException, GitAPIException { Map<String, Set<String>> authoritiesMap = new HashMap<String, Set<String>>(); for (RoleGrantedAuthority rga : authorities) { GrantedAuthorityTarget target = rga.getTarget(); String targetStr = target.getType().name() + ":" + target.getTargetId(); //$NON-NLS-1$ Set<String> roleNames = authoritiesMap.get(targetStr); if (roleNames == null) { roleNames = Sets.newHashSet(); authoritiesMap.put(targetStr, roleNames); }/*from w ww . j av a 2 s . c o m*/ roleNames.add(rga.getRoleName()); } Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create(); String json = gson.toJson(authoritiesMap); File workingDir = RepositoryUtil.getWorkingDir(repo.r()); File workingFile = new File(workingDir, loginName + AUTHORITIES_SUFFIX); FileUtils.write(workingFile, json, Charsets.UTF_8); Git git = Git.wrap(repo.r()); git.add().addFilepattern(loginName + AUTHORITIES_SUFFIX).call(); if (commit) { PersonIdent ident = new PersonIdent(currentUser.getLoginName(), currentUser.getEmail()); git.commit().setAuthor(ident).setCommitter(ident).setMessage(loginName).call(); } }
From source file:de.blizzy.documentr.access.UserStore.java
License:Open Source License
public void deleteUser(String loginName, User currentUser) throws IOException { Assert.hasLength(loginName);/*from w w w . ja v a2 s .c o m*/ 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 { Closeables.closeQuietly(repo); } }
From source file:de.blizzy.documentr.access.UserStore.java
License:Open Source License
public void renameUser(String loginName, String newLoginName, User currentUser) throws IOException { Assert.hasLength(loginName);//from w w w . j ava 2s . c o m 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 { Closeables.closeQuietly(repo); } }
From source file:de.blizzy.documentr.access.UserStore.java
License:Open Source License
public void renameRole(String roleName, String newRoleName, User currentUser) throws IOException { Assert.hasLength(roleName);//from w w w . jav a2s.co m 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 { Closeables.closeQuietly(repo); } }
From source file:de.blizzy.documentr.access.UserStore.java
License:Open Source License
public void deleteRole(String roleName, User currentUser) throws IOException { Assert.hasLength(roleName);/*from w ww . jav a 2 s . c o m*/ 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 { Closeables.closeQuietly(repo); } }
From source file:de.blizzy.documentr.page.CherryPicker.java
License:Open Source License
private List<CommitCherryPickResult> cherryPick(String projectName, String branchName, String path, List<String> commits, String targetBranch, Set<CommitCherryPickConflictResolve> conflictResolves, boolean dryRun, User user, Locale locale) throws IOException, GitAPIException { ILockedRepository repo = null;//from w ww. j av a 2 s.c o m List<CommitCherryPickResult> cherryPickResults = Lists.newArrayList(); boolean hadConflicts = false; boolean failed = false; try { repo = globalRepositoryManager.getProjectBranchRepository(projectName, targetBranch); String tempBranchName = "_temp_" + String.valueOf((long) (Math.random() * Long.MAX_VALUE)); //$NON-NLS-1$ Git git = Git.wrap(repo.r()); git.branchCreate().setName(tempBranchName).setStartPoint(targetBranch).call(); git.checkout().setName(tempBranchName).call(); for (String commit : commits) { PageVersion pageVersion = PageUtil.toPageVersion(CommitUtils.getCommit(repo.r(), commit)); if (!hadConflicts) { CommitCherryPickResult singleCherryPickResult = cherryPick(repo, branchName, path, pageVersion, targetBranch, conflictResolves, user, locale); if (singleCherryPickResult != null) { cherryPickResults.add(singleCherryPickResult); if (singleCherryPickResult.getStatus() == CommitCherryPickResult.Status.CONFLICT) { hadConflicts = true; } } else { failed = true; break; } } else { cherryPickResults .add(new CommitCherryPickResult(pageVersion, CommitCherryPickResult.Status.UNKNOWN)); } } if (hadConflicts || failed) { git.reset().setMode(ResetCommand.ResetType.HARD).call(); } git.checkout().setName(targetBranch).call(); if (!dryRun && !hadConflicts && !failed) { git.merge().include(repo.r().resolve(tempBranchName)).call(); } git.branchDelete().setBranchNames(tempBranchName).setForce(true).call(); if (failed) { throw new IOException("cherry-picking failed"); //$NON-NLS-1$ } } finally { Closeables.closeQuietly(repo); } if (!dryRun && !hadConflicts && !failed) { eventBus.post(new PageChangedEvent(projectName, targetBranch, path)); } return cherryPickResults; }
From source file:de.blizzy.documentr.page.CherryPicker.java
License:Open Source License
private CommitCherryPickResult cherryPick(ILockedRepository repo, String branchName, String path, PageVersion pageVersion, String targetBranch, Set<CommitCherryPickConflictResolve> conflictResolves, User user, Locale locale) throws IOException, GitAPIException { CommitCherryPickResult cherryPickResult; CherryPickResult result = Git.wrap(repo.r()).cherryPick() .include(repo.r().resolve(pageVersion.getCommitName())).call(); CherryPickStatus status = result.getStatus(); switch (status) { case OK:/*from w w w . j a v a 2 s. com*/ cherryPickResult = new CommitCherryPickResult(pageVersion, CommitCherryPickResult.Status.OK); break; case CONFLICTING: cherryPickResult = tryResolveConflict(repo, branchName, path, pageVersion, targetBranch, conflictResolves, user, locale); break; default: cherryPickResult = null; break; } return cherryPickResult; }
From source file:de.blizzy.documentr.page.CherryPicker.java
License:Open Source License
private CommitCherryPickResult tryResolveConflict(ILockedRepository repo, String branchName, String path, PageVersion pageVersion, String targetBranch, Set<CommitCherryPickConflictResolve> conflictResolves, User user, Locale locale) throws IOException, GitAPIException { File workingDir = RepositoryUtil.getWorkingDir(repo.r()); File pagesDir = new File(workingDir, DocumentrConstants.PAGES_DIR_NAME); File workingFile = Util.toFile(pagesDir, path + DocumentrConstants.PAGE_SUFFIX); String resolveText = getCherryPickConflictResolveText(conflictResolves, targetBranch, pageVersion.getCommitName()); CommitCherryPickResult result;/* ww w . j av a 2 s . c o m*/ if (resolveText != null) { if (!CONFLICT_MARKERS_RE.matcher("\n" + resolveText).matches()) { //$NON-NLS-1$ FileUtils.writeStringToFile(workingFile, resolveText, Charsets.UTF_8.name()); Git git = Git.wrap(repo.r()); git.add() .addFilepattern( DocumentrConstants.PAGES_DIR_NAME + "/" + path + DocumentrConstants.PAGE_SUFFIX) //$NON-NLS-1$ .call(); PersonIdent ident = new PersonIdent(user.getLoginName(), user.getEmail()); git.commit().setAuthor(ident).setCommitter(ident) .setMessage(DocumentrConstants.PAGES_DIR_NAME + "/" + path + DocumentrConstants.PAGE_SUFFIX) //$NON-NLS-1$ .call(); result = new CommitCherryPickResult(pageVersion, CommitCherryPickResult.Status.OK); } else { result = new CommitCherryPickResult(pageVersion, resolveText); } } else { String text = FileUtils.readFileToString(workingFile, Charsets.UTF_8); text = StringUtils.replace(text, "<<<<<<< OURS", //$NON-NLS-1$ "<<<<<<< " + messageSource.getMessage("targetBranchX", new Object[] { targetBranch }, locale)); //$NON-NLS-1$ //$NON-NLS-2$ text = StringUtils.replace(text, ">>>>>>> THEIRS", //$NON-NLS-1$ ">>>>>>> " + messageSource.getMessage("sourceBranchX", new Object[] { branchName }, locale)); //$NON-NLS-1$ //$NON-NLS-2$ result = new CommitCherryPickResult(pageVersion, text); } return result; }