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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
use std::ffi::CString;
use std::marker;

use crate::{raw, util::Binding, Error, Oid, Reflog, Repository, Signature};

/// A structure representing a transactional update of a repository's references.
///
/// Transactions work by locking loose refs for as long as the [`Transaction`]
/// is held, and committing all changes to disk when [`Transaction::commit`] is
/// called. Note that committing is not atomic: if an operation fails, the
/// transaction aborts, but previous successful operations are not rolled back.
pub struct Transaction<'repo> {
    raw: *mut raw::git_transaction,
    _marker: marker::PhantomData<&'repo Repository>,
}

impl Drop for Transaction<'_> {
    fn drop(&mut self) {
        unsafe { raw::git_transaction_free(self.raw) }
    }
}

impl<'repo> Binding for Transaction<'repo> {
    type Raw = *mut raw::git_transaction;

    unsafe fn from_raw(ptr: *mut raw::git_transaction) -> Transaction<'repo> {
        Transaction {
            raw: ptr,
            _marker: marker::PhantomData,
        }
    }

    fn raw(&self) -> *mut raw::git_transaction {
        self.raw
    }
}

impl<'repo> Transaction<'repo> {
    /// Lock the specified reference by name.
    pub fn lock_ref(&mut self, refname: &str) -> Result<(), Error> {
        let refname = CString::new(refname).unwrap();
        unsafe {
            try_call!(raw::git_transaction_lock_ref(self.raw, refname));
        }

        Ok(())
    }

    /// Set the target of the specified reference.
    ///
    /// The reference must have been locked via `lock_ref`.
    ///
    /// If `reflog_signature` is `None`, the [`Signature`] is read from the
    /// repository config.
    pub fn set_target(
        &mut self,
        refname: &str,
        target: Oid,
        reflog_signature: Option<&Signature<'_>>,
        reflog_message: &str,
    ) -> Result<(), Error> {
        let refname = CString::new(refname).unwrap();
        let reflog_message = CString::new(reflog_message).unwrap();
        unsafe {
            try_call!(raw::git_transaction_set_target(
                self.raw,
                refname,
                target.raw(),
                reflog_signature.map(|s| s.raw()),
                reflog_message
            ));
        }

        Ok(())
    }

    /// Set the target of the specified symbolic reference.
    ///
    /// The reference must have been locked via `lock_ref`.
    ///
    /// If `reflog_signature` is `None`, the [`Signature`] is read from the
    /// repository config.
    pub fn set_symbolic_target(
        &mut self,
        refname: &str,
        target: &str,
        reflog_signature: Option<&Signature<'_>>,
        reflog_message: &str,
    ) -> Result<(), Error> {
        let refname = CString::new(refname).unwrap();
        let target = CString::new(target).unwrap();
        let reflog_message = CString::new(reflog_message).unwrap();
        unsafe {
            try_call!(raw::git_transaction_set_symbolic_target(
                self.raw,
                refname,
                target,
                reflog_signature.map(|s| s.raw()),
                reflog_message
            ));
        }

        Ok(())
    }

    /// Add a [`Reflog`] to the transaction.
    ///
    /// This commit the in-memory [`Reflog`] to disk when the transaction commits.
    /// Note that atomicity is **not* guaranteed: if the transaction fails to
    /// modify `refname`, the reflog may still have been committed to disk.
    ///
    /// If this is combined with setting the target, that update won't be
    /// written to the log (i.e. the `reflog_signature` and `reflog_message`
    /// parameters will be ignored).
    pub fn set_reflog(&mut self, refname: &str, reflog: Reflog) -> Result<(), Error> {
        let refname = CString::new(refname).unwrap();
        unsafe {
            try_call!(raw::git_transaction_set_reflog(
                self.raw,
                refname,
                reflog.raw()
            ));
        }

        Ok(())
    }

    /// Remove a reference.
    ///
    /// The reference must have been locked via `lock_ref`.
    pub fn remove(&mut self, refname: &str) -> Result<(), Error> {
        let refname = CString::new(refname).unwrap();
        unsafe {
            try_call!(raw::git_transaction_remove(self.raw, refname));
        }

        Ok(())
    }

