This repository was archived by the owner on Apr 16, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
Extract rad remote helper into lib #254
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,3 +18,4 @@ | |
extern crate radicle_keystore as keystore; | ||
|
||
pub mod credential; | ||
pub mod remote_helper; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// This file is part of radicle-link | ||
// <https://github.com/radicle-dev/radicle-link> | ||
// | ||
// Copyright (C) 2019-2020 The Radicle Team <[email protected]> | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License version 3 or | ||
// later as published by the Free Software Foundation. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
use std::{ | ||
env, | ||
io, | ||
path::{Path, PathBuf}, | ||
}; | ||
|
||
use crate::credential; | ||
use librad::{ | ||
git::local::{ | ||
transport::{LocalTransport, Localio, Mode::Stateful, Settings}, | ||
url::LocalUrl, | ||
}, | ||
keys::{PublicKey, SecretKey}, | ||
paths::Paths, | ||
}; | ||
use radicle_keystore::{crypto::Pwhash, FileStorage, Keystore}; | ||
|
||
// FIXME: this should be defined elsewhere to be consistent between applications | ||
const SECRET_KEY_FILE: &str = "librad.key"; | ||
|
||
pub fn run() -> anyhow::Result<()> { | ||
let url = { | ||
let args = env::args().skip(1).take(2).collect::<Vec<_>>(); | ||
args[0] | ||
.parse() | ||
.or_else(|_| args[1].parse()) | ||
.map_err(|_| anyhow::anyhow!("invalid args: {:?}", args)) | ||
}?; | ||
|
||
let git_dir = env::var("GIT_DIR").map(PathBuf::from)?; | ||
|
||
let mut transport = { | ||
let paths = Paths::from_env()?; | ||
let key = get_signer(&git_dir, paths.keys_dir(), &url)?; | ||
LocalTransport::new(Settings { paths, signer: key }) | ||
}?; | ||
|
||
loop { | ||
let mut buf = String::with_capacity(32); | ||
io::stdin().read_line(&mut buf)?; | ||
let line = buf.trim(); | ||
|
||
if line == "capabilities" { | ||
println!("connect\n\n"); | ||
continue; | ||
} | ||
|
||
if let Some(service) = line.strip_prefix("connect ") { | ||
let service = match service { | ||
"git-upload-pack" => Ok(git2::transport::Service::UploadPack), | ||
"git-receive-pack" => Ok(git2::transport::Service::ReceivePack), | ||
unknown => Err(anyhow::anyhow!("unknown service: {}", unknown)), | ||
}?; | ||
|
||
println!(); | ||
|
||
transport | ||
.connect(url, service, Stateful, Localio::inherit())? | ||
.wait()?; | ||
|
||
break; | ||
} | ||
|
||
return Err(anyhow::anyhow!("unexpected command: {}", line)); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn get_signer(git_dir: &Path, keys_dir: &Path, url: &LocalUrl) -> anyhow::Result<SecretKey> { | ||
let pass = credential::Git::new(git_dir).get(url)?; | ||
let file = keys_dir.join(SECRET_KEY_FILE); | ||
let keystore = FileStorage::<_, PublicKey, _, _>::new(&file, Pwhash::new(pass)); | ||
keystore | ||
.get_key() | ||
.map(|keypair| keypair.secret_key) | ||
.map_err(|e| e.into()) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of putting this code here, I'd suggest:
pub mod remote_helper
hereremote_helper.rs
file and moving this code into that filerun
remote_helper::run()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1547c52