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
//!
#![allow(clippy::empty_docs)]
use gix_hash::ObjectId;
use gix_ref::FullNameRef;
use crate::{
Head, HeadOwned, Reference,
ext::{ObjectIdExt, ReferenceExt},
};
/// Represents the kind of `HEAD` reference.
#[derive(Clone)]
pub enum Kind<'repo> {
/// The existing reference the symbolic HEAD points to.
///
/// This is the common case.
Symbolic(Reference<'repo>),
/// The yet-to-be-created reference the symbolic HEAD refers to.
///
/// This is the case in a newly initialized repository.
Unborn(gix_ref::FullName),
/// The head points to an object directly, not to a symbolic reference.
///
/// This state is less common and can occur when checking out commits directly.
Detached {
/// The object to which the head points to
target: ObjectId,
/// Possibly the final destination of `target` after following the object chain from tag objects to commits.
peeled: Option<ObjectId>,
},
}
impl Kind<'_> {
/// Attach this instance to a `repo`, reattaching the symbolic referent if present, to produce a [`Head`].
pub fn attach(self, repo: &crate::Repository) -> Head<'_> {
let kind = match self {
Self::Symbolic(r) => Kind::Symbolic(r.detach().attach(repo)),
Self::Unborn(name) => Kind::Unborn(name),
Self::Detached { target, peeled } => Kind::Detached { target, peeled },
};
Head { kind, repo }
}
}
impl HeadOwned {
/// Attach this snapshot to `repo` without rereading `HEAD`.
pub fn attach(self, repo: &crate::Repository) -> Head<'_> {
let kind = match self {
Self::Symbolic(r) => Kind::Symbolic(r.attach(repo)),
Self::Unborn(name) => Kind::Unborn(name),
Self::Detached { target, peeled } => Kind::Detached { target, peeled },
};
Head { kind, repo }
}
/// Returns the full reference name that HEAD points to, even if it is unborn.
pub fn referent_name(&self) -> Option<&FullNameRef> {
match self {
Self::Symbolic(r) => Some(r.name.as_ref()),
Self::Unborn(name) => Some(name.as_ref()),
Self::Detached { .. } => None,
}
}
/// Returns true if HEAD points directly to an object.
pub fn is_detached(&self) -> bool {
matches!(self, Self::Detached { .. })
}
/// Returns true if HEAD points to a reference that does not yet exist.
pub fn is_unborn(&self) -> bool {
matches!(self, Self::Unborn(_))
}
/// Returns the cached peeled ID if known, or the target ID otherwise.
/// This uses only the snapshot, including peeled IDs from packed references.
pub fn id(&self) -> Option<ObjectId> {
match self {
Self::Symbolic(r) => r.peeled.or_else(|| r.target.try_id().map(ToOwned::to_owned)),
Self::Unborn(_) => None,
Self::Detached { target, peeled } => Some(peeled.unwrap_or(*target)),
}
}
}
/// Access
impl<'repo> Head<'repo> {
/// Return the repository that owns this HEAD and its symbolic referent.
pub fn repo(&self) -> &'repo crate::Repository {
self.repo
}
/// Consume this instance and return its snapshot without borrowing the repository or changing `HEAD`.
#[doc(alias = "detach")]
pub fn detach_repo(self) -> HeadOwned {
match self.kind {
Kind::Symbolic(r) => HeadOwned::Symbolic(r.detach()),
Kind::Unborn(name) => HeadOwned::Unborn(name),
Kind::Detached { target, peeled } => HeadOwned::Detached { target, peeled },
}
}
/// Returns the name of this references, always `HEAD`.
pub fn name(&self) -> &'static FullNameRef {
// TODO: use a statically checked version of this when available.
"HEAD".try_into().expect("HEAD is valid")
}
/// Returns the full reference name of this head if it is not detached, or `None` otherwise.
///
/// # Examples
///
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
/// # mod doctest { include!(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/doctest.rs")); }
/// # let repo = doctest::open_repo(doctest::basic_repo_dir()?)?;
/// let head = repo.head()?;
///
/// assert_eq!(head.referent_name().expect("branch head"), "refs/heads/main");
/// # Ok(()) }
/// ```
pub fn referent_name(&self) -> Option<&FullNameRef> {
Some(match &self.kind {
Kind::Symbolic(r) => r.name(),
Kind::Unborn(name) => name.as_ref(),
Kind::Detached { .. } => return None,
})
}
/// Returns true if this instance is detached, and points to an object directly.
pub fn is_detached(&self) -> bool {
matches!(self.kind, Kind::Detached { .. })
}
/// Returns true if this instance is not yet born, hence it points to a ref that doesn't exist yet.
///
/// This is the case in a newly initialized repository.
pub fn is_unborn(&self) -> bool {
matches!(self.kind, Kind::Unborn(_))
}
/// Returns the id the head points to, which isn't possible on unborn heads.
///
/// # Examples
///
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
/// # mod doctest { include!(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/doctest.rs")); }
/// # let repo = doctest::open_repo(doctest::basic_repo_dir()?)?;
/// let head = repo.head()?;
///
/// assert_eq!(head.id().expect("born"), repo.head_id()?);
/// # Ok(()) }
/// ```
pub fn id(&self) -> Option<crate::Id<'repo>> {
match &self.kind {
Kind::Symbolic(r) => r.try_id(),
Kind::Detached { peeled, target } => {
(*peeled).unwrap_or_else(|| target.to_owned()).attach(self.repo).into()
}
Kind::Unborn(_) => None,
}
}
/// Try to transform this instance into the symbolic reference that it points to, or return `None` if head is detached or unborn.
///
/// # Examples
///
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
/// # mod doctest { include!(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/doctest.rs")); }
/// # let repo = doctest::open_repo(doctest::basic_repo_dir()?)?;
/// let branch = repo.head()?.try_into_referent().expect("symbolic head");
///
/// assert_eq!(branch, "refs/heads/main");
/// # Ok(()) }
/// ```
pub fn try_into_referent(self) -> Option<Reference<'repo>> {
match self.kind {
Kind::Symbolic(r) => Some(r),
_ => None,
}
}
}
mod remote {
use super::Head;
use crate::{Remote, Result, remote};
/// Remote
impl<'repo> Head<'repo> {
/// Return the remote with which the currently checked our reference can be handled as configured by `branch.<name>.remote|pushRemote`
/// or fall back to the non-branch specific remote configuration. `None` is returned if the head is detached or unborn, so there is
/// no branch specific remote.
///
/// This is equivalent to calling [`Reference::remote(…)`][crate::Reference::remote()] and
/// [`Repository::remote_default_name()`][crate::Repository::remote_default_name()] in order.
///
/// Combine it with [`Repository::find_default_remote()`][crate::Repository::find_default_remote()] as fallback to
/// handle detached heads, i.e. obtain a remote even in case of detached heads,
/// or call [`Repository::find_fetch_remote(…)`](crate::Repository::find_fetch_remote()) for the highest-level way of finding
/// the right remote, just like `git fetch` does.
pub fn into_remote(self, direction: remote::Direction) -> Option<Result<Remote<'repo>>> {
let repo = self.repo;
self.try_into_referent()?
.remote(direction)
.or_else(|| repo.find_default_remote(direction))
}
}
}
///
pub mod log;
///
pub mod peel;