Skip to content

Commit dac0214

Browse files
author
Stephan Dilly
committed
preset rebase commit msg
1 parent d1d47c4 commit dac0214

File tree

4 files changed

+68
-23
lines changed

4 files changed

+68
-23
lines changed

asyncgit/src/sync/rebase.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ pub struct RebaseProgress {
143143
pub steps: usize,
144144
///
145145
pub current: usize,
146+
///
147+
pub current_commit: Option<CommitId>,
146148
}
147149

148150
///
@@ -151,9 +153,15 @@ pub fn get_rebase_progress(
151153
) -> Result<RebaseProgress> {
152154
let mut rebase = repo.open_rebase(None)?;
153155

156+
let current_commit: Option<CommitId> = rebase
157+
.operation_current()
158+
.and_then(|idx| rebase.nth(idx))
159+
.map(|op| op.id().into());
160+
154161
let progress = RebaseProgress {
155162
steps: rebase.len(),
156163
current: rebase.operation_current().unwrap_or_default(),
164+
current_commit,
157165
};
158166

159167
Ok(progress)
@@ -286,7 +294,8 @@ mod test_rebase {
286294

287295
create_branch(repo_path, "foo").unwrap();
288296

289-
write_commit_file(&repo, "test.txt", "test2", "commit2");
297+
let c =
298+
write_commit_file(&repo, "test.txt", "test2", "commit2");
290299

291300
checkout_branch(repo_path, "refs/heads/master").unwrap();
292301

@@ -306,7 +315,8 @@ mod test_rebase {
306315
get_rebase_progress(&repo).unwrap(),
307316
RebaseProgress {
308317
current: 0,
309-
steps: 1
318+
steps: 1,
319+
current_commit: Some(c)
310320
}
311321
);
312322

src/components/commit.rs

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ use anyhow::Result;
1313
use asyncgit::{
1414
cached,
1515
sync::{
16-
self, get_config_string, CommitId, HookResult, RepoState,
16+
self, get_commit_info, get_config_string, CommitId,
17+
HookResult, RepoState,
1718
},
1819
CWD,
1920
};
@@ -254,6 +255,19 @@ impl CommitComponent {
254255
!= self.commit_template.as_ref().map(|s| s.trim())
255256
}
256257

258+
fn rebase_commit_msg() -> Result<String> {
259+
let progress = sync::rebase_progress(CWD)?;
260+
261+
let id = if let Some(id) = progress.current_commit {
262+
id
263+
} else {
264+
anyhow::bail!("no commit id")
265+
};
266+
267+
let info = get_commit_info(CWD, &id)?;
268+
Ok(info.message)
269+
}
270+
257271
fn amend(&mut self) -> Result<()> {
258272
if self.can_amend() {
259273
let id = sync::get_head(CWD)?;
@@ -369,26 +383,36 @@ impl Component for CommitComponent {
369383

370384
self.mode = Mode::Normal;
371385

372-
self.mode = if sync::repo_state(CWD)? == RepoState::Merge {
373-
let ids = sync::mergehead_ids(CWD)?;
374-
self.input.set_title(strings::commit_title_merge());
375-
self.input.set_text(sync::merge_msg(CWD)?);
376-
Mode::Merge(ids)
377-
} else {
378-
self.commit_template =
379-
get_config_string(CWD, "commit.template")
380-
.ok()
381-
.flatten()
382-
.and_then(|path| read_to_string(path).ok());
383-
384-
if self.is_empty() {
385-
if let Some(s) = &self.commit_template {
386-
self.input.set_text(s.clone());
387-
}
386+
self.mode = match sync::repo_state(CWD)? {
387+
RepoState::Merge => {
388+
let ids = sync::mergehead_ids(CWD)?;
389+
self.input.set_title(strings::commit_title_merge());
390+
self.input.set_text(sync::merge_msg(CWD)?);
391+
Mode::Merge(ids)
392+
}
393+
RepoState::Rebase => {
394+
self.input.set_title(strings::commit_title_rebase());
395+
self.input.set_text(
396+
Self::rebase_commit_msg().unwrap_or_default(),
397+
);
398+
Mode::Normal
388399
}
400+
_ => {
401+
self.commit_template =
402+
get_config_string(CWD, "commit.template")
403+
.ok()
404+
.flatten()
405+
.and_then(|path| read_to_string(path).ok());
406+
407+
if self.is_empty() {
408+
if let Some(s) = &self.commit_template {
409+
self.input.set_text(s.clone());
410+
}
411+
}
389412

390-
self.input.set_title(strings::commit_title());
391-
Mode::Normal
413+
self.input.set_title(strings::commit_title());
414+
Mode::Normal
415+
}
392416
};
393417

394418
self.input.show()?;

src/strings.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ pub fn commit_title() -> String {
8686
pub fn commit_title_merge() -> String {
8787
"Commit (Merge)".to_string()
8888
}
89+
pub fn commit_title_rebase() -> String {
90+
"Commit (Rebase)".to_string()
91+
}
8992
pub fn commit_title_amend() -> String {
9093
"Commit (Amend)".to_string()
9194
}

src/tabs/status.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use crate::{
1414
use anyhow::Result;
1515
use asyncgit::{
1616
cached,
17-
sync::BranchCompare,
1817
sync::{self, status::StatusType, RepoState},
18+
sync::{BranchCompare, CommitId},
1919
AsyncDiff, AsyncGitNotification, AsyncStatus, DiffParams,
2020
DiffType, StatusParams, CWD,
2121
};
@@ -235,7 +235,15 @@ impl Status {
235235
RepoState::Rebase => {
236236
let progress =
237237
if let Ok(p) = sync::rebase_progress(CWD) {
238-
format!("{}/{}", p.current + 1, p.steps)
238+
format!(
239+
"[{}] {}/{}",
240+
p.current_commit
241+
.as_ref()
242+
.map(CommitId::get_short_string)
243+
.unwrap_or_default(),
244+
p.current + 1,
245+
p.steps
246+
)
239247
} else {
240248
String::new()
241249
};

0 commit comments

Comments
 (0)