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
use crate::pristine::*;

pub const START_MARKER: &str = "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n";

pub const SEPARATOR: &str = "\n================================\n";

pub const END_MARKER: &str = "\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n";

/// A trait for outputting keys and their contents. This trait allows
/// to retain more information about conflicts than directly
/// outputting as bytes to a `Write`. The diff algorithm uses that
/// information, for example.
pub trait VertexBuffer {
    fn output_line<E, F>(&mut self, key: Vertex<ChangeId>, contents: F) -> Result<(), E>
    where
        E: From<std::io::Error>,
        F: FnOnce(&mut Vec<u8>) -> Result<(), E>;

    fn output_conflict_marker(&mut self, s: &str) -> Result<(), std::io::Error>;
    fn begin_conflict(&mut self) -> Result<(), std::io::Error> {
        self.output_conflict_marker(START_MARKER)
    }
    fn begin_zombie_conflict(&mut self) -> Result<(), std::io::Error> {
        self.begin_conflict()
    }
    fn begin_cyclic_conflict(&mut self) -> Result<(), std::io::Error> {
        self.begin_conflict()
    }
    fn conflict_next(&mut self) -> Result<(), std::io::Error> {
        self.output_conflict_marker(SEPARATOR)
    }
    fn end_conflict(&mut self) -> Result<(), std::io::Error> {
        self.output_conflict_marker(END_MARKER)
    }
    fn end_zombie_conflict(&mut self) -> Result<(), std::io::Error> {
        self.end_conflict()
    }
    fn end_cyclic_conflict(&mut self) -> Result<(), std::io::Error> {
        self.output_conflict_marker(END_MARKER)
    }
}

pub(crate) struct ConflictsWriter<'a, 'b, W: std::io::Write> {
    pub w: W,
    pub lines: usize,
    pub new_line: bool,
    pub path: &'b str,
    pub conflicts: &'a mut Vec<crate::output::Conflict>,
    pub buf: Vec<u8>,
}

impl<'a, 'b, W: std::io::Write> ConflictsWriter<'a, 'b, W> {
    pub fn new(w: W, path: &'b str, conflicts: &'a mut Vec<crate::output::Conflict>) -> Self {
        ConflictsWriter {
            w,
            new_line: true,
            lines: 1,
            path,
            conflicts,
            buf: Vec::new(),
        }
    }
}

impl<'a, 'b, W: std::io::Write> std::ops::Deref for ConflictsWriter<'a, 'b, W> {
    type Target = W;
    fn deref(&self) -> &Self::Target {
        &self.w
    }
}

impl<'a, 'b, W: std::io::Write> std::ops::DerefMut for ConflictsWriter<'a, 'b, W> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.w
    }
}

impl<'a, 'b, W: std::io::Write> VertexBuffer for ConflictsWriter<'a, 'b, W> {
    fn output_line<E, C>(&mut self, v: Vertex<ChangeId>, c: C) -> Result<(), E>
    where
        E: From<std::io::Error>,
        C: FnOnce(&mut Vec<u8>) -> Result<(), E>,
    {
        self.buf.clear();
        c(&mut self.buf)?;
        debug!("vbuf {:?} {:?}", v, std::str::from_utf8(&self.buf));
        let ends_with_newline = self.buf.ends_with(b"\n");
        self.lines += self.buf.iter().filter(|c| **c == b'\n').count();
        self.w.write_all(&self.buf)?;
        if !self.buf.is_empty() {
            // empty "lines" (such as in the beginning of a file)
            // don't change the status of self.new_line.
            self.new_line = ends_with_newline;
        }
        Ok(())
    }

    fn output_conflict_marker(&mut self, s: &str) -> Result<(), std::io::Error> {
        debug!("output_conflict_marker {:?}", self.new_line);
        if !self.new_line {
            self.lines += 2;
            self.w.write_all(s.as_bytes())?;
        } else {
            self.lines += 1;
            debug!("{:?}", &s.as_bytes()[1..]);
            self.w.write_all(&s.as_bytes()[1..])?;
        }
        self.new_line = true;
        Ok(())
    }

    fn begin_conflict(&mut self) -> Result<(), std::io::Error> {
        self.conflicts.push(crate::output::Conflict::Order {
            path: self.path.to_string(),
            line: self.lines,
        });
        self.output_conflict_marker(START_MARKER)
    }
    fn begin_zombie_conflict(&mut self) -> Result<(), std::io::Error> {
        self.conflicts.push(crate::output::Conflict::Zombie {
            path: self.path.to_string(),
            line: self.lines,
        });
        self.output_conflict_marker(START_MARKER)
    }
    fn begin_cyclic_conflict(&mut self) -> Result<(), std::io::Error> {
        self.conflicts.push(crate::output::Conflict::Cyclic {
            path: self.path.to_string(),
            line: self.lines,
        });
        self.output_conflict_marker(START_MARKER)
    }
}

pub struct Writer<W: std::io::Write> {
    w: W,
    buf: Vec<u8>,
    new_line: bool,
    is_zombie: bool,
}

impl<W: std::io::Write> Writer<W> {
    pub fn new(w: W) -> Self {
        Writer {
            w,
            new_line: true,
            buf: Vec::new(),
            is_zombie: false,
        }
    }
    pub fn into_inner(self) -> W {
        self.w
    }
}

impl<W: std::io::Write> std::ops::Deref for Writer<W> {
    type Target = W;
    fn deref(&self) -> &Self::Target {
        &self.w
    }
}

impl<W: std::io::Write> std::ops::DerefMut for Writer<W> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.w
    }
}

impl<W: std::io::Write> VertexBuffer for Writer<W> {
    fn output_line<E, C>(&mut self, v: Vertex<ChangeId>, c: C) -> Result<(), E>
    where
        E: From<std::io::Error>,
        C: FnOnce(&mut Vec<u8>) -> Result<(), E>,
    {
        self.buf.clear();
        c(&mut self.buf)?;
        debug!("vbuf {:?} {:?}", v, std::str::from_utf8(&self.buf));
        let ends_with_newline = self.buf.ends_with(b"\n");
        self.w.write_all(&self.buf[..])?;
        if !self.buf.is_empty() {
            // empty "lines" (such as in the beginning of a file)
            // don't change the status of self.new_line.
            self.new_line = ends_with_newline;
        }
        Ok(())
    }

    fn output_conflict_marker(&mut self, s: &str) -> Result<(), std::io::Error> {
        debug!("output_conflict_marker {:?}", self.new_line);
        if !self.new_line {
            self.w.write_all(s.as_bytes())?;
        } else {
            debug!("{:?}", &s.as_bytes()[1..]);
            self.w.write_all(&s.as_bytes()[1..])?;
        }
        Ok(())
    }

    fn begin_conflict(&mut self) -> Result<(), std::io::Error> {
        self.output_conflict_marker(START_MARKER)
    }
    fn end_conflict(&mut self) -> Result<(), std::io::Error> {
        self.is_zombie = false;
        self.output_conflict_marker(END_MARKER)
    }
    fn begin_zombie_conflict(&mut self) -> Result<(), std::io::Error> {
        if self.is_zombie {
            Ok(())
        } else {
            self.is_zombie = true;
            self.begin_conflict()
        }
    }
    fn end_zombie_conflict(&mut self) -> Result<(), std::io::Error> {
        self.is_zombie = false;
        self.output_conflict_marker(END_MARKER)
    }
    fn begin_cyclic_conflict(&mut self) -> Result<(), std::io::Error> {
        self.output_conflict_marker(START_MARKER)
    }
}