Upload
alex-blewitt
View
852
Download
2
Tags:
Embed Size (px)
DESCRIPTION
Embedding JGit presentation slides from EclipseCon Europe 2013.
Citation preview
Embedding JGitAlex Blewitt
@alblue
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
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
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
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
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
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()
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
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
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
Level Three
• repository.getTags()
• repository.getAllRefs()
• repository.getBranch() (current branch)
• repository.getRef(...)
• HEAD = repository.getRef(“HEAD”)
• repository.open(HEAD.getObjectId()). copyTo(System.out)
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
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)
Level FourRevWalk 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())
Level FourTreeWalk 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)
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
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)
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
Levels of Embedding0. 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
ThankyouAlex Blewitt
@alblue
Winners of Eclipse 4 Plug-in development
Vincenzo Caselli@vcaselli
Lorenzo Bettini@lorenzo_bettini