Skip to content

Commit

Permalink
Merge pull request #60 from nikita-skobov/topbase-optional-remote-bra…
Browse files Browse the repository at this point in the history
…nch-cli

Topbase optional remote branch cli
  • Loading branch information
nikita-skobov authored Oct 18, 2020
2 parents 644562c + 7889fd8 commit d76d9e3
Show file tree
Hide file tree
Showing 15 changed files with 393 additions and 80 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ jobs:
with:
use-cross: ${{ matrix.job.usecross }}
command: test
args: --release --target=${{ matrix.job.target }}
args: --target=${{ matrix.job.target }}
- name: e2e_test_general
run: bash -c "bats test/general"
env:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build_test_no_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
with:
use-cross: ${{ matrix.job.usecross }}
command: test
args: --release --target=${{ matrix.job.target }}
args: --target=${{ matrix.job.target }}
- name: e2e_test_general
run: bash -c "bats test/general"
env:
Expand Down
13 changes: 7 additions & 6 deletions src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use super::exec_helpers;
use super::split::Runner;
use super::repo_file::RepoFile;
use super::split;
use super::die;
use super::commands::REPO_FILE_ARG;
use super::commands::LOCAL_ARG;
use super::commands::RECURSIVE_ARG;
Expand Down Expand Up @@ -43,15 +44,15 @@ impl<'a> CheckUpdates for Runner<'a> {
let repo = if let Some(ref r) = self.repo {
r
} else {
panic!("Failed to get repo");
die!("Failed to get repo");
};
// TODO: probably need to add blob_applies_to_repo_file here?
// I think in most cases this isnt necessary, but I should
// try to think of what edge cases this would be needed
let all_upstream_blobs = get_all_blobs_in_branch(upstream_branch);
let all_commits_of_current = match git_helpers::get_all_commits_from_ref(repo, current_branch) {
Ok(v) => v,
Err(e) => panic!("Failed to get all commits! {}", e),
Err(e) => die!("Failed to get all commits! {}", e),
};
// println!("GOT ALL UPSTREAM BLOBS: {}", all_upstream_blobs.len());
// println!("GOT ALL CURRENT COMMITS: {}", all_commits_of_current.len());
Expand Down Expand Up @@ -92,7 +93,7 @@ impl<'a> CheckUpdates for Runner<'a> {
// println!("Succesfully deleted FETCH_HEAD");
// println!("git prune successful? {}", tf);
// },
// Err(e) => panic!("Failed to delete FETCH_HEAD:\n{}", e),
// Err(e) => die!("Failed to delete FETCH_HEAD:\n{}", e),
// };
}

Expand Down Expand Up @@ -334,7 +335,7 @@ pub fn fetch_branch(remote: &str, branch: &str) {
},
};
if let Some(err) = err_msg {
panic!("Error fetching {} {}\n{}", remote, branch, err);
die!("Error fetching {} {}\n{}", remote, branch, err);
}
}

