1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* Tackler-NG 2023-2025
* SPDX-License-Identifier: Apache-2.0
*/
use itertools::Itertools;
use std::collections::HashSet;
use std::path::{Path, PathBuf};
use std::str;
//use std::time::{SystemTime, UNIX_EPOCH};
use crate::kernel::Settings;
use crate::kernel::settings::GitInputSelector;
use crate::model::{TxnData, Txns};
use crate::parser::tackler_parser;
use crate::tackler;
use gix as git;
use gix::date::time::CustomFormat;
use gix::hash as gix_hash;
use gix::objs::tree::EntryKind;
use tackler_api::metadata::items::{GitInputReference, MetadataItem};
pub fn string_to_txns(
input: &mut &str,
settings: &mut Settings,
) -> Result<TxnData, tackler::Error> {
let txns = tackler_parser::txns_text(input, settings)?;
// feature: a94d4a60-40dc-4ec0-97a3-eeb69399f01b
// coverage: "sorted" tested by 200aad57-9275-4d16-bdad-2f1c484bcf17
TxnData::from(None, txns, &settings.get_hash())
}
pub fn paths_to_txns(
paths: &[PathBuf],
settings: &mut Settings,
) -> Result<TxnData, tackler::Error> {
let txns: Result<Txns, tackler::Error> = paths
.iter()
.map(|p| tackler_parser::txns_file(p, settings))
.flatten_ok()
.collect();
TxnData::from(None, txns?, &settings.get_hash())
}
pub fn git_to_txns(
repo_path: &Path,
dir: &str,
extension: &str,
input_selector: GitInputSelector,
settings: &mut Settings,
) -> Result<TxnData, tackler::Error> {
// perf: let mut ts_par_total: u128 = 0;
// perf: let ts_start = SystemTime::now().duration_since(UNIX_EPOCH).unwrap(/*:test:*/);
let repo = git::open(repo_path)?;
let (object, reference) = match input_selector {
GitInputSelector::CommitId(id) => {
let mut candidates = Some(HashSet::default());
let prefix = match gix_hash::Prefix::try_from(id.as_str()) {
Ok(v) => v,
Err(err) => {
let msg = format!("Invalid commit id '{id}': {err}");
return Err(msg.into());
}
};
let res = repo.objects.lookup_prefix(prefix, candidates.as_mut())?;
let object_id = match res {
Some(Ok(id)) => id,
Some(Err(())) => return Err(format!("Ambiguous abbreviated commit id {id}").into()),
None => return Err(format!("Unknown commit id '{id}'").into()),
};
// This is originally commit, so no need to peel it
(repo.find_object(object_id)?.try_into_commit()?, None)
}
GitInputSelector::Reference(ref_str) => {
let id = repo.rev_parse_single(ref_str.as_bytes())?;
let reference = if id.to_string().starts_with(ref_str.as_str()) {
// This is tackler specific logic: don't show ref if it's plain commit id
None
} else {
Some(ref_str.clone())
};
// Peel it so that tags are ok
(id.object()?.peel_to_commit()?, reference)
}
};
let signature = object.author()?;
let author = format!("{} <{}>", signature.name, signature.email);
let date = signature
.time()?
.format(CustomFormat::new("%Y-%m-%d %H:%M:%S %z"))
.to_string();
let gitmd = GitInputReference {
commit: object.id.to_string(),
reference,
dir: dir.to_string(),
extension: extension.to_string(),
subject: object.message()?.summary().to_string(),
author,
date,
};
let dir = if dir.ends_with('/') {
dir.to_string()
} else {
format!("{}/", dir)
};
let ext = format!(".{}", extension);
let tree = object.tree()?;
// fixme: Optimization
// In the future, this could be optimized with custom walker,
// which does the filtering in the first place.
let txns: Result<Txns, tackler::Error> = tree
.traverse()
.breadthfirst
.files()?
.iter()
.map(|entry| {
use git::objs::tree::EntryKind::{Blob, Link};
match EntryKind::from(entry.mode) {
Blob => {
if entry.filepath.starts_with(str::as_bytes(dir.as_str()))
&& entry.filepath.ends_with(str::as_bytes(ext.as_str()))
{
let obj = repo.find_object(entry.oid)?;
// perf: let ts_par_start = SystemTime::now().duration_since(UNIX_EPOCH).unwrap(/*:test:*/);
let par_res =
tackler_parser::txns_text(&mut str::from_utf8(&obj.data)?, settings);
// perf: let ts_par_end = SystemTime::now().duration_since(UNIX_EPOCH).unwrap(/*:test:*/);
// perf: ts_par_total = ts_par_total + (ts_par_end.as_millis() - ts_par_start.as_millis());
match par_res {
Ok(txns) => Ok(txns),
Err(err) => {
let msg = format!(
"\
GIT: Error while processing git object\n\
\x20 commit id: {}\n\
\x20 object id: {}\n\
\x20 path: {}\n\
\x20 msg: {}\
",
object.id, obj.id, entry.filepath, err
);
Err(msg.into())
}
}
} else {
// It's blob but outside of our file path filter
Ok(Vec::default())
}
}
Link => {
let obj = repo.find_object(entry.oid)?;
let msg = format!(
"\
GIT: Error while processing git object\n\
\x20 commit id: {}\n\
\x20 object id: {}\n\
\x20 path: {}\n\
\x20 msg: {}\
",
object.id,
obj.id,
entry.filepath,
"Links inside repository are not supported"
);
Err(msg.into())
}
// It's not a blob
_ => Ok(Vec::default()),
}
})
.flatten_ok()
.collect::<Result<Txns, tackler::Error>>();
// perf: let ts_end = SystemTime::now().duration_since(UNIX_EPOCH).unwrap(/*:test:*/);
// perf: eprintln!("total time: {}ms, parse time: {}ms, git: {}ms", (ts_end.as_millis() - ts_start.as_millis()), ts_par_total, (ts_end.as_millis() - ts_start.as_millis())-ts_par_total);
let hash = &settings.get_hash();
TxnData::from(Some(MetadataItem::GitInputReference(gitmd)), txns?, hash)
}