Skip to content

Commit 9622e44

Browse files
committed
wip
1 parent c2ba360 commit 9622e44

File tree

11 files changed

+925
-532
lines changed

11 files changed

+925
-532
lines changed

src/cli/src/cmd_setup.rs

+8-13
Original file line numberDiff line numberDiff line change
@@ -805,40 +805,35 @@ pub fn pull() -> Command {
805805

806806
pub fn diff() -> Command {
807807
Command::new(DIFF)
808-
.about("Compare two files against each other or against versions. The first parameter can be one of three things 1) another file 2) a commit hash 3) a branch name. If the first parameter is a revision it will compare the second parameter path to that version of the file.")
809-
.arg(Arg::new("FILE_OR_REVISION").required(true))
810-
.arg(Arg::new("PATH").required(false))
811-
}
812-
813-
pub fn compare() -> Command {
814-
Command::new(COMPARE)
815-
.about("Compare two tabular files with some schematic overlap. The two resource paramaters can be specified by filepath or `file:revision` syntax.")
808+
.about("Compare two files against each other or against versions. The two resource paramaters can be specified by filepath or `file:revision` syntax.")
816809
.arg(Arg::new("RESOURCE1")
817810
.required(true)
818811
.help("First resource, in format `file` or `file:revision`")
819812
.index(1)
820813
)
821814
.arg(Arg::new("RESOURCE2")
822-
.required(true)
823-
.help("Second resource, in format `file` or `file:revision`")
815+
.required(false)
816+
.help("Second resource, in format `file` or `file:revision`. If left blank, RESOURCE1 will be compared with HEAD.")
824817
.index(2))
825818
.arg(Arg::new("keys")
826-
.required(true)
819+
.required(false)
827820
.long("keys")
821+
.short("k")
828822
.help("Comma-separated list of columns to compare on. If not specified, all columns are used for comparison.")
829823
.use_value_delimiter(true)
830824
.action(clap::ArgAction::Set))
831825
.arg(Arg::new("targets")
832-
.required(true)
826+
.required(false)
833827
.long("targets")
828+
.short("t")
834829
.help("Comma-separated list of columns in which to view changes. If not specified, all columns are viewed")
835830
.use_value_delimiter(true)
836831
.action(clap::ArgAction::Set))
837832
.arg(Arg::new("output")
838833
.required(false)
839834
.long("output")
840835
.short('o')
841-
.help("Output directory path to write the results of the comparison. Will write both match.csv (rows with same keys and targets) and diff.csv (rows with different targets between files")
836+
.help("Output directory path to write the results of the comparison. Will write both match.csv (rows with same keys and targets) and diff.csv (rows with different targets between files.")
842837
.action(clap::ArgAction::Set))
843838
}
844839

src/cli/src/dispatch.rs

+56-25
Original file line numberDiff line numberDiff line change
@@ -501,53 +501,84 @@ pub async fn pull(remote: &str, branch: &str, all: bool) -> Result<(), OxenError
501501
Ok(())
502502
}
503503

