Make sure git2 revwalk is linear

Hi, I'm using a git2::Revwalk. I'm not clear from the documentation about whether or not its possible to get a linear history of commits.

My goal is to walk the commit graph from the very first commit to the tip of a given branch. Is there a way to make sure that the walk doesn't also traverse other branches/paths? Maybe that is already the default? I'm very new to this crate so I don't fully understand the behavior.

You fundamentally cannot do that in git; you must walk in the other direction. (You could collect that into something and use it however you want, but the walk itself can only go from HEAD backwards.)

1 Like

You might not be able to do that with git in general, but the git2 crate provides an API to reverse the results of the revwalk. My main concern was not being able to walk backwards, it was that the other branches along the way might get walked as well.


I did some experiments, and it turns out that the behaviour I want works pretty much out of the box.

Here's my test code:

// passed the branch into `branch()` in RepoBuilder so the right branch gets checked out
let mut revwalk = repo.revwalk()?;
revwalk.push_head()?;
revwalk.set_sorting(git2::Sort::TIME | git2::Sort::REVERSE);
for rev in revwalk {
    let commit = repo.find_commit(rev?)?;
    let message = commit.summary_bytes().unwrap_or_else(|| commit.message_bytes());
    println!("{}\t{}", commit.id(), String::from_utf8_lossy(message));
}

The commit graph of my test repo is shown below. The branch names are b1 to b6.

The output for this repo (shown below) is exactly what I wanted. :smile:

branch = b1

a00c020dafbce294edb14701377de4a61fb3e15d        1.1
3c0b760d139ff8cbf3fa8f8de42b4fbf2090d65c	1.2
71a9786f5bb8f387b333a530f4edb7399df96358	1.3
2ebbdeba6267f94ac11cdb5ac74136a092de6fda	1.4
a88128d6f7d9777ca5d32403d75b5d04d7dfd637	1.5
79f459480c21633de1bb28c2f8af365aaf77c9f6	1.6
2acf3a2985580f7e1d44f10cb548bc6e21f65b04	1.7
fc49791f8210a1a0ec4e866250fdc9227dbbb686	1.8
ff0457f2a2d56e0bf082c02473aac3b07958093f	1.9
50e0de44bdbad510be260b99075b700d7bdbef3d	1.10
40a88fa4ecea0d20166cf5d8395f9e5a49b4d36b	1.11
d813246473667380a24454cead5c6b2531992029	1.12

branch = b2

a00c020dafbce294edb14701377de4a61fb3e15d        1.1
3c0b760d139ff8cbf3fa8f8de42b4fbf2090d65c	1.2
71a9786f5bb8f387b333a530f4edb7399df96358	1.3
1c069073f7a006df003b6636059eacf94cf8c014	2.1
b0c91bf5a0f688a14e25934ab08ff9528ea5ca6c	2.2

branch = b3

a00c020dafbce294edb14701377de4a61fb3e15d        1.1
3c0b760d139ff8cbf3fa8f8de42b4fbf2090d65c	1.2
71a9786f5bb8f387b333a530f4edb7399df96358	1.3
2ebbdeba6267f94ac11cdb5ac74136a092de6fda	1.4
a88128d6f7d9777ca5d32403d75b5d04d7dfd637	1.5
ea7b8bf232974ed7d82fb58b17bd5cd511995c9b	3.1
eff5d1631d867aaa2a4b0e9747e7cb87a79aa115	3.2
827937946baf3a00fc11ca4310cb68a1d5fa7567	3.3
1ef0bf89bcccb03ed34cdf0f241c230295f295bb	3.4
2de82e7f418e583b5eb792f3eb492bebebfce0a3	3.5

branch = b4

a00c020dafbce294edb14701377de4a61fb3e15d        1.1
3c0b760d139ff8cbf3fa8f8de42b4fbf2090d65c	1.2
71a9786f5bb8f387b333a530f4edb7399df96358	1.3
2ebbdeba6267f94ac11cdb5ac74136a092de6fda	1.4
a88128d6f7d9777ca5d32403d75b5d04d7dfd637	1.5
ea7b8bf232974ed7d82fb58b17bd5cd511995c9b	3.1
eff5d1631d867aaa2a4b0e9747e7cb87a79aa115	3.2
25871a26546d917e70321bcc54587e557194b07e	4.1
c7e8650cccfa384610b48419a38b6e51d69ba0ac	4.2

branch = b5

a00c020dafbce294edb14701377de4a61fb3e15d        1.1
3c0b760d139ff8cbf3fa8f8de42b4fbf2090d65c	1.2
71a9786f5bb8f387b333a530f4edb7399df96358	1.3
2ebbdeba6267f94ac11cdb5ac74136a092de6fda	1.4
a88128d6f7d9777ca5d32403d75b5d04d7dfd637	1.5
67171d63bcc63215b3c10bbaf95e781b2a4cdd76	5.1

branch = b6

a00c020dafbce294edb14701377de4a61fb3e15d        1.1
3c0b760d139ff8cbf3fa8f8de42b4fbf2090d65c	1.2
71a9786f5bb8f387b333a530f4edb7399df96358	1.3
2ebbdeba6267f94ac11cdb5ac74136a092de6fda	1.4
a88128d6f7d9777ca5d32403d75b5d04d7dfd637	1.5
79f459480c21633de1bb28c2f8af365aaf77c9f6	1.6
2acf3a2985580f7e1d44f10cb548bc6e21f65b04	1.7
fc49791f8210a1a0ec4e866250fdc9227dbbb686	1.8
ff0457f2a2d56e0bf082c02473aac3b07958093f	1.9
50e0de44bdbad510be260b99075b700d7bdbef3d	1.10
40a88fa4ecea0d20166cf5d8395f9e5a49b4d36b	1.11
d813246473667380a24454cead5c6b2531992029	1.12
7117fd110cbde358eb2042edf409665baaa636a2	6.1
9772a66c49807bb9b9c4a28365a4ab93c2983747	6.2
e8fd1659fdb9af99e36d51a2e7ef03481ac960ab	6.3
0d6d500607ea154041bc9bb0a4c154141e81f84d	6.4
e2394061a04a3f53d3e8822ae67c2feb3bec979b	6.5
6cc8eb1be1b51a44b6b364601dc21fdee2d9a716	6.6
4c2adfe3f1275758d92e284cee66f1862750ee8a	6.7

Hope this helps someone else who is wanting to do something similar. :tada: :tada: :tada: :tada: