summaryrefslogtreecommitdiffstats
path: root/vendor/gix-refspec/src/write.rs
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/gix-refspec/src/write.rs')
-rw-r--r--vendor/gix-refspec/src/write.rs74
1 files changed, 74 insertions, 0 deletions
diff --git a/vendor/gix-refspec/src/write.rs b/vendor/gix-refspec/src/write.rs
new file mode 100644
index 000000000..74c71c6e1
--- /dev/null
+++ b/vendor/gix-refspec/src/write.rs
@@ -0,0 +1,74 @@
+use bstr::BString;
+
+use crate::{
+ instruction::{Fetch, Push},
+ Instruction, RefSpecRef,
+};
+
+impl RefSpecRef<'_> {
+ /// Reproduce ourselves in parseable form.
+ pub fn to_bstring(&self) -> BString {
+ let mut buf = Vec::with_capacity(128);
+ self.write_to(&mut buf).expect("no io error");
+ buf.into()
+ }
+
+ /// Serialize ourselves in a parseable format to `out`.
+ pub fn write_to(&self, out: impl std::io::Write) -> std::io::Result<()> {
+ self.instruction().write_to(out)
+ }
+}
+
+impl Instruction<'_> {
+ /// Reproduce ourselves in parseable form.
+ pub fn to_bstring(&self) -> BString {
+ let mut buf = Vec::with_capacity(128);
+ self.write_to(&mut buf).expect("no io error");
+ buf.into()
+ }
+
+ /// Serialize ourselves in a parseable format to `out`.
+ pub fn write_to(&self, mut out: impl std::io::Write) -> std::io::Result<()> {
+ match self {
+ Instruction::Push(Push::Matching {
+ src,
+ dst,
+ allow_non_fast_forward,
+ }) => {
+ if *allow_non_fast_forward {
+ out.write_all(&[b'+'])?;
+ }
+ out.write_all(src)?;
+ out.write_all(&[b':'])?;
+ out.write_all(dst)
+ }
+ Instruction::Push(Push::AllMatchingBranches { allow_non_fast_forward }) => {
+ if *allow_non_fast_forward {
+ out.write_all(&[b'+'])?;
+ }
+ out.write_all(&[b':'])
+ }
+ Instruction::Push(Push::Delete { ref_or_pattern }) => {
+ out.write_all(&[b':'])?;
+ out.write_all(ref_or_pattern)
+ }
+ Instruction::Fetch(Fetch::Only { src }) => out.write_all(src),
+ Instruction::Fetch(Fetch::Exclude { src }) => {
+ out.write_all(&[b'^'])?;
+ out.write_all(src)
+ }
+ Instruction::Fetch(Fetch::AndUpdate {
+ src,
+ dst,
+ allow_non_fast_forward,
+ }) => {
+ if *allow_non_fast_forward {
+ out.write_all(&[b'+'])?;
+ }
+ out.write_all(src)?;
+ out.write_all(&[b':'])?;
+ out.write_all(dst)
+ }
+ }
+ }
+}