<commit_before>package org.tigris.subversion.subclipse.graph.editors;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.gef.GraphicalViewer;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.tigris.subversion.subclipse.core.ISVNRemoteResource;
import org.tigris.subversion.subclipse.core.SVNException;
import org.tigris.subversion.subclipse.core.SVNProviderPlugin;
import org.tigris.subversion.subclipse.graph.Activator;
import org.tigris.subversion.subclipse.graph.cache.Cache;
import org.tigris.subversion.subclipse.graph.cache.Graph;
import org.tigris.subversion.subclipse.graph.cache.Node;
import org.tigris.subversion.subclipse.ui.operations.SVNOperation;
import org.tigris.subversion.svnclientadapter.ISVNClientAdapter;
import org.tigris.subversion.svnclientadapter.ISVNInfo;
import org.tigris.subversion.svnclientadapter.SVNRevision;

public class GraphBackgroundTask extends SVNOperation {
	
	private IResource resource;
	private ISVNRemoteResource remoteResource;
	private GraphicalViewer viewer;
	private RevisionGraphEditor editor;
	private SVNRevision refreshRevision;
	private Node refreshNode;
	private SVNRevision[] refreshRevisions;
	private Node[] refreshNodes;
	private boolean includeMergedRevisions = false;
	private boolean getNewRevisions = true;
	private Graph graph;

	private static final int TOTAL_STEPS = Integer.MAX_VALUE;
	private static final int SHORT_TASK_STEPS = TOTAL_STEPS / 50; // 2%
	private static final int VERY_LONG_TASK = TOTAL_STEPS / 2; // 50%
	private static final int TASK_STEPS = (TOTAL_STEPS - SHORT_TASK_STEPS*3 - VERY_LONG_TASK) / 2;

	protected GraphBackgroundTask(IWorkbenchPart part, GraphicalViewer viewer, RevisionGraphEditor editor) {
		super(part);
		this.viewer = viewer;
		this.editor = editor;
	}	
	
	public GraphBackgroundTask(IWorkbenchPart part, GraphicalViewer viewer, RevisionGraphEditor editor, IResource resource) {
		this(part, viewer, editor);
		this.resource = resource;
	}
	
	public GraphBackgroundTask(IWorkbenchPart part, GraphicalViewer viewer, RevisionGraphEditor editor, ISVNRemoteResource remoteResource) {
		this(part, viewer, editor);
		this.remoteResource = remoteResource;
	}
	
	public void setGetNewRevisions(boolean getNewRevisions) {
		this.getNewRevisions = getNewRevisions;
	}

