Skip to content

support push to origin #266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Sep 2, 2020
2 changes: 1 addition & 1 deletion .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]

runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v2

Expand Down
7 changes: 4 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ jobs:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
rust: [nightly, stable]

runs-on: ${{ matrix.os }}
continue-on-error: ${{ matrix.rust == 'nightly' }}

Expand Down Expand Up @@ -54,14 +53,13 @@ jobs:
build-linux-musl:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@master
- name: Install Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
profile: minimal
target: x86_64-unknown-linux-musl

- name: Setup MUSL
run: |
sudo apt-get -qq install musl-tools
Expand All @@ -73,6 +71,9 @@ jobs:
run: |
make build-linux-musl-release
./target/x86_64-unknown-linux-musl/release/gitui --version
- name: Test
run: |
make test-linux-musl

rustfmt:
name: Rustfmt
Expand Down
47 changes: 47 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ build-linux-musl-debug:
build-linux-musl-release:
cargo build --release --target=x86_64-unknown-linux-musl --no-default-features

test-linux-musl:
cargo test --workspace --target=x86_64-unknown-linux-musl --no-default-features

test:
cargo test --workspace

Expand Down
1 change: 1 addition & 0 deletions assets/vim_style_key_config.ron
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@
commit_amend: ( code: Char('A'), modifiers: ( bits: 0,),),
copy: ( code: Char('y'), modifiers: ( bits: 0,),),
create_branch: ( code: Char('b'), modifiers: ( bits: 0,),),
push: ( code: Char('p'), modifiers: ( bits: 0,),),
)
2 changes: 1 addition & 1 deletion asyncgit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ keywords = ["git"]

[dependencies]
scopetime = { path = "../scopetime", version = "0.1" }
git2 = { version = "0.13.10", default-features = false }
git2 = { version = "0.13", features = ["vendored-openssl"] }
rayon-core = "1.8"
crossbeam-channel = "0.4"
log = "0.4"
Expand Down
5 changes: 5 additions & 0 deletions asyncgit/src/cached/branchname.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ impl BranchName {
self.fetch(current_head)
}

///
pub fn last(&self) -> Option<String> {
self.last_result.as_ref().map(|last| last.1.clone())
}

