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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#![deny(warnings)]
#![warn(unused_extern_crates)]

#[macro_export]
macro_rules! some_or {
    ($e:expr, $b:block) => {
        if let Some(x) = $e {
            x
        } else {
            $b
        }
    };
}

#[macro_export]
macro_rules! ok_or {
    ($e:expr, $b:block) => {
        if let Ok(x) = $e {
            x
        } else {
            $b
        }
    };
}

#[macro_use]
extern crate rs_tracing;

#[macro_use]
extern crate handlebars;

#[macro_use]
extern crate pest_derive;

#[macro_use]
extern crate serde_json;

use std::collections::HashMap;
use tracing;

pub mod cache;
pub mod filter;
pub mod graphql;
pub mod history;
pub mod housekeeping;
pub mod query;
pub mod shell;

#[derive(Clone)]
pub enum UnapplyResult {
    Done(git2::Oid),
    RejectMerge(String),
    RejectAmend(String),
    BranchDoesNotExist,
}

const FRAGMENT: &percent_encoding::AsciiSet = &percent_encoding::CONTROLS
    .add(b'/')
    .add(b'*')
    .add(b' ')
    .add(b'~')
    .add(b'^')
    .add(b':')
    .add(b'?')
    .add(b'[')
    .add(b']')
    .add(b'{')
    .add(b'}')
    .add(b'@')
    .add(b'\\');

pub fn to_ns(path: &str) -> String {
    return percent_encoding::utf8_percent_encode(path.trim_matches('/'), FRAGMENT).to_string();
}

pub fn from_ns(path: &str) -> String {
    return percent_encoding::percent_decode_str(path.trim_matches('/'))
        .decode_utf8_lossy()
        .to_string();
}

pub fn to_filtered_ref(upstream_repo: &str, filter_spec: &str) -> String {
    return format!(
        "josh/filtered/{}/{}",
        to_ns(upstream_repo),
        to_ns(filter_spec)
    );
}

#[derive(Debug, Clone)]
pub struct JoshError(pub String);

pub fn josh_error(s: &str) -> JoshError {
    JoshError(s.to_owned())
}

impl std::fmt::Display for JoshError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "JoshError({})", self.0)
    }
}

pub type JoshResult<T> = std::result::Result<T, JoshError>;

impl<T> std::convert::From<T> for JoshError
where
    T: std::error::Error,
{
    fn from(item: T) -> Self {
        tracing::event!(tracing::Level::ERROR, item = ?item, error = true);
        log::error!("JoshError: {:?}", item);
        josh_error(&format!("converted {:?}", item))
    }
}

#[macro_use]
extern crate lazy_static;

#[macro_export]
macro_rules! regex_parsed {
    ($name:ident, $re:literal,  [$( $i:ident ),+]) => {

        struct $name {
            $(
                $i: String,
            )+
        }

impl $name {
    fn from_str(path: &str) -> Option<$name> {

lazy_static! {
    static ref REGEX: regex::Regex =
        regex::Regex::new($re)
            .expect("can't compile regex");
}

        let caps = if let Some(caps) = REGEX.captures(&path) {
            caps
        } else {
            return None;
        };

        let as_str = |x: regex::Match| x.as_str().to_owned();

        return Some($name {
            $(
            $i: caps.name(stringify!($i)).map(as_str).unwrap_or("".to_owned()),
            )+
        });
    }
}
    }
}

pub fn get_change_id(commit: &git2::Commit) -> Option<String> {
    for line in commit.message().unwrap_or("").split('\n') {
        if line.starts_with("Change-Id: ") {
            let id = line.replace("Change-Id: ", "");
            return Some(id);
        }
    }
    None
}

#[tracing::instrument(skip(transaction))]
pub fn filter_ref(
    transaction: &cache::Transaction,
    filterobj: filter::Filter,
    from_refsname: &str,
    to_refname: &str,
    permissions: filter::Filter,
) -> JoshResult<usize> {
    let mut updated_count = 0;
    if let Ok(reference) = transaction.repo().revparse_single(from_refsname) {
        let original_commit = reference.peel_to_commit()?;
        let oid = original_commit.id();

        let perms_commit = if let Some(s) = transaction.get_ref(permissions, oid) {
            s
        } else {
            tracing::trace!("apply_to_commit (permissions)");

            filter::apply_to_commit(permissions, &original_commit, &transaction)?
        };

        if perms_commit != git2::Oid::zero() {
            let perms_commit = transaction.repo().find_commit(perms_commit)?;
            if !perms_commit.tree()?.is_empty() || perms_commit.parents().len() > 0 {
                tracing::event!(
                    tracing::Level::WARN,
                    msg = "filter_refs: missing permissions for ref",
                    warn = true,
                    reference = from_refsname,
                );
                return Err(josh_error("missing permissions for ref"));
            }
        }

        let filter_commit = if let Some(s) = transaction.get_ref(filterobj, oid) {
            s
        } else {
            tracing::trace!("apply_to_commit");

            filter::apply_to_commit(filterobj, &original_commit, transaction)?
        };

        let previous = transaction
            .repo()
            .revparse_single(to_refname)
            .map(|x| x.id())
            .unwrap_or(git2::Oid::zero());

        if filter_commit != previous {
            updated_count += 1;
            tracing::trace!(
                "filter_ref: update reference: {:?} -> {:?}, target: {:?}, filter: {:?}",
                &from_refsname,
                &to_refname,
                filter_commit,
                &filter::spec(filterobj),
            );
        }

        transaction.insert_ref(filterobj, oid, filter_commit);

        if filter_commit != git2::Oid::zero() {
            ok_or!(
                transaction
                    .repo()
                    .reference(to_refname, filter_commit, true, "apply_filter")
                    .map(|_| ()),
                {
                    tracing::error!(
                        "can't update reference: {:?} -> {:?}, target: {:?}, filter: {:?}",
                        &from_refsname,
                        &to_refname,
                        filter_commit,
                        &filter::spec(filterobj),
                    );
                }
            );
        }
    } else {
        tracing::warn!("filter_ref: Can't find reference {:?}", &from_refsname);
    };
    Ok(updated_count)
}

