Skip to content

Commit 9b48715

Browse files
committed
Add push with SSH creds attempt
1 parent f5d0832 commit 9b48715

File tree

5 files changed

+32
-16
lines changed

5 files changed

+32
-16
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ ref_debug = []
1616
[profile.dev]
1717
opt-level = 1
1818

19+
# Sometimes faster, sometimes slower
20+
# parallel-compiler = true
21+
1922
# Build optimizations: https://github.com/johnthagen/min-sized-rust
2023
[profile.release]
2124
panic = "abort"

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,16 @@ Repo mbrav/test-repo
8585
Success commit 'Change bootstrap server url from prod-kafka to dev-kafka' d93cb354791ccb4a540b767c70ea480d4cbd580a
8686
REPORT
8787
Fn - Matched file with number of matched lines
88-
On - Original line, line number
88+
Ln - Original line, line number
8989
Rn - Replace line (if present), line number
9090
GIT REPOSITORIES
9191
9292
Repository: mbrav/test-repo
9393
Branch: development
9494
F2: mbrav/test-repo/values.yaml
95-
F1: kafka_bootstrapservers: "prod-kafka.backend:9092"
95+
L1: kafka_bootstrapservers: "prod-kafka.backend:9092"
9696
R1: kafka_bootstrapservers: "test-kafka.backend:9092"
97-
F4: kafka_url: prod-kafka.backend:9092
97+
L4: kafka_url: prod-kafka.backend:9092
9898
R4: kafka_url: test-kafka.backend:9092
9999
Elapsed: 39.170ms
100100
```

src/git.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use git2::{Branch, BranchType, Branches, Commit, Oid, PushOptions, RemoteCallbacks, Repository};
1+
use git2::{
2+
Branch, BranchType, Branches, Commit, Cred, Oid, PushOptions, RemoteCallbacks, Repository,
3+
};
24
use std::path::{Path, PathBuf};
35

46
/// Get repo from path
@@ -115,17 +117,31 @@ pub fn commit(repo: &mut Repository, msg: &str) -> Result<(), git2::Error> {
115117
}
116118

117119
/// Push changes to remote
118-
pub fn push(repo: &Repository) -> Result<(), git2::Error> {
120+
pub fn push(repo: &Repository, username: &str) -> Result<(), git2::Error> {
119121
// Setup remote
120122
let mut opts = PushOptions::default();
121-
let callbacks = RemoteCallbacks::new();
123+
let mut callbacks = RemoteCallbacks::new();
124+
125+
// Configure authentication credentials
126+
callbacks.credentials(|_url, username_from_url, _allowed_types| {
127+
Cred::ssh_key_from_agent(username_from_url.unwrap_or(username))
128+
});
129+
130+
// Set the callbacks for the remote
122131
opts.remote_callbacks(callbacks);
123132

124133
// Assume the remote's name is "origin"
125134
let mut remote = repo.find_remote("origin")?;
126135

127-
// Push to remote
128-
remote.push::<&str>(&[], Some(&mut opts))?;
136+
// Push changes of the current checkout branch
137+
let current_branch = get_branch_name(repo).expect("Error getting branch");
138+
let refspec = format!(
139+
"refs/heads/{}:refs/heads/{}",
140+
current_branch, current_branch
141+
);
142+
143+
// Push changes based on settings
144+
remote.push(&[refspec], Some(&mut opts))?;
129145

130146
Ok(())
131147
}

src/main.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,12 @@ fn main() {
4848
raider.commit(commit_message.as_str());
4949

5050
// If push flag is set, push to remote
51-
if conf.push && conf.username.is_some() && conf.password.is_some() {
52-
raider.remote_push(
53-
conf.username.expect("Error unwrapping username"),
54-
conf.password.expect("Error unwrapping password"),
55-
);
51+
if conf.push && conf.username.is_some() {
52+
raider.remote_push(conf.username.expect("Error unwrapping username").as_str());
5653
}
5754
// If username or password was not set then throw an error
5855
else if conf.push {
59-
panic!("ERROR: Git username and password must be specified for push");
56+
panic!("ERROR: Git username must be specified for push");
6057
}
6158
}
6259

src/raider.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ impl RepoRaider {
288288
}
289289

290290
/// Push changes to remote
291-
pub fn remote_push(&self, _username: String, _password: String) {
291+
pub fn remote_push(&self, username: &str) {
292292
self.dirs.iter().for_each(|dir| {
293293
let repo = dir.repo.as_ref().expect("Error unwrapping repo");
294294

@@ -299,7 +299,7 @@ impl RepoRaider {
299299
);
300300
} else {
301301
println!("Pushing {} to remote", dir.relative_path.display());
302-
git::push(repo).expect("Error pushing repository");
302+
git::push(repo, username).expect("Error pushing repository");
303303
}
304304
});
305305
}

0 commit comments

Comments
 (0)