upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/git.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/git.rs')
-rw-r--r--src/git.rs34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/git.rs b/src/git.rs
index 0a06ab5..6edca0f 100644
--- a/src/git.rs
+++ b/src/git.rs
@@ -344,6 +344,12 @@ impl RepoActions for Repo {
344 } 344 }
345 345
346 fn create_branch_at_commit(&self, branch_name: &str, commit: &str) -> Result<()> { 346 fn create_branch_at_commit(&self, branch_name: &str, commit: &str) -> Result<()> {
347 let branch_checkedout = self.get_checked_out_branch_name()?.eq(branch_name);
348 if branch_checkedout {
349 let (name, _) = self.get_main_or_master_branch()?;
350 self.checkout(name)?;
351 }
352
347 self.git_repo 353 self.git_repo
348 .branch( 354 .branch(
349 branch_name, 355 branch_name,
@@ -351,6 +357,10 @@ impl RepoActions for Repo {
351 true, 357 true,
352 ) 358 )
353 .context("branch could not be created")?; 359 .context("branch could not be created")?;
360
361 if branch_checkedout {
362 self.checkout(branch_name)?;
363 }
354 Ok(()) 364 Ok(())
355 } 365 }
356 /* returns patches applied */ 366 /* returns patches applied */
@@ -1292,6 +1302,30 @@ mod tests {
1292 assert_eq!(test_repo.checkout(branch_name)?, ahead_1_oid); 1302 assert_eq!(test_repo.checkout(branch_name)?, ahead_1_oid);
1293 Ok(()) 1303 Ok(())
1294 } 1304 }
1305
1306 #[test]
1307 fn when_branch_is_checkedout_new_tip_specified_it_is_updated() -> Result<()> {
1308 let test_repo = GitTestRepo::default();
1309 test_repo.populate()?;
1310 // create feature branch and add 2 commits
1311 test_repo.create_branch("feature")?;
1312 test_repo.checkout("feature")?;
1313 std::fs::write(test_repo.dir.join("t3.md"), "some content")?;
1314 let ahead_1_oid = test_repo.stage_and_commit("add t3.md")?;
1315 std::fs::write(test_repo.dir.join("t4.md"), "some content")?;
1316 let ahead_2_oid = test_repo.stage_and_commit("add t4.md")?;
1317
1318 let git_repo = Repo::from_path(&test_repo.dir)?;
1319
1320 let branch_name = "test-name-1";
1321 git_repo.create_branch_at_commit(branch_name, &ahead_1_oid.to_string())?;
1322 test_repo.checkout(branch_name)?;
1323 git_repo.create_branch_at_commit(branch_name, &ahead_2_oid.to_string())?;
1324 test_repo.checkout("main")?;
1325
1326 assert_eq!(test_repo.checkout(branch_name)?, ahead_2_oid);
1327 Ok(())
1328 }
1295 } 1329 }
1296 } 1330 }
1297 1331