Skip to content

Commit

Permalink
Merge pull request #81 from nikita-skobov/better-cli-usage
Browse files Browse the repository at this point in the history
Better cli usage
  • Loading branch information
nikita-skobov authored Dec 26, 2020
2 parents 77e16e3 + f2ef7a7 commit b8aca6a
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 85 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ edition = "2018"
toml = { version = "0.5.7", features = ["preserve_order"] }
git-url-parse = "0.3.0"
die = { path="die" }
gumdrop = { git = "https://github.com/nikita-skobov/gumdrop" }
gumdrop = { git = "https://github.com/nikita-skobov/gumdrop", rev = "f29c1f9839329cf5998257e097fc18930255627e" }

[features]
gittests = []
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ Next we will run `mgt split-in` to take the remote repository
defined in the above file, fetch it, and rewrite the paths to match our rules according to the `[include_as]` section:

```sh
mgt split-in meta.rf -r --num-commits 1
mgt split-in meta.rf --rebase --num-commits 1
# output:
Pulling from https://github.com/nikita-skobov/monorepo-git-tools
Running filter commands on temporary branch: monorepo-git-tools
Rebasing
Success!
```

We also passed 2 arguments: `-r` (a short flag for rebase) will automatically rebase the temporary created branch onto our current branch for us, and `--num-commits 1` will only fetch 1 commit from the latest HEAD of the remote repository.
We also passed 2 arguments: `--rebase` will automatically rebase the temporary created branch onto our current branch for us, and `--num-commits 1` will only fetch 1 commit from the latest HEAD of the remote repository.

After running the above, we will be in a branch called `monorepo-git-tools` that was created for us, and then rebased such that it can now be fast forwared into master. Let's now merge into master, and then delete the temporary branch:

Expand Down
78 changes: 2 additions & 76 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// use std::str::FromStr;
use gumdrop::Options;

use die::die;
Expand All @@ -9,29 +8,6 @@ use super::split_in::run_split_in;
use super::split_in::run_split_in_as;
use super::topbase::run_topbase;

// TODO: implement way to use
// --rebase
// --rebase branchname
// --rebase --other-args
// ...
// #[derive(Debug)]
// pub struct OptionalOption<T: From<String>> {
// val: Option<T>,
// }

// impl<T: From<String>> FromStr for OptionalOption<T> {
// type Err = String;

// fn from_str(s: &str) -> Result<Self, Self::Err> {
// let s_string = s.to_string();
// let o = OptionalOption {
// val: Some(s_string.into())
// };

// Ok(o)
// }
// }

