gix_refspec/
write.rs

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
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: &mut dyn 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, out: &mut dyn 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)
            }
        }
    }
}