	protected void execute(IProgressMonitor monitor) throws SVNException,
			InterruptedException {
		boolean error = false;
		Cache cache = null;
		monitor.beginTask("Calculating graph information", TOTAL_STEPS);
		monitor.worked(SHORT_TASK_STEPS);
		try {
			ISVNClientAdapter client = SVNProviderPlugin.getPlugin().getSVNClient();
			ISVNInfo info;
			if (resource == null) info = client.getInfo(remoteResource.getUrl());
			else {
				if (resource.getRawLocation() == null) info = client.getInfoFromWorkingCopy(resource.getLocation().toFile());
				else info = client.getInfoFromWorkingCopy(resource.getRawLocation().toFile());
				if (info.getUuid() == null) {
					info = client.getInfo(info.getUrl());
				}
			}
			
			if (editor != null) ((RevisionGraphEditorInput)editor.getEditorInput()).setInfo(info);
			
			long revision = info.getRevision().getNumber();
			String path = info.getUrl().toString().substring(info.getRepository().toString().length());
			
			monitor.setTaskName("Initializating cache");
			cache = getCache(info.getUuid());
			monitor.worked(SHORT_TASK_STEPS);
			
			// update the cache
			long latestRevisionStored = cache.getLatestRevision();
			SVNRevision latest = null;
			SVNRevision endRevision = null;
			monitor.setTaskName("Connecting to the repository");
			// TODO: try-catch this line and make it work off-line
			long latestRevisionInRepository = client.getInfo(info.getRepository()).getLastChangedRevision().getNumber();
			monitor.worked(SHORT_TASK_STEPS);

			if(refreshRevision != null || refreshRevisions != null || latestRevisionInRepository > latestRevisionStored) {
				if (refreshRevision == null) {
					if(latestRevisionStored == 0)
						latest = SVNRevision.START;
					else
						latest = new SVNRevision.Number(latestRevisionStored+1);
				} else {
					latest = refreshRevision;
				}
				
				if (refreshRevision == null) endRevision = SVNRevision.HEAD;
				else endRevision = refreshRevision;

				try {
					monitor.setTaskName("Retrieving revision history");
					int unitWork;
					if (refreshRevision == null && refreshRevisions == null) unitWork = VERY_LONG_TASK / (int) (latestRevisionInRepository - latestRevisionStored);<commit_msg>Fix error on first use of revision graph.<commit_after>package org.tigris.subversion.subclipse.graph.editors;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.gef.GraphicalViewer;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.tigris.subversion.subclipse.core.ISVNRemoteResource;
import org.tigris.subversion.subclipse.core.SVNException;
import org.tigris.subversion.subclipse.core.SVNProviderPlugin;
import org.tigris.subversion.subclipse.graph.Activator;
import org.tigris.subversion.subclipse.graph.cache.Cache;
import org.tigris.subversion.subclipse.graph.cache.Graph;
import org.tigris.subversion.subclipse.graph.cache.Node;
import org.tigris.subversion.subclipse.ui.operations.SVNOperation;
import org.tigris.subversion.svnclientadapter.ISVNClientAdapter;
import org.tigris.subversion.svnclientadapter.ISVNInfo;
import org.tigris.subversion.svnclientadapter.SVNRevision;

public class GraphBackgroundTask extends SVNOperation {
	
	private IResource resource;
	private ISVNRemoteResource remoteResource;
	private GraphicalViewer viewer;
	private RevisionGraphEditor editor;
	private SVNRevision refreshRevision;
	private Node refreshNode;
	private SVNRevision[] refreshRevisions;
	private Node[] refreshNodes;
	private boolean includeMergedRevisions = false;
	private boolean getNewRevisions = true;
	private Graph graph;

	private static final int TOTAL_STEPS = Integer.MAX_VALUE;
	private static final int SHORT_TASK_STEPS = TOTAL_STEPS / 50; // 2%
	private static final int VERY_LONG_TASK = TOTAL_STEPS / 2; // 50%
	private static final int TASK_STEPS = (TOTAL_STEPS - SHORT_TASK_STEPS*3 - VERY_LONG_TASK) / 2;

	protected GraphBackgroundTask(IWorkbenchPart part, GraphicalViewer viewer, RevisionGraphEditor editor) {
		super(part);
		this.viewer = viewer;
		this.editor = editor;
	}	
	
	public GraphBackgroundTask(IWorkbenchPart part, GraphicalViewer viewer, RevisionGraphEditor editor, IResource resource) {
		this(part, viewer, editor);
		this.resource = resource;
	}
	
	public GraphBackgroundTask(IWorkbenchPart part, GraphicalViewer viewer, RevisionGraphEditor editor, ISVNRemoteResource remoteResource) {
		this(part, viewer, editor);
		this.remoteResource = remoteResource;
	}
	
	public void setGetNewRevisions(boolean getNewRevisions) {
		this.getNewRevisions = getNewRevisions;
	}

	protected void execute(IProgressMonitor monitor) throws SVNException,
			InterruptedException {
		boolean error = false;
		Cache cache = null;
		monitor.beginTask("Calculating graph information", TOTAL_STEPS);
		monitor.worked(SHORT_TASK_STEPS);
		try {
			ISVNClientAdapter client = SVNProviderPlugin.getPlugin().getSVNClient();
			ISVNInfo info;
			if (resource == null) info = client.getInfo(remoteResource.getUrl());
			else {
				if (resource.getRawLocation() == null) info = client.getInfoFromWorkingCopy(resource.getLocation().toFile());
				else info = client.getInfoFromWorkingCopy(resource.getRawLocation().toFile());
				if (info.getUuid() == null) {
					info = client.getInfo(info.getUrl());
				}
			}
			
			if (editor != null) ((RevisionGraphEditorInput)editor.getEditorInput()).setInfo(info);
			
			long revision = info.getRevision().getNumber();
			String path = info.getUrl().toString().substring(info.getRepository().toString().length());
			
			monitor.setTaskName("Initializating cache");
			cache = getCache(info.getUuid());
			monitor.worked(SHORT_TASK_STEPS);
			
			// update the cache
			long latestRevisionStored = cache.getLatestRevision();
			SVNRevision latest = null;
			SVNRevision endRevision = null;
			monitor.setTaskName("Connecting to the repository");
			// TODO: try-catch this line and make it work off-line
			long latestRevisionInRepository = client.getInfo(info.getRepository()).getLastChangedRevision().getNumber();
			monitor.worked(SHORT_TASK_STEPS);

			if(refreshRevision != null || refreshRevisions != null || latestRevisionInRepository > latestRevisionStored) {
				if (refreshRevision == null) {
//					if(latestRevisionStored == 0)
//						latest = SVNRevision.START;
//					else
						latest = new SVNRevision.Number(latestRevisionStored+1);
				} else {
					latest = refreshRevision;
				}
				
				if (refreshRevision == null) endRevision = SVNRevision.HEAD;
				else endRevision = refreshRevision;

				try {
					monitor.setTaskName("Retrieving revision history");
					int unitWork;
					if (refreshRevision == null && refreshRevisions == null) unitWork = VERY_LONG_TASK / (int) (latestRevisionInRepository - latestRevisionStored);
{'fraction_non_alphanumeric': 0.09917617237008872, 'fraction_numerical': 0.0021123785382340513, 'mean_word_length': 5.160702667534157, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '686035', 'n_tokens_mistral': 3265, 'n_tokens_neox': 2799, 'n_words': 590}