1+ pub mod vbranch {
2+ use crate :: command:: debug_print;
3+ use futures:: executor:: block_on;
4+ use gitbutler_branch:: { BranchCreateRequest , VirtualBranchesHandle } ;
5+ use gitbutler_branch_actions:: VirtualBranchActions ;
6+ use gitbutler_project:: Project ;
7+
8+ pub fn list ( project : Project ) -> anyhow:: Result < ( ) > {
9+ let branches = VirtualBranchesHandle :: new ( project. gb_dir ( ) ) . list_all_branches ( ) ?;
10+ for vbranch in branches {
11+ println ! (
12+ "{active} {id} {name} {upstream}" ,
13+ active = if vbranch. applied { "✔️" } else { "⛌" } ,
14+ id = vbranch. id,
15+ name = vbranch. name,
16+ upstream = vbranch
17+ . upstream
18+ . map_or_else( Default :: default , |b| b. to_string( ) )
19+ ) ;
20+ }
21+ Ok ( ( ) )
22+ }
23+
24+ pub fn create ( project : Project , branch_name : String ) -> anyhow:: Result < ( ) > {
25+ debug_print ( block_on ( VirtualBranchActions . create_virtual_branch (
26+ & project,
27+ & BranchCreateRequest {
28+ name : Some ( branch_name) ,
29+ ..Default :: default ( )
30+ } ,
31+ ) ) ?)
32+ }
33+ }
34+
35+ pub mod project {
36+ use crate :: command:: debug_print;
37+ use anyhow:: { Context , Result } ;
38+ use futures:: executor:: block_on;
39+ use gitbutler_branch_actions:: VirtualBranchActions ;
40+ use gitbutler_project:: Project ;
41+ use gitbutler_reference:: RemoteRefname ;
42+ use std:: path:: PathBuf ;
43+
44+ pub fn list ( ctrl : gitbutler_project:: Controller ) -> Result < ( ) > {
45+ for project in ctrl. list ( ) ? {
46+ println ! (
47+ "{id} {name} {path}" ,
48+ id = project. id,
49+ name = project. title,
50+ path = project. path. display( )
51+ ) ;
52+ }
53+ Ok ( ( ) )
54+ }
55+
56+ pub fn add (
57+ ctrl : gitbutler_project:: Controller ,
58+ path : PathBuf ,
59+ refname : Option < RemoteRefname > ,
60+ ) -> Result < ( ) > {
61+ let path = gix:: discover ( path) ?
62+ . work_dir ( )
63+ . context ( "Only non-bare repositories can be added" ) ?
64+ . to_owned ( )
65+ . canonicalize ( ) ?;
66+ let project = ctrl. add ( path) ?;
67+ if let Some ( refname) = refname {
68+ block_on ( VirtualBranchActions . set_base_branch ( & project, & refname) ) ?;
69+ } ;
70+ debug_print ( project)
71+ }
72+
73+ pub fn switch_to_integration ( project : Project , refname : RemoteRefname ) -> Result < ( ) > {
74+ debug_print ( block_on (
75+ VirtualBranchActions . set_base_branch ( & project, & refname) ,
76+ ) ?)
77+ }
78+ }
179pub mod snapshot {
280 use anyhow:: Result ;
381 use gitbutler_oplog:: OplogExt ;
@@ -21,3 +99,55 @@ pub mod snapshot {
2199 Ok ( ( ) )
22100 }
23101}
102+
103+ pub mod prepare {
104+ use anyhow:: { bail, Context } ;
105+ use gitbutler_project:: Project ;
106+ use std:: path:: PathBuf ;
107+
108+ pub fn project_from_path ( path : PathBuf ) -> anyhow:: Result < Project > {
109+ let worktree_dir = gix:: discover ( path) ?
110+ . work_dir ( )
111+ . context ( "Bare repositories aren't supported" ) ?
112+ . to_owned ( ) ;
113+ Ok ( Project {
114+ path : worktree_dir,
115+ ..Default :: default ( )
116+ } )
117+ }
118+
119+ pub fn project_controller (
120+ app_suffix : Option < String > ,
121+ app_data_dir : Option < PathBuf > ,
122+ ) -> anyhow:: Result < gitbutler_project:: Controller > {
123+ let path = if let Some ( dir) = app_data_dir {
124+ std:: fs:: create_dir_all ( & dir)
125+ . context ( "Failed to assure the designated data-dir exists" ) ?;
126+ dir
127+ } else {
128+ dirs_next:: data_dir ( )
129+ . map ( |dir| {
130+ dir. join ( format ! (
131+ "com.gitbutler.app{}" ,
132+ app_suffix
133+ . map( |mut suffix| {
134+ suffix. insert( 0 , '.' ) ;
135+ suffix
136+ } )
137+ . unwrap_or_default( )
138+ ) )
139+ } )
140+ . context ( "no data-directory available on this platform" ) ?
141+ } ;
142+ if !path. is_dir ( ) {
143+ bail ! ( "Path '{}' must be a valid directory" , path. display( ) ) ;
144+ }
145+ eprintln ! ( "Using projects from '{}'" , path. display( ) ) ;
146+ Ok ( gitbutler_project:: Controller :: from_path ( path) )
147+ }
148+ }
149+
150+ fn debug_print ( this : impl std:: fmt:: Debug ) -> anyhow:: Result < ( ) > {
151+ eprintln ! ( "{:#?}" , this) ;
152+ Ok ( ( ) )
153+ }
0 commit comments