Skip to content

fix!: Rename git-branch-backup to git-branch-stash #79

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

Merged
merged 1 commit into from
Oct 8, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,65 +21,65 @@ pub struct Args {

#[derive(structopt::StructOpt)]
pub enum Subcommand {
/// Backup all branches
/// Stash all branches
Push(PushArgs),
/// List all backups
/// List all stashed snapshots
List(ListArgs),
/// Clear all backups
/// Clear all snapshots
Clear(ClearArgs),
/// Delete the last backup
/// Delete the last snapshot
Drop(DropArgs),
/// Apply the last backup, deleting it
/// Apply the last snapshot, deleting it
Pop(PopArgs),
/// Apply the last backup
/// Apply the last snapshot
Apply(ApplyArgs),
/// List all backup stacks
/// List all snapshot stacks
Stacks(StacksArgs),
}

#[derive(structopt::StructOpt)]
pub struct PushArgs {
/// Specify which backup stack to use
#[structopt(default_value = git_stack::backup::Stack::DEFAULT_STACK)]
/// Specify which stash stack to use
#[structopt(default_value = git_stack::stash::Stack::DEFAULT_STACK)]
pub stack: String,

/// Annotate the backup with the given message
/// Annotate the snapshot with the given message
#[structopt(short, long)]
pub message: Option<String>,
}

#[derive(structopt::StructOpt)]
pub struct ListArgs {
/// Specify which backup stack to use
#[structopt(default_value = git_stack::backup::Stack::DEFAULT_STACK)]
/// Specify which stash stack to use
#[structopt(default_value = git_stack::stash::Stack::DEFAULT_STACK)]
pub stack: String,
}

#[derive(structopt::StructOpt)]
pub struct ClearArgs {
/// Specify which backup stack to use
#[structopt(default_value = git_stack::backup::Stack::DEFAULT_STACK)]
/// Specify which stash stack to use
#[structopt(default_value = git_stack::stash::Stack::DEFAULT_STACK)]
pub stack: String,
}

#[derive(structopt::StructOpt)]
pub struct DropArgs {
/// Specify which backup stack to use
#[structopt(default_value = git_stack::backup::Stack::DEFAULT_STACK)]
/// Specify which stash stack to use
#[structopt(default_value = git_stack::stash::Stack::DEFAULT_STACK)]
pub stack: String,
}

#[derive(structopt::StructOpt)]
pub struct PopArgs {
/// Specify which backup stack to use
#[structopt(default_value = git_stack::backup::Stack::DEFAULT_STACK)]
/// Specify which stash stack to use
#[structopt(default_value = git_stack::stash::Stack::DEFAULT_STACK)]
pub stack: String,
}

#[derive(structopt::StructOpt)]
pub struct ApplyArgs {
/// Specify which backup stack to use
#[structopt(default_value = git_stack::backup::Stack::DEFAULT_STACK)]
/// Specify which stash stack to use
#[structopt(default_value = git_stack::stash::Stack::DEFAULT_STACK)]
pub stack: String,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn push(args: args::PushArgs) -> proc_exit::ExitResult {
let cwd = std::env::current_dir().with_code(proc_exit::Code::USAGE_ERR)?;
let repo = git2::Repository::discover(&cwd).with_code(proc_exit::Code::USAGE_ERR)?;
let repo = git_stack::git::GitRepo::new(repo);
let mut stack = git_stack::backup::Stack::new(&args.stack, &repo);
let mut stack = git_stack::stash::Stack::new(&args.stack, &repo);

let repo_config = git_stack::config::RepoConfig::from_all(repo.raw())
.with_code(proc_exit::Code::CONFIG_ERR)?;
Expand All @@ -66,13 +66,13 @@ fn push(args: args::PushArgs) -> proc_exit::ExitResult {
log::warn!("Working tree is dirty, only capturing committed changes");
}

let mut backup =
git_stack::backup::Backup::from_repo(&repo).with_code(proc_exit::Code::FAILURE)?;
let mut snapshot =
git_stack::stash::Snapshot::from_repo(&repo).with_code(proc_exit::Code::FAILURE)?;
if let Some(message) = args.message.as_deref() {
backup.insert_message(message);
snapshot.insert_message(message);
}
backup.insert_parent(&repo, &branches, &protected_branches);
stack.push(backup)?;
snapshot.insert_parent(&repo, &branches, &protected_branches);
stack.push(snapshot)?;

Ok(())
}
Expand All @@ -87,23 +87,27 @@ fn list(args: args::ListArgs, colored: bool) -> proc_exit::ExitResult {
let cwd = std::env::current_dir().with_code(proc_exit::Code::USAGE_ERR)?;
let repo = git2::Repository::discover(&cwd).with_code(proc_exit::Code::USAGE_ERR)?;
let repo = git_stack::git::GitRepo::new(repo);
let stack = git_stack::backup::Stack::new(&args.stack, &repo);
let stack = git_stack::stash::Stack::new(&args.stack, &repo);

let backups: Vec<_> = stack.iter().collect();
for (i, backup_path) in backups.iter().enumerate() {
let style = if i < backups.len() - 1 {
let snapshots: Vec<_> = stack.iter().collect();
for (i, snapshot_path) in snapshots.iter().enumerate() {
let style = if i < snapshots.len() - 1 {
palette.info
} else {
palette.good
};
let backup = match git_stack::backup::Backup::load(backup_path) {
Ok(backup) => backup,
let snapshot = match git_stack::stash::Snapshot::load(snapshot_path) {
Ok(snapshot) => snapshot,
Err(err) => {
log::error!("Failed to load backup {}: {}", backup_path.display(), err);
log::error!(
"Failed to load snapshot {}: {}",
snapshot_path.display(),
err
);
continue;
}
};
match backup.metadata.get("message") {
match snapshot.metadata.get("message") {
Some(message) => {
writeln!(
std::io::stdout(),
Expand All @@ -115,11 +119,11 @@ fn list(args: args::ListArgs, colored: bool) -> proc_exit::ExitResult {
writeln!(
std::io::stdout(),
"{}",
style.paint(format_args!("Path: {}", backup_path.display()))
style.paint(format_args!("Path: {}", snapshot_path.display()))
)?;
}
}
for branch in backup.branches.iter() {
for branch in snapshot.branches.iter() {
let summary = if let Some(summary) = branch.metadata.get("summary") {
summary.to_string()
} else {
Expand Down Expand Up @@ -177,7 +181,7 @@ fn clear(args: args::ClearArgs) -> proc_exit::ExitResult {
let cwd = std::env::current_dir().with_code(proc_exit::Code::USAGE_ERR)?;
let repo = git2::Repository::discover(&cwd).with_code(proc_exit::Code::USAGE_ERR)?;
let repo = git_stack::git::GitRepo::new(repo);
let mut stack = git_stack::backup::Stack::new(&args.stack, &repo);
let mut stack = git_stack::stash::Stack::new(&args.stack, &repo);

stack.clear();

Expand All @@ -188,7 +192,7 @@ fn drop(args: args::DropArgs) -> proc_exit::ExitResult {
let cwd = std::env::current_dir().with_code(proc_exit::Code::USAGE_ERR)?;
let repo = git2::Repository::discover(&cwd).with_code(proc_exit::Code::USAGE_ERR)?;
let repo = git_stack::git::GitRepo::new(repo);
let mut stack = git_stack::backup::Stack::new(&args.stack, &repo);
let mut stack = git_stack::stash::Stack::new(&args.stack, &repo);

stack.pop();

Expand All @@ -199,17 +203,17 @@ fn pop(args: args::PopArgs) -> proc_exit::ExitResult {
let cwd = std::env::current_dir().with_code(proc_exit::Code::USAGE_ERR)?;
let repo = git2::Repository::discover(&cwd).with_code(proc_exit::Code::USAGE_ERR)?;
let mut repo = git_stack::git::GitRepo::new(repo);
let mut stack = git_stack::backup::Stack::new(&args.stack, &repo);
let mut stack = git_stack::stash::Stack::new(&args.stack, &repo);

if repo.is_dirty() {
return Err(proc_exit::Code::USAGE_ERR.with_message("Working tree is dirty, aborting"));
}

match stack.peek() {
Some(last) => {
let backup =
git_stack::backup::Backup::load(&last).with_code(proc_exit::Code::FAILURE)?;
backup
let snapshot =
git_stack::stash::Snapshot::load(&last).with_code(proc_exit::Code::FAILURE)?;
snapshot
.apply(&mut repo)
.with_code(proc_exit::Code::FAILURE)?;
let _ = std::fs::remove_file(&last);
Expand All @@ -226,17 +230,17 @@ fn apply(args: args::ApplyArgs) -> proc_exit::ExitResult {
let cwd = std::env::current_dir().with_code(proc_exit::Code::USAGE_ERR)?;
let repo = git2::Repository::discover(&cwd).with_code(proc_exit::Code::USAGE_ERR)?;
let mut repo = git_stack::git::GitRepo::new(repo);
let mut stack = git_stack::backup::Stack::new(&args.stack, &repo);
let mut stack = git_stack::stash::Stack::new(&args.stack, &repo);

if repo.is_dirty() {
return Err(proc_exit::Code::USAGE_ERR.with_message("Working tree is dirty, aborting"));
}

match stack.peek() {
Some(last) => {
let backup =
git_stack::backup::Backup::load(&last).with_code(proc_exit::Code::FAILURE)?;
backup
let snapshot =
git_stack::stash::Snapshot::load(&last).with_code(proc_exit::Code::FAILURE)?;
snapshot
.apply(&mut repo)
.with_code(proc_exit::Code::FAILURE)?;
}
Expand All @@ -253,7 +257,7 @@ fn stacks(_args: args::StacksArgs) -> proc_exit::ExitResult {
let repo = git2::Repository::discover(&cwd).with_code(proc_exit::Code::USAGE_ERR)?;
let repo = git_stack::git::GitRepo::new(repo);

for stack in git_stack::backup::Stack::all(&repo) {
for stack in git_stack::stash::Stack::all(&repo) {
writeln!(std::io::stdout(), "{}", stack.name)?;
}

Expand Down
20 changes: 10 additions & 10 deletions src/bin/git-stack/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct State {
pull: bool,
push: bool,
dry_run: bool,
backup_capacity: Option<usize>,
snapshot_capacity: Option<usize>,

show_format: git_stack::config::Format,
show_stacked: bool,
Expand All @@ -44,7 +44,7 @@ impl State {
)
.with_code(proc_exit::Code::CONFIG_ERR)?;
let dry_run = args.dry_run;
let backup_capacity = repo_config.capacity();
let snapshot_capacity = repo_config.capacity();

let show_format = repo_config.show_format();
let show_stacked = repo_config.show_stacked();
Expand Down Expand Up @@ -156,7 +156,7 @@ impl State {
pull,
push,
dry_run,
backup_capacity,
snapshot_capacity,

show_format,
show_stacked,
Expand Down Expand Up @@ -272,21 +272,21 @@ pub fn stack(args: &crate::args::Args, colored_stdout: bool) -> proc_exit::ExitR
}
}

const BACKUP_NAME: &str = "git-stack";
const STASH_STACK_NAME: &str = "git-stack";
let mut success = true;
let mut backed_up = false;
if state.rebase {
if state.repo.is_dirty() {
return Err(proc_exit::Code::USAGE_ERR.with_message("Working tree is dirty, aborting"));
}

let mut backups = git_stack::backup::Stack::new(BACKUP_NAME, &state.repo);
backups.capacity(state.backup_capacity);
let mut backup = git_stack::backup::Backup::from_repo(&state.repo)
let mut snapshots = git_stack::stash::Stack::new(STASH_STACK_NAME, &state.repo);
snapshots.capacity(state.snapshot_capacity);
let mut snapshot = git_stack::stash::Snapshot::from_repo(&state.repo)
.with_code(proc_exit::Code::FAILURE)?;
backup.insert_parent(&state.repo, &state.branches, &state.protected_branches);
snapshot.insert_parent(&state.repo, &state.branches, &state.protected_branches);
if !state.dry_run {
backups.push(backup)?;
snapshots.push(snapshot)?;
backed_up = true;
}

Expand Down Expand Up @@ -328,7 +328,7 @@ pub fn stack(args: &crate::args::Args, colored_stdout: bool) -> proc_exit::ExitR
show(&state, colored_stdout).with_code(proc_exit::Code::FAILURE)?;

if backed_up {
log::info!("To undo, run `git branch-backup pop {}`", BACKUP_NAME);
log::info!("To undo, run `git branch-stash pop {}`", STASH_STACK_NAME);
}

if !success {
Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ static PUSH_REMOTE_FIELD: &str = "stack.push-remote";
static PULL_REMOTE_FIELD: &str = "stack.pull-remote";
static FORMAT_FIELD: &str = "stack.show-format";
static STACKED_FIELD: &str = "stack.show-stacked";
static BACKUP_CAPACITY_FIELD: &str = "branch-backup.capacity";
static BACKUP_CAPACITY_FIELD: &str = "branch-stash.capacity";

static DEFAULT_PROTECTED_BRANCHES: [&str; 4] = ["main", "master", "dev", "stable"];
const DEFAULT_CAPACITY: usize = 30;
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#[macro_use]
extern crate clap;

pub mod backup;
pub mod config;
pub mod git;
pub mod graph;
pub mod log;
pub mod stash;
4 changes: 2 additions & 2 deletions src/backup/mod.rs → src/stash/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[allow(clippy::module_inception)]
mod backup;
mod snapshot;
mod stack;

pub use backup::*;
pub use snapshot::*;
pub use stack::*;
4 changes: 2 additions & 2 deletions src/backup/backup.rs → src/stash/snapshot.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct Backup {
pub struct Snapshot {
pub branches: Vec<Branch>,
#[serde(default)]
#[serde(skip_serializing_if = "std::collections::BTreeMap::is_empty")]
pub metadata: std::collections::BTreeMap<String, serde_json::Value>,
}

impl Backup {
impl Snapshot {
pub fn load(path: &std::path::Path) -> Result<Self, std::io::Error> {
let file = std::fs::File::open(path)?;
let reader = std::io::BufReader::new(file);
Expand Down
Loading