Git log: show second parent on left

Consider this git repository example.

#!/bin/bash -e
rm -rf example
git init example
cd example
echo 0 > file.txt
git add file.txt
git commit -am "initial commit"
git branch branch1
echo 1 > file.txt
git commit -am "1 (on master)"
git branch branch2
git checkout branch1
echo 2 > file2.txt
git add file2.txt
git commit -m "2 (on branch1)"
git merge --no-edit master
echo 3 > file2.txt
git commit -am "3 (on branch1)"
git checkout master
echo 4 > file.txt
git commit -am "4 (on master)"
git checkout branch1
git merge --no-edit master
echo 5 > file2.txt
git commit -am "5 (on branch1)"
git checkout master
git merge --no-edit --no-ff branch1
git log --graph --oneline

We create a branch branch1, weld it from the branch a mastercouple of times, and finally return to master.

git log --oneline --graph looks strangely twisted.

*   2c3b97a Merge branch 'branch1'
|\
| * 1d722df 5 (on branch1)
| *   54039b6 Merge branch 'master' into branch1
| |\
| |/
|/|
* | d80d82c 4 (on master)
| * 2399286 3 (on branch1)
| *   9a0cb38 Merge branch 'master' into branch1
| |\
| |/
|/|
* | 31df841 1 (on master)
| * 5809072 2 (on branch1)
|/
* 08fc7a6 initial commit

The graph will be simpler if it looks like this:

*   2c3b97a Merge branch 'branch1'
|\
| * 1d722df 5 (on branch1)
| *   54039b6 Merge branch 'master' into branch1
|/|
* | d80d82c 4 (on master)
| * 2399286 3 (on branch1)
| *   9a0cb38 Merge branch 'master' into branch1
|/|
* | 31df841 1 (on master)
| * 5809072 2 (on branch1)
|/
* 08fc7a6 initial commit

The reason I understand it is not like I git logalways prefer to show the “second parent” of each merge merge on the right side, so for each commit merge it has a snake to the right. ("Second parent" is the merging of branches, "first parent" is the branch we are joining.)

  • git log , ? 2013 ( " , ", ), .
  • , git log , git , ?
+4
3

torek suggestion, dot (aka Graphviz) Python script dot. , :

enter image description here

#!/usr/bin/python
import subprocess, sys, re

args = ['git', 'log', '--pretty=format:%x00%H%x01%h%x01%P%x00'] + sys.argv[1:]

rows = subprocess.check_output(args).split('\x00')[1::2]

seen_commits = set([row.split('\x01')[0] for row in rows])

print("digraph G {")
for row in rows:
    columns = row.split('\x01')
    commit = columns[0]
    label = columns[1]
    print('"{}" [label="{}"];'.format(commit, label))
    for parent in columns[2].split(' '):
        if parent == "":
            continue
        if not parent in seen_commits:
            continue
        print('"{}" -> "{}"'.format(commit, parent))
print("}")

$PATH git-log-dot chmod +x git-log-dot, git , git log-dot. ( , git , git-checkout, git-reset ..)

Graphviz. macOS brew install graphviz; Ubuntu , sudo apt-get graphviz.

PNG git log-dot :

git log-dot | dot -Tpng -o graph.png

( -Grankdir=LR, , .)

+2
  • git , ? 2013 , ( " , ", ), .

. (, , , - "NP hard" . git , , , , .)

  1. , git , git , ?

. gitk, , git log, , , " DAG ", . , . , dot .

. Wikipedia CS stackexchange.

+2

, git , /w20 > commit, ?

, , ,
, (, , ), - rebase ( --onto) , , .

- , , - git

, ( , )

git log --all --graph --decorate --oneline --boundary master...branch1

--topo-order && --no-walk .

summerize:

, - git.

, git , /w20 > commit, ?

I don’t know that any other tool can do this, since git will not “display” different dummy commits (in your example 9a0cb38) on the same “line” with a different branch

0
source

Source: https://habr.com/ru/post/1672398/


All Articles