Skip to content

Commit 28c9532

Browse files
authored
Merge pull request #9931 from gitbutlerapp/cli-add-project-command
feat(cli): add project command to but-testing
2 parents f2e4f89 + 9c54cd1 commit 28c9532

File tree

6 files changed

+67
-2
lines changed

6 files changed

+67
-2
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/but-testing/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ but-hunk-assignment.workspace = true
2929
gitbutler-branch-actions.workspace = true
3030
gitbutler-branch.workspace = true
3131
gitbutler-operating-modes.workspace = true
32+
gitbutler-reference.workspace = true
3233

3334
gitbutler-commit = { workspace = true, optional = true, features = ["testing"] }
3435

crates/but-testing/src/args.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use gitbutler_reference::RemoteRefname;
12
use gitbutler_stack::StackId;
23
use std::path::PathBuf;
34

@@ -35,6 +36,16 @@ pub struct Args {
3536

3637
#[derive(Debug, clap::Subcommand)]
3738
pub enum Subcommands {
39+
/// Add the given Git repository as project for use with GitButler.
40+
AddProject {
41+
/// The long name of the remote reference to track, like `refs/remotes/origin/main`,
42+
/// when switching to the workspace branch.
43+
#[clap(short = 's', long)]
44+
switch_to_workspace: Option<RemoteRefname>,
45+
/// The path at which the repository worktree is located.
46+
#[clap(default_value = ".", value_name = "REPOSITORY")]
47+
path: PathBuf,
48+
},
3849
/// Commit or amend all worktree changes to a new commit.
3950
Commit {
4051
/// The repo-relative path to the changed file to commit.

crates/but-testing/src/command/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ pub use commit::commit;
111111
use gitbutler_command_context::CommandContext;
112112

113113
pub mod diff;
114+
pub mod project;
114115

115116
pub mod assignment {
116117
use crate::command::{debug_print, project_from_path};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use std::path::PathBuf;
2+
3+
use anyhow::{Context, Result};
4+
use but_settings::AppSettings;
5+
use gitbutler_command_context::CommandContext;
6+
use gitbutler_reference::RemoteRefname;
7+
8+
use crate::command::debug_print;
9+
10+
pub fn add(data_dir: PathBuf, path: PathBuf, refname: Option<RemoteRefname>) -> Result<()> {
11+
let path = gix::discover(path)?
12+
.workdir()
13+
.context("Only non-bare repositories can be added")?
14+
.to_owned()
15+
.canonicalize()?;
16+
let project = gitbutler_project::add_with_path(data_dir, path, None, None)?;
17+
let ctx = CommandContext::open(&project, AppSettings::default())?;
18+
if let Some(refname) = refname {
19+
gitbutler_branch_actions::set_base_branch(
20+
&ctx,
21+
&refname,
22+
false,
23+
ctx.project().exclusive_worktree_access().write_permission(),
24+
)?;
25+
};
26+
debug_print(project)
27+
}

crates/but-testing/src/main.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! A debug-CLI for making `but`-crates functionality available in real-world repositories.
22
#![deny(rust_2018_idioms)]
3-
use std::str::FromStr;
3+
use std::{path::PathBuf, str::FromStr};
44

5-
use anyhow::{Result, bail};
5+
use anyhow::{Context, Result, bail};
66
use but_workspace::HunkHeader;
77
use command::parse_diff_spec;
88
use gix::bstr::BString;
@@ -24,6 +24,15 @@ async fn main() -> Result<()> {
2424
let _op_span = tracing::info_span!("cli-op").entered();
2525

2626
match &args.cmd {
27+
args::Subcommands::AddProject {
28+
switch_to_workspace,
29+
path,
30+
} => command::project::add(
31+
data_dir(args.app_data_dir)?,
32+
path.to_owned(),
33+
switch_to_workspace.to_owned(),
34+
),
35+
2736
args::Subcommands::RemoveReference {
2837
permit_empty_stacks,
2938
keep_metadata,
@@ -213,3 +222,18 @@ mod trace {
213222
Ok(())
214223
}
215224
}
225+
226+
pub fn data_dir(app_data_dir: Option<PathBuf>) -> anyhow::Result<PathBuf> {
227+
let path = if let Some(dir) = app_data_dir {
228+
std::fs::create_dir_all(&dir).context("Failed to assure the designated data-dir exists")?;
229+
dir
230+
} else {
231+
dirs_next::data_dir()
232+
.map(|dir| dir.join("com.gitbutler.app"))
233+
.context("no data-directory available on this platform")?
234+
};
235+
if !path.is_dir() {
236+
bail!("Path '{}' must be a valid directory", path.display());
237+
}
238+
Ok(path)
239+
}

0 commit comments

Comments
 (0)