diff --git a/oxen/python/oxen/repo.py b/oxen/python/oxen/repo.py index 344f8b9..a6e8d45 100644 --- a/oxen/python/oxen/repo.py +++ b/oxen/python/oxen/repo.py @@ -213,3 +213,9 @@ def current_branch(self): Returns the current branch. """ return self._repo.current_branch() + + def merge(self, branch: str): + """ + Merge a branch into the current branch. + """ + return self._repo.merge(branch) diff --git a/oxen/src/py_repo.rs b/oxen/src/py_repo.rs index f445a05..c7df01a 100644 --- a/oxen/src/py_repo.rs +++ b/oxen/src/py_repo.rs @@ -202,13 +202,21 @@ impl PyRepo { branch: branch.to_string(), subtree_paths: None, depth: None, - all + all, }; repositories::pull_remote_branch(&repo, &fetch_opts).await })?; Ok(()) } + pub fn merge(&self, branch: &str) -> Result, PyOxenError> { + let repo = LocalRepository::from_dir(&self.path)?; + match repositories::merge::merge(&repo, branch)? { + Some(commit) => Ok(Some(PyCommit { commit })), + None => Ok(None), + } + } + // pub fn diff(&self, path: &str) -> Result { // let repo = LocalRepository::from_dir(&self.path)?; // let diff = diff --git a/oxen/tests/conftest.py b/oxen/tests/conftest.py index a4d932b..d302d08 100644 --- a/oxen/tests/conftest.py +++ b/oxen/tests/conftest.py @@ -114,6 +114,16 @@ def house_prices_local_repo_no_commits(shared_datadir): yield repo +@pytest.fixture +def house_prices_local_repo_fully_committed(house_prices_local_repo_no_commits): + repo = house_prices_local_repo_no_commits + + repo.add(os.path.join(repo.path, "prices.csv")) + repo.commit("Add prices.csv") + + yield repo + + @pytest.fixture def empty_remote_repo(): repo_name = f"py-ox/test_repo_{str(uuid.uuid4())}" diff --git a/oxen/tests/test_merge.py b/oxen/tests/test_merge.py new file mode 100644 index 0000000..6ce90ab --- /dev/null +++ b/oxen/tests/test_merge.py @@ -0,0 +1,45 @@ +import os + + +def test_merge(house_prices_local_repo_fully_committed): + repo = house_prices_local_repo_fully_committed + + prices_file = "prices.csv" + full_path = os.path.join(repo.path, prices_file) + initial_branch = repo.current_branch.name + + # read initial prices.csv contents + with open(full_path, "r") as f: + initial_contents = f.read() + + # oxen checkout -b new_branch + new_branch = "new_branch" + repo.checkout(new_branch, create=True) + assert repo.current_branch.name == "new_branch" + + # update prices.csv + new_line = "6000000,6000,7,7,2015" + with open(full_path, "a") as f: + f.write(new_line) + + # oxen add prices.csv + repo.add(full_path) + + # oxen commit + repo.commit("Add new price") + with open(full_path, "r") as f: + updated_contents = f.read() + + # oxen checkout main + repo.checkout(initial_branch) + assert repo.current_branch.name == initial_branch + with open(full_path, "r") as f: + contents = f.read() + assert contents == initial_contents + + # oxen merge new_branch + repo.merge(new_branch) + assert repo.current_branch.name == initial_branch + with open(full_path, "r") as f: + contents = f.read() + assert contents == updated_contents