Skip to content

Instantly share code, notes, and snippets.

@RutledgePaulV
Created March 10, 2021 20:42
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 RutledgePaulV/7c2219fc11c3e86f8063c0b43c463cd0 to your computer and use it in GitHub Desktop.
Save RutledgePaulV/7c2219fc11c3e86f8063c0b43c463cd0 to your computer and use it in GitHub Desktop.
dufus-diff.clj
(defn diff-branches [repo branch1 branch2]
(letfn [(tree-iterator [^Repository repo branch]
(let [head (.exactRef repo branch)]
(with-open [walk (RevWalk. repo)]
(let [commit (.parseCommit repo (.getObjectId head))
tree (.parseTree walk (.getId (.getTree commit)))
parser (CanonicalTreeParser.)]
(with-open [reader (.newObjectReader repo)]
(.reset parser reader (.getId tree)))
(.dispose walk)
parser))))
(as-data [^DiffEntry entry]
{:old/id (.getOldId entry)
:old/path (.getOldPath entry)
:old/mode (str (.getOldMode entry))
:new/id (.getNewId entry)
:new/path (.getNewPath entry)
:new/mode (str (.getNewMode entry))
:type (keyword (.name (.getChangeType entry)))})]
(let [git (->repo repo)
repo (.getRepository git)
t1 (tree-iterator repo (str "refs/heads/" branch1))
t2 (tree-iterator repo (str "refs/heads/" branch2))]
(set (map as-data
(-> (.diff git)
(.setOldTree t1)
(.setNewTree t2)
(.call)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment