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 is_read_only option #987

Merged
merged 1 commit into from
Jul 4, 2023
Merged
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
10 changes: 8 additions & 2 deletions git-branchless-record/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,17 @@ fn record_interactive(
)?;
process_diff_for_record(repo, &diff)?
};
let record_state = RecordState { files };
let record_state = RecordState {
is_read_only: false,
files,
};

let recorder = Recorder::new(record_state, EventSource::Crossterm);
let result = recorder.run();
let RecordState { files: result } = match result {
let RecordState {
is_read_only: _,
files: result,
} = match result {
Ok(result) => result,
Err(RecordError::Cancelled) => {
println!("Aborted.");
Expand Down
1 change: 1 addition & 0 deletions scm-record/benches/benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ fn bench_record(c: &mut Criterion) {
change_type: ChangeType::Added,
};
let record_state = RecordState {
is_read_only: false,
files: vec![File {
old_path: None,
path: Cow::Borrowed(Path::new("foo")),
Expand Down
5 changes: 4 additions & 1 deletion scm-record/examples/load_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ fn main() {
let result = recorder.run();
match result {
Ok(result) => {
let RecordState { files } = result;
let RecordState {
is_read_only: _,
files,
} = result;
for file in files {
println!("--- Path {:?} final lines: ---", file.path);
let (selected, _unselected) = file.get_selected_contents();
Expand Down
10 changes: 8 additions & 2 deletions scm-record/examples/static_contents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,19 @@ fn main() {
],
},
];
let record_state = RecordState { files };
let record_state = RecordState {
is_read_only: false,
files,
};

let recorder = Recorder::new(record_state, EventSource::Crossterm);
let result = recorder.run();
match result {
Ok(result) => {
let RecordState { files } = result;
let RecordState {
is_read_only: _,
files,
} = result;
for file in files {
println!("--- Path {:?} final lines: ---", file.path);
let (selected, _unselected) = file.get_selected_contents();
Expand Down
96 changes: 86 additions & 10 deletions scm-record/src/bin/scm-diff-editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,10 @@ mod render {
}

fn print_dry_run(write_root: &Path, state: RecordState) {
let scm_record::RecordState { files } = state;
let scm_record::RecordState {
is_read_only: _,
files,
} = state;
for file in files {
let file_path = write_root.join(file.path.clone());
let (selected_contents, _unselected_contents) = file.get_selected_contents();
Expand Down Expand Up @@ -843,7 +846,13 @@ fn apply_changes(
write_root: &Path,
state: RecordState,
) -> Result<()> {
let scm_record::RecordState { files } = state;
let scm_record::RecordState {
is_read_only,
files,
} = state;
if is_read_only {
return Ok(());
}
for file in files {
let file_path = write_root.join(file.path.clone());
let (selected_contents, _unselected_contents) = file.get_selected_contents();
Expand Down Expand Up @@ -882,6 +891,8 @@ struct Opts {
dir_diff: bool,
left: PathBuf,
right: PathBuf,
#[clap(long = "read-only")]
read_only: bool,
#[clap(short = 'N', long = "dry-run")]
dry_run: bool,
#[clap(
Expand All @@ -903,6 +914,7 @@ fn process_opts(filesystem: &dyn Filesystem, opts: &Opts) -> Result<(Vec<File<'s
right,
base: None,
output: _,
read_only: _,
dry_run: _,
} => {
let files = vec![render::create_file(
Expand All @@ -921,6 +933,7 @@ fn process_opts(filesystem: &dyn Filesystem, opts: &Opts) -> Result<(Vec<File<'s
right,
base: None,
output: _,
read_only: _,
dry_run: _,
} => {
let display_paths = filesystem.read_dir_diff_paths(left, right)?;
Expand All @@ -943,6 +956,7 @@ fn process_opts(filesystem: &dyn Filesystem, opts: &Opts) -> Result<(Vec<File<'s
right,
base: Some(base),
output: Some(output),
read_only: _,
dry_run: _,
} => {
let files = vec![render::create_merge_file(
Expand All @@ -961,6 +975,7 @@ fn process_opts(filesystem: &dyn Filesystem, opts: &Opts) -> Result<(Vec<File<'s
right: _,
base: Some(_),
output: None,
read_only: _,
dry_run: _,
} => {
unreachable!("--output is required when --base is provided");
Expand All @@ -972,6 +987,7 @@ fn process_opts(filesystem: &dyn Filesystem, opts: &Opts) -> Result<(Vec<File<'s
right: _,
base: Some(_),
output: _,
read_only: _,
dry_run: _,
} => {
unimplemented!("--base cannot be used with --dir-diff");
Expand All @@ -984,7 +1000,10 @@ fn main_inner() -> Result<()> {
let opts = Opts::parse();
let filesystem = RealFilesystem;
let (files, write_root) = process_opts(&filesystem, &opts)?;
let state = scm_record::RecordState { files };
let state = scm_record::RecordState {
is_read_only: opts.read_only,
files,
};
let event_source = scm_record::EventSource::Crossterm;
let recorder = scm_record::Recorder::new(state, event_source);
match recorder.run() {
Expand Down Expand Up @@ -1144,6 +1163,7 @@ qux2
right: PathBuf::from("right"),
base: None,
output: None,
read_only: false,
dry_run: false,
},
)?;
Expand Down Expand Up @@ -1196,7 +1216,14 @@ qux2
"###);

select_all(&mut files);
apply_changes(&mut filesystem, &write_root, RecordState { files })?;
apply_changes(
&mut filesystem,
&write_root,
RecordState {
is_read_only: false,
files,
},
)?;
insta::assert_debug_snapshot!(filesystem, @r###"
TestFilesystem {
files: {
Expand Down Expand Up @@ -1254,11 +1281,19 @@ qux2
right: PathBuf::from("right"),
base: None,
output: None,
read_only: false,
dry_run: false,
},
)?;

apply_changes(&mut filesystem, &write_root, RecordState { files })?;
apply_changes(
&mut filesystem,
&write_root,
RecordState {
is_read_only: false,
files,
},
)?;
insta::assert_debug_snapshot!(filesystem, @r###"
TestFilesystem {
files: {
Expand Down Expand Up @@ -1305,6 +1340,7 @@ qux2
right: PathBuf::from("right"),
base: None,
output: None,
read_only: false,
dry_run: false,
},
)?;
Expand Down Expand Up @@ -1332,7 +1368,14 @@ qux2
"###);

select_all(&mut files);
apply_changes(&mut filesystem, &write_root, RecordState { files })?;
apply_changes(
&mut filesystem,
&write_root,
RecordState {
is_read_only: false,
files,
},
)?;
insta::assert_debug_snapshot!(filesystem, @r###"
TestFilesystem {
files: {
Expand Down Expand Up @@ -1369,6 +1412,7 @@ qux2
right: PathBuf::from("right"),
base: None,
output: None,
read_only: false,
dry_run: false,
},
)?;
Expand Down Expand Up @@ -1396,7 +1440,14 @@ qux2
"###);

select_all(&mut files);
apply_changes(&mut filesystem, &write_root, RecordState { files })?;
apply_changes(
&mut filesystem,
&write_root,
RecordState {
is_read_only: false,
files,
},
)?;
insta::assert_debug_snapshot!(filesystem, @r###"
TestFilesystem {
files: {
Expand Down Expand Up @@ -1434,6 +1485,7 @@ qux2
right: PathBuf::from("right"),
base: None,
output: None,
read_only: false,
dry_run: false,
},
);
Expand Down Expand Up @@ -1467,11 +1519,19 @@ qux2
right: PathBuf::from("right/foo"),
base: None,
output: None,
read_only: false,
dry_run: false,
},
)?;

apply_changes(&mut filesystem, &write_root, RecordState { files })?;
apply_changes(
&mut filesystem,
&write_root,
RecordState {
is_read_only: false,
files,
},
)?;
assert_debug_snapshot!(filesystem, @r###"
TestFilesystem {
files: {
Expand Down Expand Up @@ -1522,11 +1582,19 @@ qux2
right: PathBuf::from("right/foo"),
base: None,
output: None,
read_only: false,
dry_run: false,
},
)?;

apply_changes(&mut filesystem, &write_root, RecordState { files })?;
apply_changes(
&mut filesystem,
&write_root,
RecordState {
is_read_only: false,
files,
},
)?;
assert_debug_snapshot!(filesystem, @r###"
TestFilesystem {
files: {
Expand Down Expand Up @@ -1594,6 +1662,7 @@ Hello world 4
dir_diff: false,
left: "left".into(),
right: "right".into(),
read_only: false,
dry_run: false,
base: Some("base".into()),
output: Some("output".into()),
Expand Down Expand Up @@ -1644,7 +1713,14 @@ Hello world 4
"###);

select_all(&mut files);
apply_changes(&mut filesystem, &write_root, RecordState { files })?;
apply_changes(
&mut filesystem,
&write_root,
RecordState {
is_read_only: false,
files,
},
)?;

assert_debug_snapshot!(filesystem, @r###"
TestFilesystem {
Expand Down
4 changes: 4 additions & 0 deletions scm-record/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ use thiserror::Error;
#[derive(Clone, Debug, Default, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct RecordState<'a> {
/// Render the UI as read-only, such that the checkbox states cannot be
/// changed by the user.
pub is_read_only: bool,

/// The state of each file. This is rendered in order, so you may want to
/// sort this list by path before providing it.
pub files: Vec<File<'a>>,
Expand Down
Loading
Loading