branch command #9

Merged
krauterbaquette merged 15 commits from cmd-branch into main 2024-12-22 14:01:09 +00:00

This command will behave like git branch with some add-ons.

When executing sac branch without arguments, it will list all the local branches and highlight the current branch.

When executing sac branch with an argument, it will try to create a branch with the given argument as a branch name.
Afterwards it will push the branch to the remote and set up the upstream correctly.

This way the branch will be in sync with the remote from the start.
For situations where one does not has an internet connection, the --local flag can be used to skip the branch syncing with the remote.

This command will behave like `git branch` with some add-ons. When executing `sac branch` **without** arguments, it will list all the local branches and highlight the current branch. When executing `sac branch` **with** an argument, it will try to create a branch with the given argument as a branch name. Afterwards it will push the branch to the remote and set up the upstream correctly. This way the branch will be in sync with the remote from the start. For situations where one does not has an internet connection, the `--local` flag can be used to skip the branch syncing with the remote.
started branch command, started branch list functionality
All checks were successful
Rust Checks / Run Rust Check (push) Successful in 2m33s
f522aa95e4
branch only lists local branches, because there is no way of knowing which branches exist on the remote repo.
git branch --remote even shows already deleted branches, which should not be shown to the user
implemented branch creation
Some checks failed
Rust Checks / Run Rust Check (push) Has been cancelled
f4f73f8b02
- try fallback when branch push fails

Others:
- added create command for switch-command -> run switch command from within sac
delte branch in fallback
All checks were successful
Rust Checks / Run Rust Check (push) Successful in 2m53s
c58e922b1e
colorized output (branch list, warnings)
All checks were successful
Rust Checks / Run Rust Check (push) Successful in 2m41s
738af47757
added color error output
All checks were successful
Rust Checks / Run Rust Check (push) Successful in 2m40s
1ce6c0a86e
added progress info
All checks were successful
Rust Checks / Run Rust Check (push) Successful in 2m43s
dc91653d06
- can be made better :)
fixed branch recovery
All checks were successful
Rust Checks / Run Rust Check (push) Successful in 2m43s
c8f52078a8
Author
Owner

Todo

  • better error message when no internet connection
    • this should inform the user about the --local flag
  • better error message when remote is a-head (branch already exists)
  • better progress indication (loading spinner when pushing etc)
# Todo - [ ] better error message when no internet connection - this should inform the user about the `--local` flag - [x] better error message when remote is a-head (branch already exists) - [x] better progress indication (loading spinner when pushing etc)
removed unused 'new' function
All checks were successful
Rust Checks / Run Rust Check (push) Successful in 2m34s
add5908eb3
add: better error message for no internet connection
All checks were successful
Rust Checks / Run Rust Check (push) Successful in 5m29s
9300b5004e
add: better error message for branch ahead on remote
All checks were successful
Rust Checks / Run Rust Check (push) Successful in 2m46s
1426645aed
add: loading spinners for fetching
All checks were successful
Rust Checks / Run Rust Check (push) Successful in 2m26s
da3cddccb7
krauterbaquette changed title from WIP: branch command to branch command 2024-12-18 09:11:11 +00:00
Author
Owner

Testing

To test the branch creation feature a remote repository is needed with a local clone.

All errors should be user friendly

List branch

$ sac branch

This should work the same as git branch.
Log all locally known branches and highlight the current branch.

Create branch

$ sac branch test

This should create a local branch, push it to the remote and sync them.
HEAD should point to the new branch.

Already existing branch

$ sac branch main

This should error.

Local only branch

$ sac branch --local test-local

Only the local branch will be created.
No branch should be created on the remote.

No internet

Turn off the internet connection

$ sac branch test

The branch creation should fail and inform the user about --local for work without internet.
No branch should be created and the user should stay inside the current branch.

Remote branch ahead

This is a bit harder to reproduce.

Create a branch

$ git checkout -b ahead

Make a commit & push to remote

$ touch ahead.txt
$ git add .
$ git commit -m "now ahead of main"
$ git push origin ahead

Delete the local branch

$ git switch main
$ git branch -D ahead

Now a remote branch should exist, which is ahead of main, named ahead.

Try to create a new branch ahead

$ sac branch ahead

The process should fail (with a user friendly error).
No branch should be created, and the user should stay on the starting branch.

