Skip to content

Commit

Permalink
feat(record)!: use default commit message for --stash
Browse files Browse the repository at this point in the history
But only when no message is explicitly supplied.

This is a breaking change. Previously, the user would be prompted for a message
if none was supplied.
  • Loading branch information
claytonrcarter committed Dec 16, 2024
1 parent 2cb8484 commit 7da4d76
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
9 changes: 9 additions & 0 deletions git-branchless-lib/src/git/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ pub struct Diff<'repo> {
pub(super) inner: git2::Diff<'repo>,
}

impl Diff<'_> {
/// TODO
pub fn short_stats(&self) -> eyre::Result<String> {
let stats = self.inner.stats()?;
let buf = stats.to_buf(git2::DiffStatsFormat::SHORT, usize::MAX)?;
Ok(buf.as_str().expect("TODO").trim().to_string())
}
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
struct GitHunk {
old_start: usize,
Expand Down
36 changes: 36 additions & 0 deletions git-branchless-record/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,42 @@ fn record(
)?);
}
} else {
let messages = if messages.is_empty() && stash {
let diff_stats = {
let (old_tree, new_tree) = match working_copy_changes_type {
WorkingCopyChangesType::Unstaged => {
let old_tree = snapshot.commit_stage0.get_tree()?;
let new_tree = snapshot.commit_unstaged.get_tree()?;
(Some(old_tree), new_tree)
}
WorkingCopyChangesType::Staged => {
let old_tree = match snapshot.head_commit {
None => None,
Some(ref commit) => Some(commit.get_tree()?),
};
let new_tree = snapshot.commit_stage0.get_tree()?;
(old_tree, new_tree)
}
WorkingCopyChangesType::None | WorkingCopyChangesType::Conflicts => {
unreachable!("already handled via early exit")
}
};

let (effects, _progress) = effects.start_operation(OperationType::CalculateDiff);
let diff = repo.get_diff_between_trees(
&effects,
old_tree.as_ref(),
&new_tree,
0, // we don't care about the context here
)?;

diff.short_stats()?
};

vec![format!("stash: {diff_stats}")]
} else {
messages
};
let args = {
let mut args = vec!["commit"];
args.extend(messages.iter().flat_map(|message| ["--message", message]));
Expand Down
36 changes: 36 additions & 0 deletions git-branchless-record/tests/test_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,42 @@ fn test_record_stash() -> eyre::Result<()> {
Ok(())
}

#[test]
fn test_record_stash_default_message() -> eyre::Result<()> {
let git = make_git()?;

if !git.supports_reference_transactions()? {
return Ok(());
}
git.init_repo()?;

git.commit_file("test1", 1)?;

{
git.write_file_txt("test1", "new test1 contents\n")?;

let (stdout, _stderr) = git.branchless("record", &["--stash"])?;
insta::assert_snapshot!(stdout, @r###"
[master 4f8603e] stash: 1 file changed, 1 insertion(+), 1 deletion(-)
1 file changed, 1 insertion(+), 1 deletion(-)
branchless: running command: <git-executable> branch -f master 62fc20d2a290daea0d52bdc2ed2ad4be6491010e
branchless: running command: <git-executable> checkout master
"###);
}

{
let stdout = git.smartlog()?;
insta::assert_snapshot!(stdout, @r###"
:
@ 62fc20d (> master) create test1.txt
|
o 4f8603e stash: 1 file changed, 1 insertion(+), 1 deletion(-)
"###);
}

Ok(())
}

#[test]
fn test_record_create_branch() -> eyre::Result<()> {
let git = make_git()?;
Expand Down

0 comments on commit 7da4d76

Please sign in to comment.