Skip to content

Commit 70b9a36

Browse files
committed
refactor(submit): Forge::update returns UpdateStatuses
For uniformity with `CreateStatus`.
1 parent 14b111e commit 70b9a36

File tree

4 files changed

+61
-18
lines changed

4 files changed

+61
-18
lines changed

git-branchless-submit/src/branch_forge.rs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use lib::try_exit_code;
1515
use lib::util::{ExitCode, EyreExitOr};
1616
use tracing::{instrument, warn};
1717

18-
use crate::{CommitStatus, CreateStatus, Forge, SubmitOptions, SubmitStatus};
18+
use crate::{CommitStatus, CreateStatus, Forge, SubmitOptions, SubmitStatus, UpdateStatus};
1919

2020
#[derive(Debug)]
2121
pub struct BranchForge<'a> {
@@ -269,20 +269,20 @@ These remotes are available: {}",
269269
&mut self,
270270
commits: HashMap<NonZeroOid, CommitStatus>,
271271
_options: &SubmitOptions,
272-
) -> EyreExitOr<()> {
273-
let branches_by_remote: BTreeMap<String, BTreeSet<String>> = commits
274-
.into_values()
275-
.flat_map(|commit_status| match commit_status {
272+
) -> EyreExitOr<HashMap<NonZeroOid, UpdateStatus>> {
273+
let branches_by_remote: BTreeMap<String, BTreeSet<(String, NonZeroOid)>> = commits
274+
.into_iter()
275+
.flat_map(|(commit_oid, commit_status)| match commit_status {
276276
CommitStatus {
277277
submit_status: _,
278278
remote_name: Some(remote_name),
279279
local_commit_name: Some(local_commit_name),
280280
remote_commit_name: _,
281-
} => Some((remote_name, local_commit_name)),
281+
} => Some((remote_name, (local_commit_name, commit_oid))),
282282
commit_status => {
283283
warn!(
284284
?commit_status,
285-
"Commit was requested to be updated, but it did not have the requisite information (remote name, local branch name)."
285+
"Commit was requested to be updated, but it did not have an associated remote and a local commit name"
286286
);
287287
None
288288
}
@@ -299,24 +299,39 @@ These remotes are available: {}",
299299
.values()
300300
.map(|branch_names| branch_names.len())
301301
.sum();
302+
let mut result = HashMap::new();
302303
progress.notify_progress(0, total_num_branches);
303304
for (remote_name, branch_names) in branches_by_remote {
304305
let mut args = vec!["push", "--force-with-lease", &remote_name];
305-
args.extend(branch_names.iter().map(|s| s.as_str()));
306+
args.extend(
307+
branch_names
308+
.iter()
309+
.map(|(branch, _commit_oid)| branch.as_str()),
310+
);
306311
match self.git_run_info.run(&effects, Some(event_tx_id), &args)? {
307312
Ok(()) => {}
308313
Err(exit_code) => {
309314
writeln!(
310315
effects.get_output_stream(),
311316
"Failed to push branches: {}",
312-
branch_names.into_iter().join(", ")
317+
branch_names
318+
.iter()
319+
.map(|(branch, _commit_oid)| branch)
320+
.join(", ")
313321
)?;
314322
return Ok(Err(exit_code));
315323
}
316324
}
317325
progress.notify_progress_inc(branch_names.len());
326+
327+
// FIXME: report push errors
328+
result.extend(
329+
branch_names
330+
.iter()
331+
.map(|(_branch, commit_oid)| (*commit_oid, UpdateStatus::Updated)),
332+
);
318333
}
319334

320-
Ok(Ok(()))
335+
Ok(Ok(result))
321336
}
322337
}

git-branchless-submit/src/github.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use tracing::warn;
3333

3434
use crate::branch_forge::BranchForge;
3535
use crate::SubmitStatus;
36+
use crate::UpdateStatus;
3637
use crate::{CommitStatus, CreateStatus, Forge, SubmitOptions};
3738

3839
/// Testing environment variable. When this is set, the executable will use the
@@ -370,7 +371,7 @@ impl Forge for GithubForge<'_> {
370371
&mut self,
371372
commit_statuses: HashMap<NonZeroOid, CommitStatus>,
372373
options: &SubmitOptions,
373-
) -> EyreExitOr<()> {
374+
) -> EyreExitOr<HashMap<NonZeroOid, UpdateStatus>> {
374375
let effects = self.effects;
375376
let SubmitOptions {
376377
create: _,
@@ -394,6 +395,7 @@ impl Forge for GithubForge<'_> {
394395

395396
let commit_set: CommitSet = commit_statuses.keys().copied().collect();
396397
let commit_oids = self.dag.sort(&commit_set)?;
398+
let mut result = HashMap::new();
397399
{
398400
let (effects, progress) = effects.start_operation(OperationType::UpdateCommits);
399401
progress.notify_progress(0, commit_oids.len());
@@ -474,7 +476,7 @@ impl Forge for GithubForge<'_> {
474476
branch_forge.update(singleton(&commit_statuses, commit_oid, |x| x), options)?
475477
);
476478

477-
// Update metdata:
479+
// Update metadata:
478480
try_exit_code!(self.client.update_pull_request(
479481
&effects,
480482
pull_request_info.number,
@@ -487,10 +489,13 @@ impl Forge for GithubForge<'_> {
487489
options
488490
)?);
489491
progress.notify_progress_inc(1);
492+
493+
// FIXME: report push/update errors
494+
result.insert(commit_oid, UpdateStatus::Updated);
490495
}
491496
}
492497

493-
Ok(Ok(()))
498+
Ok(Ok(result))
494499
}
495500
}
496501