504-
pub async fn diff(commit_id: Option<&str>, path: &str, remote: bool) -> Result<(), OxenError> {
505-
let repo_dir = env::current_dir().unwrap();
506-
let repository = LocalRepository::from_dir(&repo_dir)?;
507-
let path = Path::new(path);
508-
509-
let result = if remote {
510-
command::remote::diff(&repository, commit_id, path).await?
511-
} else {
512-
command::diff(&repository, commit_id, path)?
513-
};
514-
println!("{result}");
515-
Ok(())
516-
}
517-
518-
pub fn compare(
504+
#[allow(clippy::too_many_arguments)]
505+
pub async fn diff(
519506
file_1: PathBuf,
520507
revision_1: Option<&str>,
521-
file_2: PathBuf,
508+
file_2: Option<PathBuf>,
522509
revision_2: Option<&str>,
523510
keys: Vec<String>,
524511
targets: Vec<String>,
525512
output: Option<PathBuf>,
513+
is_remote: bool,
526514
) -> Result<(), OxenError> {
527515
let repo_dir = env::current_dir().unwrap();
528516
let repository = LocalRepository::from_dir(&repo_dir)?;
529517
check_repo_migration_needed(&repository)?;
530518

531519
let current_commit = api::local::commits::head_commit(&repository)?;
532520
// For revision_1 and revision_2, if none, set to current_commit
533-
let revision_1 = revision_1.unwrap_or(current_commit.id.as_str());
534-
let revision_2 = revision_2.unwrap_or(current_commit.id.as_str());
521+
// let revision_1 = revision_1.unwrap_or(current_commit.id.as_str());
522+
// let revision_2 = revision_2.unwrap_or(current_commit.id.as_str());
535523

536524
let commit_1 = api::local::revisions::get(&repository, revision_1)?
537525
.ok_or_else(|| OxenError::revision_not_found(revision_1.into()))?;
538526
let commit_2 = api::local::revisions::get(&repository, revision_2)?
539527
.ok_or_else(|| OxenError::revision_not_found(revision_2.into()))?;
540528

541-
let cpath_1 = CommitPath {
542-
commit: commit_1,
543-
path: file_1,
529+
// TODONOW: might be able to clean this logic up - pull out into function so we can early return and be less confusing
530+
let (cpath_1, cpath_2) = if let Some(file_2) = file_2 {
531+
let cpath_1 = if let Some(revison) = revision_1 {
532+
let commit_1 = api::local::revisions::get(&repository, revison)?;
533+
CommitPath {
534+
commit: commit_1,
535+
path: file_1.clone(),
536+
}
537+
} else {
538+
CommitPath {
539+
commit: None,
540+
path: file_1.clone(),
541+
}
542+
};
543+
544+
let cpath_2 = if let Some(revison) = revision_2 {
545+
let commit = api::local::revisions::get(&repository, revison)?;
546+
547+
CommitPath {
548+
commit,
549+
path: file_2,
550+
}
551+
} else {
552+
CommitPath {
553+
commit: None,
554+
path: file_2,
555+
}
556+
};
557+
558+
(cpath_1, cpath_2)
559+
} else {
560+
// If no file2, compare with file1 at head.
561+
let commit = Some(api::local::commits::head_commit(&repository)?);
562+
563+
(
564+
CommitPath {
565+
commit: None,
566+
path: file_1.clone(),
567+
},
568+
CommitPath {
569+
commit,
570+
path: file_1.clone(),
571+
},
572+
)
544573
};
545-
let cpath_2 = CommitPath {
546-
commit: commit_2,
547-
path: file_2,
574+
575+
let result = if is_remote {
576+
command::remote::diff(&repository, revision_1, &file_1).await?
577+
} else {
578+
command::compare(&repository, cpath_1, cpath_2, keys, targets, output)?
548579
};
549580

550-
command::compare(&repository, cpath_1, cpath_2, keys, targets, output)?;
581+
println!("{result}");
551582
Ok(())
552583
}
553584

src/cli/src/main.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ async fn main() {
2121
.subcommand(cmd_setup::clone())
2222
.subcommand(cmd_setup::commit_cache())
2323
.subcommand(cmd_setup::commit())
24-
.subcommand(cmd_setup::compare())
2524
.subcommand(cmd_setup::config())
2625
.subcommand(cmd_setup::create_remote())
2726
.subcommand(cmd_setup::df())
@@ -56,13 +55,12 @@ async fn main() {
5655
parse_and_run::compute_commit_cache(sub_matches).await
5756
}
5857
Some((cmd_setup::COMMIT, sub_matches)) => parse_and_run::commit(sub_matches).await,
59-
Some((cmd_setup::COMPARE, sub_matches)) => parse_and_run::compare(sub_matches).await,
58+
Some((cmd_setup::DIFF, sub_matches)) => parse_and_run::diff(sub_matches).await,
6059
Some((cmd_setup::CONFIG, sub_matches)) => parse_and_run::config(sub_matches),
6160
Some((cmd_setup::CREATE_REMOTE, sub_matches)) => {
6261
parse_and_run::create_remote(sub_matches).await
6362
}
6463
Some((cmd_setup::DF, sub_matches)) => parse_and_run::df(sub_matches),
65-
Some((cmd_setup::DIFF, sub_matches)) => parse_and_run::diff(sub_matches).await,
6664
Some((cmd_setup::DOWNLOAD, sub_matches)) => parse_and_run::download(sub_matches).await,
6765
Some((cmd_setup::INIT, sub_matches)) => parse_and_run::init(sub_matches).await,
6866
Some((cmd_setup::INFO, sub_matches)) => parse_and_run::info(sub_matches),

src/cli/src/parse_and_run.rs

+46-36
Original file line numberDiff line numberDiff line change
@@ -943,72 +943,82 @@ pub async fn pull(sub_matches: &ArgMatches) {
943943
}
944944
}
945945

946-
pub async fn remote_diff(sub_matches: &ArgMatches) {
946+
pub async fn diff(sub_matches: &ArgMatches) {
947947
let is_remote = true;
948948
p_diff(sub_matches, is_remote).await
949949
}
950950

951-
pub async fn compare(sub_matches: &ArgMatches) {
951+
async fn p_diff(sub_matches: &ArgMatches, is_remote: bool) {
952952
let resource1 = sub_matches
953953
.get_one::<String>("RESOURCE1")
954954
.expect("required");
955-
let resource2 = sub_matches
956-
.get_one::<String>("RESOURCE2")
957-
.expect("required");
955+
let resource2 = sub_matches.get_one::<String>("RESOURCE2");
958956

959957
let (file1, revision1) = parse_file_and_revision(resource1);
960-
let (file2, revision2) = parse_file_and_revision(resource2);
961958

962959
let file1 = PathBuf::from(file1);
963-
let file2 = PathBuf::from(file2);
960+
961+
let (file2, revision2) = match resource2 {
962+
Some(resource) => {
963+
let (file, revision) = parse_file_and_revision(resource);
964+
(Some(PathBuf::from(file)), Some(revision))
965+
}
966+
None => (None, None),
967+
};
964968

965969
let keys: Vec<String> = match sub_matches.get_many::<String>("keys") {
966970
Some(values) => values.cloned().collect(),
967971
None => Vec::new(),
968972
};
969973

970-
let targets: Vec<String> = match sub_matches.get_many::<String>("targets") {
974+
let maybe_targets = sub_matches.get_many::<String>("targets");
975+
976+
let targets = match maybe_targets {
971977
Some(values) => values.cloned().collect(),
972978
None => Vec::new(),
973979
};
974980

975981
let output = sub_matches.get_one::<String>("output").map(PathBuf::from);
976982

977-
match dispatch::compare(file1, revision1, file2, revision2, keys, targets, output) {
983+
match dispatch::diff(
984+
file1, revision1, file2, revision2, keys, targets, output, is_remote,
985+
)
986+
.await
987+
{
978988
Ok(_) => {}
979989
Err(err) => {
980990
eprintln!("{err}")
981991
}
982992
}
983993
}
984994

985-
pub async fn diff(sub_matches: &ArgMatches) {
986-
let is_remote = false;
987-
p_diff(sub_matches, is_remote).await
988-
}
989-
990-
async fn p_diff(sub_matches: &ArgMatches, is_remote: bool) {
991-
// First arg is optional
992-
let file_or_commit_id = sub_matches
993-
.get_one::<String>("FILE_OR_REVISION")
994-
.expect("required");
995-
let path = sub_matches.get_one::<String>("PATH");
996-
if let Some(path) = path {
997-
match dispatch::diff(Some(file_or_commit_id), path, is_remote).await {
998-
Ok(_) => {}
999-
Err(err) => {
1000-
eprintln!("{err}")
1001-
}
1002-
}
1003-
} else {
1004-
match dispatch::diff(None, file_or_commit_id, is_remote).await {
1005-
Ok(_) => {}
1006-
Err(err) => {
1007-
eprintln!("{err}")
1008-
}
1009-
}
1010-
}
1011-
}
995+
// pub async fn diff(sub_matches: &ArgMatches) {
996+
// let is_remote = false;
997+
// p_diff(sub_matches, is_remote).await
998+
// }
999+
1000+
// async fn p_diff(sub_matches: &ArgMatches, is_remote: bool) {
1001+
// // First arg is optional
1002+
// let file_or_commit_id = sub_matches
1003+
// .get_one::<String>("FILE_OR_REVISION")
1004+
// .expect("required");
1005+
// let path = sub_matches.get_one::<String>("PATH");
1006+
// if let Some(path) = path {
1007+
// match dispatch::diff(Some(file_or_commit_id), path, is_remote).await {
1008+
// Ok(_) => {}
1009+
// Err(err) => {
1010+
// eprintln!("{err}")
1011+
// }
1012+
// }
1013+
// } else {
1014+
// match dispatch::diff(None, file_or_commit_id, is_remote).await {
1015+
// Ok(_) => {}
1016+
// Err(err) => {
1017+
// eprintln!("{err}")
1018+
// }
1019+
// }
1020+
// }
1021+
// }
10121022

10131023
pub async fn clone(sub_matches: &ArgMatches) {
10141024
let url = sub_matches.get_one::<String>("URL").expect("required");

0 commit comments

Comments
 (0)