| 1 |
use crate::buf::Buf; |
| 2 |
use crate::reference::Reference; |
| 3 |
use crate::repo::Repository; |
| 4 |
use crate::util::{self, Binding}; |
| 5 |
use crate::{raw, Error}; |
| 6 |
use std::os::raw::c_int; |
| 7 |
use std::path::Path; |
| 8 |
use std::ptr; |
| 9 |
use std::str; |
| 10 |
use std::{marker, mem}; |
| 11 |
|
| 12 |
/// An owned git worktree |
| 13 |
/// |
| 14 |
/// This structure corresponds to a `git_worktree` in libgit2. |
| 15 |
// |
| 16 |
pub struct Worktree { |
| 17 |
raw: *mut raw::git_worktree, |
| 18 |
} |
| 19 |
|
| 20 |
/// Options which can be used to configure how a worktree is initialized |
| 21 |
pub struct WorktreeAddOptions<'a> { |
| 22 |
raw: raw::git_worktree_add_options, |
| 23 |
_marker: marker::PhantomData<Reference<'a>>, |
| 24 |
} |
| 25 |
|
| 26 |
/// Options to configure how worktree pruning is performed |
| 27 |
pub struct WorktreePruneOptions { |
| 28 |
raw: raw::git_worktree_prune_options, |
| 29 |
} |
| 30 |
|
| 31 |
/// Lock Status of a worktree |
| 32 |
#[derive(PartialEq, Debug)] |
| 33 |
pub enum WorktreeLockStatus { |
| 34 |
/// Worktree is Unlocked |
| 35 |
Unlocked, |
| 36 |
/// Worktree is locked with the optional message |
| 37 |
Locked(Option<String>), |
| 38 |
} |
| 39 |
|
| 40 |
impl Worktree { |
| 41 |
/// Open a worktree of a the repository |
| 42 |
/// |
| 43 |
/// If a repository is not the main tree but a worktree, this |
| 44 |
/// function will look up the worktree inside the parent |
| 45 |
/// repository and create a new `git_worktree` structure. |
| 46 |
pub fn open_from_repository(repo: &Repository) -> Result<Worktree, Error> { |
| 47 |
let mut raw = ptr::null_mut(); |
| 48 |
unsafe { |
| 49 |
try_call!(raw::git_worktree_open_from_repository(&mut raw, repo.raw())); |
| 50 |
Ok(Binding::from_raw(raw)) |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
/// Retrieves the name of the worktree |
| 55 |
/// |
| 56 |
/// This is the name that can be passed to repo::Repository::find_worktree |
| 57 |
/// to reopen the worktree. This is also the name that would appear in the |
| 58 |
/// list returned by repo::Repository::worktrees |
| 59 |
pub fn name(&self) -> Result<Option<&str>, Error> { |
| 60 |
let opt_bytes = unsafe { crate::opt_bytes(self, raw::git_worktree_name(self.raw)) }; |
| 61 |
match opt_bytes { |
| 62 |
Some(ob) => str::from_utf8(ob).map(|s| Some(s)).map_err(|e| e.into()), |
| 63 |
None => Ok(None), |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
/// Retrieves the path to the worktree |
| 68 |
/// |
| 69 |
/// This is the path to the top-level of the source and not the path to the |
| 70 |
/// .git file within the worktree. This path can be passed to |
| 71 |
/// repo::Repository::open. |
| 72 |
pub fn path(&self) -> &Path { |
| 73 |
unsafe { |
| 74 |
util::bytes2path(crate::opt_bytes(self, raw::git_worktree_path(self.raw)).unwrap()) |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
/// Validates the worktree |
| 79 |
/// |
| 80 |
/// This checks that it still exists on the |
| 81 |
/// filesystem and that the metadata is correct |
| 82 |
pub fn validate(&self) -> Result<(), Error> { |
| 83 |
unsafe { |
| 84 |
try_call!(raw::git_worktree_validate(self.raw)); |
| 85 |
} |
| 86 |
Ok(()) |
| 87 |
} |
| 88 |
|
| 89 |
/// Locks the worktree |
| 90 |
pub fn lock(&self, reason: Option<&str>) -> Result<(), Error> { |
| 91 |
let reason = crate::opt_cstr(reason)?; |
| 92 |
unsafe { |
| 93 |
try_call!(raw::git_worktree_lock(self.raw, reason)); |
| 94 |
} |
| 95 |
Ok(()) |
| 96 |
} |
| 97 |
|
| 98 |
/// Unlocks the worktree |
| 99 |
pub fn unlock(&self) -> Result<(), Error> { |
| 100 |
unsafe { |
| 101 |
try_call!(raw::git_worktree_unlock(self.raw)); |
| 102 |
} |
| 103 |
Ok(()) |
| 104 |
} |
| 105 |
|
| 106 |
/// Checks if worktree is locked |
| 107 |
pub fn is_locked(&self) -> Result<WorktreeLockStatus, Error> { |
| 108 |
let buf = Buf::new(); |
| 109 |
unsafe { |
| 110 |
match try_call!(raw::git_worktree_is_locked(buf.raw(), self.raw)) { |
| 111 |
0 => Ok(WorktreeLockStatus::Unlocked), |
| 112 |
_ => { |
| 113 |
let v = buf.to_vec(); |
| 114 |
Ok(WorktreeLockStatus::Locked(match v.len() { |
| 115 |
0 => None, |
| 116 |
_ => Some(String::from_utf8(v).unwrap()), |
| 117 |
})) |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
/// Prunes the worktree |
| 124 |
pub fn prune(&self, opts: Option<&mut WorktreePruneOptions>) -> Result<(), Error> { |
| 125 |
// When successful the worktree should be removed however the backing structure |
| 126 |
// of the git_worktree should still be valid. |
| 127 |
unsafe { |
| 128 |
try_call!(raw::git_worktree_prune(self.raw, opts.map(|o| o.raw()))); |
| 129 |
} |
| 130 |
Ok(()) |
| 131 |
} |
| 132 |
|
| 133 |
/// Checks if the worktree is prunable |
| 134 |
pub fn is_prunable(&self, opts: Option<&mut WorktreePruneOptions>) -> Result<bool, Error> { |
| 135 |
unsafe { |
| 136 |
let rv = try_call!(raw::git_worktree_is_prunable( |
| 137 |
self.raw, |
| 138 |
opts.map(|o| o.raw()) |
| 139 |
)); |
| 140 |
Ok(rv != 0) |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
impl<'a> WorktreeAddOptions<'a> { |
| 146 |
/// Creates a default set of add options. |
| 147 |
/// |
| 148 |
/// By default this will not lock the worktree |
| 149 |
pub fn new() -> WorktreeAddOptions<'a> { |
| 150 |
unsafe { |
| 151 |
let mut raw = mem::zeroed(); |
| 152 |
assert_eq!( |
| 153 |
raw::git_worktree_add_options_init(&mut raw, raw::GIT_WORKTREE_ADD_OPTIONS_VERSION), |
| 154 |
0 |
| 155 |
); |
| 156 |
WorktreeAddOptions { |
| 157 |
raw, |
| 158 |
_marker: marker::PhantomData, |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
/// If enabled, this will cause the newly added worktree to be locked |
| 164 |
pub fn lock(&mut self, enabled: bool) -> &mut WorktreeAddOptions<'a> { |
| 165 |
self.raw.lock = enabled as c_int; |
| 166 |
self |
| 167 |
} |
| 168 |
|
| 169 |
/// If enabled, this will checkout the existing branch matching the worktree name. |
| 170 |
pub fn checkout_existing(&mut self, enabled: bool) -> &mut WorktreeAddOptions<'a> { |
| 171 |
self.raw.checkout_existing = enabled as c_int; |
| 172 |
self |
| 173 |
} |
| 174 |
|
| 175 |
/// reference to use for the new worktree HEAD |
| 176 |
pub fn reference( |
| 177 |
&mut self, |
| 178 |
reference: Option<&'a Reference<'_>>, |
| 179 |
) -> &mut WorktreeAddOptions<'a> { |
| 180 |
self.raw.reference = if let Some(reference) = reference { |
| 181 |
reference.raw() |
| 182 |
} else { |
| 183 |
ptr::null_mut() |
| 184 |
}; |
| 185 |
self |
| 186 |
} |
| 187 |
|
| 188 |
/// Get a set of raw add options to be used with `git_worktree_add` |
| 189 |
pub fn raw(&self) -> *const raw::git_worktree_add_options { |
| 190 |
&self.raw |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
impl WorktreePruneOptions { |
| 195 |
/// Creates a default set of pruning options |
| 196 |
/// |
| 197 |
/// By defaults this will prune only worktrees that are no longer valid |
| 198 |
/// unlocked and not checked out |
| 199 |
pub fn new() -> WorktreePruneOptions { |
| 200 |
unsafe { |
| 201 |
let mut raw = mem::zeroed(); |
| 202 |
assert_eq!( |
| 203 |
raw::git_worktree_prune_options_init( |
| 204 |
&mut raw, |
| 205 |
raw::GIT_WORKTREE_PRUNE_OPTIONS_VERSION |
| 206 |
), |
| 207 |
0 |
| 208 |
); |
| 209 |
WorktreePruneOptions { raw } |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
/// Controls whether valid (still existing on the filesystem) worktrees |
| 214 |
/// will be pruned |
| 215 |
/// |
| 216 |
/// Defaults to false |
| 217 |
pub fn valid(&mut self, valid: bool) -> &mut WorktreePruneOptions { |
| 218 |
self.flag(raw::GIT_WORKTREE_PRUNE_VALID, valid) |
| 219 |
} |
| 220 |
|
| 221 |
/// Controls whether locked worktrees will be pruned |
| 222 |
/// |
| 223 |
/// Defaults to false |
| 224 |
pub fn locked(&mut self, locked: bool) -> &mut WorktreePruneOptions { |
| 225 |
self.flag(raw::GIT_WORKTREE_PRUNE_LOCKED, locked) |
| 226 |
} |
| 227 |
|
| 228 |
/// Controls whether the actual working tree on the filesystem is recursively removed |
| 229 |
/// |
| 230 |
/// Defaults to false |
| 231 |
pub fn working_tree(&mut self, working_tree: bool) -> &mut WorktreePruneOptions { |
| 232 |
self.flag(raw::GIT_WORKTREE_PRUNE_WORKING_TREE, working_tree) |
| 233 |
} |
| 234 |
|
| 235 |
fn flag(&mut self, flag: raw::git_worktree_prune_t, on: bool) -> &mut WorktreePruneOptions { |
| 236 |
if on { |
| 237 |
self.raw.flags |= flag as u32; |
| 238 |
} else { |
| 239 |
self.raw.flags &= !(flag as u32); |
| 240 |
} |
| 241 |
self |
| 242 |
} |
| 243 |
|
| 244 |
/// Get a set of raw prune options to be used with `git_worktree_prune` |
| 245 |
pub fn raw(&mut self) -> *mut raw::git_worktree_prune_options { |
| 246 |
&mut self.raw |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
impl Binding for Worktree { |
| 251 |
type Raw = *mut raw::git_worktree; |
| 252 |
unsafe fn from_raw(ptr: *mut raw::git_worktree) -> Worktree { |
| 253 |
Worktree { raw: ptr } |
| 254 |
} |
| 255 |
fn raw(&self) -> *mut raw::git_worktree { |
| 256 |
self.raw |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
impl Drop for Worktree { |
| 261 |
fn drop(&mut self) { |
| 262 |
unsafe { raw::git_worktree_free(self.raw) } |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
#[cfg(test)] |
| 267 |
mod tests { |
| 268 |
use crate::WorktreeAddOptions; |
| 269 |
use crate::WorktreeLockStatus; |
| 270 |
|
| 271 |
use tempfile::TempDir; |
| 272 |
|
| 273 |
#[test] |
| 274 |
fn smoke_add_no_ref() { |
| 275 |
let (_td, repo) = crate::test::repo_init(); |
| 276 |
|
| 277 |
let wtdir = TempDir::new().unwrap(); |
| 278 |
let wt_path = wtdir.path().join("tree-no-ref-dir"); |
| 279 |
let opts = WorktreeAddOptions::new(); |
| 280 |
|
| 281 |
let wt = repo.worktree("tree-no-ref", &wt_path, Some(&opts)).unwrap(); |
| 282 |
assert_eq!(wt.name(), Ok(Some("tree-no-ref"))); |
| 283 |
assert_eq!( |
| 284 |
wt.path().canonicalize().unwrap(), |
| 285 |
wt_path.canonicalize().unwrap() |
| 286 |
); |
| 287 |
let status = wt.is_locked().unwrap(); |
| 288 |
assert_eq!(status, WorktreeLockStatus::Unlocked); |
| 289 |
} |
| 290 |
|
| 291 |
#[test] |
| 292 |
fn smoke_add_locked() { |
| 293 |
let (_td, repo) = crate::test::repo_init(); |
| 294 |
|
| 295 |
let wtdir = TempDir::new().unwrap(); |
| 296 |
let wt_path = wtdir.path().join("locked-tree"); |
| 297 |
let mut opts = WorktreeAddOptions::new(); |
| 298 |
opts.lock(true); |
| 299 |
|
| 300 |
let wt = repo.worktree("locked-tree", &wt_path, Some(&opts)).unwrap(); |
| 301 |
// shouldn't be able to lock a worktree that was created locked |
| 302 |
assert!(wt.lock(Some("my reason")).is_err()); |
| 303 |
assert_eq!(wt.name(), Ok(Some("locked-tree"))); |
| 304 |
assert_eq!( |
| 305 |
wt.path().canonicalize().unwrap(), |
| 306 |
wt_path.canonicalize().unwrap() |
| 307 |
); |
| 308 |
assert_eq!(wt.is_locked().unwrap(), WorktreeLockStatus::Locked(None)); |
| 309 |
assert!(wt.unlock().is_ok()); |
| 310 |
assert!(wt.lock(Some("my reason")).is_ok()); |
| 311 |
assert_eq!( |
| 312 |
wt.is_locked().unwrap(), |
| 313 |
WorktreeLockStatus::Locked(Some("my reason".to_string())) |
| 314 |
); |
| 315 |
} |
| 316 |
|
| 317 |
#[test] |
| 318 |
fn smoke_add_from_branch() { |
| 319 |
let (_td, repo) = crate::test::repo_init(); |
| 320 |
|
| 321 |
let (wt_top, branch) = crate::test::worktrees_env_init(&repo); |
| 322 |
let wt_path = wt_top.path().join("test"); |
| 323 |
let mut opts = WorktreeAddOptions::new(); |
| 324 |
let reference = branch.into_reference(); |
| 325 |
opts.reference(Some(&reference)); |
| 326 |
|
| 327 |
let wt = repo |
| 328 |
.worktree("test-worktree", &wt_path, Some(&opts)) |
| 329 |
.unwrap(); |
| 330 |
assert_eq!(wt.name(), Ok(Some("test-worktree"))); |
| 331 |
assert_eq!( |
| 332 |
wt.path().canonicalize().unwrap(), |
| 333 |
wt_path.canonicalize().unwrap() |
| 334 |
); |
| 335 |
let status = wt.is_locked().unwrap(); |
| 336 |
assert_eq!(status, WorktreeLockStatus::Unlocked); |
| 337 |
} |
| 338 |
} |