History

Merge branch 'describe-rev'

Sebastian Thiel committed 4 years ago

Commit 77b7cd92 parents a87844a d24074027 files · +432 −65Browse files

Unified · Split

27 files
  1. Cargo.lock +9 −0
  2. Makefile +1 −0
  3. cargo-smart-release/src/utils.rs +2 −2
  4. git-actor/src/lib.rs +3 −3
  5. git-actor/src/signature/decode.rs +10 −2
  6. git-actor/src/signature/mod.rs +9 −9
  7. git-actor/src/time.rs +11 −11
  8. git-actor/tests/signature/mod.rs +2 −2
  9. git-actor/tests/time/mod.rs +6 −6
  10. git-object/src/commit/ref_iter.rs +4 −6
  11. git-object/tests/immutable/commit/from_bytes.rs +2 −2
  12. git-object/tests/immutable/commit/message.rs +2 −2
  13. git-object/tests/immutable/mod.rs +4 −4
  14. git-object/tests/immutable/tag.rs +2 −2
  15. git-odb/tests/odb/store/loose.rs +2 −2
  16. git-ref/src/store/file/log/line.rs +4 −4
  17. git-ref/src/store/file/loose/reflog/create_or_update/tests.rs +2 −2
  18. git-ref/tests/file/transaction/mod.rs +2 −2
  19. git-revision/Cargo.toml +13 −1
  20. git-revision/src/describe.rs +198 −0
  21. git-revision/src/lib.rs +3 −0
  22. git-revision/tests/describe/format.rs +30 −0
  23. git-revision/tests/describe/mod.rs +74 −0
  24. git-revision/tests/fixtures/make_repo_with_branches.sh +23 −0
  25. git-revision/tests/revision.rs +1 −0
  26. git-traverse/src/commit.rs +3 −1
  27. gitoxide-core/src/hours.rs +10 −2
