Skip to content

Commit

Permalink
Add Repo.merge function
Browse files Browse the repository at this point in the history
  • Loading branch information
jcelliott committed Jan 15, 2025
1 parent d3aa100 commit 7a74b25
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
6 changes: 6 additions & 0 deletions oxen/python/oxen/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
10 changes: 9 additions & 1 deletion oxen/src/py_repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<PyCommit>, 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<PyDiff, PyOxenError> {
// let repo = LocalRepository::from_dir(&self.path)?;
// let diff =
Expand Down
10 changes: 10 additions & 0 deletions oxen/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())}"
Expand Down
45 changes: 45 additions & 0 deletions oxen/tests/test_merge.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 7a74b25

Please sign in to comment.