1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
//! Push status reporting that mirrors Git's `transport_print_push_status`.
//!
//! After a push, Git prints one line per reference describing how the update
//! resolved (`[up to date]`, `[new branch]`, `[deleted]`, `[rejected]`, …).
//! There are two output styles: a human-readable form on stderr and a
//! machine-readable `--porcelain` form on stdout. This module reproduces both
//! exactly, including the ordering and the fixed-width summary column.
//!
//! The canonical C implementation lives in `transport.c`
//! (`print_ref_status`, `print_ok_ref_status`, `print_one_push_report`,
//! `transport_print_push_status`).
use crate::objects::ObjectId;
use std::fmt::Write as _;
/// The resolved outcome of a single reference update during a push.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum PushRefStatus {
/// The reference was already at the requested value (`=`, `[up to date]`).
UpToDate,
/// A successful update (`*` new, `-` delete, ` ` fast-forward, `+` forced).
Ok,
/// Client-side rejection: the update is not a fast-forward and `--force`
/// was not given (`!`, `[rejected] (non-fast-forward)`).
RejectNonFastForward,
/// Rejected because the new ref already exists (`!`, `[rejected] (already exists)`).
RejectAlreadyExists,
/// Rejected because the remote has the ref but we need to fetch first.
RejectFetchFirst,
/// Rejected because a forced update is required.
RejectNeedsForce,
/// Rejected because force-with-lease found stale info.
RejectStale,
/// The remote `receive-pack` declined the update (`!`, `[remote rejected] (<reason>)`).
RemoteRejected,
/// Part of an atomic push that failed because another ref was rejected
/// (`!`, `[rejected] (atomic push failed)`).
AtomicPushFailed,
}
impl PushRefStatus {
/// Whether this status represents a hard failure (causes a non-zero exit).
#[must_use]
pub fn is_error(&self) -> bool {
!matches!(self, PushRefStatus::UpToDate | PushRefStatus::Ok)
}
}
/// One reference's resolved push result, ready for display.
#[derive(Clone, Debug)]
pub struct PushRefResult {
/// The local ref name (source side), e.g. `refs/heads/next`. `None` for deletions.
pub local_ref: Option<String>,
/// The remote ref name (destination side), e.g. `refs/heads/next`.
pub remote_ref: String,
/// Old value of the remote ref (`None`/zero for a new ref).
pub old_oid: Option<ObjectId>,
/// New value of the remote ref (`None`/zero for a deletion).
pub new_oid: Option<ObjectId>,
/// Whether this update was a forced (non-fast-forward but `--force`d) update.
pub forced: bool,
/// Whether this update deletes the remote ref.
pub deletion: bool,
/// The resolved status.
pub status: PushRefStatus,
/// Extra reason text (used for `[remote rejected]`).
pub message: Option<String>,
}
impl PushRefResult {
/// Abbreviated 7-char hex of an OID, or seven zeros for `None`.
fn short(oid: Option<ObjectId>) -> String {
match oid {
Some(o) => o.to_hex()[..7].to_owned(),
None => "0000000".to_owned(),
}
}
}
/// Strip the common `refs/heads/`, `refs/tags/`, `refs/remotes/` prefix the way
/// Git's `prettify_refname` does for human-readable output.
fn prettify_refname(name: &str) -> &str {
name.strip_prefix("refs/heads/")
.or_else(|| name.strip_prefix("refs/tags/"))
.or_else(|| name.strip_prefix("refs/remotes/"))
.unwrap_or(name)
}
/// The flag character, fixed summary text, and optional parenthetical reason for
/// a result, matching `print_one_push_report` / `print_ok_ref_status`.
///
/// Returns `(flag, summary, message)` where `summary` is the bracketed status or
/// the `old..new` quickref, and `message` is the trailing `(reason)` if any.
fn describe(result: &PushRefResult) -> (char, String, Option<String>) {
match result.status {
PushRefStatus::UpToDate => ('=', "[up to date]".to_owned(), None),
PushRefStatus::Ok => {
if result.deletion {
('-', "[deleted]".to_owned(), None)
} else if result.old_oid.is_none() {
let summary = if result.remote_ref.starts_with("refs/tags/") {
"[new tag]"
} else if result.remote_ref.starts_with("refs/heads/") {
"[new branch]"
} else {
"[new reference]"
};
('*', summary.to_owned(), None)
} else {
let old = PushRefResult::short(result.old_oid);
let new = PushRefResult::short(result.new_oid);
if result.forced {
(
'+',
format!("{old}...{new}"),
Some("forced update".to_owned()),
)
} else {
(' ', format!("{old}..{new}"), None)
}
}
}
PushRefStatus::RejectNonFastForward => (
'!',
"[rejected]".to_owned(),
Some("non-fast-forward".to_owned()),
),
PushRefStatus::RejectAlreadyExists => (
'!',
"[rejected]".to_owned(),
Some("already exists".to_owned()),
),
PushRefStatus::RejectFetchFirst => {
('!', "[rejected]".to_owned(), Some("fetch first".to_owned()))
}
PushRefStatus::RejectNeedsForce => {
('!', "[rejected]".to_owned(), Some("needs force".to_owned()))
}
PushRefStatus::RejectStale => ('!', "[rejected]".to_owned(), Some("stale info".to_owned())),
PushRefStatus::RemoteRejected => (
'!',
"[remote rejected]".to_owned(),
result
.message
.clone()
.or_else(|| Some("remote rejected".to_owned())),
),
PushRefStatus::AtomicPushFailed => (
'!',
"[rejected]".to_owned(),
Some("atomic push failed".to_owned()),
),
}
}
/// Output produced by [`format_push_status`].
#[derive(Default, Debug)]
pub struct PushStatusOutput {
/// Lines for stdout (porcelain mode writes everything here).
pub stdout: String,
/// Lines for stderr (human-readable mode writes everything here).
pub stderr: String,
/// Whether any reference failed (the push command should exit non-zero).
pub had_errors: bool,
}
/// Sort key mirroring `transport_print_push_status`: up-to-date refs first,
/// then successful updates, then everything else (errors), preserving the
/// original order within each bucket.
fn status_bucket(status: &PushRefStatus) -> u8 {
match status {
PushRefStatus::UpToDate => 0,
PushRefStatus::Ok => 1,
_ => 2,
}
}
/// Render the full set of per-ref push results the way Git does.
///
/// `dest` is the (already credential-scrubbed) destination URL printed in the
/// `To <url>` header. When `porcelain` is true the machine-readable format is
/// emitted to `stdout` and terminated with a `Done` line; otherwise the
/// human-readable format is emitted to `stderr`. `quiet` suppresses all output
/// unless there were errors (matching `if (!quiet || err)` in `transport_push`).
///
/// Results are reordered into Git's display order but the input slice is left
/// untouched.
#[must_use]
pub fn format_push_status(
dest: &str,
results: &[PushRefResult],
porcelain: bool,
quiet: bool,
) -> PushStatusOutput {
let mut out = PushStatusOutput {
had_errors: results.iter().any(|r| r.status.is_error()),
..PushStatusOutput::default()
};
if quiet && !out.had_errors {
return out;
}
// Sort into Git's three buckets (up-to-date, ok, errors); within a bucket the
// remote `refs` list is advertised in sorted order, so order by ref name.
let mut order: Vec<usize> = (0..results.len()).collect();
order.sort_by(|&a, &b| {
status_bucket(&results[a].status)
.cmp(&status_bucket(&results[b].status))
.then_with(|| results[a].remote_ref.cmp(&results[b].remote_ref))
});
// Compute the fixed summary-column width for human-readable output:
// 2 * max_abbrev + 3, where abbrev is 7 here (DEFAULT_ABBREV).
let summary_width = 2 * 7 + 3;
let buf = if porcelain {
&mut out.stdout
} else {
&mut out.stderr
};
let _ = writeln!(buf, "To {dest}");
for &i in &order {
let result = &results[i];
let (flag, summary, message) = describe(result);
if porcelain {
let to_name = &result.remote_ref;
// Git prints the source side from `ref->peer_ref`. A *successful*
// deletion is reported via `print_ok_ref_status` with `from = NULL`
// (just `:dst`). Most error paths pass `ref->peer_ref` for deletions
// (whose name is the literal `(delete)`), except `REF_STATUS_REMOTE_REJECT`,
// which explicitly passes `NULL` for a deletion (`ref->deletion ? NULL : …`).
if result.deletion {
let from_delete = result.status.is_error()
&& !matches!(result.status, PushRefStatus::RemoteRejected);
if from_delete {
let _ = write!(buf, "{flag}\t(delete):{to_name}\t");
} else {
let _ = write!(buf, "{flag}\t:{to_name}\t");
}
} else if let Some(from) = &result.local_ref {
let _ = write!(buf, "{flag}\t{from}:{to_name}\t");
} else {
let _ = write!(buf, "{flag}\t:{to_name}\t");
}
match &message {
Some(msg) => {
let _ = writeln!(buf, "{summary} ({msg})");
}
None => {
let _ = writeln!(buf, "{summary}");
}
}
} else {
let _ = write!(buf, " {flag} {summary:<summary_width$} ");
match &result.local_ref {
Some(from) if !result.deletion => {
let _ = write!(
buf,
"{} -> {}",
prettify_refname(from),
prettify_refname(&result.remote_ref)
);
}
_ => {
let _ = write!(buf, "{}", prettify_refname(&result.remote_ref));
}
}
if let Some(msg) = &message {
let _ = write!(buf, " ({msg})");
}
let _ = writeln!(buf);
}
}
if porcelain {
let _ = writeln!(buf, "Done");
}
out
}
#[cfg(test)]
mod tests {
use super::*;
fn oid(byte: u8) -> ObjectId {
ObjectId::from_bytes(&[byte; 20]).expect("valid 20-byte oid")
}
fn results() -> Vec<PushRefResult> {
vec![
PushRefResult {
local_ref: Some("refs/heads/main".to_owned()),
remote_ref: "refs/heads/main".to_owned(),
old_oid: Some(oid(0xbb)),
new_oid: Some(oid(0xaa)),
forced: false,
deletion: false,
status: PushRefStatus::RejectNonFastForward,
message: None,
},
PushRefResult {
local_ref: None,
remote_ref: "refs/heads/foo".to_owned(),
old_oid: Some(oid(0xaa)),
new_oid: None,
forced: false,
deletion: true,
status: PushRefStatus::Ok,
message: None,
},
PushRefResult {
local_ref: Some("refs/heads/baz".to_owned()),
remote_ref: "refs/heads/baz".to_owned(),
old_oid: Some(oid(0xaa)),
new_oid: Some(oid(0xaa)),
forced: false,
deletion: false,
status: PushRefStatus::UpToDate,
message: None,
},
PushRefResult {
local_ref: Some("refs/heads/next".to_owned()),
remote_ref: "refs/heads/next".to_owned(),
old_oid: None,
new_oid: Some(oid(0xaa)),
forced: false,
deletion: false,
status: PushRefStatus::Ok,
message: None,
},
]
}
#[test]
fn porcelain_orders_and_formats() {
let out = format_push_status("URL", &results(), true, false);
let expected = "To URL\n\
=\trefs/heads/baz:refs/heads/baz\t[up to date]\n\
-\t:refs/heads/foo\t[deleted]\n\
*\trefs/heads/next:refs/heads/next\t[new branch]\n\
!\trefs/heads/main:refs/heads/main\t[rejected] (non-fast-forward)\n\
Done\n";
assert_eq!(out.stdout, expected);
assert!(out.had_errors);
assert!(out.stderr.is_empty());
}
#[test]
fn quiet_suppresses_when_no_errors() {
let mut rs = results();
// Make main up-to-date so there are no errors.
rs[0].status = PushRefStatus::UpToDate;
let out = format_push_status("URL", &rs, true, true);
assert!(out.stdout.is_empty());
assert!(!out.had_errors);
}
#[test]
fn atomic_failure_message() {
let mut rs = results();
rs[1].status = PushRefStatus::AtomicPushFailed;
let out = format_push_status("URL", &rs, true, false);
// A rejected deletion (not REMOTE_REJECT) prints `(delete)` as its source,
// matching Git's `print_ref_status(... ref->peer_ref ...)`.
assert!(out
.stdout
.contains("!\t(delete):refs/heads/foo\t[rejected] (atomic push failed)"));
}
#[test]
fn remote_rejected_deletion_omits_delete_source() {
let mut rs = results();
rs[1].status = PushRefStatus::RemoteRejected;
rs[1].message = Some("pre-receive hook declined".to_owned());
let out = format_push_status("URL", &rs, true, false);
// REMOTE_REJECT explicitly passes NULL for a deletion: just `:dst`.
assert!(out
.stdout
.contains("!\t:refs/heads/foo\t[remote rejected] (pre-receive hook declined)"));
}
}