Slide 1

Slide 1 text

Embedding JGit Alex Blewitt @alblue

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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()

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

Level Three • repository.getTags() • repository.getAllRefs() • repository.getBranch() (current branch) • repository.getRef(...) • HEAD = repository.getRef(“HEAD”) • repository.open(HEAD.getObjectId()). copyTo(System.out)

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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)

Slide 14

Slide 14 text

Level Four RevWalk rw = new RevWalk(repository); HEAD = repository.resolve(“HEAD”) rw.markStart(rw.parseCommit(HEAD)) Iterator it = rw.iterator() while(it.hasNext()) RevCommit commit = it.next() System.out.println( commit.abbreivate(6) .name() + “ ” + commit.getShortMessage())

Slide 15

Slide 15 text

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)

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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)

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

Thankyou Alex Blewitt @alblue Winners of Eclipse 4 Plug-in development Vincenzo Caselli @vcaselli Lorenzo Bettini @lorenzo_bettini