Cargo.lock+9−0
@@ -1397,6 +1397,15 @@ dependencies = [ · 1396 unmodified lines
13971397 [[package]]
13981398 name = "git-revision"
13991399 version = "0.0.0"
1400+dependencies = [
1401+ "git-hash",
1402+ "git-object",
1403+ "git-repository",
1404+ "git-testtools",
1405+ "hash_hasher",
1406+ "quick-error",
1407+ "serde",
1408+]
14001409
14011410 [[package]]
14021411 name = "git-submodule"
Makefile+1−0
@@ -89,6 +89,7 @@ check: ## Build all code in suitable configurations · 88 unmodified lines
8989 cd git-object && cargo check --all-features \
9090 && cargo check --features verbose-object-parsing-errors
9191 cd git-index && cargo check --features serde1
92+ cd git-revision && cargo check --features serde1
9293 cd git-worktree && cargo check --features serde1
9394 cd git-actor && cargo check --features serde1
9495 cd git-pack && cargo check --features serde1 \
cargo-smart-release/src/utils.rs+2−2
@@ -300,7 +300,7 @@ mod tests { · 299 unmodified lines
300300 }
301301
302302 pub fn time_to_offset_date_time(time: git::actor::Time) -> OffsetDateTime {
303− time::OffsetDateTime::from_unix_timestamp(time.time as i64)
303+ time::OffsetDateTime::from_unix_timestamp(time.seconds_since_unix_epoch as i64)
304304 .expect("always valid unix time")
305− .replace_offset(time::UtcOffset::from_whole_seconds(time.offset).expect("valid offset"))
305+ .replace_offset(time::UtcOffset::from_whole_seconds(time.offset_in_seconds).expect("valid offset"))
306306 }
git-actor/src/lib.rs+3−3
@@ -56,10 +56,10 @@ pub enum Sign { · 55 unmodified lines
5656 #[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
5757 #[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
5858 pub struct Time {
59− /// time in seconds from epoch.
60− pub time: u32,
59+ /// time in seconds since epoch.
60+ pub seconds_since_unix_epoch: u32,
6161 /// time offset in seconds, may be negative to match the `sign` field.
62− pub offset: i32,
62+ pub offset_in_seconds: i32,
6363 /// the sign of `offset`, used to encode `-0000` which would otherwise loose sign information.
6464 pub sign: Sign,
6565 }
git-actor/src/signature/decode.rs+10−2
@@ -56,7 +56,11 @@ pub fn decode<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>( · 55 unmodified lines
5656 SignatureRef {
5757 name: name.as_bstr(),
5858 email: email.as_bstr(),
59− time: Time { time, offset, sign },
59+ time: Time {
60+ seconds_since_unix_epoch: time,
61+ offset_in_seconds: offset,
62+ sign,
63+ },
6064 },
6165 ))
6266 }
@@ -84,7 +88,11 @@ mod tests { · 21 unmodified lines
8488 SignatureRef {
8589 name: name.as_bytes().as_bstr(),
8690 email: email.as_bytes().as_bstr(),
87− time: Time { time, offset, sign },
91+ time: Time {
92+ seconds_since_unix_epoch: time,
93+ offset_in_seconds: offset,
94+ sign,
95+ },
8896 }
8997 }
9098
git-actor/src/signature/mod.rs+9−9
@@ -37,8 +37,8 @@ mod convert { · 36 unmodified lines
3737 name: Default::default(),
3838 email: Default::default(),
3939 time: Time {
40− time: 0,
41− offset: 0,
40+ seconds_since_unix_epoch: 0,
41+ offset_in_seconds: 0,
4242 sign: Sign::Plus,
4343 },
4444 }
@@ -145,11 +145,11 @@ mod init { · 100 unmodified lines
145145 name: name.into(),
146146 email: email.into(),
147147 time: Time {
148− time: std::time::SystemTime::now()
148+ seconds_since_unix_epoch: std::time::SystemTime::now()
149149 .duration_since(std::time::UNIX_EPOCH)
150150 .expect("the system time doesn't run backwards that much")
151151 .as_secs() as u32,
152− offset,
152+ offset_in_seconds: offset,
153153 sign: offset.into(),
154154 },
155155 })
@@ -164,11 +164,11 @@ mod init { · 8 unmodified lines
164164 name: name.into(),
165165 email: email.into(),
166166 time: Time {
167− time: std::time::SystemTime::now()
167+ seconds_since_unix_epoch: std::time::SystemTime::now()
168168 .duration_since(std::time::UNIX_EPOCH)
169169 .expect("the system time doesn't run backwards that much")
170170 .as_secs() as u32,
171− offset,
171+ offset_in_seconds: offset,
172172 sign: offset.into(),
173173 },
174174 }
@@ -183,15 +183,15 @@ mod init { · 8 unmodified lines
183183 name: name.into(),
184184 email: email.into(),
185185 time: Time {
186− time: seconds_since_epoch(),
187− offset: utc_offset,
186+ seconds_since_unix_epoch: seconds_since_unix_epoch(),
187+ offset_in_seconds: utc_offset,
188188 sign: utc_offset.into(),
189189 },
190190 }
191191 }
192192 }
193193
194− fn seconds_since_epoch() -> u32 {
194+ fn seconds_since_unix_epoch() -> u32 {
195195 std::time::SystemTime::now()
196196 .duration_since(std::time::UNIX_EPOCH)
197197 .expect("the system time doesn't run backwards that much")
git-actor/src/time.rs+11−11
@@ -16,7 +16,7 @@ impl Time { · 15 unmodified lines
1616 /// Serialize this instance to `out` in a format suitable for use in header fields of serialized git commits or tags.
1717 pub fn write_to(&self, mut out: impl io::Write) -> io::Result<()> {
1818 let mut itoa = itoa::Buffer::new();
19− out.write_all(itoa.format(self.time).as_bytes())?;
19+ out.write_all(itoa.format(self.seconds_since_unix_epoch).as_bytes())?;
2020 out.write_all(SPACE)?;
2121 out.write_all(match self.sign {
2222 Sign::Plus => b"+",
@@ -26,7 +26,7 @@ impl Time { · 3 unmodified lines
2626 const ZERO: &[u8; 1] = b"0";
2727
2828 const SECONDS_PER_HOUR: i32 = 60 * 60;
29− let offset = self.offset.abs();
29+ let offset = self.offset_in_seconds.abs();
3030 let hours = offset / SECONDS_PER_HOUR;
3131 assert!(hours < 25, "offset is more than a day: {}", hours);
3232 let minutes = (offset - (hours * SECONDS_PER_HOUR)) / 60;
@@ -45,23 +45,23 @@ impl Time { · 12 unmodified lines
4545 pub fn size(&self) -> usize {
4646 // TODO: this is not year 2038 safe…but we also can't parse larger numbers (or represent them) anyway. It's a trap nonetheless
4747 // that can be fixed by increasing the size to usize.
48− (if self.time >= 1_000_000_000 {
48+ (if self.seconds_since_unix_epoch >= 1_000_000_000 {
4949 10
50− } else if self.time >= 100_000_000 {
50+ } else if self.seconds_since_unix_epoch >= 100_000_000 {
5151 9
52− } else if self.time >= 10_000_000 {
52+ } else if self.seconds_since_unix_epoch >= 10_000_000 {
5353 8
54− } else if self.time >= 1_000_000 {
54+ } else if self.seconds_since_unix_epoch >= 1_000_000 {
5555 7
56− } else if self.time >= 100_000 {
56+ } else if self.seconds_since_unix_epoch >= 100_000 {
5757 6
58− } else if self.time >= 10_000 {
58+ } else if self.seconds_since_unix_epoch >= 10_000 {
5959 5
60− } else if self.time >= 1_000 {
60+ } else if self.seconds_since_unix_epoch >= 1_000 {
6161 4
62− } else if self.time >= 100 {
62+ } else if self.seconds_since_unix_epoch >= 100 {
6363 3
64− } else if self.time >= 10 {
64+ } else if self.seconds_since_unix_epoch >= 10 {
6565 2
6666 } else {
6767 1
git-actor/tests/signature/mod.rs+2−2
@@ -43,8 +43,8 @@ mod write_to { · 42 unmodified lines
4343
4444 fn default_time() -> Time {
4545 Time {
46− time: 0,
47− offset: 0,
46+ seconds_since_unix_epoch: 0,
47+ offset_in_seconds: 0,
4848 sign: Sign::Plus,
4949 }
5050 }
git-actor/tests/time/mod.rs+6−6
@@ -6,24 +6,24 @@ fn write_to() -> Result<(), Box<dyn std::error::Error>> { · 5 unmodified lines
66 for (time, expected) in &[
77 (
88 Time {
9− time: 500,
10− offset: 9000,
9+ seconds_since_unix_epoch: 500,
10+ offset_in_seconds: 9000,
1111 sign: Sign::Plus,
1212 },
1313 "500 +0230",
1414 ),
1515 (
1616 Time {
17− time: 189009009,
18− offset: 36000,
17+ seconds_since_unix_epoch: 189009009,
18+ offset_in_seconds: 36000,
1919 sign: Sign::Minus,
2020 },
2121 "189009009 -1000",
2222 ),
2323 (
2424 Time {
25− time: 0,
26− offset: 0,
25+ seconds_since_unix_epoch: 0,
26+ offset_in_seconds: 0,
2727 sign: Sign::Minus,
2828 },
2929 "0 -0000",
git-object/src/commit/ref_iter.rs+4−6
@@ -58,12 +58,10 @@ impl<'a> CommitRefIter<'a> { · 57 unmodified lines
5858 /// if not exactly two signatures were iterable.
5959 /// Errors are not the common case - if an error needs to be detectable, use this instance as iterator.
6060 pub fn signatures(&'a mut self) -> impl Iterator<Item = git_actor::SignatureRef<'_>> + 'a {
61− self.filter_map(Result::ok)
62− .skip_while(|t| !matches!(t, Token::Author { .. } | Token::Committer { .. }))
63− .filter_map(|t| match t {
64− Token::Author { signature } | Token::Committer { signature } => Some(signature),
65− _ => None,
66− })
61+ self.filter_map(|t| match t {
62+ Ok(Token::Author { signature }) | Ok(Token::Committer { signature }) => Some(signature),
63+ _ => None,
64+ })
6765 }
6866
6967 /// Returns the committer signature if there is no decoding error.
git-object/tests/immutable/commit/from_bytes.rs+2−2
@@ -139,8 +139,8 @@ fn with_trailer() -> crate::Result { · 138 unmodified lines
139139 name: "Kim Altintop".into(),
140140 email: "kim@eagain.st".into(),
141141 time: Time {
142− time: 1631514803,
143− offset: 7200,
142+ seconds_since_unix_epoch: 1631514803,
143+ offset_in_seconds: 7200,
144144 sign: Sign::Plus,
145145 },
146146 };
git-object/tests/immutable/commit/message.rs+2−2
@@ -236,8 +236,8 @@ mod summary { · 235 unmodified lines
236236 name: "name".into(),
237237 email: "email".into(),
238238 time: Time {
239− time: 0,
240− offset: 0,
239+ seconds_since_unix_epoch: 0,
240+ offset_in_seconds: 0,
241241 sign: Sign::Plus,
242242 },
243243 };
git-object/tests/immutable/mod.rs+4−4
@@ -29,8 +29,8 @@ fn signature(time: u32) -> git_actor::SignatureRef<'static> { · 28 unmodified lines
2929 name: b"Sebastian Thiel".as_bstr(),
3030 email: b"sebastian.thiel@icloud.com".as_bstr(),
3131 time: Time {
32− time,
33− offset: 28800,
32+ seconds_since_unix_epoch: time,
33+ offset_in_seconds: 28800,
3434 sign: Sign::Plus,
3535 },
3636 }
@@ -42,8 +42,8 @@ fn linus_signature(time: u32) -> git_actor::SignatureRef<'static> { · 5 unmodified lines
4242 name: b"Linus Torvalds".as_bstr(),
4343 email: b"torvalds@linux-foundation.org".as_bstr(),
4444 time: Time {
45− time,
46− offset: -25200,
45+ seconds_since_unix_epoch: time,
46+ offset_in_seconds: -25200,
4747 sign: Sign::Minus,
4848 },
4949 }
git-object/tests/immutable/tag.rs+2−2
@@ -234,8 +234,8 @@ cjHJZXWmV4CcRfmLsXzU8s2cR9A0DBvOxhPD1TlKC2JhBFXigjuL9U4Rbq9tdegB · 233 unmodified lines
234234 name: b"Sebastian Thiel".as_bstr(),
235235 email: b"byronimo@gmail.com".as_bstr(),
236236 time: git_actor::Time {
237− time: 1528473343,
238− offset,
237+ seconds_since_unix_epoch: 1528473343,
238+ offset_in_seconds: offset,
239239 sign: git_actor::Sign::Plus,
240240 },
241241 }),
git-odb/tests/odb/store/loose.rs+2−2
@@ -288,8 +288,8 @@ fn signature(time: u32) -> git_actor::SignatureRef<'static> { · 287 unmodified lines
288288 name: b"Sebastian Thiel".as_bstr(),
289289 email: b"byronimo@gmail.com".as_bstr(),
290290 time: Time {
291− time,
292− offset: 7200,
291+ seconds_since_unix_epoch: time,
292+ offset_in_seconds: 7200,
293293 sign: Sign::Plus,
294294 },
295295 }
git-ref/src/store/file/log/line.rs+4−4
@@ -229,8 +229,8 @@ pub mod decode { · 228 unmodified lines
229229 name: b"name".as_bstr(),
230230 email: b"foo@example.com".as_bstr(),
231231 time: Time {
232− time: 1234567890,
233− offset: 0,
232+ seconds_since_unix_epoch: 1234567890,
233+ offset_in_seconds: 0,
234234 sign: Sign::Minus
235235 }
236236 },
@@ -255,8 +255,8 @@ pub mod decode { · 18 unmodified lines
255255 name: b"Sebastian Thiel".as_bstr(),
256256 email: b"foo@example.com".as_bstr(),
257257 time: Time {
258− time: 1618030561,
259− offset: 28800,
258+ seconds_since_unix_epoch: 1618030561,
259+ offset_in_seconds: 28800,
260260 sign: Sign::Plus,
261261 },
262262 },
git-ref/src/store/file/loose/reflog/create_or_update/tests.rs+2−2
@@ -73,8 +73,8 @@ fn missing_reflog_creates_it_even_if_similarly_named_empty_dir_exists_and_append · 72 unmodified lines
7373 name: "committer".into(),
7474 email: "commiter@example.com".into(),
7575 time: Time {
76− time: 1234,
77− offset: 1800,
76+ seconds_since_unix_epoch: 1234,
77+ offset_in_seconds: 1800,
7878 sign: Sign::Plus,
7979 },
8080 };
git-ref/tests/file/transaction/mod.rs+2−2
@@ -25,8 +25,8 @@ mod prepare_and_commit { · 24 unmodified lines
2525 name: "committer".into(),
2626 email: "committer@example.com".into(),
2727 time: Time {
28− time: 1234,
29− offset: 1800,
28+ seconds_since_unix_epoch: 1234,
29+ offset_in_seconds: 1800,
3030 sign: Sign::Plus,
3131 },
3232 }
git-revision/Cargo.toml+13−1
@@ -10,6 +10,18 @@ edition = "2018" · 9 unmodified lines
1010 [lib]
1111 doctest = false
1212
13−# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
13+[features]
14+## Data structures implement `serde::Serialize` and `serde::Deserialize`.
15+serde1 = [ "serde", "git-hash/serde1", "git-object/serde1" ]
1416
17+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1518 [dependencies]
19+git-hash = { version = "^0.9.1", path = "../git-hash" }
20+git-object = { version = "^0.17.0", path = "../git-object" }
21+hash_hasher = "2.0.3"
22+quick-error = "2.0.0"
23+serde = { version = "1.0.114", optional = true, default-features = false, features = ["derive"] }
24+
25+[dev-dependencies]
26+git-testtools = { path = "../tests/tools" }
27+git-repository = { version = "^0.15.0", path = "../git-repository", default-features = false, features = ["unstable"] }
git-revision/src/describe.rs+198−0
@@ -0,0 +1,198 @@
1+use git_object::bstr::BStr;
2+use std::borrow::Cow;
3+use std::fmt::{Display, Formatter};
4+
5+#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
6+#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
7+pub struct Outcome<'a> {
8+ pub name: Cow<'a, BStr>,
9+ pub id: git_hash::ObjectId,
10+ pub depth: u32,
11+}
12+
13+impl<'a> Outcome<'a> {
14+ pub fn into_format(self, hex_len: usize) -> Format<'a> {
15+ Format {
16+ name: self.name,
17+ id: self.id,
18+ hex_len,
19+ depth: self.depth,
20+ long: false,
21+ dirty_suffix: None,
22+ }
23+ }
24+}
25+
26+#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
27+#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
28+pub struct Format<'a> {
29+ pub name: Cow<'a, BStr>,
30+ pub id: git_hash::ObjectId,
31+ pub hex_len: usize,
32+ pub depth: u32,
33+ pub long: bool,
34+ pub dirty_suffix: Option<String>,
35+}
36+
37+impl<'a> Format<'a> {
38+ pub fn is_exact_match(&self) -> bool {
39+ self.depth == 0
40+ }
41+ pub fn long(&mut self) -> &mut Self {
42+ self.long = true;
43+ self
44+ }
45+ pub fn short(&mut self) -> &mut Self {
46+ self.long = false;
47+ self
48+ }
49+}
50+
51+impl<'a> Display for Format<'a> {
52+ fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
53+ if !self.long && self.is_exact_match() {
54+ self.name.fmt(f)?;
55+ } else {
56+ write!(
57+ f,
58+ "{}-{}-g{}",
59+ self.name,
60+ self.depth,
61+ self.id.to_hex_with_len(self.hex_len)
62+ )?;
63+ }
64+ if let Some(suffix) = &self.dirty_suffix {
65+ write!(f, "-{}", suffix)?;
66+ }
67+ Ok(())
68+ }
69+}
70+
71+pub(crate) mod function {
72+ use super::Outcome;
73+ use git_hash::{oid, ObjectId};
74+ use git_object::bstr::BStr;
75+ use git_object::CommitRefIter;
76+ use std::borrow::Cow;
77+ use std::collections::{hash_map, HashMap, VecDeque};
78+ use std::iter::FromIterator;
79+
80+ #[allow(clippy::result_unit_err)]
81+ pub fn describe<'a, Find, E>(
82+ commit: &oid,
83+ mut find: Find,
84+ name_set: &HashMap<ObjectId, Cow<'a, BStr>>,
85+ ) -> Result<Option<Outcome<'a>>, E>
86+ where
87+ Find: for<'b> FnMut(&oid, &'b mut Vec<u8>) -> Result<CommitRefIter<'b>, E>,
88+ E: std::error::Error + Send + Sync + 'static,
89+ {
90+ if let Some(name) = name_set.get(commit) {
91+ return Ok(Some(Outcome {
92+ name: name.clone(),
93+ id: commit.to_owned(),
94+ depth: 0,
95+ }));
96+ }
97+
98+ let mut buf = Vec::new();
99+ let mut parent_buf = Vec::new();
100+ let mut parents = Vec::new();
101+
102+ let mut queue = VecDeque::from_iter(Some(commit.to_owned()));
103+ let mut candidates = Vec::new();
104+ let mut seen_commits = 0;
105+ let mut _gave_up_on_commit = None;
106+ let mut seen = hash_hasher::HashedMap::default();
107+ seen.insert(commit.to_owned(), 0u32);
108+
109+ const MAX_CANDIDATES: usize = std::mem::size_of::<Flags>() * 8;
110+ while let Some(commit) = queue.pop_front() {
111+ seen_commits += 1;
112+ if let Some(name) = name_set.get(&commit) {
113+ if candidates.len() < MAX_CANDIDATES {
114+ let identity_bit = 1 << candidates.len();
115+ candidates.push(Candidate {
116+ name: name.clone(),
117+ commits_in_its_future: seen_commits - 1,
118+ identity_bit,
119+ order: candidates.len(),
120+ });
121+ *seen.get_mut(&commit).expect("inserted") |= identity_bit;
122+ } else {
123+ _gave_up_on_commit = Some(commit);
124+ break;
125+ }
126+ }
127+
128+ let flags = seen[&commit];
129+ for candidate in candidates
130+ .iter_mut()
131+ .filter(|c| (flags & c.identity_bit) != c.identity_bit)
132+ {
133+ candidate.commits_in_its_future += 1;
134+ }
135+
136+ let commit_iter = find(&commit, &mut buf)?;
137+ parents.clear();
138+ for token in commit_iter {
139+ match token {
140+ Ok(git_object::commit::ref_iter::Token::Tree { .. }) => continue,
141+ Ok(git_object::commit::ref_iter::Token::Parent { id: parent_id }) => {
142+ match seen.entry(parent_id) {
143+ hash_map::Entry::Vacant(entry) => {
144+ let mut parent = find(&parent_id, &mut parent_buf)?;
145+ // TODO: figure out if not having a date is a hard error.
146+ let parent_commit_date = parent
147+ .committer()
148+ .map(|committer| committer.time.seconds_since_unix_epoch)
149+ .unwrap_or_default();
150+
151+ entry.insert(flags);
152+ parents.push((parent_id, parent_commit_date));
153+ }
154+ hash_map::Entry::Occupied(mut entry) => {
155+ *entry.get_mut() |= flags;
156+ }
157+ }
158+ }
159+ Ok(_unused_token) => break,
160+ Err(_err) => todo!("return a decode error"),
161+ }
162+ }
163+
164+ if !parents.is_empty() {
165+ parents.sort_by(|a, b| a.1.cmp(&b.1).reverse());
166+ seen.extend(parents.iter().map(|e| (e.0, flags)));
167+ queue.extend(parents.iter().map(|e| e.0));
168+ }
169+ }
170+
171+ if candidates.is_empty() {
172+ return Ok(None);
173+ }
174+
175+ candidates.sort_by(|a, b| {
176+ a.commits_in_its_future
177+ .cmp(&b.commits_in_its_future)
178+ .then_with(|| a.order.cmp(&b.order))
179+ });
180+ Ok(candidates.into_iter().next().map(|c| Outcome {
181+ name: c.name,
182+ id: commit.to_owned(),
183+ depth: c.commits_in_its_future,
184+ }))
185+ }
186+
187+ type Flags = u32;
188+
189+ #[derive(Debug)]
190+ struct Candidate<'a> {
191+ name: Cow<'a, BStr>,
192+ commits_in_its_future: Flags,
193+ /// A single bit identifying this candidate uniquely in a bitset
194+ identity_bit: Flags,
195+ /// The order at which we found the candidate, first one has order = 0
196+ order: usize,
197+ }
198+}
git-revision/src/lib.rs+3−0
@@ -1 +1,4 @@
11 #![forbid(unsafe_code, rust_2018_idioms)]
2+
3+pub mod describe;
4+pub use describe::function::describe;
git-revision/tests/describe/format.rs+30−0
@@ -0,0 +1,30 @@
1+use git_object::bstr::ByteSlice;
2+use git_revision::describe;
3+use git_testtools::hex_to_id;
4+use std::borrow::Cow;
5+
6+#[test]
7+fn exact_match_with_dirty_and_long() {
8+ let mut format = describe::Outcome {
9+ name: Cow::Borrowed(b"main".as_bstr()),
10+ id: hex_to_id("b920bbb055e1efb9080592a409d3975738b6efb3"),
11+ depth: 0,
12+ }
13+ .into_format(7);
14+ assert!(format.is_exact_match());
15+ assert_eq!(format.to_string(), "main");
16+ assert_eq!(format.long().to_string(), "main-0-gb920bbb");
17+
18+ format.dirty_suffix = Some("dirty".into());
19+ assert_eq!(format.short().to_string(), "main-dirty");
20+ assert_eq!(format.long().to_string(), "main-0-gb920bbb-dirty");
21+
22+ format.dirty_suffix = None;
23+ format.depth = 42;
24+ assert!(!format.is_exact_match());
25+ assert_eq!(format.short().to_string(), "main-42-gb920bbb");
26+
27+ format.dirty_suffix = Some("dirty".into());
28+ assert_eq!(format.to_string(), "main-42-gb920bbb-dirty");
29+ assert_eq!(format.long().to_string(), "main-42-gb920bbb-dirty");
30+}
git-revision/tests/describe/mod.rs+74−0
@@ -0,0 +1,74 @@
1+use git_object::bstr::ByteSlice;
2+use git_repository::odb::FindExt;
3+use git_repository::Repository;
4+use git_revision::describe;
5+use git_testtools::hex_to_id;
6+use std::borrow::Cow;
7+
8+mod format;
9+
10+#[test]
11+fn option_none_if_no_tag_found() {
12+ let repo = repo();
13+ let commit = repo.head().unwrap().peel_to_commit_in_place().unwrap();
14+ let res = git_revision::describe(
15+ &commit.id,
16+ |id, buf| repo.objects.find_commit_iter(id, buf),
17+ &Default::default(),
18+ )
19+ .unwrap();
20+ assert_eq!(res, None, "cannot find anything if there's no candidate");
21+}
22+#[test]
23+fn typical_usecases() {
24+ let repo = repo();
25+ let commit = repo.head().unwrap().peel_to_commit_in_place().unwrap();
26+ let name = Cow::Borrowed(b"main".as_bstr());
27+ let res = git_revision::describe(
28+ &commit.id,
29+ |_, _| Err(std::io::Error::new(std::io::ErrorKind::Other, "shouldn't be called")),
30+ &vec![(commit.id, name.clone())].into_iter().collect(),
31+ )
32+ .unwrap();
33+
34+ assert_eq!(
35+ res,
36+ Some(describe::Outcome {
37+ name,
38+ id: commit.id,
39+ depth: 0,
40+ }),
41+ "this is an exact match"
42+ );
43+
44+ let name = Cow::Borrowed(b"at-c5".as_bstr());
45+ let res = git_revision::describe(
46+ &commit.id,
47+ |id, buf| repo.objects.find_commit_iter(id, buf),
48+ &vec![
49+ (hex_to_id("efd9a841189668f1bab5b8ebade9cd0a1b139a37"), name.clone()),
50+ (
51+ hex_to_id("9152eeee2328073cf23dcf8e90c949170b711659"),
52+ b"at-b1c1".as_bstr().into(),
53+ ),
54+ ]
55+ .into_iter()
56+ .collect(),
57+ )
58+ .unwrap();
59+
60+ assert_eq!(
61+ res,
62+ Some(describe::Outcome {
63+ name,
64+ id: commit.id,
65+ depth: 3,
66+ }),
67+ "a match to a tag 1 commit away with 2 commits on the other side of the merge/head"
68+ );
69+}
70+
71+fn repo() -> Repository {
72+ let dir = git_testtools::scripted_fixture_repo_read_only("make_repo_with_branches.sh").unwrap();
73+ git_repository::open(dir).unwrap()
74+}
git-revision/tests/fixtures/make_repo_with_branches.sh+23−0
@@ -0,0 +1,23 @@
1+#!/bin/bash
2+set -eu -o pipefail
3+
4+git init -q
5+git config commit.gpgsign false
6+git config merge.ff false
7+
8+git checkout -q -b main
9+git commit -q --allow-empty -m c1
10+git tag at-c1
11+git commit -q --allow-empty -m c2
12+git commit -q --allow-empty -m c3
13+git commit -q --allow-empty -m c4
14+
15+git checkout -q -b branch1
16+git commit -q --allow-empty -m b1c1
17+git tag at-b1c1
18+git commit -q --allow-empty -m b1c2
19+
20+git checkout -q main
21+git commit -q --allow-empty -m c5
22+git tag at-c5
23+git merge branch1 -m m1b1
git-revision/tests/revision.rs+1−0
@@ -0,0 +1 @@
1+mod describe;
git-traverse/src/commit.rs+3−1
@@ -217,7 +217,9 @@ pub mod ancestors { · 216 unmodified lines
217217 .and_then(|mut parent| parent.committer().map(|committer| committer.time));
218218
219219 if let Some(parent_committer_date) = parent_committer_date {
220− state.parents_with_date.push((id, parent_committer_date.time));
220+ state
221+ .parents_with_date
222+ .push((id, parent_committer_date.seconds_since_unix_epoch));
221223 }
222224
223225 if matches!(self.parents, Parents::First) {
gitoxide-core/src/hours.rs+10−2
@@ -104,7 +104,14 @@ where · 103 unmodified lines
104104 },
105105 )
106106 .ok_or_else(|| anyhow!("An error occurred when decoding commits - one commit could not be parsed"))?;
107− all_commits.sort_by(|a, b| a.email.cmp(&b.email).then(a.time.time.cmp(&b.time.time).reverse()));
107+ all_commits.sort_by(|a, b| {
108+ a.email.cmp(&b.email).then(
109+ a.time
110+ .seconds_since_unix_epoch
111+ .cmp(&b.time.seconds_since_unix_epoch)
112+ .reverse(),
113+ )
114+ });
108115 if all_commits.is_empty() {
109116 bail!("No commits to process");
110117 }
@@ -185,7 +192,8 @@ fn estimate_hours(commits: &[actor::Signature]) -> WorkByEmail { · 74 unmodified lines
185192 + commits.iter().rev().tuple_windows().fold(
186193 0_f32,
187194 |hours, (cur, next): (&actor::Signature, &actor::Signature)| {
188− let change_in_minutes = (next.time.time - cur.time.time) as f32 / MINUTES_PER_HOUR;
195+ let change_in_minutes =
196+ (next.time.seconds_since_unix_epoch - cur.time.seconds_since_unix_epoch) as f32 / MINUTES_PER_HOUR;
189197 if change_in_minutes < MAX_COMMIT_DIFFERENCE_IN_MINUTES {
190198 hours + change_in_minutes as f32 / MINUTES_PER_HOUR
191199 } else {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t