#[derive(Debug, Options)]
pub struct MgtCommandCheck {
// flags
Expand Down Expand Up @@ -87,14 +63,10 @@ pub struct MgtCommandSplit {
#[options(short = "o", help = "name of branch that will be created with new split history")]
pub output_branch: Option<String>,

#[options(no_long, short = "r", help = "after generating a branch with rewritten history, rebase that branch such that it can be fast forwarded back into the comparison branch. for split-in that is the branch you started on. For split-out, that is the remote branch")]
pub rebase_flag: bool,
#[options(meta = "BRANCH-NAME", help = "like the -r flag, but you can specify the name of the branch you want to use as the comparison branch instead of using the default")]
#[options(optional, short = "r", help = "after generating a branch with rewritten history, rebase that branch such that it can be fast forwarded back into the comparison branch. for split-in that is the branch you started on. For split-out, that is the remote branch. Optionally provide a '--rebase BRANCH-NAME' to rebase onto that branch instead of the default.")]
pub rebase: Option<String>,

#[options(no_long, short = "t", help = "like rebase, but it finds a fork point by stopping at the first commit that two branches have in common. This is useful as an 'update' mechanism.")]
pub topbase_flag: bool,
#[options(meta = "BRANCH-NAME", help = "like the -t flag, but you can specify the name of the remote branch that will be used instead of what is defined in your repo file")]
#[options(optional, short = "t", help = "like --rebase, but it finds a fork point by stopping at the first commit that two branches have in common. This is useful as an 'update' mechanism. Optionally provide a '--topbase BRANCH-NAME' to topbase onto that branch instead of the default.")]
pub topbase: Option<String>,

#[options(long = "as", help = "path relative to root of the local repository that will contain the entire repository being split")]
Expand Down Expand Up @@ -279,34 +251,6 @@ impl Mgt {
}
}

// TODO: use optional args
// pub fn get_cli_input_with_retries(args: Option<Vec<String>>) -> Result<Mgt, gumdrop::Error> {
// let mut args = match args {
// Some(v) => v,
// None => ::std::env::args().collect::<Vec<_>>(),
// };

// match <Mgt as Options>::parse_args_default(&args[1..]) {
// Err(e) => {
// // if its a missing argument, see if its something we can recover
// // by checking if it can be an optional option
// if let gumdrop::ErrorKind::MissingArgument(ref s) = e.kind {
// match s.as_str() {
// "-r" | "--rebase" => {
// let arg_pos = args.iter().position(|a| a == s).unwrap();
// args.insert(arg_pos + 1, "".into());
// get_cli_input_with_retries(Some(args))
// },
// _ => Err(e),
// }
// } else {
// Err(e)
// }
// }
// Ok(m) => Ok(m),
// }
// }

pub fn get_cli_input() -> Mgt {
let args = ::std::env::args().collect::<Vec<_>>();
let cli = match <Mgt as Options>::parse_args_default(&args[1..]) {
Expand Down Expand Up @@ -410,12 +354,6 @@ pub fn validate_input_and_run(mgt_opts: Mgt) {
cmd.dry_run = mgt_opts.dry_run || cmd.dry_run;
cmd.direction = Some(Direction::In);

if cmd.topbase_flag && cmd.topbase.is_none() {
cmd.topbase = Some("".into());
}
if cmd.rebase_flag && cmd.rebase.is_none() {
cmd.rebase = Some("".into())
}
if cmd.rebase.is_some() && cmd.topbase.is_some() {
die!("Cannot use both --topbase and --rebase");
}
Expand All @@ -427,12 +365,6 @@ pub fn validate_input_and_run(mgt_opts: Mgt) {
cmd.dry_run = mgt_opts.dry_run || cmd.dry_run;
cmd.direction = Some(Direction::In);

if cmd.topbase_flag && cmd.topbase.is_none() {
cmd.topbase = Some("".into());
}
if cmd.rebase_flag && cmd.rebase.is_none() {
cmd.rebase = Some("".into())
}
if cmd.rebase.is_some() && cmd.topbase.is_some() {
die!("Cannot use both --topbase and --rebase");
}
Expand All @@ -445,12 +377,6 @@ pub fn validate_input_and_run(mgt_opts: Mgt) {
cmd.dry_run = mgt_opts.dry_run || cmd.dry_run;
cmd.direction = Some(Direction::Out);

if cmd.topbase_flag && cmd.topbase.is_none() {
cmd.topbase = Some("".into());
}
if cmd.rebase_flag && cmd.rebase.is_none() {
cmd.rebase = Some("".into())
}
if cmd.rebase.is_some() && cmd.topbase.is_some() {
die!("Cannot use both --topbase and --rebase");
}
Expand Down
6 changes: 3 additions & 3 deletions test/splitin/end-to-end.bats
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ function teardown() {
# this is the conflict:
echo "conflicthere" > lib/abc.txt && git add lib/abc.txt && git commit -m "conflict"

run $PROGRAM_PATH split-in repo_file.sh --rebase --verbose
run $PROGRAM_PATH split-in repo_file.sh -r --verbose
echo "$output"
echo "$(git status)"
[[ $status != "0" ]]
Expand Down Expand Up @@ -621,7 +621,7 @@ function teardown() {
# this is the conflict:
echo "conflicthere" > lib/abc.txt && git add lib/abc.txt && git commit -m "conflict"

run $PROGRAM_PATH split-in repo_file.sh --topbase --verbose
run $PROGRAM_PATH split-in repo_file.sh -t --verbose
echo "$output"
echo "$(git status)"
[[ $status != "0" ]]
Expand Down Expand Up @@ -704,7 +704,7 @@ function teardown() {
mkdir -p lib
echo "conflict" > lib/libfile1.txt && git add lib/libfile1.txt && git commit -m "conflict"

run $PROGRAM_PATH split-in repo_file.sh --topbase --verbose
run $PROGRAM_PATH split-in repo_file.sh -t --verbose
echo "$output"
echo "$(git status)"
[[ $status != "0" ]]
Expand Down
6 changes: 3 additions & 3 deletions test/splitout/end-to-end.bats
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ function teardown() {
echo "test" > lib/libfile1.txt && git add lib/libfile1.txt && git commit -m "libfile1mod"
cd "$curr_dir"

run $PROGRAM_PATH split-out repo_file.sh --topbase --verbose
run $PROGRAM_PATH split-out repo_file.sh -t --verbose
echo "$output"
echo "$(git status)"
[[ $output != *"Success"* ]]
Expand All @@ -858,7 +858,7 @@ function teardown() {
echo "conffflict" > lib/libfile1.txt && git add lib/libfile1.txt && git commit -m "where it conflicts"
cd "$curr_dir"

run $PROGRAM_PATH split-out repo_file.sh --topbase --verbose
run $PROGRAM_PATH split-out repo_file.sh -t --verbose
echo "$output"
echo "$(git status)"
[[ $output != *"Success"* ]]
Expand Down Expand Up @@ -932,7 +932,7 @@ function teardown() {
echo "conffflict" > lib/libfile1.txt && git add lib/libfile1.txt && git commit -m "where it conflicts"
cd "$curr_dir"

run $PROGRAM_PATH split-out repo_file.sh --rebase --verbose
run $PROGRAM_PATH split-out repo_file.sh -r --verbose
echo "$output"
echo "$(git status)"
[[ $output != *"Success"* ]]
Expand Down

0 comments on commit b8aca6a

Please sign in to comment.