Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(record)!: add default commit message for --stash #1462

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading