From b0619a58d0f2a2c4acdc11996eddf605e5edb94f Mon Sep 17 00:00:00 2001 From: Simon Brooke Date: Sun, 11 Jan 2015 10:31:25 +0000 Subject: [PATCH] fetch-version now Just Works[TM]. diff sort of works - it diffs all files in a commit, but in the special case of the wiki I can make the simplifying assumption that there is only one file in a commit. Still, should fix this. --- src/smeagol/history.clj | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/src/smeagol/history.clj b/src/smeagol/history.clj index 326c96b..77b0c3d 100644 --- a/src/smeagol/history.clj +++ b/src/smeagol/history.clj @@ -5,7 +5,7 @@ (:import [org.eclipse.jgit.api Git] [org.eclipse.jgit.lib Repository ObjectId] [org.eclipse.jgit.revwalk RevCommit RevTree RevWalk] - [org.eclipse.jgit.treewalk AbstractTreeIterator CanonicalTreeParser] + [org.eclipse.jgit.treewalk TreeWalk AbstractTreeIterator CanonicalTreeParser] [org.eclipse.jgit.treewalk.filter PathFilter] [org.eclipse.jgit.diff DiffEntry DiffFormatter])) @@ -84,19 +84,34 @@ (.format formatter %) %) (.call - (.setPathFilter - (.setNewTree - (.setOldTree - (.diff git-r) - old-parse) - new-parse) - (PathFilter/create file-path)))) - (.toString out) - ))) + (.setOutputStream + (.setPathFilter + (.setNewTree + (.setOldTree (.diff git-r) old-parse) + new-parse) + (PathFilter/create file-path)) + out))) + (.toString out)))) (defn fetch-version "Return (as a String) the text of this `version` of the file at this - `file-path` in the git directory at this `git-directory-path`." + `file-path` in the git directory at this `git-directory-path`. + + Based on JGit Cookbook ReadFileFromCommit." [^String git-directory-path ^String file-path ^String version] - "TODO: Doesn't work yet") + (let [git-r (git/load-repo git-directory-path) + repo (.getRepository git-r) + walk (i/new-rev-walk git-r) + commit (i/bound-commit git-r walk (ObjectId/fromString version)) + tree (.parseTree walk (.getId (.getTree commit))) + tw (TreeWalk. repo) + out (java.io.ByteArrayOutputStream.)] + (.addTree tw tree) + (.setRecursive tw true) + (.setFilter tw (PathFilter/create file-path)) + (if (not (.next tw)) + (throw (IllegalStateException. + (str "Did not find expected file '" file-path "'")))) + (.copyTo (.open repo (.getObjectId tw 0)) out) + (.toString out))) \ No newline at end of file