Skip to content

Commit

Permalink
Run remote prune origin (#12)
Browse files Browse the repository at this point in the history
Feat: Implement remote connection and pruning
Feat: Allow passing in the root of the repository
Chore: Remote rust gh action workflow
Docs: Update readme with  output
  • Loading branch information
graysonarts authored Jul 19, 2024
1 parent db30ed2 commit 2e2d6ec
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 25 deletions.
22 changes: 0 additions & 22 deletions .github/workflows/rust.yml

This file was deleted.

3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ This is a dry run that prints out which branches will be deleted.
```

Delete the branches

```
> git superprune -h
40 changes: 37 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::sync::atomic::AtomicU8;
use std::{env, sync::atomic::AtomicU8};

use branch::BranchList;
use clap::Parser;
use color_eyre::Result;
use const_format::formatcp;
use git2::Repository;
use git2::{Cred, Repository};
use git_version::git_version;
use tracing::{debug, info};

Expand All @@ -29,9 +29,21 @@ struct Args {
/// Display verbose output
verbose: bool,

/// run `git remote prune <upstream>` before scanning
#[clap(short, long, required = false)]
upstream: Option<String>,

/// SSH key in `~/.ssh/`` to use for authentication with remote, defaults to `id_rsa`
#[clap(short, long, required = false)]
ssh_key: Option<String>,

#[clap(short('x'), long, default_value = "false")]
/// Execute the branch deletion
execute: bool,

/// Root directory of the git repository
#[clap(required = false)]
root: Option<String>,
}

fn main() -> Result<()> {
Expand All @@ -51,12 +63,34 @@ fn main() -> Result<()> {
.event_format(format)
.init();

let repository = Repository::discover(args.root.as_deref().unwrap_or("."))?;

if let Some(upstream) = &args.upstream {
debug!("Finding upstream {upstream}");
let mut callbacks = git2::RemoteCallbacks::new();
callbacks.credentials(|_url, username_from_url, _allowed_types| {
let key_name = args.ssh_key.as_deref().unwrap_or("id_rsa");
Cred::ssh_key(
username_from_url.unwrap(),
None,
std::path::Path::new(&format!("{}/.ssh/{key_name}", env::var("HOME").unwrap())),
None,
)
});
let mut remote = repository.find_remote(upstream)?;
remote.connect_auth(git2::Direction::Fetch, Some(callbacks), None)?;
debug!(
"Pruning remote {upstream} {}",
remote.url().unwrap_or_default()
);
remote.prune(None)?;
}

debug!("Scanning Repository");

let dry_run = !args.execute;
let deleted = AtomicU8::new(0);

let repository = Repository::discover(".")?;
let branches = repository
.get_branches()
.into_iter()
Expand Down

0 comments on commit 2e2d6ec

Please sign in to comment.