Skip to content

Commit 680b178

Browse files
author
Stephan Dilly
committed
make revlog sorted by time
1 parent 76002f1 commit 680b178

File tree

5 files changed

+122
-20
lines changed

5 files changed

+122
-20
lines changed

asyncgit/src/sync/branch/merge_commit.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,15 @@ pub(crate) fn commit_merge_with_head(
9494

9595
#[cfg(test)]
9696
mod test {
97+
use git2::Time;
98+
9799
use super::*;
98100
use crate::sync::{
99101
branch_compare_upstream,
100102
remotes::{fetch, push::push},
101103
tests::{
102104
debug_cmd_print, get_commit_ids, repo_clone,
103-
repo_init_bare, write_commit_file,
105+
repo_init_bare, write_commit_file, write_commit_file_at,
104106
},
105107
RepoState,
106108
};
@@ -119,8 +121,13 @@ mod test {
119121

120122
// clone1
121123

122-
let commit1 =
123-
write_commit_file(&clone1, "test.txt", "test", "commit1");
124+
let commit1 = write_commit_file_at(
125+
&clone1,
126+
"test.txt",
127+
"test",
128+
"commit1",
129+
Time::new(1, 0),
130+
);
124131

125132
push(
126133
clone1_dir.path().to_str().unwrap(),
@@ -134,11 +141,12 @@ mod test {
134141

135142
// clone2
136143

137-
let commit2 = write_commit_file(
144+
let commit2 = write_commit_file_at(
138145
&clone2,
139146
"test2.txt",
140147
"test",
141148
"commit2",
149+
Time::new(2, 0),
142150
);
143151

144152
//push should fail since origin diverged

asyncgit/src/sync/branch/merge_rebase.rs

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ pub fn merge_upstream_rebase(
4747
rebase.commit(None, &signature, None)?;
4848
}
4949

50+
if repo.index()?.has_conflicts() {
51+
rebase.abort()?;
52+
return Err(Error::Generic(String::from(
53+
"conflicts while merging",
54+
)));
55+
}
56+
5057
rebase.finish(Some(&signature))?;
5158

5259
Ok(())
@@ -60,11 +67,11 @@ mod test {
6067
remotes::{fetch, push::push},
6168
tests::{
6269
debug_cmd_print, get_commit_ids, repo_clone,
63-
repo_init_bare, write_commit_file,
70+
repo_init_bare, write_commit_file, write_commit_file_at,
6471
},
6572
RepoState,
6673
};
67-
use git2::Repository;
74+
use git2::{Repository, Time};
6875

6976
fn get_commit_msgs(r: &Repository) -> Vec<String> {
7077
let commits = get_commit_ids(r, 10);
@@ -90,8 +97,13 @@ mod test {
9097

9198
// clone1
9299

93-
let _commit1 =
94-
write_commit_file(&clone1, "test.txt", "test", "commit1");
100+
let _commit1 = write_commit_file_at(
101+
&clone1,
102+
"test.txt",
103+
"test",
104+
"commit1",
105+
git2::Time::new(0, 0),
106+
);
95107

96108
assert_eq!(clone1.head_detached().unwrap(), false);
97109

@@ -107,11 +119,12 @@ mod test {
107119

108120
let clone2_dir = clone2_dir.path().to_str().unwrap();
109121

110-
let _commit2 = write_commit_file(
122+
let _commit2 = write_commit_file_at(
111123
&clone2,
112124
"test2.txt",
113125
"test",
114126
"commit2",
127+
git2::Time::new(1, 0),
115128
);
116129

117130
assert_eq!(clone2.head_detached().unwrap(), false);
@@ -123,11 +136,12 @@ mod test {
123136

124137
// clone1
125138

126-
let _commit3 = write_commit_file(
139+
let _commit3 = write_commit_file_at(
127140
&clone1,
128141
"test3.txt",
129142
"test",
130143
"commit3",
144+
git2::Time::new(2, 0),
131145
);
132146

133147
assert_eq!(clone1.head_detached().unwrap(), false);
@@ -144,7 +158,7 @@ mod test {
144158
1
145159
);
146160

147-
// debug_cmd_print(clone1_dir, "git log");
161+
// debug_cmd_print(clone1_dir, "git status");
148162

149163
assert_eq!(clone1.head_detached().unwrap(), false);
150164

@@ -179,7 +193,13 @@ mod test {
179193

180194
// clone1
181195

182-
write_commit_file(&clone1, "test.txt", "test", "commit1");
196+
write_commit_file_at(
197+
&clone1,
198+
"test.txt",
199+
"test",
200+
"commit1",
201+
Time::new(0, 0),
202+
);
183203

184204
push(clone1_dir, "origin", "master", false, None, None)
185205
.unwrap();
@@ -191,15 +211,33 @@ mod test {
191211

192212
let clone2_dir = clone2_dir.path().to_str().unwrap();
193213

194-
write_commit_file(&clone2, "test2.txt", "test", "commit2");
214+
write_commit_file_at(
215+
&clone2,
216+
"test2.txt",
217+
"test",
218+
"commit2",
219+
Time::new(1, 0),
220+
);
195221

196222
push(clone2_dir, "origin", "master", false, None, None)
197223
.unwrap();
198224

199225
// clone1
200226

201-
write_commit_file(&clone1, "test3.txt", "test", "commit3");
202-
write_commit_file(&clone1, "test4.txt", "test", "commit4");
227+
write_commit_file_at(
228+
&clone1,
229+
"test3.txt",
230+
"test",
231+
"commit3",
232+
Time::new(2, 0),
233+
);
234+
write_commit_file_at(
235+
&clone1,
236+
"test4.txt",
237+
"test",
238+
"commit4",
239+
Time::new(3, 0),
240+
);
203241

204242
//lets fetch from origin
205243

asyncgit/src/sync/commit.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use super::{get_head, utils::repo, CommitId};
2-
use crate::error::Result;
1+
use super::{utils::repo, CommitId};
2+
use crate::{error::Result, sync::utils::get_head_repo};
33
use git2::{ErrorCode, ObjectType, Repository, Signature};
44
use scopetime::scope_time;
55

@@ -68,7 +68,7 @@ pub fn commit(repo_path: &str, msg: &str) -> Result<CommitId> {
6868
let tree_id = index.write_tree()?;
6969
let tree = repo.find_tree(tree_id)?;
7070

71-
let parents = if let Ok(id) = get_head(repo_path) {
71+
let parents = if let Ok(id) = get_head_repo(&repo) {
7272
vec![repo.find_commit(id.into())?]
7373
} else {
7474
Vec::new()

asyncgit/src/sync/logwalker.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::CommitId;
22
use crate::error::Result;
3-
use git2::{Repository, Revwalk};
3+
use git2::{Repository, Revwalk, Sort};
44

55
///
66
pub struct LogWalker<'a> {
@@ -27,7 +27,10 @@ impl<'a> LogWalker<'a> {
2727

2828
if self.revwalk.is_none() {
2929
let mut walk = self.repo.revwalk()?;
30+
3031
walk.push_head()?;
32+
walk.set_sorting(Sort::TIME)?;
33+
3134
self.revwalk = Some(walk);
3235
}
3336

asyncgit/src/sync/mod.rs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ mod tests {
7979
use super::{
8080
commit, stage_add_file,
8181
status::{get_status, StatusType},
82-
utils::repo_write_file,
82+
utils::{get_head_repo, repo, repo_write_file},
8383
CommitId, LogWalker,
8484
};
8585
use crate::error::Result;
@@ -128,6 +128,59 @@ mod tests {
128128
.unwrap()
129129
}
130130

131+
/// write, stage and commit a file giving the commit a specific timestamp
132+
pub fn write_commit_file_at(
133+
repo: &Repository,
134+
file: &str,
135+
content: &str,
136+
commit_name: &str,
137+
time: git2::Time,
138+
) -> CommitId {
139+
repo_write_file(repo, file, content).unwrap();
140+
141+
let path = repo.workdir().unwrap().to_str().unwrap();
142+
143+
stage_add_file(path, Path::new(file)).unwrap();
144+
145+
commit_at(path, commit_name, time)
146+
}
147+
148+
fn commit_at(
149+
repo_path: &str,
150+
msg: &str,
151+
time: git2::Time,
152+
) -> CommitId {
153+
let repo = repo(repo_path).unwrap();
154+
155+
let signature =
156+
git2::Signature::new("name", "email", &time).unwrap();
157+
let mut index = repo.index().unwrap();
158+
let tree_id = index.write_tree().unwrap();
159+
let tree = repo.find_tree(tree_id).unwrap();
160+
161+
let parents = if let Ok(id) = get_head_repo(&repo) {
162+
vec![repo.find_commit(id.into()).unwrap()]
163+
} else {
164+
Vec::new()
165+
};
166+
167+
let parents = parents.iter().collect::<Vec<_>>();
168+
169+
let commit = repo
170+
.commit(
171+
Some("HEAD"),
172+
&signature,
173+
&signature,
174+
msg,
175+
&tree,
176+
parents.as_slice(),
177+
)
178+
.unwrap()
179+
.into();
180+
181+
commit
182+
}
183+
131184
///
132185
pub fn repo_init_empty() -> Result<(TempDir, Repository)> {
133186
sandbox_config_files();

0 commit comments

Comments
 (0)