fn fetch(&mut self, head: Head) -> Result<String> {
let name = sync::get_branch_name(self.repo_path.as_str())?;
self.last_result = Some((head, name.clone()));
Expand Down
4 changes: 4 additions & 0 deletions asyncgit/src/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod hooks;
mod hunks;
mod ignore;
mod logwalker;
mod remotes;
mod reset;
mod stash;
pub mod status;
Expand All @@ -29,6 +30,9 @@ pub use hooks::{hooks_commit_msg, hooks_post_commit, HookResult};
pub use hunks::{reset_hunk, stage_hunk, unstage_hunk};
pub use ignore::add_to_ignore;
pub use logwalker::LogWalker;
pub use remotes::{
fetch_origin, get_remotes, push_origin, remote_push_master,
};
pub use reset::{reset_stage, reset_workdir};
pub use stash::{get_stashes, stash_apply, stash_drop, stash_save};
pub use tags::{get_tags, CommitTags, Tags};
Expand Down
103 changes: 103 additions & 0 deletions asyncgit/src/sync/remotes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
//!

use crate::{error::Result, sync::utils};
use git2::{Cred, FetchOptions, PushOptions, RemoteCallbacks};
use scopetime::scope_time;

///
pub fn get_remotes(repo_path: &str) -> Result<Vec<String>> {
scope_time!("get_remotes");

let repo = utils::repo(repo_path)?;
let remotes = repo.remotes()?;
let remotes: Vec<String> =
remotes.iter().filter_map(|s| s).map(String::from).collect();

Ok(remotes)
}

///
pub fn remote_push_master(repo_path: &str) -> Result<()> {
scope_time!("remote_push_master");

let repo = utils::repo(repo_path)?;
let mut remote = repo.find_remote("origin")?;

remote.push(&["refs/heads/master"], None)?;

Ok(())
}

///
pub fn fetch_origin(repo_path: &str, branch: &str) -> Result<usize> {
scope_time!("remote_fetch_master");

let repo = utils::repo(repo_path)?;
let mut remote = repo.find_remote("origin")?;

let mut options = FetchOptions::new();
options.remote_callbacks(remote_callbacks());

remote.fetch(&[branch], Some(&mut options), None)?;

Ok(remote.stats().received_bytes())
}

///
pub fn push_origin(repo_path: &str, branch: &str) -> Result<()> {
scope_time!("push_origin");

let repo = utils::repo(repo_path)?;
let mut remote = repo.find_remote("origin")?;

let mut options = PushOptions::new();
options.remote_callbacks(remote_callbacks());

remote.push(&[branch], Some(&mut options))?;

Ok(())
}

fn remote_callbacks<'a>() -> RemoteCallbacks<'a> {
let mut callbacks = RemoteCallbacks::new();
callbacks.credentials(|url, username_from_url, allowed_types| {
log::debug!(
"creds: '{}' {:?} ({:?})",
url,
username_from_url,
allowed_types
);

Cred::ssh_key_from_agent(
username_from_url.expect("username not found"),
)
});

callbacks
}

#[cfg(test)]
mod tests {
use super::*;
use crate::sync::tests::debug_cmd_print;
use tempfile::TempDir;

#[test]
fn test_smoke() {
let td = TempDir::new().unwrap();

debug_cmd_print(
td.path().as_os_str().to_str().unwrap(),
"git clone https://github.com/extrawurst/brewdump.git",
);

let repo_path = td.path().join("brewdump");
let repo_path = repo_path.as_os_str().to_str().unwrap();

let remotes = get_remotes(repo_path).unwrap();

assert_eq!(remotes, vec![String::from("origin")]);

fetch_origin(repo_path, "master").unwrap();
}
}
9 changes: 7 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ impl App {
let msg =
format!("failed to launch editor:\n{}", e);
log::error!("{}", msg.as_str());
self.msg.show_msg(msg.as_str())?;
self.msg.show_error(msg.as_str())?;
}

self.requires_redraw.set(true);
Expand Down Expand Up @@ -454,7 +454,12 @@ impl App {
flags.insert(NeedsUpdate::COMMANDS);
}
InternalEvent::ShowErrorMsg(msg) => {
self.msg.show_msg(msg.as_str())?;
self.msg.show_error(msg.as_str())?;
flags
.insert(NeedsUpdate::ALL | NeedsUpdate::COMMANDS);
}
InternalEvent::ShowInfoMsg(msg) => {
self.msg.show_info(msg.as_str())?;
flags
.insert(NeedsUpdate::ALL | NeedsUpdate::COMMANDS);
}
Expand Down
2 changes: 1 addition & 1 deletion src/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn copy_string(_string: String) -> Result<()> {
}

#[cfg(feature = "clipboard")]
pub fn is_supported() -> bool {
pub const fn is_supported() -> bool {
true
}

Expand Down
5 changes: 5 additions & 0 deletions src/components/changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ impl ChangesComponent {
Ok(())
}

///
pub fn branch_name(&self) -> Option<String> {
self.branch_name.last()
}

///
pub fn set_items(&mut self, list: &[StatusItem]) -> Result<()> {
self.files.update(list)?;
Expand Down
19 changes: 15 additions & 4 deletions src/components/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use tui::{
use ui::style::SharedTheme;

pub struct MsgComponent {
title: String,
msg: String,
visible: bool,
theme: SharedTheme,
Expand All @@ -39,9 +40,7 @@ impl DrawableComponent for MsgComponent {
Paragraph::new(txt.iter())
.block(
Block::default()
.title(&strings::msg_title_error(
&self.key_config,
))
.title(self.title.as_str())
.title_style(self.theme.text_danger())
.borders(Borders::ALL)
.border_type(BorderType::Thick),
Expand Down Expand Up @@ -104,14 +103,26 @@ impl MsgComponent {
key_config: SharedKeyConfig,
) -> Self {
Self {
title: String::new(),
msg: String::new(),
visible: false,
theme,
key_config,
}
}

///
pub fn show_error(&mut self, msg: &str) -> Result<()> {
self.title = strings::msg_title_error(&self.key_config);
self.msg = msg.to_string();
self.show()?;

Ok(())
}

///
pub fn show_msg(&mut self, msg: &str) -> Result<()> {
pub fn show_info(&mut self, msg: &str) -> Result<()> {
self.title = strings::msg_title_info(&self.key_config);
self.msg = msg.to_string();
self.show()?;

Expand Down
2 changes: 2 additions & 0 deletions src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub struct KeyConfig {
pub commit_amend: KeyEvent,
pub copy: KeyEvent,
pub create_branch: KeyEvent,
pub push: KeyEvent,
}

#[rustfmt::skip]
Expand Down Expand Up @@ -109,6 +110,7 @@ impl Default for KeyConfig {
commit_amend: KeyEvent { code: KeyCode::Char('a'), modifiers: KeyModifiers::CONTROL},
copy: KeyEvent { code: KeyCode::Char('y'), modifiers: KeyModifiers::empty()},
create_branch: KeyEvent { code: KeyCode::Char('b'), modifiers: KeyModifiers::empty()},
push: KeyEvent { code: KeyCode::Char('p'), modifiers: KeyModifiers::empty()},
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ pub enum InternalEvent {
///
ShowErrorMsg(String),
///
ShowInfoMsg(String),
///
Update(NeedsUpdate),
/// open commit msg input
OpenCommit,
Expand Down
Loading