    /// Commit the changes from the transaction.
    ///
    /// The updates will be made one by one, and the first failure will stop the
    /// processing.
    pub fn commit(self) -> Result<(), Error> {
        unsafe {
            try_call!(raw::git_transaction_commit(self.raw));
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use crate::{Error, ErrorClass, ErrorCode, Oid, Repository};

    #[test]
    fn smoke() {
        let (_td, repo) = crate::test::repo_init();

        let mut tx = t!(repo.transaction());

        t!(tx.lock_ref("refs/heads/main"));
        t!(tx.lock_ref("refs/heads/next"));

        t!(tx.set_target("refs/heads/main", Oid::zero(), None, "set main to zero"));
        t!(tx.set_symbolic_target(
            "refs/heads/next",
            "refs/heads/main",
            None,
            "set next to main",
        ));

        t!(tx.commit());

        assert_eq!(repo.refname_to_id("refs/heads/main").unwrap(), Oid::zero());
        assert_eq!(
            repo.find_reference("refs/heads/next")
                .unwrap()
                .symbolic_target()
                .unwrap(),
            "refs/heads/main"
        );
    }

    #[test]
    fn locks_same_repo_handle() {
        let (_td, repo) = crate::test::repo_init();

        let mut tx1 = t!(repo.transaction());
        t!(tx1.lock_ref("refs/heads/seen"));

        let mut tx2 = t!(repo.transaction());
        assert!(matches!(tx2.lock_ref("refs/heads/seen"), Err(e) if e.code() == ErrorCode::Locked))
    }

    #[test]
    fn locks_across_repo_handles() {
        let (td, repo1) = crate::test::repo_init();
        let repo2 = t!(Repository::open(&td));

        let mut tx1 = t!(repo1.transaction());
        t!(tx1.lock_ref("refs/heads/seen"));

        let mut tx2 = t!(repo2.transaction());
        assert!(matches!(tx2.lock_ref("refs/heads/seen"), Err(e) if e.code() == ErrorCode::Locked))
    }

    #[test]
    fn drop_unlocks() {
        let (_td, repo) = crate::test::repo_init();

        let mut tx = t!(repo.transaction());
        t!(tx.lock_ref("refs/heads/seen"));
        drop(tx);

        let mut tx2 = t!(repo.transaction());
        t!(tx2.lock_ref("refs/heads/seen"))
    }

    #[test]
    fn commit_unlocks() {
        let (_td, repo) = crate::test::repo_init();

        let mut tx = t!(repo.transaction());
        t!(tx.lock_ref("refs/heads/seen"));
        t!(tx.commit());

        let mut tx2 = t!(repo.transaction());
        t!(tx2.lock_ref("refs/heads/seen"));
    }

    #[test]
    fn prevents_non_transactional_updates() {
        let (_td, repo) = crate::test::repo_init();
        let head = t!(repo.refname_to_id("HEAD"));

        let mut tx = t!(repo.transaction());
        t!(tx.lock_ref("refs/heads/seen"));

        assert!(matches!(
            repo.reference("refs/heads/seen", head, true, "competing with lock"),
            Err(e) if e.code() == ErrorCode::Locked
        ));
    }

    #[test]
    fn remove() {
        let (_td, repo) = crate::test::repo_init();
        let head = t!(repo.refname_to_id("HEAD"));
        let next = "refs/heads/next";

        t!(repo.reference(
            next,
            head,
            true,
            "refs/heads/next@{0}: branch: Created from HEAD"
        ));

        {
            let mut tx = t!(repo.transaction());
            t!(tx.lock_ref(next));
            t!(tx.remove(next));
            t!(tx.commit());
        }
        assert!(matches!(repo.refname_to_id(next), Err(e) if e.code() == ErrorCode::NotFound))
    }

    #[test]
    fn must_lock_ref() {
        let (_td, repo) = crate::test::repo_init();

        // 🤷
        fn is_not_locked_err(e: &Error) -> bool {
            e.code() == ErrorCode::NotFound
                && e.class() == ErrorClass::Reference
                && e.message() == "the specified reference is not locked"
        }

        let mut tx = t!(repo.transaction());
        assert!(matches!(
            tx.set_target("refs/heads/main", Oid::zero(), None, "set main to zero"),
            Err(e) if is_not_locked_err(&e)
        ))
    }
}