@Override public File clone(String branch, Set<String> checkoutFiles, ProgressMonitor monitor) throws GitException { checkGitUrl(); File gitDir = getGitPath().toFile(); // delete existing git folder since may have conflict if (gitDir.exists()) { try { FileUtils.delete(gitDir.getParentFile(), FileUtils.RECURSIVE); } catch (IOException e) { // IO error on delete existing folder } } // git init initGit(checkoutFiles); pull(branch, monitor); return gitDir; }
/** * * Clones a git repository using the given uri and stores it in the parent directory. Checks out the * given reference or (if value is null) does not check out a branch * (which reduces time needed to complete command). * @param cloneURI the uri to the remote repository * @param parentDirectory the directory in which to store the git meta directory (".git" directory) * @param checkoutRef the ref name ("refs/heads/master"), branch name ("master") or tag name ("v1.2.3"). If * {@code null} is passed, will not checkout a branch. * @param branchesToClone the branches to clone or all branches if passed a {@code null} value. * @param monitor reports the progress of the clone command; can be null * @return the cloned Git repository * @throws GitAPIException */ public static Git cloneRepo(String cloneURI, File parentDirectory, String remoteName, String checkoutRef, List<String> branchesToClone, ProgressMonitor monitor) throws GitAPIException { CloneCommand clone = Git.cloneRepository(); if (checkoutRef == null) { clone.setNoCheckout(true); } else { clone.setBranch(checkoutRef); } if (branchesToClone == null) { clone.setCloneAllBranches(true); } else { clone.setBranchesToClone(branchesToClone); } if (monitor != null) { clone.setProgressMonitor(monitor); } return clone .setURI(cloneURI) .setDirectory(parentDirectory) .setRemote(remoteName) .call(); }
@Override public Result indexAll(ChangeIndex index) { ProgressMonitor pm = new TextProgressMonitor(); pm.beginTask("Collecting projects", ProgressMonitor.UNKNOWN); SortedSet<ProjectHolder> projects = new TreeSet<>(); int changeCount = 0; Stopwatch sw = Stopwatch.createStarted(); for (Project.NameKey name : projectCache.all()) { try (Repository repo = repoManager.openRepository(name)) { long size = estimateSize(repo); changeCount += size; projects.add(new ProjectHolder(name, size)); } catch (IOException e) { log.error("Error collecting projects", e); return new Result(sw, false, 0, 0); } pm.update(1); } pm.endTask(); setTotalWork(changeCount); return indexAll(index, projects); }
@Override protected void migrateData(ReviewDb db, UpdateUI ui) throws OrmException, SQLException { try { try (Repository repo = repoManager.openRepository(allUsersName)) { ProgressMonitor pm = new TextProgressMonitor(); pm.beginTask("Collecting accounts", ProgressMonitor.UNKNOWN); Set<Account> accounts = scanAccounts(db, pm); pm.endTask(); pm.beginTask("Migrating accounts to NoteDb", accounts.size()); for (Account account : accounts) { updateAccountInNoteDb(repo, account); pm.update(1); } pm.endTask(); } } catch (IOException | ConfigInvalidException e) { throw new OrmException("Migrating accounts to NoteDb failed", e); } }
private Set<Account> scanAccounts(ReviewDb db, ProgressMonitor pm) throws SQLException { try (Statement stmt = newStatement(db); ResultSet rs = stmt.executeQuery( "SELECT account_id," + " registered_on," + " full_name, " + " preferred_email," + " status," + " inactive" + " FROM accounts")) { Set<Account> s = new HashSet<>(); while (rs.next()) { Account a = new Account(new Account.Id(rs.getInt(1)), rs.getTimestamp(2)); a.setFullName(rs.getString(3)); a.setPreferredEmail(rs.getString(4)); a.setStatus(rs.getString(5)); a.setActive(rs.getString(6).equals("N")); s.add(a); pm.update(1); } return s; } }
@Override protected void migrateData(ReviewDb db, UpdateUI ui) throws OrmException { try { try (Repository repo = repoManager.openRepository(allUsersName)) { ProgressMonitor pm = new TextProgressMonitor(); pm.beginTask("Removing \"My Drafts\" menu items", ProgressMonitor.UNKNOWN); for (Account.Id id : (Iterable<Account.Id>) Accounts.readUserRefs(repo)::iterator) { if (removeMyDrafts(repo, id)) { pm.update(1); } } pm.endTask(); } } catch (IOException | ConfigInvalidException e) { throw new OrmException("Removing \"My Drafts\" menu items failed", e); } }
@Override public void doImport(ProgressMonitor progress) throws GitCloneFailedException, GitDestinationAlreadyExistsException, GitDestinationNotWritableException { CloneCommand clone = new CloneCommand(); clone.setCredentialsProvider(getRepository().getCredentialsProvider()); String sourceUri = getSourceUri(); clone.setURI(sourceUri); clone.setBare(true); clone.setDirectory(destinationDirectory); if (progress != null) { clone.setProgressMonitor(progress); } try { LOG.info(sourceUri + "| Clone into " + destinationDirectory); clone.call(); } catch (Throwable e) { throw new GitCloneFailedException(sourceUri, e); } }
@Override public void doImport(ProgressMonitor progress) throws Exception { MetaDataUpdate md = null; try { md = metaDataUpdateFactory.create(getProjectNameKey()); projectConfig = ProjectConfig.read(md); progress.beginTask("Configure Gerrit project", 2); setProjectSettings(); progress.update(1); setProjectPermissions(); progress.update(1); md.setMessage("Imported from " + getSourceUri()); projectConfig.commit(md); projectCache.onCreateProject(getProjectNameKey()); } finally { if (md != null) { md.close(); } progress.endTask(); } }
private void createNewRepo(ProgressMonitor monitor) throws GitAPIException, IllegalArgumentException { File localRepo = new File(localPath); if (localRepo.exists()) // Safety check so we don't accidentally delete directory throw new IllegalStateException("Directory already exists"); try { CloneCommand cloneCommand = Git.cloneRepository() .setCredentialsProvider(credentialsProvider) .setURI(remotePath) .setBranch(branch) .setDirectory(localRepo) .setBare(false); if (monitor != null) cloneCommand.setProgressMonitor(monitor); cloneCommand.call(); } catch (GitAPIException e) { FileUtils.deleteDirectory(localRepo); throw e; } }
private ProgressMonitor gitMonitor ( Writer w ) { return new TextProgressMonitor( w ) { }; // return new ProgressMonitor() { // // @Override // public void update ( int completed ) { // // TODO Auto-generated method stub // logger.info( "items completed: {}", completed ); // } // // @Override // public void start ( int totalTasks ) { // // TODO Auto-generated method stub // // } // // @Override // public boolean isCancelled () { // // TODO Auto-generated method stub // return false; // } // // @Override // public void endTask () { // // TODO Auto-generated method stub // // } // // @Override // public void beginTask ( String title, int totalWork ) { // // TODO Auto-generated method stub // logger.info( "progress: {}, items remaining: {}", title, totalWork ); // // } // } ; }
@Override public String fetch(String branch, String filePath, ProgressMonitor monitor) throws GitException { checkProject(); try { GitlabRepositoryFile file = connect.getRepositoryFile(project, filePath, branch); String base64Content = file.getContent(); byte[] contentBytes = Base64.getDecoder().decode(base64Content); return new String(contentBytes, "UTF-8"); } catch (IOException e) { return null; } }
@Override public String fetch(String branch, String filePath, ProgressMonitor monitor) throws GitException { clone(branch, Sets.newHashSet(filePath), monitor); Path targetPath = Paths.get(targetDir.toString(), filePath); if (Files.exists(targetPath)) { try { return com.google.common.io.Files.toString(targetPath.toFile(), Charset.forName("UTF-8")); } catch (IOException e) { return null; } } return null; }
@Override public void pull(String branch, ProgressMonitor monitor) throws GitException { try (Git git = gitOpen()) { PullCommand pullCommand = pullCommand(branch, git); if (monitor != null) { pullCommand.setProgressMonitor(monitor); } else { pullCommand.setProgressMonitor(new DebugProgressMonitor()); } pullCommand.call(); } catch (Throwable e) { throw new GitException("Fail to pull with specific files: " + ExceptionUtil.findRootCause(e).getMessage()); } }
private static void setupMergeCommand(MergeCommand merge, List<Ref> commitsByRef, List<AnyObjectId> commitsById, List<NamedCommit> commitsByNameAndId, ProgressMonitor monitor, MergeStrategy strategy, MergeCommand.FastForwardMode fastForwardMode) { commitsByRef.forEach(merge::include); commitsById.forEach(merge::include); commitsByNameAndId.forEach(nc -> merge.include(nc.getName(), nc.getObjectId())); if (monitor != null) { merge.setProgressMonitor(monitor); } merge .setFastForward(fastForwardMode) .setStrategy(strategy); }
public static MergeResult mergeWithSquash(Git git, MergeStrategy strategy, List<Ref> commitsByRef, List<AnyObjectId> commitsById, List<NamedCommit> commitsByNameAndId, MergeCommand.FastForwardMode fastForwardMode, ProgressMonitor monitor) throws GitAPIException { MergeCommand merge = git.merge(); setupMergeCommand(merge, commitsByRef, commitsById, commitsByNameAndId, monitor, strategy, fastForwardMode); return merge .setStrategy(strategy) .setFastForward(fastForwardMode) .setSquash(true) .call(); }
public static MergeResult mergeWithoutCommit(Git git, MergeStrategy strategy, List<Ref> commitsByRef, List<AnyObjectId> commitsById, List<NamedCommit> commitsByNameAndId, MergeCommand.FastForwardMode fastForwardMode, ProgressMonitor monitor) throws GitAPIException { MergeCommand merge = git.merge(); setupMergeCommand(merge, commitsByRef, commitsById, commitsByNameAndId, monitor, strategy, fastForwardMode); return merge .setStrategy(strategy) .setFastForward(fastForwardMode) .setCommit(false) .call(); }
public static MergeResult mergeWithCommit(Git git, MergeStrategy strategy, List<Ref> commitsByRef, List<AnyObjectId> commitsById, List<NamedCommit> commitsByNameAndId, String commitMessage, MergeCommand.FastForwardMode fastForwardMode, ProgressMonitor monitor) throws GitAPIException { MergeCommand merge = git.merge(); setupMergeCommand(merge, commitsByRef, commitsById, commitsByNameAndId, monitor, strategy, fastForwardMode); return git.merge() .setMessage(commitMessage) // message to be used for merge commit .call(); }
/** * Pulls from the given repository and merges changes from the given remote branch into * the local checked-out branch using the given strategy. Progress is reported via the {@code monitor}. * @param git the git repository * @param strategy the merge strategy: * @param remoteName the name of the repository * @param branchName the name of the remote branch * @param monitor reports the progress of the pull * @return result of the pull * @throws GitAPIException */ public static PullResult pullWithMerge(Git git, MergeStrategy strategy, String remoteName, String branchName, ProgressMonitor monitor) throws GitAPIException { PullCommand pull = git.pull(); if (monitor != null) { pull.setProgressMonitor(monitor); } return pull .setStrategy(strategy) .setRemote(remoteName) // value -> current branch config -> DEFAULT_REMOTE_NAME = "origin" .setRemoteBranchName(branchName) // value -> current branch config -> current branch name .call(); }
/** * Pulls from the given repository and rebases the currently checked-out branch onto the given remote branch * and includes a report on the progress of the pull. * @param git the git repository * @param remoteName the name of the remote repository * @param branchName the name of the branch on which to rebase the current branch * @param monitor reports the progress of the pull * @return result of the pull * @throws GitAPIException */ public static PullResult pullWithRebase(Git git, String remoteName, String branchName, ProgressMonitor monitor) throws GitAPIException { PullCommand pull = git.pull(); if (monitor != null) { pull.setProgressMonitor(monitor); } return pull .setRebase(true) // when true, ignores merge strategy .setRemote(remoteName) // value -> current branch config -> DEFAULT_REMOTE_NAME = "origin" .setRemoteBranchName(branchName) // value -> current branch config -> current branch name .setProgressMonitor(monitor) .call(); }
@Override public int run() throws Exception { mustHaveValidSite(); Injector dbInjector = createDbInjector(SINGLE_USER); manager.add(dbInjector); manager.start(); RuntimeShutdown.add(manager::stop); dbInjector.injectMembers(this); ProgressMonitor progress = new TextProgressMonitor(); progress.beginTask("Importing entities", ProgressMonitor.UNKNOWN); try (ReviewDb db = schemaFactory.open()) { for (RelationModel model : new JavaSchemaModel(ReviewDb.class).getRelations()) { relations.put(model.getRelationID(), Relation.create(model, db)); } Parser<UnknownFieldSet> parser = UnknownFieldSet.getDefaultInstance().getParserForType(); try (InputStream in = new BufferedInputStream(Files.newInputStream(file.toPath()))) { UnknownFieldSet msg; while ((msg = parser.parseDelimitedFrom(in)) != null) { Map.Entry<Integer, UnknownFieldSet.Field> e = Iterables.getOnlyElement(msg.asMap().entrySet()); Relation rel = checkNotNull( relations.get(e.getKey()), "unknown relation ID %s in message: %s", e.getKey(), msg); List<ByteString> values = e.getValue().getLengthDelimitedList(); checkState(values.size() == 1, "expected one string field in message: %s", msg); upsert(rel, values.get(0)); progress.update(1); } } progress.endTask(); } return 0; }
private int reindexAccounts() throws Exception { monitor.beginTask("Reindex accounts", ProgressMonitor.UNKNOWN); String[] reindexArgs = { "--site-path", getSitePath().toString(), "--index", AccountSchemaDefinitions.NAME }; System.out.println("Migration complete, reindexing accounts with:"); System.out.println(" reindex " + String.join(" ", reindexArgs)); Reindex reindexPgm = new Reindex(); int exitCode = reindexPgm.main(reindexArgs); monitor.endTask(); return exitCode; }
private ErrorListener( ListenableFuture<?> future, String desc, ProgressMonitor progress, AtomicBoolean ok) { this.future = future; this.desc = desc; this.progress = progress; this.ok = ok; }
@Override public SiteIndexer.Result indexAll(final ProjectIndex index) { ProgressMonitor progress = new TextProgressMonitor(new PrintWriter(progressOut)); progress.start(2); List<Project.NameKey> names = collectProjects(progress); return reindexProjects(index, names, progress); }
private List<Project.NameKey> collectProjects(ProgressMonitor progress) { progress.beginTask("Collecting projects", ProgressMonitor.UNKNOWN); List<Project.NameKey> names = new ArrayList<>(); for (Project.NameKey nameKey : projectCache.all()) { names.add(nameKey); } progress.endTask(); return names; }
@Override public SiteIndexer.Result indexAll(GroupIndex index) { ProgressMonitor progress = new TextProgressMonitor(new PrintWriter(progressOut)); progress.start(2); Stopwatch sw = Stopwatch.createStarted(); List<AccountGroup.UUID> uuids; try { uuids = collectGroups(progress); } catch (OrmException e) { log.error("Error collecting groups", e); return new SiteIndexer.Result(sw, false, 0, 0); } return reindexGroups(index, uuids, progress); }
private List<AccountGroup.UUID> collectGroups(ProgressMonitor progress) throws OrmException { progress.beginTask("Collecting groups", ProgressMonitor.UNKNOWN); try (ReviewDb db = schemaFactory.open()) { return groups.getAllUuids(db).collect(toImmutableList()); } finally { progress.endTask(); } }
@Override public SiteIndexer.Result indexAll(AccountIndex index) { ProgressMonitor progress = new TextProgressMonitor(new PrintWriter(progressOut)); progress.start(2); Stopwatch sw = Stopwatch.createStarted(); List<Account.Id> ids; try { ids = collectAccounts(progress); } catch (IOException e) { log.error("Error collecting accounts", e); return new SiteIndexer.Result(sw, false, 0, 0); } return reindexAccounts(index, ids, progress); }
private List<Account.Id> collectAccounts(ProgressMonitor progress) throws IOException { progress.beginTask("Collecting accounts", ProgressMonitor.UNKNOWN); List<Account.Id> ids = new ArrayList<>(); for (Account.Id accountId : accounts.allIds()) { ids.add(accountId); progress.update(1); } progress.endTask(); return ids; }
private ProjectIndexer( ChangeIndexer indexer, Project.NameKey project, ProgressMonitor done, ProgressMonitor failed) { this.indexer = indexer; this.project = project; this.done = done; this.failed = failed; }
@Override public void doImport(ProgressMonitor progress) throws Exception { progress.beginTask("Setting up Gerrit replication", 2); String repositoryName = getOrganisation() + "/" + getRepositoryName(); progress.update(1); replicationConfig.addSecureCredentials(authUsername, authToken); progress.update(1); replicationConfig.addReplicationRemote( authUsername, gitHubUrl + "/${name}.git", repositoryName); progress.endTask(); }
private Git initGitRepo(ProgressMonitor monitor) throws Exception { if (new File(localPath).exists() == false) createNewRepo(monitor); FileRepository fileRepository = new FileRepository(localPath + "/.git"); return new Git(fileRepository); }
public Git getGit(ProgressMonitor monitor) throws Exception { if (this.git == null) this.git = initGitRepo(monitor); boolean hasConflicts = false; try { hasConflicts = this.git.status().call().getConflicting().isEmpty() == false; } catch (Exception ex) {} if (hasConflicts) throw new IllegalStateException("Unresolved conflict(s) in git repository"); return this.git; }
public void updateChanges(ProgressMonitor monitor) throws Exception { Git git = getGit(monitor); FetchCommand fetch = git.fetch(); fetch.setCredentialsProvider(credentialsProvider); if (monitor != null) fetch.setProgressMonitor(monitor); fetch.call(); SyncState state = getSyncState(git); Ref fetchHead = git.getRepository().getRef("FETCH_HEAD"); switch (state) { case Equal: // Do nothing Log.d("Git", "Local branch is up-to-date"); break; case Ahead: Log.d("Git", "Local branch ahead, pushing changes to remote"); git.push().setCredentialsProvider(credentialsProvider).setRemote(remotePath).call(); break; case Behind: Log.d("Git", "Local branch behind, fast forwarding changes"); MergeResult result = git.merge().include(fetchHead).setFastForward(MergeCommand.FastForwardMode.FF_ONLY).call(); if (result.getMergeStatus().isSuccessful() == false) throw new IllegalStateException("Fast forward failed on behind merge"); break; case Diverged: Log.d("Git", "Branches are diverged, merging with strategy " + mergeStrategy.getName()); MergeResult mergeResult = git.merge().include(fetchHead).setStrategy(mergeStrategy).call(); if (mergeResult.getMergeStatus().isSuccessful()) { git.push().setCredentialsProvider(credentialsProvider).setRemote(remotePath).call(); } else throw new IllegalStateException("Merge failed for diverged branches using strategy " + mergeStrategy.getName()); break; } }
@Override public void update(int i) { mWorkDone += i; if (mTotalWork != ProgressMonitor.UNKNOWN && mTotalWork != 0 && mTotalWork - mLastProgress >= 1) { setProgress(); mLastProgress = mWorkDone; } }
public DelegatingProgressMonitor (org.netbeans.libs.git.progress.ProgressMonitor monitor) { this.monitor = monitor; }
private ProgressMonitor progressMonitor(String name) { return progressMonitors.computeIfAbsent(name, MirrorProgressMonitor::new); }
@Override public File clone(String branch, Set<String> checkoutFiles, ProgressMonitor monitor) throws GitException { throw new UnsupportedOperationException("Git clone not supported for GitLab"); }
@Override public void pull(String branch, ProgressMonitor monitor) throws GitException { throw new UnsupportedOperationException("Pull not supported for GitLab"); }
protected final void addErrorListener( ListenableFuture<?> future, String desc, ProgressMonitor progress, AtomicBoolean ok) { future.addListener( new ErrorListener(future, desc, progress, ok), MoreExecutors.directExecutor()); }
ChangeProgressOp(ProgressMonitor progress) { this.progress = progress; }