pub fn filter_refs(
    transaction: &cache::Transaction,
    filterobj: filter::Filter,
    refs: &[(String, String)],
    permissions: filter::Filter,
) -> JoshResult<usize> {
    rs_tracing::trace_scoped!("filter_refs", "spec": filter::spec(filterobj));
    let s = tracing::Span::current();
    let _e = s.enter();

    tracing::trace!("filter_refs");

    let mut updated_count = 0;
    for (k, v) in refs {
        updated_count += ok_or!(filter_ref(&transaction, filterobj, &k, &v, permissions), {
            tracing::event!(
                tracing::Level::WARN,
                msg = "filter_refs: Can't filter reference",
                warn = true,
                from = k.as_str(),
                to = v.as_str()
            );
            0
        });
    }
    Ok(updated_count)
}

pub fn normalize_path(path: &std::path::Path) -> std::path::PathBuf {
    let mut components = path.components().peekable();
    let mut ret = if let Some(c @ std::path::Component::Prefix(..)) = components.peek().cloned() {
        components.next();
        std::path::PathBuf::from(c.as_os_str())
    } else {
        std::path::PathBuf::new()
    };

    for component in components {
        match component {
            std::path::Component::Prefix(..) => unreachable!(),
            std::path::Component::RootDir => {
                ret.push(component.as_os_str());
            }
            std::path::Component::CurDir => {}
            std::path::Component::ParentDir => {
                ret.pop();
            }
            std::path::Component::Normal(c) => {
                ret.push(c);
            }
        }
    }
    ret
}

type Users = HashMap<String, User>;

#[derive(Debug, serde::Deserialize)]
struct User {
    pub groups: toml::value::Array,
}

type Groups = HashMap<String, HashMap<String, Group>>;
#[derive(Debug, serde::Deserialize)]
struct Group {
    pub whitelist: String,
    pub blacklist: String,
}

pub fn get_acl(
    users: &str,
    groups: &str,
    user: &str,
    repo: &str,
) -> JoshResult<(filter::Filter, filter::Filter)> {
    let users =
        std::fs::read_to_string(users).map_err(|_| josh_error("failed to read users file"))?;
    let users: Users = serde_yaml::from_str(&users)
        .map_err(|err| josh_error(format!("failed to parse users file: {}", err).as_str()))?;
    let groups =
        std::fs::read_to_string(groups).map_err(|_| josh_error("failed to read groups file"))?;
    let groups: Groups = serde_yaml::from_str(&groups)
        .map_err(|err| josh_error(format!("failed to parse groups file: {}", err).as_str()))?;

    return users
        .get(user)
        .and_then(|u| {
            let mut whitelist = filter::empty();
            let mut blacklist = filter::empty();
            for g in &u.groups {
                let lists = groups.get(repo).and_then(|repo| {
                    repo.get(g.as_str()?).and_then(|group| {
                        let w = filter::parse(&group.whitelist);
                        let b = filter::parse(&group.blacklist);
                        Some((w, b))
                    })
                })?;
                if let Err(e) = lists.0 {
                    return Some(Err(JoshError(format!("Error parsing whitelist: {}", e))));
                }
                if let Err(e) = lists.1 {
                    return Some(Err(JoshError(format!("Error parsing blacklist: {}", e))));
                }
                if let Ok(w) = lists.0 {
                    whitelist = filter::compose(whitelist, w);
                }
                if let Ok(b) = lists.1 {
                    blacklist = filter::compose(blacklist, b);
                }
            }
            println!("w: {:?}, b: {:?}", whitelist, blacklist);
            Some(Ok((whitelist, blacklist)))
        })
        .unwrap_or(Ok((filter::empty(), filter::nop())));
}