git-branchless-submit/src/lib.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,15 @@ pub enum CreateStatus {
171171
},
172172
}
173173

174+
/// The result of updating a commit.
175+
///
176+
/// TODO: have an `Err` case.
177+
#[derive(Clone, Debug)]
178+
pub enum UpdateStatus {
179+
/// The commit was successfully updated.
180+
Updated,
181+
}
182+
174183
/// "Forge" refers to a Git hosting provider, such as GitHub, GitLab, etc.
175184
/// Commits can be pushed for review to a forge.
176185
pub trait Forge: Debug {
@@ -192,7 +201,7 @@ pub trait Forge: Debug {
192201
&mut self,
193202
commits: HashMap<NonZeroOid, CommitStatus>,
194203
options: &SubmitOptions,
195-
) -> EyreExitOr<()>;
204+
) -> EyreExitOr<HashMap<NonZeroOid, UpdateStatus>>;
196205
}
197206

198207
/// `submit` command.

git-branchless-submit/src/phabricator.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ use serde::{Deserialize, Serialize};
3535
use thiserror::Error;
3636
use tracing::{instrument, warn};
3737

38-
use crate::{CommitStatus, CreateStatus, Forge, SubmitOptions, SubmitStatus, STYLE_PUSHED};
38+
use crate::{
39+
CommitStatus, CreateStatus, Forge, SubmitOptions, SubmitStatus, UpdateStatus, STYLE_PUSHED,
40+
};
3941

4042
/// Wrapper around the Phabricator "ID" type. (This is *not* a PHID, just a
4143
/// regular ID).
@@ -614,7 +616,7 @@ fi
614616
&mut self,
615617
commits: HashMap<NonZeroOid, crate::CommitStatus>,
616618
options: &SubmitOptions,
617-
) -> EyreExitOr<()> {
619+
) -> EyreExitOr<HashMap<NonZeroOid, UpdateStatus>> {
618620
let SubmitOptions {
619621
create: _,
620622
draft: _,
@@ -747,12 +749,24 @@ fi
747749

748750
try_exit_code!(self.update_dependencies(
749751
&success_commits
750-
.into_iter()
752+
.iter()
751753
.map(|(commit_oid, _test_output)| commit_oid)
754+
.copied()
752755
.collect(),
753756
&CommitSet::empty()
754757
)?);
755-
Ok(Ok(()))
758+
Ok(Ok(success_commits
759+
.into_iter()
760+
.map(|(commit_oid, _test_output)| (commit_oid, UpdateStatus::Updated))
761+
.chain(
762+
failure_commits
763+
.into_iter()
764+
.map(|(commit_oid, _test_output)| {
765+
// FIXME: report error
766+
(commit_oid, UpdateStatus::Updated)
767+
}),
768+
)
769+
.collect()))
756770
}
757771
}
758772

0 commit comments

Comments
 (0)