• Stars
    star
    139
  • Rank 261,580 (Top 6 %)
  • Language
    Go
  • License
    MIT License
  • Created about 9 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Generate a git graph structure from linear git history

Logo

Given a list of commits and their parents, return a structure that tells you exactly how to draw the git graph.
The algorithm try to reproduce the "sourcetree" graph style.

It takes a json:

[
  {"id": "1", "parents": ["3"], "non_related_attr": "non_related_value"},
  {"id": "2", "parents": ["3"]},
  {"id": "3", "parents": []}
]

and returns a structure that represent a git graph:

[
  {"id":"1",
   "parents":["3"],
   "column":0,
   "parents_paths":[{"id":"3","path":[{"x":0,"y":0,"type":0},{"x":0,"y":2,"type":0}],"color":"#5aa1be"}],
   "idx":0,
   "color":"#5aa1be",
   "non_related_attr": "non_related_value"},
  {"id":"2",
   "parents":["3"],
   "column":1,
   "parents_paths":[{"id":"3","path":[{"x":1,"y":1,"type":0},{"x":1,"y":2,"type":1},{"x":0,"y":2,"type":0}],"color":"#c065b8"}],
   "idx":1,
   "color":"#c065b8"},
  {"id":"3",
  "parents":[],
  "column":0,
  "parents_paths":[],
  "idx":2,
  "color":"#5aa1be"}
]

This structure can be directly rendered with D3.js, you can try it out here.

Logo

Other examples

Logo Logo Logo Logo

How to use

Inline

git2graph -j '[{"id": 1, "parents": ["2"]}, ...]'

File

git2graph -f path/to/file.json

Repository

git2graph -r (You must be in the repository directory)

In code

package main

import (
  "fmt"
  "git2graph"
)

func main() {
  in := []map[string]interface{}{}
  in = append(in, map[string]interface{}{"id": "1", "parents": []string{"3"}})
  in = append(in, map[string]interface{}{"id": "2", "parents": []string{"3"}})
  in = append(in, map[string]interface{}{"id": "3", "parents": []string{}})
  
  out, err := git2graph.Get(in)
  fmt.Println(out, err)
}

See it in action

renderer/index.html

Use D3.js to render the graph represented by the output of Git2Graph.

How to run

go run main.go -j '...'

Or

go install
git2graph -j '...'

How to test

go test ./...

TODO

  • Pagination
  • Colors algorithm

How to contribute

  • Fork the repo
  • Create a new branch
  • Make your changes
  • Create new tests
  • Append your name/email in main.go (contributors list)
  • Make a pull request :)