git for debugging (a.k.a code archaeology)

34
Git for debuggin Riaan Corneliu a.k.a Code archaeolo

Upload: riaan-cornelius

Post on 17-Aug-2015

77 views

Category:

Presentations & Public Speaking


2 download

TRANSCRIPT

Page 1: Git for debugging (a.k.a Code archaeology)

Git for debugging

Riaan Cornelius

a.k.a Code archaeology

Page 2: Git for debugging (a.k.a Code archaeology)

TopicsBecause nobody likes a surprise

2

• Code Archaeology• Git Blame• Git log• Pickaxe• Git show

• Example: Jenkins• Debugging with git bisect• Automating git bisect

• Bitbucket code review

Page 3: Git for debugging (a.k.a Code archaeology)

Code ArchaeologyUncovering the history of your code

3

• All code is always documented.

• Separating the interesting commits from the uninteresting ones.

• Git has tools to make this easier.

Page 4: Git for debugging (a.k.a Code archaeology)

Code ArchaeologyUncovering the history of your code

4

Git blameAnnotates each line in the given file with information from the revision which last modified the line.

With -w, blame ignores lines where only whitespace changed.With -M, blame detects moved or copied lines within a file.With -C, blame extends this move or copy detection to other files that were modified in the same commit.

Page 5: Git for debugging (a.k.a Code archaeology)

Code ArchaeologyUncovering the history of your code

5

Git logShows the commit logs.

With --date=short shows only the date, but not the time, in YYYY-MM-DD format.With --graph draws a text-based graphical representation of the commit history on the left hand side of the output. With --pretty you can specify a format.

Page 6: Git for debugging (a.k.a Code archaeology)

Code ArchaeologyUncovering the history of your code

6

PickaxeFor Detecting Addition/Deletion of Specified String. Actually a git log param: -S‘<block of text>’

When used with --pickaxe-regex, treat the <block of text> as an extended POSIX regular expression to match, instead of a literal string.

Page 7: Git for debugging (a.k.a Code archaeology)

Code ArchaeologyUncovering the history of your code

7

Git showFor commits it shows the log message and textual diff.

When used with --quiet, Don’t show the diff.With --pretty you can specify a format.

Page 8: Git for debugging (a.k.a Code archaeology)

ExampleJenkins

8

Uncovering some of Jenkin’s history

Page 9: Git for debugging (a.k.a Code archaeology)

Why do we need a Jenkins repoWho wrote that code?

9

Page 10: Git for debugging (a.k.a Code archaeology)

Git blameWho wrote that code?

10

Page 11: Git for debugging (a.k.a Code archaeology)

Git showWhat info can we get from the commit?

11

Page 12: Git for debugging (a.k.a Code archaeology)

Git blameWho wrote that code?

12

Page 13: Git for debugging (a.k.a Code archaeology)

Git showWhat info can we get from the commit?

13

Page 14: Git for debugging (a.k.a Code archaeology)

PickaxeDigging deeper

14

Page 15: Git for debugging (a.k.a Code archaeology)

Git logSeeing the bigger picture

15

git log

Page 16: Git for debugging (a.k.a Code archaeology)

Git logSeeing the bigger picture

16

Page 17: Git for debugging (a.k.a Code archaeology)

Git logSeeing the bigger picture

17

git log --graph --abbrev-commit --pretty=format:'%Cred%h%Creset –%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'

Page 18: Git for debugging (a.k.a Code archaeology)

Git logMaking it prettier

18

Page 19: Git for debugging (a.k.a Code archaeology)

Git logAdvanced use case - churn

19

git log --all -M -C --name-only --format='format:' "$@" | sort | grep -v '^$' | uniq -c | sort | awk 'BEGIN {print "count\tfile"} {print $1 "\t" $2}' | sort -g

Page 20: Git for debugging (a.k.a Code archaeology)

Git bisectHunting down bugs

20

Git BisectFind by binary search the change that introduced a bug.

Simple process:• Git bisect start <BAD> <GOOD>• Git bisect good | bad• Repeat until first bad commit found• Git bisect reset

Page 21: Git for debugging (a.k.a Code archaeology)

Git bisectHunting down bugs

21

Simple example.

After a night of drunken coding, the main.java file is missing. Find out what happened.

Page 22: Git for debugging (a.k.a Code archaeology)

Git bisectHunting down bugs

22

Page 23: Git for debugging (a.k.a Code archaeology)

Git bisectHunting down bugs

23

Page 24: Git for debugging (a.k.a Code archaeology)

Git bisectHunting down bugs

24

Page 25: Git for debugging (a.k.a Code archaeology)

Git bisect (automated)Hunting down bugs the easy way

25

How do we automate this.

Essentially same steps as previously, but we write a test script to determine whether the commit is good or bad.

• Git bisect start <BAD> <GOOD>• Git bisect run <TEST SCRIPT>• Wait for bad commit to be found• Git bisect reset

Page 26: Git for debugging (a.k.a Code archaeology)

Git bisect (automated)Hunting down bugs the easy way

26

Page 27: Git for debugging (a.k.a Code archaeology)

Git bisect (automated)Hunting down bugs the easy way

27

Page 28: Git for debugging (a.k.a Code archaeology)

Bitbucket peer reviewWhy?

28

Why peer review?

• Improved code quality.• Knowledge transfer.

Page 29: Git for debugging (a.k.a Code archaeology)

Bitbucket peer reviewWhy?

29

We know we should do it, but polls consistently show only about 50% of teams peer review.

Page 30: Git for debugging (a.k.a Code archaeology)

Bitbucket peer reviewHow?

30

Page 31: Git for debugging (a.k.a Code archaeology)

Bitbucket peer reviewHow?

31

Page 32: Git for debugging (a.k.a Code archaeology)

Bitbucket peer reviewHow?

32

Page 33: Git for debugging (a.k.a Code archaeology)

In closingWhat should you take from this?

33

Code archaeology• All code is always documented

Learn to use Git more productively• Git blame• Git log• Pickaxe

• Git show• Third party extensions• So many others!

Do peer reviews!

Page 34: Git for debugging (a.k.a Code archaeology)

34

Questions?

References:

Git man pages: http://git-scm.com/docsCode Archaeology: http://jfirebaugh.github.com/blog/2012/03/07/code-archaeology-with-gitGit tutorial: http://www.vogella.com/tutorials/Git/article.html

XKCD References you might have noticed (Less useful, but definitely more amusing)Ballmer peak: https://xkcd.com/323/Commit messages: https://xkcd.com/1296/