# Testing To test the _branch creation_ feature a remote repository is needed with a local clone. > All errors should be user friendly ## List branch ``` $ sac branch ``` This should work the same as `git branch`. \ Log all locally known branches and highlight the current branch. ## Create branch ``` $ sac branch test ``` This should create a local branch, push it to the remote and sync them. \ HEAD should point to the new branch. ## Already existing branch ``` $ sac branch main ``` This should error. ## Local only branch ``` $ sac branch --local test-local ``` Only the local branch will be created. \ No branch should be created on the remote. ## No internet > Turn off the internet connection ``` $ sac branch test ``` The branch creation should fail and inform the user about `--local` for work without internet. \ No branch should be created and the user should stay inside the current branch. ## Remote branch ahead > This is a bit harder to reproduce. Create a branch ``` $ git checkout -b ahead ``` Make a commit & push to remote ``` $ touch ahead.txt $ git add . $ git commit -m "now ahead of main" $ git push origin ahead ``` Delete the local branch ``` $ git switch main $ git branch -D ahead ``` > Now a remote branch should exist, which is ahead of main, named `ahead`. Try to create a new branch `ahead` ``` $ sac branch ahead ``` The process should fail (with a user friendly error). \ No branch should be created, and the user should stay on the starting branch.
krauterbaquette changed title from branch command to WIP: branch command 2024-12-18 16:45:40 +00:00
aviac approved these changes 2024-12-19 11:36:34 +00:00
Dismissed
aviac left a comment
Contributor

Sieht gut aus vom code her!

Sieht gut aus vom code her!
@ -0,0 +39,20 @@
let repo = Repository::open(".").context("failed to open git repository")?;
let branches = repo.branches(Some(BranchType::Local)).unwrap();
let mut out = String::from("");
for branch in branches {
let branch = branch.unwrap();
let branch_name = match branch.0.name().unwrap() {
Some(name) => name,
None => "???",
};
let prefix;
if branch.0.is_head() {
prefix = ">";
} else {
prefix = " ";
}
let mut line = format!("{} {}", prefix, branch_name);
Contributor

Allgemein bei allen diesen format strings kannst du die Variable (falls sie sich normal ausgeben laesst -> das kannst du testen indem du schaust ob sie to_string() implementiert hat) auch direkt in die Klammern hauen. Das liest sich imo besser:

format!("{prefix} {branch_name}")
Allgemein bei allen diesen format strings kannst du die Variable (falls sie sich normal ausgeben laesst -> das kannst du testen indem du schaust ob sie `to_string()` implementiert hat) auch direkt in die Klammern hauen. Das liest sich imo besser: ```rust format!("{prefix} {branch_name}") ```
krauterbaquette marked this conversation as resolved
@ -0,0 +52,20 @@
if branch.0.is_head() {
prefix = ">";
} else {
prefix = " ";
}
let mut line = format!("{} {}", prefix, branch_name);
if branch.0.is_head() {
line = line.green().to_string();
}
let line = format!("{}\n", line);
out += &line;
}
if out == "" {
println!("no branches\ncreate one with 'sac branch NAME'");
return Ok(());
}
out = out[..out.len() - 1].to_string();
Contributor

Wenn du nur das letzte newline wegmachen willst geht glaube ich auch .trim() oder alternativ kannst du auch print!() nutzen, was nicht noch ein extra newline hinten dranhaengt.

Wenn du nur das letzte newline wegmachen willst geht glaube ich auch `.trim()` oder alternativ kannst du auch `print!()` nutzen, was nicht noch ein extra newline hinten dranhaengt.
krauterbaquette marked this conversation as resolved
@ -0,0 +83,20 @@
let repo = Repository::discover(".").context("failed to open local repository")?;
let recover_to = repo.head();
let mut sp = Spinner::new(Spinners::Line, "create branch localy".into());
let checkout = Command::new("git")
.args(["checkout", "-b", name])
.output()
.context("failed to execute git")?;
if !checkout.status.success() {
sp.stop_with_symbol("");
let err = match_checkout_error(&checkout.stderr, name);
anyhow::bail!(err);
} else {
sp.stop_with_symbol("🗸");
}
if self.local {
return Ok(());
}
let mut sp = Spinner::new(Spinners::Line, "create branch remotly".into());
Contributor

Schick!

Schick!
Author
Owner

danke, auf die spinner bin ich auch sehr stolz :)

danke, auf die spinner bin ich auch sehr stolz :)
krauterbaquette marked this conversation as resolved
moved variables inside the format string where possible

Adressing comment:
- GTA-HEG/SAC#9 (comment)
simplified branch listing
Some checks failed
Rust Checks / Run Rust Check (push) Failing after 3s
882cb4cee0
no longe remove the trailing \n but use print! macro (which does not
append the \n at the end)
krauterbaquette changed title from WIP: branch command to branch command 2024-12-19 17:20:21 +00:00
krauterbaquette force-pushed cmd-branch from 882cb4cee0
Some checks failed
Rust Checks / Run Rust Check (push) Failing after 3s
to 90b116ba90 2024-12-21 17:36:44 +00:00
Compare
aviac approved these changes 2024-12-22 05:51:01 +00:00
aviac left a comment
Contributor

Sieht gut aus

Sieht gut aus
krauterbaquette force-pushed cmd-branch from 90b116ba90 to 9e0c318e50
Some checks failed
Rust Checks / Run Rust Check (push) Failing after 3s
2024-12-22 14:00:01 +00:00
Compare
krauterbaquette referenced this pull request from a commit 2024-12-22 14:01:10 +00:00
krauterbaquette deleted branch cmd-branch 2024-12-22 14:01:27 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
krauterbaquette/sac!9
No description provided.