Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Embedding JGit into Java Applications

alblue
October 29, 2013

Embedding JGit into Java Applications

The different levels of embedding JGit into Java applications, given as a lightening talk at EclipseCon Europe 2013

alblue

October 29, 2013
Tweet

More Decks by alblue

Other Decks in Technology

Transcript

  1. Level Zero • JGit is a command line-program • java

    -jar jgit.sh ... • ./jgit.sh ... • System.getRuntime().exec(“java -jar jgit.sh”) JGit.sh is a shell script with an additional ! Not ‘embedded’ - but useful for memory constrained or GC sensitive applications
  2. Level Zero • Advantages • You already know how to

    use this • No new commands needed • Simple • Useful if in-process memory is limited • Disadvantages • No re-use between runs • Spawns a new JVM each time • Have to parse the results via text stream
  3. Level One • The jgit command line processor is in

    the ‘pgm’ jar and can be invoked directly • org.eclipse.jgit.pgm.Main. main(new String[] {...}) • --git-dir /path/to/.git ls-tree HEAD • --git-dir /path/to/.git show HEAD
  4. Level One • add • archive • blame • branch

    • checkout • clone • commit • config • daemon • diff • diff-tree • fetch • gc • glog • init • log • ls-remote • ls-tree • merge • push • reflog • reset • rev-list • rev-parse • rm • show • show-ref • status
  5. Level One • Advantages • Easy to remember • Uses

    existing commands • In-process allows for repeated runs • Disadvantages • High level • Have to parse output • Does not allow for optimisations between runs
  6. Level Two • Create/use Git and built-in porcelain commands •

    Git git = org.eclipse.jgit.api.Git. open(new File(“/path/to/.git”)) • git.clean() • git.log() • git.lsRemote()
  7. Level Two • Commands use builder pattern • git.clean().setCleanDirectories(true). setIgnore(true).call()

    • git.lsRemote().setRemote().setTags(true). setHeads(true).call() i Builder pattern allows for new ‘arguments’ to be added over time
  8. Level Two • Advantages • Allows commands to be compile-time

    checked • Does not involve text processing • Can interpret/process results • Can invoke many commands on repo • Disadvantages • Required arguments may be missing • Limited to provided command API • May be more optimal to go deeper in some cases
  9. Level Three • Work directly with the repository • repository

    = FileRepositoryBuilder.create( new File(“...”)) • builder also handles cases like GIT_ environment variables and .git in parent directories • Repository provides object and ref databases
  10. Level Three • repository.getTags() • repository.getAllRefs() • repository.getBranch() (current branch)

    • repository.getRef(...) • HEAD = repository.getRef(“HEAD”) • repository.open(HEAD.getObjectId()). copyTo(System.out)
  11. Level Three • Advantages • Can use caches like RepositoryCache

    • Can work with/ update references directly • Disadvantages • Limited direct API on repository • Have to work with lower level APIs
  12. Level Four • Repositories are processed by walkers • Git

    repository content • References point to Commits (and tags,refs) • Commits point to Commits and Trees • Trees point to Trees and Blobs • Think of it as a Commit Iterator (RevWalk) or Directory/File Iterator (TreeWalk)
  13. Level Four RevWalk rw = new RevWalk(repository); HEAD = repository.resolve(“HEAD”)

    rw.markStart(rw.parseCommit(HEAD)) Iterator<RevCommit> it = rw.iterator() while(it.hasNext()) RevCommit commit = it.next() System.out.println( commit.abbreivate(6) .name() + “ ” + commit.getShortMessage())
  14. Level Four TreeWalk tw = new TreeWalk(repository); tree = repository.resolve(“HEAD^{tree}”)

    tw.addTree(tree) // tree ‘0’ tw.setRecursive(true) tw.setFilter(PathFilter.create(“some/file”)) while(tw.next()) id = tw.getObjectId(0) repository.open(id).copyTo(System.out)
  15. Level Four • Advantages • Can construct complex filters •

    Can walk commits between ranges • Can walk multiple trees at once (e.g. for diffing) • Disadvantages • Lacks a simple API to ‘get this file’ • Seems confusing at first • Dispose to release resources before re- use ! Walkers are not thread safe, so create separate ones if needed
  16. Level Five • ObjectInserter and ObjectReader are used to put

    and get data from repositories • id = repository.newObjectInserter( Constants.OJB_BLOB, “hello world”.getBytes(“UTF-8”)) • repository.newObjectReader().open(id). copyTo(System.out)
  17. Level Five • Advantages • Ultimate flexibility • Can store

    any content needed • Use a ‘notes-like’ approach to store additional metadata • Disadvantages • Complex to use • Need to build trees and commits to prevent being garbage collected
  18. Levels of Embedding 0. System.exec(“java -jar jgit.sh ...”) 1. Main.main([]“--git-dir”,

    “/path/.git”, “...”) 2. Git.open(new File(“.../.git”)).clean().call() 3. FileRepositoryBuilder.create(...).getRef() 4. new TreeWalk/RevWalk(repository) 5. repository.newObjectInserter/Reader
  19. Thankyou Alex Blewitt @alblue Winners of Eclipse 4 Plug-in development

    Vincenzo Caselli @vcaselli Lorenzo Bettini @lorenzo_bettini