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
use impl_prelude::*;
#[derive(Debug, Default, Clone, Copy)]
pub struct SubmoduleRewind;
impl SubmoduleRewind {
pub fn new() -> Self {
SubmoduleRewind
}
}
impl Check for SubmoduleRewind {
fn name(&self) -> &str {
"submodule-rewind"
}
fn check(&self, ctx: &CheckGitContext, commit: &Commit) -> Result<CheckResult> {
let mut result = CheckResult::new();
for diff in &commit.diffs {
if diff.new_mode != "160000" {
continue;
}
match diff.status {
StatusChange::Deleted |
StatusChange::Added => continue,
_ => (),
}
let submodule_ctx = if let Some(ctx) = SubmoduleContext::new(ctx, diff.name.as_ref()) {
ctx
} else {
continue;
};
let cat_file = submodule_ctx.context
.git()
.arg("cat-file")
.arg("-t")
.arg(diff.new_blob.as_str())
.output()
.chain_err(|| "failed to construct cat-file command")?;
let object_type = String::from_utf8_lossy(&cat_file.stdout);
if !cat_file.status.success() || object_type.trim() != "commit" {
continue;
}
let cat_file = submodule_ctx.context
.git()
.arg("cat-file")
.arg("-t")
.arg(diff.old_blob.as_str())
.output()
.chain_err(|| "failed to construct cat-file command")?;
let object_type = String::from_utf8_lossy(&cat_file.stdout);
if !cat_file.status.success() || object_type.trim() != "commit" {
continue;
}
let merge_base = submodule_ctx.context
.git()
.arg("merge-base")
.arg(diff.old_blob.as_str())
.arg(diff.new_blob.as_str())
.output()
.chain_err(|| "failed to construct merge-base command")?;
if !merge_base.status.success() {
bail!(ErrorKind::Git(format!("failed to get the merge base for the `{}` \
submodule: {}",
diff.name,
String::from_utf8_lossy(&merge_base.stderr))));
}
let base = String::from_utf8_lossy(&merge_base.stdout);
if base.trim() == diff.new_blob.as_str() {
result.add_error(format!("commit {} is not allowed since it moves the submodule \
`{}` backwards from {} to {}.",
commit.sha1,
submodule_ctx.path,
diff.old_blob,
diff.new_blob));
}
}
Ok(result)
}
}
#[cfg(test)]
mod tests {
use checks::SubmoduleRewind;
use checks::test::*;
const MOVE_TOPIC: &str = "2088079e35503be3be41dbdca55080ced95614e1";
const REWIND_TOPIC: &str = "39c5d0d9dc7ee6abad72cd42c90d7c1af1be169c";
const TO_UNAVAILABLE_TOPIC: &str = "1b9275caca1557611df19d1dfea687c3ef302eef";
const FROM_UNAVAILABLE_TOPIC: &str = "4d33c389cedef6fe4003ae05633fd2356bcd2acc";
#[test]
fn test_submodule_rewind_ok() {
let check = SubmoduleRewind::new();
let conf = make_check_conf(&check);
let result = test_check_submodule("test_submodule_rewind_ok", MOVE_TOPIC, &conf);
test_result_ok(result);
}
#[test]
fn test_submodule_rewind_to_unavailable() {
let check = SubmoduleRewind::new();
let conf = make_check_conf(&check);
let result = test_check_submodule("test_submodule_rewind_to_unavailable",
TO_UNAVAILABLE_TOPIC,
&conf);
test_result_ok(result);
}
#[test]
fn test_submodule_rewind_from_unavailable() {
let check = SubmoduleRewind::new();
let conf = make_check_conf(&check);
let result = test_check_submodule("test_submodule_rewind_from_unavailable",
FROM_UNAVAILABLE_TOPIC,
&conf);
test_result_ok(result);
}
#[test]
fn test_submodule_rewind_rewind() {
let check = SubmoduleRewind::new();
let conf = make_check_conf(&check);
let result = test_check_submodule_base("test_submodule_rewind_rewind",
REWIND_TOPIC,
MOVE_TOPIC,
&conf);
test_result_errors(result, &[
"commit 39c5d0d9dc7ee6abad72cd42c90d7c1af1be169c is not allowed since it moves the \
submodule `submodule` backwards from 8a890d8c4b89560c70a059bbdd7bc59b92b5c92b to \
2a8baa8e23bb1de5eec202dd4a29adf47feb03b1.",
]);
}
}