Expand Down Expand Up @@ -365,7 +366,7 @@ fn get_local_branch(runner: &Runner) -> String {
fn get_remote_branch(runner: &Runner) -> String {
let remote_repo = match runner.repo_file.remote_repo {
Some(ref s) => s,
None => panic!("repo file missing remote_repo"),
None => die!("repo file missing remote_repo"),
};
// check if user provided a --remote <branch>
let mut remote_branch = match runner.matches.value_of(REMOTE_BRANCH_ARG[0]) {
Expand Down Expand Up @@ -437,7 +438,7 @@ pub fn run_check(matches: &ArgMatches) {
let repo_files = get_all_repo_files(repo_file_path, should_recurse, any_extension);
files_to_check = match repo_files {
Ok(files) => files,
Err(e) => panic!("Failed to read repo file directory: {}", e),
Err(e) => die!("Failed to read repo file directory: {}", e),
};
}

Expand Down
20 changes: 14 additions & 6 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ const CHECK_CMD_DESCRIPTION: &'static str = "check if remote has commits not pre
const REPO_FILE_DESCRIPTION: &'static str = "path to file that contains instructions of how to split a repository";
const REPO_URI_DESCRIPTION: &'static str = "a valid git url of the repository to split in";
const AS_SUBDIR_DESCRIPTION: &'static str = "path relative to root of the local repository that will contain the entire repository being split";
const REBASE_DESCRIPTION: &'static str = "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, the comparison branch is the branch you started on. For split-out, the comparison branch is the remote branch";
const TOPBASE_DESCRIPTION: &'static str = "like rebase, but it finds a fork point to only take the top commits from the created branch that dont exist in your starting branch";
const REBASE_DESCRIPTION: &'static str = "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, the comparison branch is the branch you started on. For split-out, the comparison branch is the remote branch. By specifying a value for <rebase>, you can use a specific remote branch and override what is in your repo file.";
const TOPBASE_DESCRIPTION: &'static str = "like rebase, but it finds a fork point to only take the top commits from the created branch that dont exist in your starting branch. Optionally pass in the name of a remote branch to override what is in your repo file.";
const TOPBASE_TOP_DESCRIPTION: &'static str = "the branch that will be rebased. defaults to current branch";
const TOPBASE_BASE_DESCRIPTION: &'static str = "the branch to rebase onto.";
const LOCAL_ARG_DESCRIPTION: &'static str = "check if the local branch has commits not present in remote";
Expand Down Expand Up @@ -132,15 +132,19 @@ fn base_command<'a, 'b>(cmd: CommandName) -> App<'a, 'b> {
Arg::with_name(REBASE_ARG[0])
.long(REBASE_ARG[0])
.short(REBASE_ARG[1])
.takes_value(true)
.default_value("")
.hide_default_value(true)
.help(REBASE_DESCRIPTION)
.conflicts_with(TOPBASE_ARG[0])
)
.arg(
Arg::with_name(TOPBASE_ARG[0])
.long(TOPBASE_ARG[0])
.short(TOPBASE_ARG[1])
.takes_value(true)
.default_value("")
.hide_default_value(true)
.help(TOPBASE_DESCRIPTION)
.conflicts_with(REBASE_ARG[0])
)
.arg(
Arg::with_name(OUTPUT_BRANCH_ARG[0])
Expand Down Expand Up @@ -180,15 +184,18 @@ pub fn split_in_as<'a, 'b>() -> App<'a, 'b> {
.long(REBASE_ARG[0])
.short(REBASE_ARG[1])
.help(REBASE_DESCRIPTION)
.conflicts_with(TOPBASE_ARG[0])
.takes_value(true)
.default_value("")
)
// TODO: should remove topbase from split-in-as? i dont think it makes sense
.arg(
Arg::with_name(TOPBASE_ARG[0])
.long(TOPBASE_ARG[0])
.short(TOPBASE_ARG[1])
.takes_value(true)
.default_value("")
.help(TOPBASE_DESCRIPTION)
.conflicts_with(REBASE_ARG[0])
.hide_default_value(true)
)
.arg(
Arg::with_name(AS_SUBDIR_ARG)
Expand All @@ -197,6 +204,7 @@ pub fn split_in_as<'a, 'b>() -> App<'a, 'b> {
.value_name(AS_SUBDIR_ARG_NAME)
.required(true)
.takes_value(true)
.hide_default_value(true)
)
.arg(
Arg::with_name(DRY_RUN_ARG[0])
Expand Down
13 changes: 7 additions & 6 deletions src/git_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::path::PathBuf;
use std::path::Path;
use std::fs;
use std::str::from_utf8;
use super::die;

pub trait Short {
fn short(self) -> String;
Expand Down Expand Up @@ -34,7 +35,7 @@ fn remove_git_from_path_buf(pathbuf: &mut PathBuf) -> PathBuf {

pub fn get_repository_and_root_directory(dir: &PathBuf) -> (Repository, PathBuf) {
let repo = match Repository::discover(dir) {
Err(e) => panic!("Failed to find or open repository from {} - {}", dir.display(), e),
Err(e) => die!("Failed to find or open repository from {} - {}", dir.display(), e),
Ok(repo) => repo,
};

Expand Down Expand Up @@ -177,7 +178,7 @@ pub fn list_everything_under_tree(
git2::ObjectType::Blob => {
let blob = match t_obj.into_blob() {
Ok(b) => b,
_ => panic!("failed to turn into blob"),
_ => die!("failed to turn into blob"),
};
println!("{} B:{}", indent, blob.id());
}
Expand All @@ -186,7 +187,7 @@ pub fn list_everything_under_tree(
git2::ObjectType::Tree => {
let t_next = match t_obj.into_tree() {
Ok(tn) => tn,
_ => panic!("failed to turn into tree"),
_ => die!("failed to turn into tree"),
};
let next_indent = format!("{} ", indent);
list_everything_under_tree(repo, t_next, next_indent.as_str())?;
Expand Down Expand Up @@ -369,7 +370,7 @@ pub fn remove_index_and_files(
if ! file_removed {
let result = fs::remove_file(f);
if result.is_err() {
panic!("Failed to remove file {}. Stopping operation without modifying index", f.display());
die!("Failed to remove file {}. Stopping operation without modifying index", f.display());
}
file_removed = true;
} else {
Expand Down Expand Up @@ -444,7 +445,7 @@ pub fn merge<'a>(
}
}
} else {
panic!("cannot fast-forward. Alternate merge strategies not implements yet");
die!("cannot fast-forward. Alternate merge strategies not implements yet");
}

Ok(())
Expand Down Expand Up @@ -472,7 +473,7 @@ pub fn pull(
repo,
&[remote_branch],
&mut remote,
).unwrap();
)?;
merge(repo, fetched_commit, None)
}

Expand Down
29 changes: 29 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,35 @@ fn get_cli_input<'a>() -> ArgMatches<'a> {
return base_app.get_matches();
}

// in debug mode, use panic so we get a stack trace
#[cfg(debug_assertions)]
#[macro_export]
macro_rules! die {
() => (::std::process::exit(1));
($x:expr; $($y:expr),+) => ({
panic!($($y),+);
});
($($y:expr),+) => ({
panic!($($y),+);
});
}

// in release mode, use print so its not ugly
#[cfg(not(debug_assertions))]
#[macro_export]
macro_rules! die {
() => (::std::process::exit(1));
($x:expr; $($y:expr),+) => ({
println!($($y),+);
::std::process::exit($x)
});
($($y:expr),+) => ({
println!($($y),+);
::std::process::exit(1)
});
}


fn main() {
let matches = get_cli_input();

Expand Down
11 changes: 6 additions & 5 deletions src/repo_file.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::path::Path;
use std::fs::File;
use std::io::{BufRead, BufReader};
use super::die;

#[derive(Debug, PartialEq)]
pub struct RepoFile {
Expand Down Expand Up @@ -245,16 +246,16 @@ fn parse_variable(variable: &mut RepoFileVariable, text: &String, line_num: usiz
}

if variable.name == VarUnknown {
panic!("Invalid variable name found on line {}:\n\"{}\"", line_num, text);
die!("Invalid variable name found on line {}:\n\"{}\"", line_num, text);
}

if variable.var_type == TypeUnknown {
panic!("Failed to parse line {}:\n\"{}\"", line_num, text);
die!("Failed to parse line {}:\n\"{}\"", line_num, text);
}

let strings = get_all_strings(&text);
if let None = strings {
panic!("Failed to parse variable at line {}:\n\"{}\"", line_num, text);
die!("Failed to parse variable at line {}:\n\"{}\"", line_num, text);
}

match variable.var_type {
Expand Down Expand Up @@ -315,12 +316,12 @@ fn should_parse_line(text: &String) -> bool {
pub fn parse_repo_file(filename: &str) -> RepoFile {
let repo_file_path = Path::new(filename);
if !repo_file_path.exists() {
panic!("Failed to find repo_file: {}", filename);
die!("Failed to find repo_file: {}", filename);
}

let file = File::open(repo_file_path);
if let Err(file_error) = file {
panic!("Failed to open file: {}, {}", filename, file_error);
die!("Failed to open file: {}, {}", filename, file_error);
}

let file_contents = file.unwrap();
Expand Down
Loading

0 comments on commit d76d9e3

Please sign in to comment.