Top Banner
Embedding JGit Alex Blewitt @alblue
20

Embedding JGit

Jun 27, 2015

Download

Technology

Alex Blewitt

Embedding JGit presentation slides from EclipseCon Europe 2013.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Embedding JGit

Embedding JGitAlex Blewitt

@alblue

Page 2: Embedding JGit

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

Page 3: Embedding JGit

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

Page 4: Embedding JGit

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

Page 5: Embedding JGit

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

Page 6: Embedding JGit

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

Page 7: Embedding JGit

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

Page 8: Embedding JGit

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

Page 9: Embedding JGit

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

Page 10: Embedding JGit

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

Page 11: Embedding JGit

Level Three

• repository.getTags()

• repository.getAllRefs()

• repository.getBranch() (current branch)

• repository.getRef(...)

• HEAD = repository.getRef(“HEAD”)

• repository.open(HEAD.getObjectId()). copyTo(System.out)

Page 12: Embedding JGit

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

Page 13: Embedding JGit

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)

Page 14: Embedding JGit

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

Page 15: Embedding JGit

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)

Page 16: Embedding JGit

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

Page 17: Embedding JGit

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)

Page 18: Embedding JGit

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

Page 19: Embedding JGit

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

Page 20: Embedding JGit

ThankyouAlex Blewitt

@alblue

Winners of Eclipse 4 Plug-in development

Vincenzo Caselli@vcaselli

Lorenzo Bettini@lorenzo_bettini