| 1 | use super::tests::{git, repo}; |
| 2 | use super::*; |
| 3 | use std::fs; |
| 4 | |
| 5 | fn request(body: Value) -> GitPushRequest { |
| 6 | serde_json::from_value(body).expect("push request") |
| 7 | } |
| 8 | |
| 9 | fn status_of(error: GitWriteError) -> (StatusCode, Option<&'static str>, Value) { |
| 10 | match error { |
| 11 | GitWriteError::Api(error) => (error.status, None, Value::Null), |
| 12 | GitWriteError::Coded { |
| 13 | status, |
| 14 | code, |
| 15 | extra, |
| 16 | .. |
| 17 | } => ( |
| 18 | status, |
| 19 | Some(code), |
| 20 | extra.get("stale").cloned().unwrap_or_default(), |
| 21 | ), |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | fn output(dir: &FsPath, args: &[&str]) -> String { |
| 26 | let output = Git::output(args, dir).expect("spawn git"); |
| 27 | assert!(output.status.success(), "git {args:?}: {output:?}"); |
| 28 | String::from_utf8_lossy(&output.stdout).trim().to_string() |
| 29 | } |
| 30 | |
| 31 | fn refs(bare: &FsPath) -> Vec<String> { |
| 32 | output(bare, &["for-each-ref", "--format=%(refname) %(objectname)"]) |
| 33 | .lines() |
| 34 | .map(str::to_string) |
| 35 | .collect() |
| 36 | } |
| 37 | |
| 38 | fn commit(dir: &FsPath, name: &str) -> String { |
| 39 | fs::write(dir.join(name), name).unwrap(); |
| 40 | git(dir, &["add", name]); |
| 41 | git(dir, &["commit", "-q", "-m", name]); |
| 42 | output(dir, &["rev-parse", "HEAD"]) |
| 43 | } |
| 44 | |
| 45 | async fn refused(dir: &FsPath, body: Value) -> (StatusCode, Option<&'static str>, Value) { |
| 46 | match push(dir, request(body)).await { |
| 47 | Ok(response) => panic!("push was not refused: {}", response.0), |
| 48 | Err(error) => status_of(error), |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | #[tokio::test] |
| 53 | async fn push_args_name_one_configured_remote_and_one_commit_refspec() { |
| 54 | let tmp = repo(); |
| 55 | let root = tmp.path(); |
| 56 | let plan = |remote: Option<&'static str>, set_upstream: bool| async move { |
| 57 | match push_plan(root, remote, set_upstream, None).await { |
| 58 | Ok(Ok(plan)) => Ok(plan), |
| 59 | Ok(Err(error)) => Err(status_of(error).0), |
| 60 | Err(error) => Err(error.status), |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | for (remote, set_upstream) in [(None, false), (Some(" "), false), (None, true)] { |
| 65 | assert_eq!( |
| 66 | plan(remote, set_upstream).await.unwrap_err(), |
| 67 | StatusCode::BAD_REQUEST, |
| 68 | "{remote:?} without a configured origin" |
| 69 | ); |
| 70 | } |
| 71 | |
| 72 | let bare = tempfile::tempdir().unwrap(); |
| 73 | git(bare.path(), &["init", "-q", "--bare"]); |
| 74 | git( |
| 75 | root, |
| 76 | &["remote", "add", "origin", &bare.path().to_string_lossy()], |
| 77 | ); |
| 78 | let head = output(root, &["rev-parse", "HEAD"]); |
| 79 | let refspec = format!("{head}:refs/heads/main"); |
| 80 | let explicit = plan(Some(" origin "), false).await.unwrap(); |
| 81 | assert_eq!( |
| 82 | explicit.args, |
| 83 | [ |
| 84 | "push", |
| 85 | "--no-follow-tags", |
| 86 | "--no-recurse-submodules", |
| 87 | "--", |
| 88 | "origin", |
| 89 | refspec.as_str() |
| 90 | ] |
| 91 | ); |
| 92 | assert!(explicit.upstream.is_none()); |
| 93 | for remote in ["--mirror", "fork", "../other"] { |
| 94 | assert_eq!( |
| 95 | plan(Some(remote), true).await.unwrap_err(), |
| 96 | StatusCode::BAD_REQUEST, |
| 97 | "{remote}" |
| 98 | ); |
| 99 | } |
| 100 | let upstream = plan(None, true).await.unwrap(); |
| 101 | assert_eq!(upstream.args, explicit.args); |
| 102 | assert_eq!( |
| 103 | upstream.upstream, |
| 104 | Some(("main".to_string(), "origin".to_string())) |
| 105 | ); |
| 106 | |
| 107 | git(root, &["checkout", "-q", "--detach"]); |
| 108 | assert_eq!( |
| 109 | plan(None, false).await.unwrap_err(), |
| 110 | StatusCode::BAD_REQUEST |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | #[test] |
| 115 | fn git_push_named_fields_are_validated_before_git_runs() { |
| 116 | let head = "a".repeat(40); |
| 117 | let expect = |body: Value| serde_json::from_value::<GitExpect>(body).unwrap(); |
| 118 | assert_eq!(named_push(None, None).unwrap(), None); |
| 119 | assert_eq!( |
| 120 | named_push( |
| 121 | Some(" main "), |
| 122 | Some(expect(json!({ "head": head.to_uppercase() }))) |
| 123 | ) |
| 124 | .unwrap(), |
| 125 | Some(NamedPush { |
| 126 | branch: "main".to_string(), |
| 127 | head: head.clone(), |
| 128 | }) |
| 129 | ); |
| 130 | for (branch, body) in [ |
| 131 | (None, Some(json!({ "head": head }))), |
| 132 | (Some("main"), None), |
| 133 | (Some("main"), Some(json!({}))), |
| 134 | (Some("main"), Some(json!({ "head": null }))), |
| 135 | (Some("main"), Some(json!({ "head": "abc" }))), |
| 136 | ( |
| 137 | Some("main"), |
| 138 | Some(json!({ "head": head, "index": "b".repeat(64) })), |
| 139 | ), |
| 140 | ( |
| 141 | Some("main"), |
| 142 | Some(json!({ "head": head, "revision": "b".repeat(64) })), |
| 143 | ), |
| 144 | (Some(""), Some(json!({ "head": head }))), |
| 145 | (Some("--force"), Some(json!({ "head": head }))), |
| 146 | (Some("main:refs/heads/other"), Some(json!({ "head": head }))), |
| 147 | (Some("a b"), Some(json!({ "head": head }))), |
| 148 | ] { |
| 149 | let error = named_push(branch, body.clone().map(expect)) |
| 150 | .expect_err(&format!("{branch:?} {body:?}")); |
| 151 | assert_eq!(error.status, StatusCode::BAD_REQUEST, "{branch:?} {body:?}"); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | #[tokio::test] |
| 156 | async fn git_push_sends_only_the_named_branch_at_the_expected_head() { |
| 157 | let tmp = repo(); |
| 158 | let root = tmp.path(); |
| 159 | let bare = tempfile::tempdir().unwrap(); |
| 160 | let remote = bare.path(); |
| 161 | git(remote, &["init", "-q", "--bare"]); |
| 162 | git( |
| 163 | root, |
| 164 | &["remote", "add", "origin", &remote.to_string_lossy()], |
| 165 | ); |
| 166 | git(root, &["config", "push.default", "matching"]); |
| 167 | git(root, &["config", "push.followTags", "true"]); |
| 168 | git( |
| 169 | root, |
| 170 | &["config", "remote.origin.push", "refs/heads/*:refs/heads/*"], |
| 171 | ); |
| 172 | git( |
| 173 | root, |
| 174 | &[ |
| 175 | "config", |
| 176 | "--add", |
| 177 | "remote.origin.push", |
| 178 | "refs/tags/*:refs/tags/*", |
| 179 | ], |
| 180 | ); |
| 181 | git(root, &["branch", "unrelated"]); |
| 182 | git(root, &["tag", "-a", "-m", "release", "v1"]); |
| 183 | let main = output(root, &["rev-parse", "HEAD"]); |
| 184 | assert!(refs(remote).is_empty()); |
| 185 | |
| 186 | let first = push(root, request(json!({ "set_upstream": true }))) |
| 187 | .await |
| 188 | .unwrap_or_else(|error| panic!("first push: {:?}", status_of(error))); |
| 189 | assert_eq!(first.0["ok"], true); |
| 190 | assert_eq!(refs(remote), [format!("refs/heads/main {main}")]); |
| 191 | assert_eq!( |
| 192 | output(root, &["rev-parse", "--abbrev-ref", "@{upstream}"]), |
| 193 | "origin/main" |
| 194 | ); |
| 195 | |
| 196 | git(root, &["switch", "-q", "-c", "feature"]); |
| 197 | let stale_feature = commit(root, "f1.txt"); |
| 198 | let feature = commit(root, "f2.txt"); |
| 199 | git(root, &["branch", "-f", "unrelated", "HEAD"]); |
| 200 | let (status, code, stale) = refused( |
| 201 | root, |
| 202 | json!({ "branch": "feature", "expect": { "head": stale_feature } }), |
| 203 | ) |
| 204 | .await; |
| 205 | assert_eq!( |
| 206 | (status, code), |
| 207 | (StatusCode::CONFLICT, Some("git_state_changed")) |
| 208 | ); |
| 209 | assert_eq!(stale, json!(["head"])); |
| 210 | let (status, code, stale) = refused( |
| 211 | root, |
| 212 | json!({ "branch": "main", "expect": { "head": main } }), |
| 213 | ) |
| 214 | .await; |
| 215 | assert_eq!( |
| 216 | (status, code), |
| 217 | (StatusCode::CONFLICT, Some("git_state_changed")) |
| 218 | ); |
| 219 | assert_eq!(stale, json!(["branch"])); |
| 220 | assert_eq!(refs(remote), [format!("refs/heads/main {main}")]); |
| 221 | |
| 222 | git(root, &["config", "remote.origin.mirror", "true"]); |
| 223 | let (status, code, _) = refused( |
| 224 | root, |
| 225 | json!({ "branch": "feature", "expect": { "head": feature } }), |
| 226 | ) |
| 227 | .await; |
| 228 | assert_eq!((status, code), (StatusCode::BAD_REQUEST, None)); |
| 229 | git(root, &["config", "--unset", "remote.origin.mirror"]); |
| 230 | assert_eq!(refs(remote), [format!("refs/heads/main {main}")]); |
| 231 | |
| 232 | let pushed = push( |
| 233 | root, |
| 234 | request( |
| 235 | json!({ "branch": "feature", "expect": { "head": feature }, "set_upstream": true }), |
| 236 | ), |
| 237 | ) |
| 238 | .await |
| 239 | .unwrap_or_else(|error| panic!("named push: {:?}", status_of(error))); |
| 240 | assert_eq!(pushed.0["ok"], true); |
| 241 | assert_eq!( |
| 242 | refs(remote), |
| 243 | [ |
| 244 | format!("refs/heads/feature {feature}"), |
| 245 | format!("refs/heads/main {main}"), |
| 246 | ] |
| 247 | ); |
| 248 | assert_eq!( |
| 249 | output(root, &["rev-parse", "--abbrev-ref", "@{upstream}"]), |
| 250 | "origin/feature" |
| 251 | ); |
| 252 | |
| 253 | let next = commit(root, "f3.txt"); |
| 254 | git(root, &["checkout", "-q", "--detach"]); |
| 255 | let (status, code, stale) = refused( |
| 256 | root, |
| 257 | json!({ "branch": "feature", "expect": { "head": next } }), |
| 258 | ) |
| 259 | .await; |
| 260 | assert_eq!( |
| 261 | (status, code), |
| 262 | (StatusCode::CONFLICT, Some("git_state_changed")) |
| 263 | ); |
| 264 | assert_eq!(stale, json!(["branch"])); |
| 265 | assert_eq!(refused(root, json!({})).await.0, StatusCode::BAD_REQUEST); |
| 266 | |
| 267 | git(root, &["switch", "-q", "feature"]); |
| 268 | let pushed = push( |
| 269 | root, |
| 270 | request(json!({ "branch": "feature", "expect": { "head": next } })), |
| 271 | ) |
| 272 | .await |
| 273 | .unwrap_or_else(|error| panic!("existing upstream push: {:?}", status_of(error))); |
| 274 | assert_eq!(pushed.0["ok"], true); |
| 275 | assert_eq!( |
| 276 | refs(remote), |
| 277 | [ |
| 278 | format!("refs/heads/feature {next}"), |
| 279 | format!("refs/heads/main {main}"), |
| 280 | ] |
| 281 | ); |
| 282 | |
| 283 | git( |
| 284 | root, |
| 285 | &["switch", "-q", "-c", "renamed", "--track", "origin/main"], |
| 286 | ); |
| 287 | let renamed = commit(root, "r.txt"); |
| 288 | assert_eq!( |
| 289 | refused( |
| 290 | root, |
| 291 | json!({ "branch": "renamed", "expect": { "head": renamed } }) |
| 292 | ) |
| 293 | .await |
| 294 | .0, |
| 295 | StatusCode::BAD_REQUEST |
| 296 | ); |
| 297 | assert_eq!( |
| 298 | refs(remote), |
| 299 | [ |
| 300 | format!("refs/heads/feature {next}"), |
| 301 | format!("refs/heads/main {main}"), |
| 302 | ] |
| 303 | ); |
| 304 | } |
| 305 |