History

[ref] refactor

Sebastian Thiel committed 5 years ago

Commit eb0328f1 parent ce7f83b4 files · +49 −58Browse files

Unified · Split

4 files
  1. git-ref/src/store/file/transaction.rs +2 −2
  2. git-ref/src/transaction.rs +34 −37
  3. git-ref/tests/file/transaction.rs +7 −7
  4. git-ref/tests/refs.rs +6 −12
git-ref/src/store/file/transaction.rs+2−2
@@ -192,11 +192,11 @@ impl<'a> Transaction<'a> { · 191 unmodified lines
192192 }
193193
194194 for change in self.updates.iter_mut() {
195+ assert!(!change.update.deref, "Deref mode is turned into splits and turned off");
195196 match &change.update.change {
196197 Change::Update { .. } => {}
197− Change::Delete { deref, mode, .. } => {
198+ Change::Delete { mode, .. } => {
198199 let lock = change.lock.take().expect("each ref is locked, even deletions");
199− assert!(!deref, "Deref mode is turned into splits and turned off");
200200 let (rm_reflog, rm_ref) = match mode {
201201 DeleteMode::RefAndRefLog => (true, true),
202202 DeleteMode::RefLogOnly => (true, false),
git-ref/src/transaction.rs+34−37
@@ -38,9 +38,6 @@ pub enum Change { · 37 unmodified lines
3838 Update {
3939 /// How to treat the reference log.
4040 mode: UpdateMode,
41− /// If set, symbolic references will be dereferenced so the change gets applied to their target. Has no effect if the reference
42− /// isn't symbolic.
43− deref: bool,
4441 /// The previous value of the ref, which will be used to assure the ref is still in the known `previous` state before
4542 /// updating it. It will also be filled in automatically for use in the reflog, if applicable.
4643 previous: Option<Target>,
@@ -56,9 +53,6 @@ pub enum Change { · 9 unmodified lines
5653 previous: Option<Target>,
5754 /// How to thread the reference log during deletion.
5855 mode: DeleteMode,
59− /// If set, symbolic references will be dereferenced so the change gets applied to their target. Has no effect if the reference
60− /// isn't symbolic.
61− deref: bool,
6256 },
6357 }
6458
@@ -69,6 +63,38 @@ pub struct RefEdit { · 4 unmodified lines
6963 pub change: Change,
7064 /// The name of the reference to apply the change to
7165 pub name: FullName,
66+ /// If set, symbolic references identified by `name` will be dereferenced to have the `change` applied to their target.
67+ /// This flag has no effect if the reference isn't symbolic.
68+ pub deref: bool,
69+}
70+
71+/// The way to deal with the Reflog in deletions.
72+#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
73+pub enum DeleteMode {
74+ /// Delete the reference and the log
75+ RefAndRefLog,
76+ /// Delete only the reflog
77+ RefLogOnly,
78+}
79+
80+/// The way to deal with the Reflog in a particular edit
81+///
82+/// If the `create_unconditionally` field is set, create a reflog even if it otherwise wouldn't be created,
83+/// as is the case for tags.
84+#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
85+pub enum UpdateMode {
86+ /// As symbolic references only ever see this when you want to detach them, we won't try to dereference them
87+ /// in this case and apply the change to it directly.
88+ RefAndRefLog {
89+ /// If set, update the reflog even if it otherwise wouldn't.
90+ create_unconditionally: bool,
91+ },
92+ /// Only update the reflog but require this to be a symbolic ref so the actual update can be performed on the
93+ /// referent.
94+ RefLogOnly {
95+ /// If set, update the reflog even if it otherwise wouldn't.
96+ create_unconditionally: bool,
97+ },
7298 }
7399
74100 mod ext {
@@ -115,13 +141,13 @@ mod ext { · 40 unmodified lines
115141 for edit in self.iter_mut() {
116142 let edit = edit.borrow_mut();
117143 match edit.change {
118− Change::Delete { ref mut deref, .. } | Change::Update { ref mut deref, .. } => {
144+ Change::Delete { .. } | Change::Update { .. } => {
119145 match store.find_one_existing(edit.name.to_partial()).ok() {
120146 Some(Target::Symbolic(_name)) => {
121147 // todo!("split into new edit")
122148 }
123149 Some(Target::Peeled(_)) => {
124− *deref = false;
150+ edit.deref = false;
125151 }
126152 None => {}
127153 }
@@ -134,32 +160,3 @@ mod ext { · 6 unmodified lines
134160 }
135161 }
136162 pub use ext::RefEditsExt;
137−
138−/// The way to deal with the Reflog in deletions.
139−#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
140−pub enum DeleteMode {
141− /// Delete the reference and the log
142− RefAndRefLog,
143− /// Delete only the reflog
144− RefLogOnly,
145−}
146−
147−/// The way to deal with the Reflog in a particular edit
148−///
149−/// If the `create_unconditionally` field is set, create a reflog even if it otherwise wouldn't be created,
150−/// as is the case for tags.
151−#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
152−pub enum UpdateMode {
153− /// As symbolic references only ever see this when you want to detach them, we won't try to dereference them
154− /// in this case and apply the change to it directly.
155− RefAndRefLog {
156− /// If set, update the reflog even if it otherwise wouldn't.
157− create_unconditionally: bool,
158− },
159− /// Only update the reflog but require this to be a symbolic ref so the actual update can be performed on the
160− /// referent.
161− RefLogOnly {
162− /// If set, update the reflog even if it otherwise wouldn't.
163− create_unconditionally: bool,
164− },
165−}
git-ref/tests/file/transaction.rs+7−7
@@ -52,9 +52,9 @@ mod prepare_and_commit { · 51 unmodified lines
5252 },
5353 new: Target::Symbolic(referent.try_into()?),
5454 previous: None, // TODO: check failure if it doesn't exist
55− deref: false,
5655 },
5756 name: "HEAD".try_into()?,
57+ deref: false,
5858 }),
5959 Fail::Immediately,
6060 );
@@ -107,9 +107,9 @@ mod prepare_and_commit { · 46 unmodified lines
107107 change: Change::Delete {
108108 previous: None,
109109 mode: DeleteMode::RefAndRefLog,
110− deref: false,
111110 },
112111 name: "DOES_NOT_EXIST".try_into().unwrap(),
112+ deref: false,
113113 }),
114114 Fail::Immediately,
115115 )
@@ -127,9 +127,9 @@ mod prepare_and_commit { · 11 unmodified lines
127127 change: Change::Delete {
128128 previous: Some(Target::Peeled(ObjectId::null_sha1())),
129129 mode: DeleteMode::RefAndRefLog,
130− deref: false,
131130 },
132131 name: "DOES_NOT_EXIST".try_into().unwrap(),
132+ deref: false,
133133 }),
134134 Fail::Immediately,
135135 )
@@ -156,9 +156,9 @@ mod prepare_and_commit { · 20 unmodified lines
156156 change: Change::Delete {
157157 previous: Some(Target::Peeled(ObjectId::null_sha1())),
158158 mode: DeleteMode::RefAndRefLog,
159− deref: false,
160159 },
161160 name: head.name().into(),
161+ deref: false,
162162 }),
163163 Fail::Immediately,
164164 )
@@ -170,9 +170,9 @@ mod prepare_and_commit { · 5 unmodified lines
170170 change: Change::Delete {
171171 previous: Some(Target::Symbolic("refs/heads/main".try_into()?)),
172172 mode: DeleteMode::RefAndRefLog,
173− deref: false
174173 },
175174 name: head.name().into(),
175+ deref: false
176176 }],
177177 "the previous value was updated with the actual one"
178178 );
@@ -197,9 +197,9 @@ mod prepare_and_commit { · 18 unmodified lines
197197 change: Change::Delete {
198198 previous: Some(Target::Symbolic("refs/heads/main".try_into()?)),
199199 mode: DeleteMode::RefLogOnly,
200− deref: false,
201200 },
202201 name: head.name().into(),
202+ deref: false,
203203 }),
204204 Fail::Immediately,
205205 )
@@ -231,9 +231,9 @@ mod prepare_and_commit { · 25 unmodified lines
231231 change: Change::Delete {
232232 previous: Some(Target::Symbolic("refs/heads/main".try_into().unwrap())),
233233 mode: DeleteMode::RefLogOnly,
234− deref: true,
235234 },
236235 name: head.name().into(),
236+ deref: true,
237237 }),
238238 Fail::Immediately,
239239 )
git-ref/tests/refs.rs+6−12
@@ -11,9 +11,9 @@ mod transaction { · 10 unmodified lines
1111 change: Change::Delete {
1212 previous: None,
1313 mode: DeleteMode::RefAndRefLog,
14− deref: false,
1514 },
1615 name: name.try_into().expect("valid name"),
16+ deref: false,
1717 }
1818 }
1919
@@ -78,12 +78,6 @@ mod transaction { · 58 unmodified lines
7878 }
7979 }
8080
81− fn is_deref(edit: &RefEdit) -> bool {
82− match edit.change {
83− Change::Delete { deref, .. } | Change::Update { deref, .. } => deref,
84− }
85− }
86−
8781 fn find<'a>(edits: &'a [RefEdit], name: &str) -> &'a RefEdit {
8882 let name: FullName = name.try_into().unwrap();
8983 edits
@@ -103,36 +97,36 @@ mod transaction { · 13 unmodified lines
10397 change: Change::Delete {
10498 previous: None,
10599 mode: DeleteMode::RefAndRefLog,
106− deref: false,
107100 },
108101 name: "SYMBOLIC_PROBABLY_BUT_DEREF_IS_FALSE_SO_IGNORED".try_into()?,
102+ deref: false,
109103 },
110104 RefEdit {
111105 change: Change::Delete {
112106 previous: None,
113107 mode: DeleteMode::RefAndRefLog,
114− deref: true,
115108 },
116109 name: "refs/heads/anything-but-not-symbolic".try_into()?,
110+ deref: true,
117111 },
118112 RefEdit {
119113 change: Change::Delete {
120114 previous: None,
121115 mode: DeleteMode::RefAndRefLog,
122− deref: true,
123116 },
124117 name: "refs/heads/does-not-exist-and-deref-is-ignored".try_into()?,
118+ deref: true,
125119 },
126120 ];
127121
128122 edits.extend_with_splits_of_symbolic_refs(&store, |_| panic!("should not be called"))?;
129123 assert_eq!(edits.len(), 3, "no edit was added");
130124 assert!(
131− !is_deref(find(&edits, "refs/heads/anything-but-not-symbolic")),
125+ !find(&edits, "refs/heads/anything-but-not-symbolic").deref,
132126 "the algorithm corrects these flags"
133127 );
134128 assert!(
135− is_deref(find(&edits, "refs/heads/does-not-exist-and-deref-is-ignored")),
129+ find(&edits, "refs/heads/does-not-exist-and-deref-is-ignored").deref,
136130 "non-existing refs won't change the flag"
137131 );
138132 store.assert_empty();