gpt4 book ai didi

git日志历史简化

转载 作者:IT王子 更新时间:2023-10-29 00:39:06 24 4
gpt4 key购买 nike

假设我有以下历史

        D---E-------F
/ \ \
B---C---G---H---I---J
/ \
A-------K---------------L--M

git log --ancestry-path D..M 会给我

            E-------F
\ \
G---H---I---J
\
L--M

但是,我只想要以下内容

            E
\
G---H---I---J
\
L--M

或者

            E-------F
\
I---J
\
L--M

本质上,我只想遍历一条路径,而不是两条路径。

这可能吗?如果是这样,命令是什么?

编辑:

我试过使用 --first-parent,但事实并非如此。git log --first-parent G..M 给我

                    F
\
H---I---J
\
L--M

它包括 F,因为 F 是 I 的第一个父级。相反,我想要

                  H---I---J
\
L--M

任何帮助将不胜感激

解决方案(对我有用):

正如@VonC 所说,没有一个单行代码可以做到这一点。所以我最终使用了 bash 脚本。

  1. 对于“git log --ancestry-path G..M”中的每个提交
  2. 确定 $commit 的父级是否包含我们之前的提交
  3. 如果是,请继续。做一些有趣的事情。
  4. 如果否,则跳过该提交。

例如git log --first-commit G..M是

H - F - I - J - L - M

但是,F 的父级是 E,而不是 H。所以我们省略 F,给了我

H - I - J - L - M

耶!

最佳答案

我不认为这是直接可能的(除非你事先知道要包含/排除的确切列表,这否定了遍历 DAG 的目的)

实际上,OP Ken Hirakawa通过以下方式设法获得预期的线性历史记录:

git log --pretty=format:"%h%n" --ancestry-path --reverse $prev_commit..$end_commit

对于每个提交,确保它是前一个提交的直接子项。

这是 script writtten by Ken Hirakawa .


这是我创建 History Simplification 中提到的 DAG 的脚本git log 手册页的部分,对于 --ancestry-path:

你会在最后找到我用来创建类似历史记录的 bash 脚本(用根目录的名称和你的用户名调用它)。

我定义:

$ git config --global alias.lgg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative"

我得到:

$ git lgg
* d7c4459 - (HEAD, M, fromA) M <VonC>
* 82b011d - (L) Merge commit 'J' into fromA <VonC>
|\
| * 190265b - (J, master) J <VonC>
| * ef8e325 - (I) Merge commit 'F' <VonC>
| |\
| | * 4b6d976 - (F, fromB) F <VonC>
| * | 45a5d4d - (H) H <VonC>
| * | 834b239 - (G) Merge commit 'E' <VonC>
| |\ \
| | |/
| | * f8e9272 - (E) E <VonC>
| | * 96b5538 - (D) D <VonC>
| * | 49eff7f - (C) C <VonC>
| |/
| * 02c3ef4 - (B) B <VonC>
* | c0d9e1e - (K) K <VonC>
|/
* 6530d79 - (A) A <VonC>

从那里,我不能排除提交 I 的 parent 之一。

祖先路径确实返回:

$ git lgg --ancestry-path D..M
* d7c4459 - (HEAD, M, fromA) M <VonC>
* 82b011d - (L) Merge commit 'J' into fromA <VonC>
* 190265b - (J, master) J <VonC>
* ef8e325 - (I) Merge commit 'F' <VonC>
|\
| * 4b6d976 - (F, fromB) F <VonC>
* | 45a5d4d - (H) H <VonC>
* | 834b239 - (G) Merge commit 'E' <VonC>
|/
* f8e9272 - (E) E <VonC>

与日志手册页一致:

A regular D..M computes the set of commits that are ancestors of M, but excludes the ones that are ancestors of D.
This is useful to see what happened to the history leading to M since D, in the sense that "what does M have that did not exist in D".
The result in this example would be all the commits, except A and B (and D itself, of course).

When we want to find out what commits in M are contaminated with the bug introduced by D and need fixing, however, we might want to view only the subset of D..M that are actually descendants of D, i.e. excluding C and K.
This is exactly what the --ancestry-path option does.


#!/bin/bash

function makeCommit() {
local letter=$1
if [[ `git tag -l $letter` == "" ]] ; then
echo $letter > $root/$letter
git add .
git commit -m "${letter}"
git tag -m "${letter}" $letter
else
echo "commit $letter already there"
fi
}

function makeMerge() {
local letter=$1
local from=$2
if [[ `git tag -l $letter` == "" ]] ; then
git merge $from
git tag -m "${letter}" $letter
else
echo "merge $letter already done"
fi
}

function makeBranch() {
local branch=$1
local from=$2
if [[ "$(git branch|grep $1)" == "" ]] ; then
git checkout -b $branch $from
else
echo "branch $branch already created"
git checkout $branch
fi
}

root=$1
user=$2
if [[ ! -e $root/.git ]] ; then
git init $root
fi
export GIT_WORK_TREE="./$root"
export GIT_DIR="./$root/.git"
git config --local user.name $2

makeCommit "A"
makeCommit "B"
makeCommit "C"
makeBranch "fromB" "B"
makeCommit "D"
makeCommit "E"
makeCommit "F"
git checkout master
makeMerge "G" "E"
makeCommit "H"
makeMerge "I" "F"
makeCommit "J"
makeBranch "fromA" "A"
makeCommit "K"
makeMerge "L" "J"
makeCommit "M"

关于git日志历史简化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6740119/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com