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
use std::marker;
use std::str;
use std::path::Path;
use {raw, Oid, Repository, Error};
use util::{self, Binding};
pub struct Submodule<'repo> {
raw: *mut raw::git_submodule,
_marker: marker::PhantomData<&'repo Repository>,
}
impl<'repo> Submodule<'repo> {
pub fn branch(&self) -> Option<&str> {
self.branch_bytes().and_then(|s| str::from_utf8(s).ok())
}
pub fn branch_bytes(&self) -> Option<&[u8]> {
unsafe {
::opt_bytes(self, raw::git_submodule_branch(self.raw))
}
}
pub fn url(&self) -> Option<&str> { str::from_utf8(self.url_bytes()).ok() }
pub fn url_bytes(&self) -> &[u8] {
unsafe {
::opt_bytes(self, raw::git_submodule_url(self.raw)).unwrap()
}
}
pub fn name(&self) -> Option<&str> { str::from_utf8(self.name_bytes()).ok() }
pub fn name_bytes(&self) -> &[u8] {
unsafe {
::opt_bytes(self, raw::git_submodule_name(self.raw)).unwrap()
}
}
pub fn path(&self) -> &Path {
util::bytes2path(unsafe {
::opt_bytes(self, raw::git_submodule_path(self.raw)).unwrap()
})
}
pub fn head_id(&self) -> Option<Oid> {
unsafe {
Binding::from_raw_opt(raw::git_submodule_head_id(self.raw))
}
}
pub fn index_id(&self) -> Option<Oid> {
unsafe {
Binding::from_raw_opt(raw::git_submodule_index_id(self.raw))
}
}
pub fn workdir_id(&self) -> Option<Oid> {
unsafe {
Binding::from_raw_opt(raw::git_submodule_wd_id(self.raw))
}
}
pub fn init(&mut self, overwrite: bool) -> Result<(), Error> {
unsafe {
try_call!(raw::git_submodule_init(self.raw, overwrite));
}
Ok(())
}
pub fn open(&self) -> Result<Repository, Error> {
let mut raw = 0 as *mut raw::git_repository;
unsafe {
try_call!(raw::git_submodule_open(&mut raw, self.raw));
Ok(Binding::from_raw(raw))
}
}
pub fn reload(&mut self, force: bool) -> Result<(), Error> {
unsafe {
try_call!(raw::git_submodule_reload(self.raw, force));
}
Ok(())
}
pub fn sync(&mut self) -> Result<(), Error> {
unsafe { try_call!(raw::git_submodule_sync(self.raw)); }
Ok(())
}
pub fn add_to_index(&mut self, write_index: bool) -> Result<(), Error> {
unsafe {
try_call!(raw::git_submodule_add_to_index(self.raw, write_index));
}
Ok(())
}
pub fn add_finalize(&mut self) -> Result<(), Error> {
unsafe { try_call!(raw::git_submodule_add_finalize(self.raw)); }
Ok(())
}
}
impl<'repo> Binding for Submodule<'repo> {
type Raw = *mut raw::git_submodule;
unsafe fn from_raw(raw: *mut raw::git_submodule) -> Submodule<'repo> {
Submodule { raw: raw, _marker: marker::PhantomData }
}
fn raw(&self) -> *mut raw::git_submodule { self.raw }
}
impl<'repo> Drop for Submodule<'repo> {
fn drop(&mut self) {
unsafe { raw::git_submodule_free(self.raw) }
}
}
#[cfg(test)]
mod tests {
use std::path::Path;
use std::fs;
use tempdir::TempDir;
use url::Url;
use Repository;
#[test]
fn smoke() {
let td = TempDir::new("test").unwrap();
let repo = Repository::init(td.path()).unwrap();
let mut s1 = repo.submodule("/path/to/nowhere",
Path::new("foo"), true).unwrap();
s1.init(false).unwrap();
s1.sync().unwrap();
let s2 = repo.submodule("/path/to/nowhere",
Path::new("bar"), true).unwrap();
drop((s1, s2));
let mut submodules = repo.submodules().unwrap();
assert_eq!(submodules.len(), 2);
let mut s = submodules.remove(0);
assert_eq!(s.name(), Some("bar"));
assert_eq!(s.url(), Some("/path/to/nowhere"));
assert_eq!(s.branch(), None);
assert!(s.head_id().is_none());
assert!(s.index_id().is_none());
assert!(s.workdir_id().is_none());
repo.find_submodule("bar").unwrap();
s.open().unwrap();
assert!(s.path() == Path::new("bar"));
s.reload(true).unwrap();
}
#[test]
fn add_a_submodule() {
let (_td, repo1) = ::test::repo_init();
let (td, repo2) = ::test::repo_init();
let url = Url::from_file_path(&repo1.workdir().unwrap()).unwrap();
let mut s = repo2.submodule(&url.to_string(), Path::new("bar"),
true).unwrap();
t!(fs::remove_dir_all(td.path().join("bar")));
t!(Repository::clone(&url.to_string(),
td.path().join("bar")));
t!(s.add_to_index(false));
t!(s.add_finalize());
}
}