Skip to content

Commit

Permalink
Сущность MyGit
Browse files Browse the repository at this point in the history
  • Loading branch information
demidko committed Mar 28, 2024
1 parent f984633 commit 049e4b7
Show file tree
Hide file tree
Showing 6 changed files with 331 additions and 35 deletions.
242 changes: 242 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
git2 = "0.18.3"
walkdir = "2.5.0"
16 changes: 14 additions & 2 deletions src/arch.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
use walkdir::WalkDir;

pub fn read() -> String {
todo!()
}
todo!("")
}

#[cfg(test)]
mod tests {
use crate::arch::read;

#[test]
fn it_works() {
read();
}
}
26 changes: 0 additions & 26 deletions src/git.rs

This file was deleted.

12 changes: 5 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
use std::env::args;

use clap::Parser;

mod git;
mod mygit;
mod arch;

fn main() {
let from_branch = args().nth(1).expect("expected source branch name");
let into_branch = git::current_branch();
let into_branch = mygit::current_branch();

git::co(&from_branch);
mygit::co(&from_branch);
let from_branch = arch::read();

git::co(&into_branch);
mygit::co(&into_branch);
let into_branch = arch::read();

println!("{}", git::diff(&from_branch, &into_branch));
println!("{}", mygit::diff(&from_branch, &into_branch));
}
68 changes: 68 additions & 0 deletions src/mygit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use std::process::Command;
use std::str;

use git2::{BranchType, Repository};

struct MyGit {
repo: Repository,
}

impl MyGit {
pub fn new() -> Self {
match Repository::init(".") {
Ok(repo) => Self { repo },
Err(e) => panic!("failed to init: {}", e)
}
}

pub fn current_branch_name(&self) -> String {
let repo = &self.repo;
let head = repo.head().unwrap();
let name = head.name().unwrap();
let name = name.trim_start_matches("refs/heads/");
name.to_string()
}

pub fn checkout(&self, branch_name: &str) {
let repo = &self.repo;
}
}

pub fn co(branch: &str) {
let status =
Command::new("git")
.arg("checkout")
.arg(branch)
.status()
.expect("failed to find 'git' command");
assert!(status.success())
}

pub fn current_branch() -> String {
let output =
Command::new("git")
.arg("branch")
.arg("--show-current")
.output()
.expect("failed to find 'git' command")
.stdout;
str::from_utf8(&output)
.expect("failed to find current branch")
.trim()
.to_string()
}

pub fn diff(from: &str, into: &str) -> String {
todo!()
}

#[cfg(test)]
mod tests {
use crate::mygit::{current_branch, MyGit};

#[test]
fn it_works() {
let branch = MyGit::new().current_branch_name();
assert_eq!(branch, "main")
}
}

0 comments on commit 049e4b7

Please sign in to comment.