Skip to content

Commit 6430484

Browse files
author
Stephan Dilly
committed
fix detached head after rebase merge
1 parent 15a6565 commit 6430484

File tree

4 files changed

+50
-25
lines changed

4 files changed

+50
-25
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
- Fast and intuitive **keyboard only** control
2929
- Context based help (**no need to memorize** tons of hot-keys)
3030
- Inspect, commit, and amend changes (incl. hooks: _commit-msg_/_post-commit_)
31-
- Stage, unstage, revert and reset files and hunks
31+
- Stage, unstage, revert and reset files, hunks and lines
3232
- Stashing (save, apply, drop, and inspect)
3333
- Push/Fetch to/from remote
3434
- Branch List (create, rename, delete)

asyncgit/src/sync/branch/merge_commit.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,10 @@ mod test {
162162
merge_upstream_commit(clone2_dir, "master").unwrap();
163163

164164
let state = crate::sync::repo_state(clone2_dir).unwrap();
165-
166165
assert_eq!(state, RepoState::Clean);
167166

167+
assert!(!clone2.head_detached().unwrap());
168+
168169
let commits = get_commit_ids(&clone2, 10);
169170
assert_eq!(commits.len(), 3);
170171
assert_eq!(commits[0], merge_commit);

asyncgit/src/sync/branch/merge_rebase.rs

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ pub fn merge_upstream_rebase(
1515
scope_time!("merge_upstream_rebase");
1616

1717
let repo = utils::repo(repo_path)?;
18+
if super::get_branch_name_repo(&repo)? != branch_name {
19+
return Err(Error::Generic(String::from(
20+
"can only rebase in head branch",
21+
)));
22+
}
23+
1824
let branch = repo.find_branch(branch_name, BranchType::Local)?;
1925
let upstream = branch.upstream()?;
2026
let upstream_commit = upstream.get().peel_to_commit()?;
@@ -24,36 +30,34 @@ pub fn merge_upstream_rebase(
2430
let branch_commit = branch.get().peel_to_commit()?;
2531
let annotated_branch =
2632
repo.find_annotated_commit(branch_commit.id())?;
33+
dbg!(annotated_branch.id());
2734

28-
let rebase = repo.rebase(
29-
Some(&annotated_branch),
30-
Some(&annotated_upstream),
31-
None,
32-
None,
33-
)?;
35+
let mut rebase =
36+
repo.rebase(None, Some(&annotated_upstream), None, None)?;
3437

3538
let signature =
3639
crate::sync::commit::signature_allow_undefined_name(&repo)?;
3740

38-
for e in rebase {
39-
let _op = e?;
40-
// dbg!(op.id());
41-
// dbg!(op.kind());
42-
}
41+
while let Some(op) = rebase.next() {
42+
let op = op?;
43+
dbg!(op.id());
4344

44-
let mut rebase = repo.open_rebase(None)?;
45+
if repo.index()?.has_conflicts() {
46+
rebase.abort()?;
47+
return Err(Error::Generic(String::from(
48+
"conflicts while merging",
49+
)));
50+
}
4551

46-
if repo.index()?.has_conflicts() {
47-
rebase.abort()?;
52+
let commit = rebase.commit(None, &signature, None)?;
53+
dbg!(commit);
54+
}
4855

49-
Err(Error::Generic(String::from("conflicts while merging")))
50-
} else {
51-
rebase.commit(None, &signature, None)?;
56+
rebase.finish(Some(&signature))?;
5257

53-
rebase.finish(Some(&signature))?;
58+
repo.index()?.read(true)?;
5459

55-
Ok(())
56-
}
60+
Ok(())
5761
}
5862

5963
#[cfg(test)]
@@ -97,9 +101,13 @@ mod test {
97101
let _commit1 =
98102
write_commit_file(&clone1, "test.txt", "test", "commit1");
99103

104+
assert_eq!(clone1.head_detached().unwrap(), false);
105+
100106
push(clone1_dir, "origin", "master", false, None, None)
101107
.unwrap();
102108

109+
assert_eq!(clone1.head_detached().unwrap(), false);
110+
103111
// clone2
104112

105113
let (clone2_dir, clone2) =
@@ -114,9 +122,13 @@ mod test {
114122
"commit2",
115123
);
116124

125+
assert_eq!(clone2.head_detached().unwrap(), false);
126+
117127
push(clone2_dir, "origin", "master", false, None, None)
118128
.unwrap();
119129

130+
assert_eq!(clone2.head_detached().unwrap(), false);
131+
120132
// clone1
121133

122134
let _commit3 = write_commit_file(
@@ -126,6 +138,8 @@ mod test {
126138
"commit3",
127139
);
128140

141+
assert_eq!(clone1.head_detached().unwrap(), false);
142+
129143
//lets fetch from origin
130144
let bytes =
131145
fetch_origin(clone1_dir, "master", None, None).unwrap();
@@ -141,12 +155,13 @@ mod test {
141155

142156
// debug_cmd_print(clone1_dir, "git log");
143157

158+
assert_eq!(clone1.head_detached().unwrap(), false);
159+
144160
merge_upstream_rebase(clone1_dir, "master").unwrap();
145161

146162
debug_cmd_print(clone1_dir, "git log");
147163

148164
let state = crate::sync::repo_state(clone1_dir).unwrap();
149-
150165
assert_eq!(state, RepoState::Clean);
151166

152167
let commits = get_commit_msgs(&clone1);
@@ -158,6 +173,8 @@ mod test {
158173
String::from("commit1")
159174
]
160175
);
176+
177+
assert_eq!(clone1.head_detached().unwrap(), false);
161178
}
162179

163180
#[test]

asyncgit/src/sync/branch/mod.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,17 @@ use utils::get_head_repo;
1919
/// returns the branch-name head is currently pointing to
2020
/// this might be expensive, see `cached::BranchName`
2121
pub(crate) fn get_branch_name(repo_path: &str) -> Result<String> {
22-
scope_time!("get_branch_name");
23-
2422
let repo = utils::repo(repo_path)?;
2523

24+
Ok(get_branch_name_repo(&repo)?)
25+
}
26+
27+
/// ditto
28+
pub(crate) fn get_branch_name_repo(
29+
repo: &Repository,
30+
) -> Result<String> {
31+
scope_time!("get_branch_name_repo");
32+
2633
let iter = repo.branches(None)?;
2734

2835
for b in iter {

0 commit comments

Comments
 (0)