Skip to content

Instantly share code, notes, and snippets.

@charles-dyfis-net
Created April 6, 2012 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charles-dyfis-net/422a11d822458acb2ce4 to your computer and use it in GitHub Desktop.
Save charles-dyfis-net/422a11d822458acb2ce4 to your computer and use it in GitHub Desktop.
(ns com.indeed.svnkit_wrapper.SvnWrapper
(:import (org.basex.query QueryModule QueryModule$Requires QueryModule$Permission QueryModule$Deterministic))
(:gen-class
:main false
:extends org.basex.query.QueryModule
:methods [^{:static true
QueryModule$Requires QueryModule$Permission/NONE}
[cat [java.lang.String] java.lang.String]
^{:static true
QueryModule$Deterministic {}
QueryModule$Requires QueryModule$Permission/NONE}
[catRev [java.lang.String int] java.lang.String]]))
(def instances (proxy [java.lang.ThreadLocal] [] (initialValue [] (ref {}))))
(defn build-url-seq [url-str] (seq (.split url-str "/")))
(defn build-trie [seed & kvs]
(reduce
(fn [trie [k v]]
(assoc-in trie (concat k [:val]) v))
seed
(partition 2 kvs)))
(defn trie-prefix-match [target trie]
(when (seq target)
(when-let [node (trie (first target))]
(or (:val node)
(recur (rest target) node)))))
(defn get-instance [url-str]
(dosync
(if-let [repository (trie-prefix-match (build-url-seq url-str) @(.get instances))]
repository
(let [url (org.tmatesoft.svn.core.SVNURL/parseURIDecoded url-str)
repository (org.tmatesoft.svn.core.io.SVNRepositoryFactory/create url)
root-url (.getRepositoryRoot repository true)
root-url-str (.toString root-url)]
(.setLocation repository root-url false)
(alter (.get instances) build-trie (build-url-seq root-url-str) repository)
repository))))
(defn cat
([filename] (cat filename -1))
([filename revision]
(try
(let [output-stream (java.io.ByteArrayOutputStream.)
repository (get-instance filename)
rel-loc (.substring filename (.. repository (getRepositoryRoot false) toString length))]
(.getFile repository rel-loc -1 nil output-stream)
(.toString output-stream))
(catch org.tmatesoft.svn.core.SVNException e
(if (= (.. e getErrorMessage getErrorCode)
org.tmatesoft.svn.core.SVNErrorCode/FS_NOT_FOUND)
nil
(throw e))))))
(defn -cat
[filename] (cat filename))
(defn -catRev
([filename revision] (cat filename revision)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment