History

smart-release: refactor (#198)

Sebastian Thiel committed 5 years ago

Commit 87ebacc1 parent 4b48a143 files · +27 −24Browse files

Unified · Split

3 files
  1. cargo-smart-release/src/command/changelog/git.rs +6 −23
  2. cargo-smart-release/src/command/changelog/history.rs +14 −0
  3. cargo-smart-release/src/command/changelog/mod.rs +7 −1
cargo-smart-release/src/command/changelog/git.rs+6−23
@@ -8,27 +8,10 @@ use git_repository::{ · 7 unmodified lines
8 prelude::{CacheAccessExt, ObjectAccessExt, ReferenceAccessExt, ReferenceExt},8 prelude::{CacheAccessExt, ObjectAccessExt, ReferenceAccessExt, ReferenceExt},
9};9};
10 10
11use crate::command::changelog_impl::{history, History};
11use crate::utils::{component_to_bytes, is_tag_name, is_tag_version, package_by_name, tag_prefix};12use crate::utils::{component_to_bytes, is_tag_name, is_tag_version, package_by_name, tag_prefix};
12use std::cell::RefCell;13use std::cell::RefCell;
13 14
14/// A head reference will all commits that are 'governed' by it, that is are in its exclusive ancestry.
15pub struct Segment<'a> {
16 _head: git::refs::Reference,
17 /// only relevant history items, that is those that change code in the respective crate.
18 history: Vec<&'a HistoryItem>,
19}
20
21pub struct History {
22 head: git::refs::Reference,
23 items: Vec<HistoryItem>,
24}
25
26pub struct HistoryItem {
27 id: git::hash::ObjectId,
28 _message: git::bstr::BString,
29 tree_data: Vec<u8>,
30}
31
32pub fn commit_history(repo: &git::Easy) -> anyhow::Result<Option<History>> {15pub fn commit_history(repo: &git::Easy) -> anyhow::Result<Option<History>> {
33 let start = Instant::now();16 let start = Instant::now();
34 let prev = repo.object_cache_size(64 * 1024)?;17 let prev = repo.object_cache_size(64 * 1024)?;
@@ -47,7 +30,7 @@ pub fn commit_history(repo: &git::Easy) -> anyhow::Result<Option<History>> { · 12 unmodified lines
47 (commit.message.to_owned(), commit.tree())30 (commit.message.to_owned(), commit.tree())
48 };31 };
49 32
50 items.push(HistoryItem {33 items.push(history::Item {
51 id: commit_id.detach(),34 id: commit_id.detach(),
52 _message: message,35 _message: message,
53 tree_data: repo.find_object(tree_id)?.data.to_owned(),36 tree_data: repo.find_object(tree_id)?.data.to_owned(),
@@ -69,11 +52,11 @@ pub fn commit_history(repo: &git::Easy) -> anyhow::Result<Option<History>> { · 15 unmodified lines
69}52}
70 53
71/// Return the head reference followed by all tags affecting `crate_name` as per our tag name rules, ordered by ancestry.54/// Return the head reference followed by all tags affecting `crate_name` as per our tag name rules, ordered by ancestry.
72pub fn crate_references_descending<'h>(55pub fn ref_segments<'h>(
73 crate_name: &str,56 crate_name: &str,
74 ctx: &crate::Context,57 ctx: &crate::Context,
75 history: &'h History,58 history: &'h History,
76) -> anyhow::Result<Vec<Segment<'h>>> {59) -> anyhow::Result<Vec<history::Segment<'h>>> {
77 let meta = &ctx.meta;60 let meta = &ctx.meta;
78 let package = package_by_name(meta, crate_name)?;61 let package = package_by_name(meta, crate_name)?;
79 let tag_prefix = tag_prefix(package, &ctx.repo);62 let tag_prefix = tag_prefix(package, &ctx.repo);
@@ -115,7 +98,7 @@ pub fn crate_references_descending<'h>( · 35 unmodified lines
115 98
116 let start = Instant::now();99 let start = Instant::now();
117 let mut segments = Vec::new();100 let mut segments = Vec::new();
118 let mut segment = Segment {101 let mut segment = history::Segment {
119 _head: history.head.to_owned(),102 _head: history.head.to_owned(),
120 history: vec![],103 history: vec![],
121 };104 };
@@ -196,7 +179,7 @@ pub fn crate_references_descending<'h>( · 74 unmodified lines
196 },179 },
197 Some(next_ref) => segments.push(std::mem::replace(180 Some(next_ref) => segments.push(std::mem::replace(
198 &mut segment,181 &mut segment,
199 Segment {182 history::Segment {
200 _head: next_ref,183 _head: next_ref,
201 history: vec![item],184 history: vec![item],
202 },185 },
cargo-smart-release/src/command/changelog/history.rs+14−0
@@ -0,0 +1,14 @@
1use git_repository as git;
2
3/// A head reference will all commits that are 'governed' by it, that is are in its exclusive ancestry.
4pub struct Segment<'a> {
5 pub _head: git::refs::Reference,
6 /// only relevant history items, that is those that change code in the respective crate.
7 pub history: Vec<&'a Item>,
8}
9
10pub struct Item {
11 pub id: git::hash::ObjectId,
12 pub _message: git::bstr::BString,
13 pub tree_data: Vec<u8>,
14}
cargo-smart-release/src/command/changelog/mod.rs+7−1
@@ -1,6 +1,12 @@
1use crate::command::changelog::Options;1use crate::command::changelog::Options;
2 2
3mod git;3mod git;
4mod history;
5
6pub struct History {
7 head: git_repository::refs::Reference,
8 items: Vec<history::Item>,
9}
4 10
5pub fn changelog(options: Options, crates: Vec<String>) -> anyhow::Result<()> {11pub fn changelog(options: Options, crates: Vec<String>) -> anyhow::Result<()> {
6 let ctx = crate::Context::new(crates)?;12 let ctx = crate::Context::new(crates)?;
@@ -15,7 +21,7 @@ pub fn changelog(options: Options, crates: Vec<String>) -> anyhow::Result<()> { · 8 unmodified lines
15 Some(history) => history,21 Some(history) => history,
16 };22 };
17 for crate_name in &crate_names {23 for crate_name in &crate_names {
18 git::crate_references_descending(crate_name, &ctx, &history)?;24 let _segments = git::ref_segments(crate_name, &ctx, &history)?;
19 }25 }
20 26
21 Ok(())27 Ok(())