This repository was archived by the owner on Aug 11, 2025. It is now read-only.
File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1- use anyhow:: { Context , Result } ;
2- use clap:: { Command , CommandFactory , Parser , Subcommand } ;
3- use colored:: * ;
4- use std:: io:: Write ;
5- use thiserror:: Error ;
1+ use clap:: Parser ;
62
73/// A CLI to streamline your Git workflow for Trunk-Based Development
84#[ derive( Parser , Debug ) ]
95#[ command( author, version, about, long_about = None ) ]
106#[ command( propagate_version = true ) ]
11- struct Cli {
7+ pub struct Cli {
128 #[ command( subcommand) ]
13- command : Commands ,
9+ pub command : Commands ,
1410}
1511
1612#[ derive( clap:: Subcommand , Debug ) ]
17- enum Commands {
13+ pub ( crate ) enum Commands {
1814 /// Show the current Git status
1915 Status ,
2016 /// Commits changes to the current branch or 'main' if no branch is checked out.
Original file line number Diff line number Diff line change 11use std:: process:: { Command , Stdio } ;
22use thiserror:: Error ;
33use anyhow:: { Context , Result } ;
4- use colored:: Colorize ;
5- use crate :: git;
64
75// Using `thiserror` to create a structured error type.
86#[ derive( Error , Debug ) ]
97pub enum GitError {
108 #[ error( "Git command failed: {0}" ) ]
119 Git ( String ) ,
12- #[ error( "Working directory is not clean: {0}" ) ]
13- DirectoryNotClean ( String ) ,
1410}
1511
1612/// Runs a Git command with the specified subcommand and arguments.
@@ -31,16 +27,9 @@ fn run_git_command(command: &str, args: &[&str]) -> Result<String> {
3127 }
3228}
3329
34- /// Checks if the git working directory is clean.
35- pub fn is_working_directory_clean ( ) -> Result < ( ) > {
36- let output = run_git_command ( "status" , & [ "--porcelain" ] ) ?;
37- if output. is_empty ( ) {
38- Ok ( ( ) )
39- } else {
40- Err ( GitError :: DirectoryNotClean (
41- "You have unstaged changes. Please commit or stash them first." . to_string ( )
42- ) . into ( ) )
43- }
30+ /// Show the current status of the repository.
31+ pub fn status ( ) -> Result < String > {
32+ run_git_command ( "status" , & [ "--short" ] )
4433}
4534
4635/// Pull the latest changes with rebase.
Original file line number Diff line number Diff line change 11mod cli;
22mod git;
3+ use clap:: Parser ;
4+ use colored:: Colorize ;
35
4- fn main ( ) {
5- println ! ( "Hello, world!" ) ;
6+ fn main ( ) -> anyhow:: Result < ( ) > {
7+ let cli = cli:: Cli :: parse ( ) ;
8+ match cli. command {
9+ cli:: Commands :: Status => {
10+ println ! ( "--- Checking Git status ---" ) ;
11+ let status = git:: status ( ) ?;
12+ println ! ( "{}" , format!( "Git Status:\n {}" , status) . green( ) ) ;
13+ }
14+ cli:: Commands :: Commit { r#type, scope, message} => {
15+ println ! ( "--- Committing changes ---" ) ;
16+ let scope_part = scope. map_or ( "" . to_string ( ) , |s| format ! ( "({})" , s) ) ;
17+ let header = format ! ( "{}{}: {}" , r#type, scope_part, message) ;
18+ let commit_message = format ! ( "{}" , header) ;
19+
20+ println ! ( "{}" , format!( "Commit message will be:\n ---\n {}\n ---" , commit_message) . blue( ) ) ;
21+ // Stage changes first, before any other operations.
22+ git:: add_all ( ) ?;
23+ git:: pull_latest_with_rebase ( ) ?;
24+ git:: commit ( & commit_message) ?;
25+ git:: push ( ) ?;
26+ println ! ( "{}" , "Successfully committed and pushed changes." . green( ) ) ;
27+ }
28+ }
29+ Ok ( ( ) )
630}
You can’t perform that action at this time.
0 commit comments