public static long toLong(Object obj, final long longDefault) { if (obj instanceof Long) { return (Long) obj; } if (obj instanceof Number) { return ((Number) obj).longValue(); } else if (obj instanceof CharSequence) { String str = Str.toString(obj); if (Dates.isJsonDate(str)) { return Dates.fromJsonDate(str).getTime(); } try { return StringScanner.parseLong(str); } catch (Exception ex) { return longDefault; } } else if (obj instanceof Date) { return ((Date) obj).getTime(); } else { return toInt(obj); } }
public static List<String> list(final String path) { URI uri = URI.create(path); if (uri.getScheme() == null) { final Path pathFromFileSystem = path(path); return list(pathFromFileSystem); } else if (uri.getScheme().equals(CLASSPATH_SCHEMA)) { return listFromDefaultClassLoader(StringScanner.split(path, ':')[1]); } else { final Path pathFromFileSystem = path(path); return list(pathFromFileSystem); } }
public static Path path(String location) { if (!location.startsWith(CLASSPATH_SCHEMA + ":")) { return Paths.get(location); } else { String path = StringScanner.split(location, ':')[1]; final List<Path> resources = Classpaths.resources(IO.class, path); Path result = Lists.idx(resources, 0); if (result == null) { return path(path); } return result; } }
public static int toInt(Object obj, int defaultValue) { if (obj.getClass() == int.class) { return int.class.cast(obj); } if (obj instanceof Number) { return ((Number) obj).intValue(); } else if (obj instanceof Boolean || obj.getClass() == Boolean.class) { boolean value = toBoolean(obj); return value ? 1 : 0; } else if (obj instanceof CharSequence) { try { return StringScanner.parseInt(Str.toString(obj)); } catch (Exception ex) { return defaultValue; } } return defaultValue; }
public static Date toEuroDate(String string) { String[] split = StringScanner.splitByChars(string, new char[] {'.', '\\', '/', ':'}); if (split.length == 3) { return Dates.getEuroDate(toInt(split[0]), toInt(split[1]), toInt(split[2])); } else if (split.length >= 6) { return Dates.getEuroDate( toInt(split[0]), toInt(split[1]), toInt(split[2]), toInt(split[3]), toInt(split[4]), toInt(split[5])); } else { die(String.format("Not able to parse %s into a Euro date", string)); return null; } }
/** * Load VCS roots * * @param project the project * @param roots the VCS root list * @param exceptions the list of of exceptions to use * @param fetchData if true, the data for remote is fetched. * @return the loaded information about vcs roots */ private static List<Root> loadRoots( final Project project, final List<VirtualFile> roots, final Collection<VcsException> exceptions, final boolean fetchData) { final ArrayList<Root> rc = new ArrayList<Root>(); for (VirtualFile root : roots) { try { Root r = new Root(); rc.add(r); r.root = root; GitBranch b = GitBranch.current(project, root); if (b != null) { r.currentBranch = b.getFullName(); r.remoteName = b.getTrackedRemoteName(project, root); r.remoteBranch = b.getTrackedBranchName(project, root); if (r.remoteName != null) { if (fetchData && !r.remoteName.equals(".")) { GitLineHandler fetch = new GitLineHandler(project, root, GitCommand.FETCH); fetch.addParameters(r.remoteName, "-v"); Collection<VcsException> exs = GitHandlerUtil.doSynchronouslyWithExceptions(fetch); exceptions.addAll(exs); } GitBranch tracked = b.tracked(project, root); assert tracked != null : "Tracked branch cannot be null here"; final boolean trackedBranchExists = tracked.exists(root); if (!trackedBranchExists) { LOG.info("loadRoots tracked branch " + tracked + " doesn't exist yet"); } // check what remote commits are not yet merged if (trackedBranchExists) { GitSimpleHandler toPull = new GitSimpleHandler(project, root, GitCommand.LOG); toPull.addParameters( "--pretty=format:%H", r.currentBranch + ".." + tracked.getFullName()); toPull.setNoSSH(true); toPull.setStdoutSuppressed(true); StringScanner su = new StringScanner(toPull.run()); while (su.hasMoreData()) { if (su.line().trim().length() != 0) { r.remoteCommits++; } } } // check what local commits are to be pushed GitSimpleHandler toPush = new GitSimpleHandler(project, root, GitCommand.LOG); // if the tracked branch doesn't exist yet (nobody pushed the branch yet), show all // commits on this branch. final String revisions = trackedBranchExists ? tracked.getFullName() + ".." + r.currentBranch : r.currentBranch; toPush.addParameters("--pretty=format:%H%x20%ct%x20%at%x20%s%n%P", revisions); toPush.setNoSSH(true); toPush.setStdoutSuppressed(true); StringScanner sp = new StringScanner(toPush.run()); while (sp.hasMoreData()) { if (sp.isEol()) { sp.line(); continue; } Commit c = new Commit(); c.root = r; String hash = sp.spaceToken(); String time = sp.spaceToken(); c.revision = new GitRevisionNumber(hash, new Date(Long.parseLong(time) * 1000L)); c.authorTime = sp.spaceToken(); c.message = sp.line(); c.isMerge = sp.line().indexOf(' ') != -1; r.commits.add(c); } } } } catch (VcsException e) { exceptions.add(e); } } return rc; }