Skip to content
1,486 linesCodeBlameRaw
1//! The repos service: repository metadata, contents, forks and git access.
2//!
3//! Each `*Args` struct is the argument of the method of the same name,
4//! served at `POST /rpc/<method>`.
5
6use serde::{Deserialize, Serialize};
7
8use crate::{User, Viewer};
9
10#[derive(Clone, Debug, Serialize, Deserialize)]
11#[serde(rename_all = "camelCase")]
12pub struct Repo {
13 pub id: String,
14 /// The slug of the workspace that owns it: the first URL segment.
15 pub namespace: String,
16 pub name: String,
17 pub description: Option<String>,
18 pub is_private: bool,
19 pub owner_id: String,
20 pub default_branch: String,
21 /// Set when this repo is a pull request's working copy of another repo.
22 pub fork_of: Option<String>,
23 /// Whether the default branch is protected: it changes only by merging
24 /// a pull request, and pushes to it are refused.
25 #[serde(default)]
26 pub protected: bool,
27 /// RFC 3339.
28 pub created_at: String,
29 /// Words that say what it is about, for search and Explore: lowercase
30 /// letters, digits and hyphens. See [`clean_topics`].
31 #[serde(default)]
32 pub topics: Vec<String>,
33 /// Its home page, an http(s) address, shown beside its description.
34 /// See [`clean_website`].
35 #[serde(default)]
36 pub website: Option<String>,
37 /// RFC 3339: when it was archived, made read-only. Null when it is not.
38 #[serde(default)]
39 pub archived_at: Option<String>,
40 /// Set when it mirrors a remote that leads (see [`crate::mirrors`]).
41 /// Unless g1t has taken over, it is read-only.
42 #[serde(default, skip_serializing_if = "Option::is_none")]
43 pub mirror: Option<crate::mirrors::RepoMirror>,
44}
45
46impl Repo {
47 pub fn archived(&self) -> bool {
48 self.archived_at.is_some()
49 }
50
51 /// Whether it is a mirror that does not take writes now.
52 pub fn mirror_read_only(&self) -> bool {
53 self.mirror.as_ref().is_some_and(|mirror| !mirror.writable())
54 }
55
56 /// Why it takes no pushes, merges, issues or agents now: archived, or
57 /// a mirror standing by. `None` when it takes them.
58 pub fn read_only_reason(&self) -> Option<String> {
59 if self.archived() {
60 return Some(archived_message(&self.namespace, &self.name));
61 }
62 self.mirror
63 .as_ref()
64 .filter(|mirror| !mirror.writable())
65 .map(|mirror| crate::mirrors::mirror_message(&self.namespace, &self.name, mirror))
66 }
67}
68
69/// `storage_options` (no arguments, `{}`): what a workspace may choose
70/// about where its repositories are kept. `eu_available`: an EU namespace
71/// is configured and takes new repositories, so a workspace may keep its
72/// data in the EU (`set_workspace_residency` on identity). Returns
73/// `StorageOptions`.
74#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
75#[serde(rename_all = "camelCase")]
76pub struct StorageOptions {
77 pub eu_available: bool,
78}
79
80/// How long a deleted repository can be restored before it is purged.
81pub const RESTORE_DAYS: u64 = 30;
82
83/// A deleted repository, as its workspace's Recently deleted list shows
84/// it: restorable until `purge_after`.
85#[derive(Clone, Debug, Serialize, Deserialize)]
86#[serde(rename_all = "camelCase")]
87pub struct DeletedRepo {
88 pub id: String,
89 pub namespace: String,
90 pub name: String,
91 pub description: Option<String>,
92 pub is_private: bool,
93 /// RFC 3339.
94 pub deleted_at: String,
95 /// The username of who deleted it.
96 pub deleted_by: String,
97 /// RFC 3339: when it is purged, unless restored first.
98 pub purge_after: String,
99}
100
101/// The longest website address a repository keeps.
102pub const MAX_WEBSITE_CHARS: usize = 255;
103
104/// A website as it is kept: an http(s) address, `https://` added when no
105/// scheme is given; empty clears it. Anything else is refused.
106pub fn clean_website(text: &str) -> Result<Option<String>, String> {
107 let text = text.trim();
108 if text.is_empty() {
109 return Ok(None);
110 }
111 let url = if text.starts_with("https://") || text.starts_with("http://") {
112 text.to_owned()
113 } else if text.contains("://") {
114 return Err("A website is an http or https address.".into());
115 } else {
116 format!("https://{text}")
117 };
118 let host = url
119 .split("://")
120 .nth(1)
121 .unwrap_or("")
122 .split(['/', '?', '#'])
123 .next()
124 .unwrap_or("");
125 if url.chars().count() > MAX_WEBSITE_CHARS
126 || host.is_empty()
127 || !host.contains('.')
128 || url.chars().any(char::is_whitespace)
129 {
130 return Err("That is not a website address, such as https://example.com.".into());
131 }
132 Ok(Some(url))
133}
134
135/// Whether `name` can be a branch people name: what `git check-ref-format
136/// --branch` accepts, less the names g1t keeps for itself
137/// ([`G1T_BRANCH_PREFIX`]).
138pub fn is_valid_branch_name(name: &str) -> bool {
139 !name.is_empty()
140 && name.len() <= 200
141 && !name.starts_with('-')
142 && !name.starts_with('/')
143 && !name.ends_with('/')
144 && !name.ends_with('.')
145 && !name.ends_with(".lock")
146 && !name.contains("..")
147 && !name.contains("//")
148 && !name.contains("@{")
149 && name != "@"
150 && !name.starts_with(G1T_BRANCH_PREFIX)
151 && !name.split('/').any(|part| part.starts_with('.'))
152 && name
153 .chars()
154 .all(|c| !c.is_control() && !matches!(c, ' ' | '~' | '^' | ':' | '?' | '*' | '[' | '\\'))
155}
156
157/// The most topics a repository has.
158pub const MAX_TOPICS: usize = 20;
159/// The longest topic.
160pub const MAX_TOPIC_CHARS: usize = 35;
161
162/// Topics as they are kept: lowercase, spaces and underscores made
163/// hyphens, each of letters, digits and hyphens, starting with a letter or
164/// digit, without repeats, at most [`MAX_TOPICS`]. Anything else is the
165/// first topic that could not be read.
166pub fn clean_topics(topics: &[String]) -> Result<Vec<String>, String> {
167 let mut kept: Vec<String> = Vec::new();
168 for topic in topics {
169 let topic: String = topic
170 .trim()
171 .to_lowercase()
172 .chars()
173 .map(|c| if c == ' ' || c == '_' { '-' } else { c })
174 .collect();
175 if topic.is_empty() {
176 continue;
177 }
178 let valid = topic.chars().count() <= MAX_TOPIC_CHARS
179 && topic.chars().all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-')
180 && topic.chars().next().is_some_and(|c| c.is_ascii_alphanumeric());
181 if !valid {
182 return Err(format!(
183 "\"{topic}\" is not a topic: use letters, digits and hyphens, at most {MAX_TOPIC_CHARS} characters."
184 ));
185 }
186 if !kept.contains(&topic) {
187 kept.push(topic);
188 }
189 }
190 if kept.len() > MAX_TOPICS {
191 return Err(format!("A repository has at most {MAX_TOPICS} topics."));
192 }
193 Ok(kept)
194}
195
196#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
197pub struct RepoPath {
198 pub namespace: String,
199 pub name: String,
200}
201
202#[derive(Clone, Debug, Serialize, Deserialize)]
203pub struct Signature {
204 pub name: String,
205 pub email: String,
206}
207
208#[derive(Clone, Debug, Serialize, Deserialize)]
209#[serde(rename_all = "camelCase")]
210pub struct Commit {
211 pub hash: String,
212 pub tree_hash: String,
213 pub message: String,
214 pub author: Signature,
215 pub parents: Vec<String>,
216 /// RFC 3339.
217 pub authored_at: String,
218}
219
220#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
221#[serde(rename_all = "lowercase")]
222pub enum EntryKind {
223 Tree,
224 Blob,
225 Symlink,
226 Gitlink,
227 Exec,
228}
229
230#[derive(Clone, Debug, Serialize, Deserialize)]
231pub struct TreeEntry {
232 pub name: String,
233 pub hash: String,
234 pub kind: EntryKind,
235}
236
237#[derive(Clone, Debug, Serialize, Deserialize)]
238pub struct Readme {
239 pub name: String,
240 /// Null when the file is binary or too large to show.
241 pub text: Option<String>,
242}
243
244#[derive(Clone, Debug, Serialize, Deserialize)]
245pub struct TreeView {
246 pub repo: Repo,
247 #[serde(rename = "ref")]
248 pub git_ref: String,
249 pub path: String,
250 /// Null when the repo has no commits yet.
251 pub head: Option<Commit>,
252 pub entries: Vec<TreeEntry>,
253 pub readme: Option<Readme>,
254}
255
256#[derive(Clone, Debug, Serialize, Deserialize)]
257pub struct BlobView {
258 pub repo: Repo,
259 #[serde(rename = "ref")]
260 pub git_ref: String,
261 pub path: String,
262 pub size: u64,
263 /// Null when the file is binary or too large to show.
264 pub text: Option<String>,
265}
266
267/// A git remote and a short-lived credential for it.
268#[derive(Clone, Debug, Serialize, Deserialize)]
269pub struct GitAccess {
270 pub remote: String,
271 pub token: String,
272}
273
274#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
275pub enum GitService {
276 #[serde(rename = "git-upload-pack")]
277 UploadPack,
278 #[serde(rename = "git-receive-pack")]
279 ReceivePack,
280}
281
282/// The result of landing a pull request.
283#[derive(Clone, Debug, Serialize, Deserialize)]
284pub struct Landed {
285 /// The commit the branch points to now.
286 pub commit: String,
287 /// The commit it pointed to before, if it had one. Comparing against
288 /// this shows what the pull request changed.
289 pub previous: Option<String>,
290}
291
292/// `get`. Returns `Outcome<Repo>`.
293#[derive(Debug, Serialize, Deserialize)]
294pub struct GetArgs {
295 pub path: RepoPath,
296 pub viewer: Viewer,
297}
298
299/// `path_by_id`: where a repository is, whoever may see it. For g1t's own
300/// services, which hold a repository's id from an event and act for its
301/// workspace; nothing outside reaches it. Returns `Option<RepoPath>`, null
302/// for a fork or an unknown id.
303#[derive(Debug, Serialize, Deserialize)]
304pub struct PathByIdArgs {
305 pub id: String,
306}
307
308/// `get_by_id`. Returns `Outcome<Repo>`.
309#[derive(Debug, Serialize, Deserialize)]
310pub struct GetByIdArgs {
311 pub id: String,
312 pub viewer: Viewer,
313}
314
315/// `list`: repos the viewer may see, newest first. Returns `Vec<Repo>`.
316#[derive(Debug, Default, Serialize, Deserialize)]
317#[serde(rename_all = "camelCase")]
318pub struct ListArgs {
319 pub viewer: Viewer,
320 #[serde(default)]
321 pub query: Option<String>,
322 /// Only repos in this workspace.
323 #[serde(default)]
324 pub namespace: Option<String>,
325 /// Only repos in workspaces the viewer belongs to.
326 #[serde(default)]
327 pub member_only: bool,
328}
329
330/// `create`. Returns `Outcome<Repo>`.
331#[derive(Debug, Serialize, Deserialize)]
332#[serde(rename_all = "camelCase")]
333pub struct CreateArgs {
334 /// Who is creating it; they must belong to the workspace.
335 pub owner: User,
336 /// The workspace it is created in.
337 pub namespace: String,
338 pub name: String,
339 #[serde(default)]
340 pub description: Option<String>,
341 #[serde(default)]
342 pub is_private: bool,
343 /// The https address of a public git repository to copy the default
344 /// branch of, such as `https://github.com/owner/repo`.
345 #[serde(default)]
346 pub import_url: Option<String>,
347 /// With `import_url`: a GitHub installation access token that opens it,
348 /// for a private repository. Every branch and tag is then copied, not
349 /// only the default branch. Set only by the integrations service.
350 #[serde(default, skip_serializing_if = "Option::is_none")]
351 pub import_token: Option<String>,
352 /// Set by the integrations service for a mirror: it is read-only from
353 /// the start. See [`crate::mirrors`].
354 #[serde(default, skip_serializing_if = "Option::is_none")]
355 pub mirror: Option<crate::mirrors::RepoMirror>,
356}
357
358/// `mirror`: makes a repository's branches and tags match another git
359/// host's, or pushes its own out to one. Services only. Returns
360/// `Outcome<Mirrored>`.
361#[derive(Debug, Serialize, Deserialize)]
362#[serde(rename_all = "camelCase")]
363pub struct MirrorArgs {
364 pub repo_id: String,
365 /// The other host's https address, such as
366 /// `https://github.com/owner/repo.git`.
367 pub url: String,
368 /// A token for it, such as a GitHub installation access token. Opaque:
369 /// any length.
370 pub token: String,
371 /// The user the token is sent as, by basic authentication.
372 /// `x-access-token` (GitHub's) when absent.
373 #[serde(default, skip_serializing_if = "Option::is_none")]
374 pub username: Option<String>,
375 pub direction: MirrorDirection,
376}
377
378#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
379#[serde(rename_all = "snake_case")]
380pub enum MirrorDirection {
381 /// The repository on g1t follows the other host: its refs are moved,
382 /// and removed, to match.
383 Pull,
384 /// The other host follows g1t: refs g1t has are pushed there; refs only
385 /// the other host has are left alone.
386 Push,
387}
388
389/// What a `mirror` changed.
390#[derive(Clone, Debug, Default, Serialize, Deserialize)]
391#[serde(rename_all = "camelCase")]
392pub struct Mirrored {
393 /// Full ref names created or moved.
394 pub updated: Vec<String>,
395 pub deleted: Vec<String>,
396 /// Refs a pull moved somewhere their old commit is not part of (a
397 /// force-push on the remote), each as the `refs/g1t/replaced/...` ref
398 /// that keeps the old commit.
399 #[serde(default)]
400 pub replaced: Vec<String>,
401}
402
403/// `mirror_refs`: a repository's branches and tags, and another host's (only
404/// the repository's when `url` is empty).
405/// Services only. Returns `Outcome<MirrorRefs>`; when the other host does
406/// not answer, `theirs` is absent and `unreachable` says why. It fails
407/// when the other host answers and refuses.
408#[derive(Debug, Serialize, Deserialize)]
409#[serde(rename_all = "camelCase")]
410pub struct MirrorRefsArgs {
411 pub repo_id: String,
412 pub url: String,
413 pub token: String,
414 #[serde(default, skip_serializing_if = "Option::is_none")]
415 pub username: Option<String>,
416}
417
418#[derive(Clone, Debug, Default, Serialize, Deserialize)]
419#[serde(rename_all = "camelCase")]
420pub struct MirrorRefs {
421 /// Full ref name to commit (or tag) id, on g1t.
422 pub ours: std::collections::BTreeMap<String, String>,
423 /// The same, on the other host; absent when it did not answer.
424 pub theirs: Option<std::collections::BTreeMap<String, String>>,
425 /// Why the other host's refs are absent.
426 #[serde(default)]
427 pub unreachable: Option<String>,
428}
429
430/// One ref moved by `mirror_apply`, from one side to the other.
431#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
432#[serde(rename_all = "camelCase")]
433pub struct RefMove {
434 pub direction: MirrorDirection,
435 /// The ref on the side it comes from.
436 #[serde(rename = "ref")]
437 pub git_ref: String,
438 /// The ref it is written to; the same name when absent.
439 #[serde(default, skip_serializing_if = "Option::is_none")]
440 pub to: Option<String>,
441 /// What the target must hold now for the move to happen; absent when it
442 /// must not exist.
443 #[serde(default)]
444 pub old: Option<String>,
445 /// What it is moved to; absent to delete it.
446 #[serde(default)]
447 pub new: Option<String>,
448}
449
450/// `mirror_apply`: moves the given refs, each only if its target still
451/// holds `old`. Pulled refs that lose their old commit keep it under
452/// `refs/g1t/replaced/`. Services only. Returns `Outcome<MirrorApplied>`.
453#[derive(Debug, Serialize, Deserialize)]
454#[serde(rename_all = "camelCase")]
455pub struct MirrorApplyArgs {
456 pub repo_id: String,
457 pub url: String,
458 pub token: String,
459 #[serde(default, skip_serializing_if = "Option::is_none")]
460 pub username: Option<String>,
461 pub moves: Vec<RefMove>,
462}
463
464#[derive(Clone, Debug, Default, Serialize, Deserialize)]
465#[serde(rename_all = "camelCase")]
466pub struct MirrorApplied {
467 pub moved: Vec<RefMoved>,
468}
469
470#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
471#[serde(rename_all = "camelCase")]
472pub struct RefMoved {
473 #[serde(rename = "ref")]
474 pub git_ref: String,
475 pub direction: MirrorDirection,
476 /// Why it did not move; absent when it did.
477 #[serde(default)]
478 pub problem: Option<String>,
479 /// The `refs/g1t/replaced/...` ref that keeps what it pointed at.
480 #[serde(default)]
481 pub replaced: Option<String>,
482}
483
484/// `set_mirror`: records a repository's [`crate::mirrors::RepoMirror`], or
485/// clears it. Services only (integrations). Returns `Outcome<Repo>`.
486#[derive(Debug, Serialize, Deserialize)]
487#[serde(rename_all = "camelCase")]
488pub struct SetMirrorArgs {
489 pub repo_id: String,
490 pub mirror: Option<crate::mirrors::RepoMirror>,
491}
492
493/// `update`: changes whichever of a repository's details are given.
494/// Members of its workspace only. Returns `Outcome<Repo>`.
495#[derive(Debug, Serialize, Deserialize)]
496#[serde(rename_all = "camelCase")]
497pub struct UpdateArgs {
498 pub actor: User,
499 pub path: RepoPath,
500 /// An empty description clears it.
501 #[serde(default)]
502 pub description: Option<String>,
503 #[serde(default)]
504 pub is_private: Option<bool>,
505 #[serde(default)]
506 pub protected: Option<bool>,
507 /// Replaces its topics; an empty list clears them.
508 #[serde(default)]
509 pub topics: Option<Vec<String>>,
510 /// Its home page; an empty string clears it.
511 #[serde(default)]
512 pub website: Option<String>,
513 /// Where the request came in, for the audit log; g1t.sh when absent.
514 #[serde(default)]
515 pub surface: Option<crate::audit::Surface>,
516}
517
518/// `tree`. Returns `Outcome<TreeView>`.
519#[derive(Debug, Serialize, Deserialize)]
520#[serde(rename_all = "camelCase")]
521pub struct TreeArgs {
522 pub path: RepoPath,
523 pub viewer: Viewer,
524 /// The default branch when absent.
525 #[serde(default, rename = "ref")]
526 pub git_ref: Option<String>,
527 #[serde(default)]
528 pub tree_path: String,
529}
530
531/// `blob`. Returns `Outcome<BlobView>`.
532#[derive(Debug, Serialize, Deserialize)]
533#[serde(rename_all = "camelCase")]
534pub struct BlobArgs {
535 pub path: RepoPath,
536 pub viewer: Viewer,
537 #[serde(rename = "ref")]
538 pub git_ref: String,
539 pub file_path: String,
540}
541
542/// `log`. Returns `Outcome<Vec<Commit>>`.
543#[derive(Debug, Serialize, Deserialize)]
544pub struct LogArgs {
545 pub path: RepoPath,
546 pub viewer: Viewer,
547 #[serde(default, rename = "ref")]
548 pub git_ref: Option<String>,
549 pub limit: u32,
550}
551
552/// `fork_for_pull`: a copy-on-write copy of the source repo, hidden from
553/// listings, for one pull request to be made in. Returns `Outcome<Repo>`.
554#[derive(Debug, Serialize, Deserialize)]
555#[serde(rename_all = "camelCase")]
556pub struct ForkArgs {
557 pub source_id: String,
558 pub pull_id: String,
559 pub actor: User,
560}
561
562/// `git_access`: authorizes a git operation and says where to send it.
563/// Pushing to a repo that does not exist creates it in the pusher's own
564/// namespace. Returns `Outcome<GitAccess>`.
565#[derive(Debug, Serialize, Deserialize)]
566pub struct GitAccessArgs {
567 pub path: RepoPath,
568 pub viewer: Viewer,
569 pub service: GitService,
570}
571
572/// `land`: moves the branch a pull request merges into (the repository's
573/// default branch unless `target_branch` names another) to the head of
574/// its source. Refused with `conflict` when the source is behind, since
575/// that would discard commits. Returns `Outcome<Landed>`.
576#[derive(Debug, Serialize, Deserialize)]
577#[serde(rename_all = "camelCase")]
578pub struct LandArgs {
579 /// The repository holding the commits: a pull request's fork, or the
580 /// target itself when landing one of its own branches.
581 pub source_id: String,
582 /// The branch of the source to land. Required when the source is the
583 /// target; a fork lands its default branch.
584 #[serde(default)]
585 pub branch: Option<String>,
586 pub actor: User,
587 /// The branch of the target to land on; its default branch when absent.
588 #[serde(default)]
589 pub target_branch: Option<String>,
590}
591
592#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
593#[serde(rename_all = "lowercase")]
594pub enum FileStatus {
595 Added,
596 Modified,
597 Deleted,
598}
599
600#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
601#[serde(rename_all = "lowercase")]
602pub enum LineKind {
603 /// Unchanged, shown for context.
604 Context,
605 Add,
606 Delete,
607}
608
609#[derive(Clone, Debug, Serialize, Deserialize)]
610pub struct DiffLine {
611 pub kind: LineKind,
612 /// Line number in the old file; absent for added lines.
613 pub old: Option<u32>,
614 /// Line number in the new file; absent for deleted lines.
615 pub new: Option<u32>,
616 pub text: String,
617}
618
619/// A run of changed lines with their surrounding context.
620#[derive(Clone, Debug, Serialize, Deserialize)]
621pub struct Hunk {
622 pub lines: Vec<DiffLine>,
623}
624
625#[derive(Clone, Debug, Serialize, Deserialize)]
626pub struct FileDiff {
627 pub path: String,
628 pub status: FileStatus,
629 pub additions: u32,
630 pub deletions: u32,
631 /// True when the file is binary or too large, so no lines are shown.
632 pub binary: bool,
633 pub hunks: Vec<Hunk>,
634}
635
636/// What changed between two commits.
637#[derive(Clone, Debug, Serialize, Deserialize)]
638pub struct Comparison {
639 /// Null when the head has no earlier commit to compare against.
640 pub base: Option<String>,
641 pub head: String,
642 pub files: Vec<FileDiff>,
643 /// True when the change was too large to return in full.
644 pub truncated: bool,
645}
646
647/// `compare`: what `head` changes relative to `base`.
648///
649/// `head` is a branch or a commit, and defaults to the default branch.
650/// With no `base`, a fork is compared against the point where it and the
651/// repository it came from last agreed; a branch against the point where it
652/// left the default branch; and the default branch against its head's
653/// parent. Returns `Outcome<Comparison>`.
654#[derive(Debug, Serialize, Deserialize)]
655#[serde(rename_all = "camelCase")]
656pub struct CompareArgs {
657 pub repo_id: String,
658 pub viewer: Viewer,
659 #[serde(default)]
660 pub base: Option<String>,
661 #[serde(default)]
662 pub head: Option<String>,
663 /// With no `base`: the branch whose shared point with the head it is
664 /// compared from, instead of the default branch. A pull request into
665 /// another branch is compared this way.
666 #[serde(default)]
667 pub base_branch: Option<String>,
668}
669
670/// Lines `start` to `end` of a file, inclusive and counted from 1, last
671/// changed by `commit`.
672#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
673pub struct BlameRange {
674 pub start: u32,
675 pub end: u32,
676 pub commit: String,
677}
678
679/// Who last changed each line of a file.
680#[derive(Clone, Debug, Serialize, Deserialize)]
681pub struct Blame {
682 /// The commit the file was read at.
683 pub head: String,
684 /// Every line, in order, in runs that share a commit.
685 pub ranges: Vec<BlameRange>,
686 /// The commits the ranges name, each once.
687 pub commits: Vec<Commit>,
688 /// True when the history was too long to read in full, so the oldest
689 /// lines are given to the oldest commit read.
690 pub partial: bool,
691}
692
693/// `blame`: who last changed each line of `path` as of `ref` (the default
694/// branch if absent). Returns `Outcome<Blame>`; not found when the file is
695/// missing or is not text.
696#[derive(Debug, Serialize, Deserialize)]
697pub struct BlameArgs {
698 pub path: RepoPath,
699 pub viewer: Viewer,
700 #[serde(default, rename = "ref")]
701 pub git_ref: Option<String>,
702 #[serde(rename = "filePath")]
703 pub file_path: String,
704}
705
706/// A branch and the commit it points to.
707#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
708pub struct Branch {
709 pub name: String,
710 pub hash: String,
711}
712
713/// `last_commits`: which commit last changed each entry of a directory at
714/// `ref` (the default branch when absent). Returns `Outcome<LastCommits>`.
715#[derive(Debug, Serialize, Deserialize)]
716#[serde(rename_all = "camelCase")]
717pub struct LastCommitsArgs {
718 pub path: RepoPath,
719 pub viewer: Viewer,
720 #[serde(default, rename = "ref")]
721 pub git_ref: Option<String>,
722 #[serde(default)]
723 pub tree_path: String,
724 /// Answer within this many milliseconds with what was found, not kept;
725 /// absent, the walk runs to the end and is kept.
726 #[serde(default)]
727 pub budget_ms: Option<u64>,
728}
729
730/// An entry of a directory and the commit that last changed it.
731#[derive(Clone, Debug, Serialize, Deserialize)]
732pub struct LastCommit {
733 pub name: String,
734 pub commit: Commit,
735}
736
737/// The entries' last commits. `complete` is false when the history walked
738/// ran out before every entry was placed; those entries are left out.
739#[derive(Clone, Debug, Serialize, Deserialize)]
740pub struct LastCommits {
741 pub entries: Vec<LastCommit>,
742 pub complete: bool,
743}
744
745/// `branch_drift`: how far each of `heads` (branch head commits) has moved
746/// from `base` (the default branch's head commit), and each one's head
747/// commit, in one call. Every answer is kept by the pair of hashes: neither
748/// history can change, so neither can it. Returns `Outcome<BranchDrifts>`.
749#[derive(Debug, Serialize, Deserialize)]
750#[serde(rename_all = "camelCase")]
751pub struct BranchDriftArgs {
752 pub path: RepoPath,
753 pub viewer: Viewer,
754 pub base: String,
755 pub heads: Vec<String>,
756}
757
758/// Commits a branch has that the default branch does not (`ahead`), and
759/// the other way round (`behind`), as `git rev-list --left-right --count`.
760#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
761pub struct Drift {
762 pub ahead: u32,
763 pub behind: u32,
764}
765
766/// One branch head's commit and drift. `drift` is absent when the two
767/// histories do not meet within what is read (or could not be read).
768#[derive(Clone, Debug, Serialize, Deserialize)]
769pub struct BranchDrift {
770 pub head: String,
771 pub commit: Option<Commit>,
772 pub drift: Option<Drift>,
773}
774
775/// `base`'s own commit, and each head's answer in the order asked.
776#[derive(Clone, Debug, Serialize, Deserialize)]
777pub struct BranchDrifts {
778 pub base: Option<Commit>,
779 pub branches: Vec<BranchDrift>,
780}
781
782/// `tags`: the repository's tags, newest commit first, each with the
783/// commit it names. Returns `Outcome<Vec<Tag>>`.
784#[derive(Debug, Serialize, Deserialize)]
785pub struct TagsArgs {
786 pub path: RepoPath,
787 pub viewer: Viewer,
788}
789
790/// A tag, and its commit when it could be read.
791#[derive(Clone, Debug, Serialize, Deserialize)]
792pub struct Tag {
793 pub name: String,
794 pub commit: Option<Commit>,
795}
796
797/// `branches`: the repository's branches, default branch first.
798/// Returns `Outcome<Vec<Branch>>`.
799#[derive(Debug, Serialize, Deserialize)]
800pub struct BranchesArgs {
801 pub path: RepoPath,
802 pub viewer: Viewer,
803}
804
805/// `behind`: whether the default branch of the repository a pull request
806/// would merge into has commits its source does not. For services that
807/// have already decided the caller may see the pull request; it reveals
808/// one bit. Returns `bool`.
809#[derive(Debug, Serialize, Deserialize)]
810#[serde(rename_all = "camelCase")]
811pub struct BehindArgs {
812 /// The pull request's fork, or the repository itself for a branch.
813 pub source_id: String,
814 /// The branch of the source. A fork is compared on its default branch.
815 #[serde(default)]
816 pub branch: Option<String>,
817 /// The branch of the target it would merge into; the default branch
818 /// when absent.
819 #[serde(default)]
820 pub target_branch: Option<String>,
821}
822
823/// `divergence`: how a pull request's source and the default branch it
824/// would merge into have moved apart since they last agreed: the files each
825/// side changed. Takes `BehindArgs`. For services that have already decided
826/// the caller may see the pull request; it reveals paths, not contents.
827/// Returns `Option<Divergence>`, null when either side has no commits.
828#[derive(Clone, Debug, Default, Serialize, Deserialize)]
829#[serde(rename_all = "camelCase")]
830pub struct Divergence {
831 /// The source's commit.
832 pub head: String,
833 /// The default branch's commit.
834 pub base: String,
835 /// Where they last agreed, if that could be found.
836 pub merge_base: Option<String>,
837 /// Whether the default branch has commits the source does not.
838 pub behind: bool,
839 /// The files the source changed since the merge base.
840 pub ours: Vec<String>,
841 /// The files the default branch changed since the merge base. Empty
842 /// when it is not behind.
843 pub theirs: Vec<String>,
844 /// Whether either list was cut short.
845 pub truncated: bool,
846}
847
848/// `update_pull_branch`: brings a pull request's source up to date with the
849/// default branch it would merge into, without a sandbox, when that can be
850/// done safely: merges the default branch's head into the source's head and
851/// pushes the merge commit to the source's branch, as `actor`, only if the
852/// branch has not moved meanwhile. It applies only when the two sides
853/// changed different files since they last agreed; otherwise the answer is
854/// [`PullBranchUpdate::NeedsAgent`] and nothing is pushed. Refused unless
855/// `actor` may push to the source. Returns `Outcome<PullBranchUpdate>`.
856#[derive(Debug, Serialize, Deserialize)]
857#[serde(rename_all = "camelCase")]
858pub struct UpdatePullBranchArgs {
859 /// The pull request's fork, or the repository itself for a branch.
860 pub source_id: String,
861 /// The branch of the source. A fork is updated on its default branch.
862 #[serde(default)]
863 pub branch: Option<String>,
864 /// The pull request's number, to name it in the merge commit's message
865 /// when its branch has the same name as the default branch.
866 pub number: u32,
867 /// Who asked: the merge commit's author and committer, and the pusher.
868 pub actor: User,
869 /// The branch of the target to merge in; its default branch when absent.
870 #[serde(default)]
871 pub target_branch: Option<String>,
872}
873
874/// Why an update has to be left to a sandbox.
875#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
876#[serde(rename_all = "snake_case")]
877pub enum NeedsAgentReason {
878 /// Both sides changed some of the same files; merging them needs a
879 /// real merge, which may or may not conflict.
880 Overlap,
881 /// Merging is known to conflict.
882 Conflicting,
883 /// The update could not be worked out here, such as when the two sides
884 /// share no history g1t can see, or the change is too large to list.
885 Unsupported,
886}
887
888/// What came of `update_pull_branch` (or the work service's `catch_up_pull`).
889#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
890#[serde(tag = "outcome", rename_all = "snake_case")]
891pub enum PullBranchUpdate {
892 /// The merge commit was pushed: the branch moved from `previous` to
893 /// `commit`.
894 Updated { commit: String, previous: String },
895 /// The source already holds the default branch's head.
896 UpToDate { commit: String },
897 /// Nothing was pushed; a sandbox has to merge it. `paths` are the
898 /// files both sides changed, or that conflict, when known.
899 NeedsAgent {
900 reason: NeedsAgentReason,
901 detail: String,
902 paths: Vec<String>,
903 },
904}
905
906/// `head`: the commit a branch points to, or null. For services reacting
907/// to a push, which have no viewer; it reveals nothing but a commit hash.
908/// Returns `Option<String>`.
909#[derive(Debug, Serialize, Deserialize)]
910#[serde(rename_all = "camelCase")]
911pub struct HeadArgs {
912 pub repo_id: String,
913 /// Empty for the repository's default branch.
914 pub branch: String,
915}
916
917/// Where g1t keeps branches of its own in a repository, such as the merge
918/// queue's tested states. Only these can be removed with `delete_branch`.
919pub const G1T_BRANCH_PREFIX: &str = "g1t-";
920
921/// `delete_branch`: removes a branch g1t made for itself once it is done
922/// with it, never one of people's: the name must start with
923/// [`G1T_BRANCH_PREFIX`]. For services, which have no viewer. Returns
924/// `Outcome<bool>`: whether there was such a branch.
925#[derive(Debug, Serialize, Deserialize)]
926#[serde(rename_all = "camelCase")]
927pub struct DeleteBranchArgs {
928 pub repo_id: String,
929 pub branch: String,
930}
931
932/// `commit_file`: writes one file on a new branch made from the default
933/// branch's head, as one commit by `actor`, without a sandbox. For a change
934/// g1t proposes on someone's behalf, such as a starter workflow, which then
935/// becomes a pull request. Refused unless `actor` may push, when the branch
936/// already exists, or when the file is already there. Returns
937/// `Outcome<CommittedFile>`.
938#[derive(Debug, Serialize, Deserialize)]
939#[serde(rename_all = "camelCase")]
940pub struct CommitFileArgs {
941 pub repo: RepoPath,
942 pub actor: User,
943 /// The new branch, which must not exist yet.
944 pub branch: String,
945 /// Where the file goes, such as `.g1t/workflows/ci.yml`.
946 pub path: String,
947 pub content: String,
948 pub message: String,
949}
950
951/// The commit `commit_file` made.
952#[derive(Clone, Debug, Serialize, Deserialize)]
953#[serde(rename_all = "camelCase")]
954pub struct CommittedFile {
955 pub branch: String,
956 pub commit: String,
957}
958
959/// `readable`: of these repository ids, the repositories the viewer may
960/// read, as `get_by_id` decides; forks and unknown ids are left out. For
961/// services that hold ids and must show only what the viewer could open.
962/// At most [`MAX_READABLE`] ids are looked at. Returns `Vec<Repo>`.
963#[derive(Debug, Serialize, Deserialize)]
964pub struct ReadableArgs {
965 pub ids: Vec<String>,
966 pub viewer: Viewer,
967}
968
969/// The most ids one `readable` call looks at.
970pub const MAX_READABLE: usize = 500;
971
972/// `public_namespaces`: the workspaces in which this account made a public
973/// repository, and so a public project, which anyone can see on its page.
974/// Returns `Vec<String>` of workspace slugs.
975#[derive(Debug, Serialize, Deserialize)]
976#[serde(rename_all = "camelCase")]
977pub struct PublicNamespacesArgs {
978 pub owner_id: String,
979}
980
981/// One file on a branch, or one a change touched: its path and blob.
982#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
983pub struct FileEntry {
984 pub path: String,
985 /// The blob it holds now; null when the change deleted it.
986 pub hash: Option<String>,
987}
988
989/// Files, and whether there were more than were listed.
990#[derive(Clone, Debug, Default, Serialize, Deserialize)]
991#[serde(rename_all = "camelCase")]
992pub struct FileList {
993 /// The commit the files were read at; null for an empty repository.
994 pub commit: Option<String>,
995 pub files: Vec<FileEntry>,
996 pub truncated: bool,
997}
998
999/// `list_files`: every file on a branch (the default branch when absent),
1000/// path order by level, never descending into a directory named in
1001/// `skip_dirs`. For services that index a repository; no viewer, since it
1002/// is only reached by g1t's own services. Returns `FileList`.
1003#[derive(Debug, Default, Serialize, Deserialize)]
1004#[serde(rename_all = "camelCase")]
1005pub struct ListFilesArgs {
1006 pub repo_id: String,
1007 #[serde(default, rename = "ref")]
1008 pub git_ref: Option<String>,
1009 #[serde(default)]
1010 pub skip_dirs: Vec<String>,
1011 /// At most this many files; capped at [`MAX_LISTED_FILES`].
1012 pub limit: u32,
1013}
1014
1015/// `changed_files`: the files that differ between two commits, as
1016/// `list_files` reads them. With no `base`, every file at `head`. Returns
1017/// `FileList`.
1018#[derive(Debug, Default, Serialize, Deserialize)]
1019#[serde(rename_all = "camelCase")]
1020pub struct ChangedFilesArgs {
1021 pub repo_id: String,
1022 #[serde(default)]
1023 pub base: Option<String>,
1024 pub head: String,
1025 #[serde(default)]
1026 pub skip_dirs: Vec<String>,
1027 pub limit: u32,
1028}
1029
1030/// The most files one `list_files` or `changed_files` call lists.
1031pub const MAX_LISTED_FILES: u32 = 10_000;
1032
1033/// `read_blobs`: the text of these blobs of a repository, for services
1034/// that index it. A blob larger than `max_bytes`, or binary, comes back
1035/// with no text. Returns `Vec<BlobText>`, in the order asked.
1036#[derive(Debug, Default, Serialize, Deserialize)]
1037#[serde(rename_all = "camelCase")]
1038pub struct ReadBlobsArgs {
1039 pub repo_id: String,
1040 pub hashes: Vec<String>,
1041 pub max_bytes: u32,
1042}
1043
1044/// The most blobs one `read_blobs` call reads.
1045pub const MAX_READ_BLOBS: usize = 100;
1046
1047/// `refs`: a repository's branches and tags with the commit each points to
1048/// (annotated tags peeled), for services that follow them, such as the
1049/// packages service's Composer registry. No viewer: g1t's own services
1050/// only. Returns `Option<RepoRefs>`, null for a fork or an unknown id.
1051#[derive(Debug, Default, Serialize, Deserialize)]
1052#[serde(rename_all = "camelCase")]
1053pub struct RefsArgs {
1054 pub repo_id: String,
1055}
1056
1057#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
1058pub struct GitRefEntry {
1059 /// The full ref: `refs/heads/main`, `refs/tags/v1.0.0`.
1060 pub name: String,
1061 pub commit: String,
1062}
1063
1064#[derive(Clone, Debug, Serialize, Deserialize)]
1065pub struct RepoRefs {
1066 pub repo: Repo,
1067 pub refs: Vec<GitRefEntry>,
1068}
1069
1070/// `raw_file`: one file's bytes at a ref or commit, base64, for g1t's own
1071/// services (no viewer). Returns `Option<RawFile>`: null when the file is
1072/// missing or larger than `max_bytes`.
1073#[derive(Debug, Default, Serialize, Deserialize)]
1074#[serde(rename_all = "camelCase")]
1075pub struct RawFileArgs {
1076 pub repo_id: String,
1077 #[serde(rename = "ref")]
1078 pub git_ref: String,
1079 pub path: String,
1080 pub max_bytes: u32,
1081}
1082
1083#[derive(Clone, Debug, Serialize, Deserialize)]
1084pub struct RawFile {
1085 pub size: u64,
1086 /// Standard base64.
1087 pub data: String,
1088}
1089
1090/// `raw_blobs`: blobs' bytes, base64, in the order asked, at most
1091/// [`MAX_READ_BLOBS`]; `data` is null for one missing or larger than
1092/// `max_bytes`. For g1t's own services. Returns `Vec<RawBlob>`.
1093#[derive(Debug, Default, Serialize, Deserialize)]
1094#[serde(rename_all = "camelCase")]
1095pub struct RawBlobsArgs {
1096 pub repo_id: String,
1097 pub hashes: Vec<String>,
1098 pub max_bytes: u32,
1099}
1100
1101#[derive(Clone, Debug, Serialize, Deserialize)]
1102pub struct RawBlob {
1103 pub hash: String,
1104 pub size: u64,
1105 pub data: Option<String>,
1106}
1107
1108#[derive(Clone, Debug, Serialize, Deserialize)]
1109pub struct BlobText {
1110 pub hash: String,
1111 pub size: u64,
1112 /// Null when the blob is missing, binary or larger than asked.
1113 pub text: Option<String>,
1114}
1115
1116/// `all_ids`: every repository that is not a fork, by id, a page at a
1117/// time, for services that index all of them. Returns `IdPage`.
1118#[derive(Debug, Default, Serialize, Deserialize)]
1119pub struct AllIdsArgs {
1120 /// Ids after this one.
1121 #[serde(default)]
1122 pub after: Option<String>,
1123 pub limit: u32,
1124}
1125
1126#[derive(Clone, Debug, Default, Serialize, Deserialize)]
1127pub struct IdPage {
1128 pub ids: Vec<String>,
1129 /// Where the next page starts; null on the last.
1130 pub next: Option<String>,
1131}
1132
1133/// `repo_creators` takes [`AllIdsArgs`]: every repository that is not a
1134/// fork, with the account that created it, a page at a time, for identity
1135/// giving creators the Admin role. Returns [`CreatorPage`].
1136#[derive(Clone, Debug, Default, Serialize, Deserialize)]
1137pub struct CreatorPage {
1138 pub repos: Vec<RepoCreator>,
1139 /// Where the next page starts; null on the last.
1140 pub next: Option<String>,
1141}
1142
1143#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
1144pub struct RepoCreator {
1145 pub id: String,
1146 /// Its workspace's slug.
1147 pub namespace: String,
1148 pub name: String,
1149 /// The account that created it.
1150 pub owner_id: String,
1151}
1152
1153/// `visibility`: which of these repositories (`namespace/name`) are
1154/// private, for billing, which pays for work on public ones from g1t's
1155/// open-source pool. A pull request's working copy answers as the
1156/// repository it is a copy of. Unknown paths are left out. Returns
1157/// `Vec<RepoVisibility>`.
1158#[derive(Debug, Default, Serialize, Deserialize)]
1159pub struct VisibilityArgs {
1160 pub paths: Vec<String>,
1161}
1162
1163#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
1164pub struct RepoVisibility {
1165 pub path: String,
1166 pub is_private: bool,
1167}
1168
1169/// `git_operations`: how many git operations (clones, fetches and pushes
1170/// through g1t's git endpoints) each workspace's repositories had in a
1171/// month, for billing's git meter. Cloudflare Artifacts charges per
1172/// operation from 2026-10-14. Pushes from agents' sandboxes go to the
1173/// store directly and are not counted here. Returns
1174/// `Vec<WorkspaceGitOperations>`.
1175#[derive(Debug, Serialize, Deserialize)]
1176pub struct GitOperationsArgs {
1177 /// YYYY-MM.
1178 pub month: String,
1179 /// Count only from this hour on, `YYYY-MM-DDTHH`, such as the day the
1180 /// provider starts charging.
1181 #[serde(default)]
1182 pub since: Option<String>,
1183 /// One workspace only; every workspace with any when absent.
1184 #[serde(default)]
1185 pub namespace: Option<String>,
1186}
1187
1188#[derive(Clone, Debug, Serialize, Deserialize)]
1189pub struct WorkspaceGitOperations {
1190 pub namespace: String,
1191 pub operations: u64,
1192}
1193
1194/// `storage`: what each workspace's private repositories hold, as far as
1195/// g1t can measure it, for billing's daily storage meter. Returns
1196/// `Vec<WorkspaceStorage>`.
1197///
1198/// The git store does not report a repository's size. What is counted is
1199/// the bytes of every pack pushed through g1t's git endpoints to the
1200/// repository or to its pull requests' working copies. Pushes made from
1201/// agents' sandboxes, which go to the store directly, and imports are not
1202/// counted, so it is a lower bound on what is stored.
1203#[derive(Debug, Default, Serialize, Deserialize)]
1204pub struct StorageArgs {}
1205
1206#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
1207pub struct WorkspaceStorage {
1208 pub namespace: String,
1209 pub private_bytes: i64,
1210 pub public_bytes: i64,
1211}
1212
1213/// `transfer`: moves a repository to another workspace, keeping its name,
1214/// its id and everything kept under it. The actor must own both
1215/// workspaces. The old path keeps working as a redirect (see
1216/// `resolve_path`) until a repository is made there. Publishes
1217/// `repo.transferred`. Returns `Outcome<Repo>`, the repository at its new
1218/// path.
1219#[derive(Debug, Serialize, Deserialize)]
1220#[serde(rename_all = "camelCase")]
1221pub struct TransferArgs {
1222 pub actor: User,
1223 pub path: RepoPath,
1224 /// The destination workspace's slug.
1225 pub to: String,
1226 /// Where the request came in, for the audit log; g1t.sh when absent.
1227 #[serde(default)]
1228 pub surface: Option<crate::audit::Surface>,
1229}
1230
1231/// `resolve_path`: where a repository that was transferred away from
1232/// `path` is now, while nothing else is there. Returns `Option<RepoPath>`:
1233/// null when `path` is a repository, or never was one that moved. Callers
1234/// check the viewer may see the repository at its new path, as for any
1235/// other.
1236#[derive(Debug, Serialize, Deserialize)]
1237pub struct ResolvePathArgs {
1238 pub path: RepoPath,
1239}
1240
1241/// `namespace_count`: how many repositories (not pull request working
1242/// copies) a workspace holds, private or not, for deciding whether it can
1243/// be deleted. Returns `u32`.
1244#[derive(Debug, Serialize, Deserialize)]
1245pub struct NamespaceCountArgs {
1246 pub namespace: String,
1247}
1248
1249/// `delete`: deletes a repository. Owners of its workspace only, who type
1250/// its full name (`namespace/name`) as `confirm`. It is hidden at once,
1251/// git refuses it, and nothing runs for it; it can be restored for
1252/// [`RESTORE_DAYS`] days, then it is purged, its git data with it. Its
1253/// name stays taken until then, or until it is purged sooner from the
1254/// workspace's Recently deleted list. Publishes `repo.deleted`. Returns
1255/// `Outcome<DeletedRepo>`.
1256#[derive(Debug, Serialize, Deserialize)]
1257#[serde(rename_all = "camelCase")]
1258pub struct DeleteArgs {
1259 pub actor: User,
1260 pub path: RepoPath,
1261 #[serde(default)]
1262 pub confirm: String,
1263 #[serde(default)]
1264 pub surface: Option<crate::audit::Surface>,
1265}
1266
1267/// `deleted`: a workspace's recently deleted repositories, newest first.
1268/// Owners only; empty for anyone else. Returns `Vec<DeletedRepo>`.
1269#[derive(Debug, Serialize, Deserialize)]
1270pub struct DeletedArgs {
1271 pub viewer: Viewer,
1272 pub namespace: String,
1273}
1274
1275/// `restore` and `purge`: a deleted repository, by the path it had.
1276/// `restore` brings it back as it was, at that path (`repo.restored`).
1277/// `purge` removes it for good now, its git data with it, and frees its
1278/// name (`repo.purged`); it takes the full name typed as `confirm`.
1279/// Owners only. Return `Outcome<Repo>` and `Outcome<bool>`.
1280#[derive(Debug, Serialize, Deserialize)]
1281#[serde(rename_all = "camelCase")]
1282pub struct DeletedRepoArgs {
1283 pub actor: User,
1284 pub path: RepoPath,
1285 #[serde(default)]
1286 pub confirm: Option<String>,
1287 #[serde(default)]
1288 pub surface: Option<crate::audit::Surface>,
1289}
1290
1291/// `purge_due`: purges deleted repositories whose time has passed, at
1292/// most `limit` (25 when absent). The service's own schedule runs it.
1293/// Returns `u32`, how many were purged.
1294#[derive(Debug, Default, Serialize, Deserialize)]
1295pub struct PurgeDueArgs {
1296 #[serde(default)]
1297 pub limit: Option<u32>,
1298}
1299
1300/// `rename`: gives a repository a new name in its workspace, keeping its
1301/// id, its git data and everything kept under it. Owners only. The old
1302/// path keeps redirecting, as after a transfer, until a repository is made
1303/// there. Publishes `repo.renamed`. Returns `Outcome<Repo>`.
1304#[derive(Debug, Serialize, Deserialize)]
1305#[serde(rename_all = "camelCase")]
1306pub struct RenameArgs {
1307 pub actor: User,
1308 pub path: RepoPath,
1309 pub name: String,
1310 #[serde(default)]
1311 pub surface: Option<crate::audit::Surface>,
1312}
1313
1314/// `archive`: makes a repository read-only (`archived: true`), or writable
1315/// again. Owners only. While archived, pushes and merges are refused,
1316/// issues and pull requests are locked, and agents and workflows do not
1317/// run; deployments keep serving. Publishes `repo.archived` or
1318/// `repo.unarchived`. Returns `Outcome<Repo>`.
1319#[derive(Debug, Serialize, Deserialize)]
1320#[serde(rename_all = "camelCase")]
1321pub struct ArchiveArgs {
1322 pub actor: User,
1323 pub path: RepoPath,
1324 pub archived: bool,
1325 #[serde(default)]
1326 pub surface: Option<crate::audit::Surface>,
1327}
1328
1329/// `set_visibility`: makes a repository public or private. Owners only,
1330/// who type its full name as `confirm`. A free workspace takes a private
1331/// repository only while its private storage has room. Publishes
1332/// `repo.updated` and `repo.visibility_changed`. Returns `Outcome<Repo>`.
1333#[derive(Debug, Serialize, Deserialize)]
1334#[serde(rename_all = "camelCase")]
1335pub struct SetVisibilityArgs {
1336 pub actor: User,
1337 pub path: RepoPath,
1338 pub is_private: bool,
1339 #[serde(default)]
1340 pub confirm: String,
1341 #[serde(default)]
1342 pub surface: Option<crate::audit::Surface>,
1343}
1344
1345/// `set_default_branch`: makes another existing branch the one everything
1346/// lands on. Members of its workspace. Open pull requests then merge into
1347/// it. Publishes `repo.default_branch_changed`. Returns `Outcome<Repo>`.
1348#[derive(Debug, Serialize, Deserialize)]
1349#[serde(rename_all = "camelCase")]
1350pub struct SetDefaultBranchArgs {
1351 pub actor: User,
1352 pub path: RepoPath,
1353 pub branch: String,
1354 #[serde(default)]
1355 pub surface: Option<crate::audit::Surface>,
1356}
1357
1358/// `rename_branch`: renames a branch. Members of its workspace; only an
1359/// owner renames the default branch, which stays the default. Pull
1360/// requests from it follow, and web addresses naming the old branch
1361/// redirect until a branch of that name is made again. Publishes
1362/// `branch.renamed` (and `repo.default_branch_changed` for the default).
1363/// Returns `Outcome<Repo>`.
1364#[derive(Debug, Serialize, Deserialize)]
1365#[serde(rename_all = "camelCase")]
1366pub struct RenameBranchArgs {
1367 pub actor: User,
1368 pub path: RepoPath,
1369 pub from: String,
1370 pub to: String,
1371 #[serde(default)]
1372 pub surface: Option<crate::audit::Surface>,
1373}
1374
1375/// `resolve_branch`: what a branch renamed away from `branch` is called
1376/// now, for web addresses that name the old one; null when `branch` was
1377/// never renamed or exists again. Returns `Option<String>`.
1378#[derive(Debug, Serialize, Deserialize)]
1379#[serde(rename_all = "camelCase")]
1380pub struct ResolveBranchArgs {
1381 pub repo_id: String,
1382 pub branch: String,
1383}
1384
1385/// `status_by_id`: whether a repository is archived or deleted, for g1t's
1386/// own services deciding whether to act on it. An unknown id answers as
1387/// deleted. Returns `RepoStatus`.
1388#[derive(Debug, Serialize, Deserialize)]
1389pub struct StatusByIdArgs {
1390 pub id: String,
1391}
1392
1393#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
1394pub struct RepoStatus {
1395 pub archived: bool,
1396 pub deleted: bool,
1397}
1398
1399impl RepoStatus {
1400 /// Whether work may start on it: neither archived nor deleted.
1401 pub fn active(&self) -> bool {
1402 !self.archived && !self.deleted
1403 }
1404}
1405
1406/// What a person is told when something would change an archived
1407/// repository.
1408pub fn archived_message(namespace: &str, name: &str) -> String {
1409 format!(
1410 "{namespace}/{name} is archived, so it is read-only. An owner can unarchive it in its settings."
1411 )
1412}
1413
1414/// The path a repository was transferred from, and when, as `transfer`
1415/// keeps it so old addresses redirect.
1416#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
1417#[serde(rename_all = "camelCase")]
1418pub struct RepoRedirect {
1419 pub from: RepoPath,
1420 pub repo_id: String,
1421 /// RFC 3339.
1422 pub created_at: String,
1423}
1424
1425#[cfg(test)]
1426mod topic_tests {
1427 use super::*;
1428
1429 fn topics(list: &[&str]) -> Result<Vec<String>, String> {
1430 clean_topics(&list.iter().map(|t| t.to_string()).collect::<Vec<_>>())
1431 }
1432
1433 #[test]
1434 fn topics_are_tidied() {
1435 assert_eq!(topics(&["Rust", " web_server ", "rust", ""]).unwrap(), vec!["rust", "web-server"]);
1436 }
1437
1438 #[test]
1439 fn websites_are_tidied() {
1440 assert_eq!(clean_website(" example.com ").unwrap().as_deref(), Some("https://example.com"));
1441 assert_eq!(clean_website("http://a.io/x").unwrap().as_deref(), Some("http://a.io/x"));
1442 assert_eq!(clean_website("").unwrap(), None);
1443 assert!(clean_website("ftp://a.io").is_err());
1444 assert!(clean_website("localhost").is_err());
1445 assert!(clean_website("https://a b.io").is_err());
1446 }
1447
1448 #[test]
1449 fn branch_names_follow_git() {
1450 for good in ["main", "trunk", "release/1.2", "feat-x_y"] {
1451 assert!(is_valid_branch_name(good), "{good}");
1452 }
1453 for bad in ["", "-x", "a..b", "a b", "x.lock", "a/", ".hidden", "a/.b", "g1t-queue", "a~1", "a:b", "@"] {
1454 assert!(!is_valid_branch_name(bad), "{bad}");
1455 }
1456 }
1457
1458 #[test]
1459 fn odd_topics_are_refused() {
1460 assert!(topics(&["c++"]).is_err());
1461 assert!(topics(&["-lead"]).is_err());
1462 assert!(topics(&[&"a".repeat(36)]).is_err());
1463 let many: Vec<String> = (0..21).map(|i| format!("t{i}")).collect();
1464 assert!(clean_topics(&many).is_err());
1465 }
1466}
1467
1468#[cfg(test)]
1469mod tests {
1470 use super::*;
1471
1472 #[test]
1473 fn a_pull_branch_update_reads_as_the_web_expects() {
1474 let update = PullBranchUpdate::NeedsAgent {
1475 reason: NeedsAgentReason::Overlap,
1476 detail: "both".into(),
1477 paths: vec!["a.rs".into()],
1478 };
1479 assert_eq!(
1480 serde_json::to_value(&update).unwrap(),
1481 serde_json::json!({ "outcome": "needs_agent", "reason": "overlap", "detail": "both", "paths": ["a.rs"] })
1482 );
1483 let done = PullBranchUpdate::UpToDate { commit: "c".into() };
1484 assert_eq!(serde_json::to_value(&done).unwrap(), serde_json::json!({ "outcome": "up_to_date", "commit": "c" }));
1485 }
1486}