A PlantUML library to draw git graphs
Go to file
Bram Schoenmakers e56be90e66 Merge branch 'main' of apps.bram85.nl:bram/git.plantuml 2023-07-24 22:41:31 +02:00
img Simplify branch / tag label placement 2022-10-26 23:27:52 +02:00
README.org Merge branch 'main' of apps.bram85.nl:bram/git.plantuml 2023-07-24 22:41:31 +02:00
git.plantuml Add URL to source file 2023-07-24 22:41:14 +02:00

README.org

Draw git branch trees with PlantUML

Example

  @startuml
  !include git.plantuml

  $branch_start(master, 5)
  $tag(master3, v0.1)
  $branch_stop()

  $branch(feature, 3, master, 2, master, 4)
  $branch(unfinished, 2, master, 2)
  @enduml

/git/bram/git.plantuml/media/branch/main/img/example.png

Source

Header

  ''''''''''''''''''''''''''''''''''''''''''
  ' THIS FILE IS GENERATED FROM README.ORG '
  ''''''''''''''''''''''''''''''''''''''''''

  ' Author: Bram Schoenmakers <me@bramschoenmakers.nl>
  ' Source: https://apps.bram85.nl/gitea/bram/git.plantuml
  ' License: Attribution 4.0 International (CC BY 4.0)
  '          https://creativecommons.org/licenses/by/4.0/

  @startuml
  <<style>>
  <<functions>>
  <<procedures>>
  @enduml

Style

  hide stereotype

  skinparam interface {
          backgroundColor Gold
  }

  skinparam rectangle {
          backgroundColor<<branchLabel>> Green
          fontColor<<branchLabel>> white
          backgroundColor<<tag>> Yellow
          fontSize 11
  }

Functions

  !function $commitName($branch, $number)
  !return %string($branch + $number)
  !endfunction

Procedures

Commit

  !procedure $declareCommit($name, $label="")
  !if ($label != "")
  circle $label as $name
  !else
  circle $name
  !endif
  !endprocedure

  !procedure $root($name, $label="")
  $declareCommit($name, $label)
  !endprocedure

  ' $parent1 and $parent2 are supposed to be declared
  !procedure $commit($name, $parent1, $parent2="", $label="")
  $declareCommit($name, $label)

  !if ($parent1 != "")
  ($parent1) -up-> ($name)
  !endif

  !if ($parent2 != "")
  ($parent2) -up-> ($name)
  !endif

  !endprocedure

Branch label

Usage
  @startuml
  !include git.plantuml

  $declareCommit(1)
  $commit(e1, 1)
  $commit(m, 1)

  $branchLabel(m, master)
  $branchLabel(e1, experiment)
  @enduml

/git/bram/git.plantuml/media/branch/main/img/branch-label.png

Source
  !procedure $branchLabel($commit, $name)
  rectangle "$name" <<branchLabel>>
  $commit . [$name] #grey
  !endprocedure
TODO Use notes instead of rectangles

To ease the layouting.

Adjust note color accordingly.

Tag

Assigns a tag label to a commit. The commit has to be added with a $commit or a $declareCommit before.

Usage
  @startuml
  !include git.plantuml

  $declareCommit(1)
  $commit(2, 1)

  $tag(2, "v0.1")
  @enduml

/git/bram/git.plantuml/media/branch/main/img/tag.png

Source
  !procedure $tag($commit, $name)
  together {
          rectangle "$name" <<tag>>
          $commit . [$name] #grey
  }
  !endprocedure
TODO Use notes instead of rectangles

Adjust note color accordingly.

Branch

  !procedure $branch_start($name, $numCommits, $fromBranch1="", $fromCommit1="", $toBranch="", $toCommit="", $fromBranch2="", $fromCommit2="")
  rectangle {
          !$counter = $numCommits
          !while $counter != 0
          !$parentCommit = %string($name + ($counter - 1))
          !$thisCommit = %string($name + $counter)

          $declareCommit($thisCommit, $counter)
          !if ($counter > 1)
          $declareCommit($parentCommit, %string($counter - 1))
          $commit($thisCommit, $parentCommit, $label=$counter)
          !endif

          !if ($counter == $numCommits)
          !if ($toBranch != "")
          $commit($commitName($toBranch, $toCommit), $thisCommit, $label=$toCommit)
          !endif

          $branchLabel($thisCommit, $name)
          !endif

          !if ($counter == 1 && $fromBranch1 != "")
          $declareCommit($commitName($fromBranch1, $fromCommit1), $fromCommit1)


          !if ($fromBranch2 != "")
          $declareCommit($commitName($fromBranch2, $fromCommit2), $fromCommit2)
          !endif

          $commit($thisCommit, $commitName($fromBranch1, $fromCommit1), $commitName($fromBranch2, $fromCommit2), $counter)
          !endif

          !$counter = $counter - 1
          !endwhile
  !endprocedure

  !procedure $branch_stop()
  }
  !endprocedure

  !procedure $branch($name, $numCommits, $fromBranch1="", $fromCommit1="", $toBranch="", $toCommit="", $fromBranch2="", $fromCommit2="")
  $branch_start($name, $numCommits, $fromBranch1, $fromCommit1, $toBranch, $toCommit, $fromBranch2, $fromCommit2)
  $branch_stop()
  !endprocedure

COMMENT Meta

Local variables

After save, extract the library content. It would've been nice to regenerate the images with org-babel-execute-buffer on each save, but that doesn't work well.

Local variables: eval: (add-hook 'after-save-hook #'org-babel-tangle 0 t) End: