Skip to content

run tests for refactored #393 #395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions git-repository/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,31 +321,25 @@ pub mod state {
#[derive(Debug, PartialEq)]
pub enum InProgress {
/// A mailbox is being applied.
// TODO: test
ApplyMailbox,
/// A rebase is happening while a mailbox is being applied.
// TODO: test
ApplyMailboxRebase,
/// A git bisect operation has not yet been concluded.
// TODO: test
Bisect,
/// A cherry pick operation.
CherryPick,
/// A cherry pick with multiple commits pending.
// TODO: test
CherryPickSequence,
/// A merge operation.
// TODO: test
Merge,
/// A rebase operation.
// TODO: test
Rebase,
/// An interactive rebase operation.
RebaseInteractive,
/// A revert operation.
Revert,
/// A revert operation with multiple commits pending.
// TODO: test
RevertSequence,
}
}
Expand Down
5 changes: 5 additions & 0 deletions git-repository/src/repository/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ impl crate::Repository {
.map_err(Into::into)
}

/// Return the name to the symbolic reference `HEAD` points to, or `None` if the head is detached.
pub fn head_name(&self) -> Result<Option<git_ref::FullName>, crate::reference::find::existing::Error> {
Ok(self.head()?.referent_name().map(|n| n.to_owned()))
}

/// Return the commit object the `HEAD` reference currently points to after peeling it fully.
///
/// Note that this may fail for various reasons, most notably because the repository
Expand Down
8 changes: 5 additions & 3 deletions git-repository/src/repository/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use crate::state;
impl crate::Repository {
/// Returns the status of an in progress operation on a repository or [`None`]
/// if no operation is currently in progress.
pub fn in_progress_operation(&self) -> Option<state::InProgress> {
///
/// Note to be confused with the repositories 'status'.
pub fn state(&self) -> Option<state::InProgress> {
let git_dir = self.path();

// This is modeled on the logic from wt_status_get_state in git's wt-status.c and
Expand All @@ -20,7 +22,7 @@ impl crate::Repository {
} else if git_dir.join("rebase-merge").is_dir() {
Some(state::InProgress::Rebase)
} else if git_dir.join("CHERRY_PICK_HEAD").is_file() {
if git_dir.join("todo").is_file() {
if git_dir.join("sequencer/todo").is_file() {
Some(state::InProgress::CherryPickSequence)
} else {
Some(state::InProgress::CherryPick)
Expand All @@ -30,7 +32,7 @@ impl crate::Repository {
} else if git_dir.join("BISECT_LOG").is_file() {
Some(state::InProgress::Bisect)
} else if git_dir.join("REVERT_HEAD").is_file() {
if git_dir.join("todo").is_file() {
if git_dir.join("sequencer/todo").is_file() {
Some(state::InProgress::RevertSequence)
} else {
Some(state::InProgress::Revert)
Expand Down
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
Git LFS file not shown
22 changes: 22 additions & 0 deletions git-repository/tests/fixtures/make_am_repo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash
set -eu -o pipefail

git init -q

echo file.main > file
git add file
git commit -m file.main file

git checkout -b other-branch
echo file.other-branch > file
git commit -m file.other-branch file
# Create an mbox formatted patch and save the path
patch_path=$(git format-patch main)

git checkout main
# Create a conflict
echo file.main.update > file
git commit -m file.main.update file

# This will fail due to the merge conflict and leave us in a 'apply mbox in progress' state
git am 0001-file.other-branch.patch || true
14 changes: 14 additions & 0 deletions git-repository/tests/fixtures/make_bisect_repo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash
set -eu -o pipefail

git init -q

touch f1 f2

git add f1
git commit -m f1 f1

git add f2
git commit -m f2 f2

git bisect start
24 changes: 24 additions & 0 deletions git-repository/tests/fixtures/make_cherry_pick_sequence_repo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash
set -eu -o pipefail

git init -q

touch f1 f2 f3

git add f1
git commit -m f1 f1

git checkout -b other-branch
echo f2.other-branch > f2
git add f2
git commit -m f2.other-branch f2
git add f3
git commit -m f3 f3

git checkout main
echo f2.main > f2
git add f2
git commit -m f2.main f2

# This should fail and leave us in a cherry-pick + sequencer state
git cherry-pick other-branch~2..other-branch || true
19 changes: 19 additions & 0 deletions git-repository/tests/fixtures/make_merge_repo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash
set -eu -o pipefail

git init -q

echo file.main > file
git add file
git commit -m file.main file

git checkout -b other-branch
echo file.other-branch > file
git add file
git commit -m file.other-branch file

git checkout main
echo file.main changed > file
git commit -m file.main\ changed file

git merge other-branch || true
14 changes: 7 additions & 7 deletions git-repository/tests/fixtures/make_revert_repo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ set -eu -o pipefail

git init -q

touch 1 2 3
git add 1
git commit -m 1 1
git add 2
git commit -m 2 2
git add 3
git commit -m 3 3
touch f1 f2 f3
git add f1
git commit -m f1 f1
git add f2
git commit -m f2 f2
git add f3
git commit -m f3 f3
git revert --no-commit HEAD~1
20 changes: 20 additions & 0 deletions git-repository/tests/fixtures/make_revert_sequence_repo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash
set -eu -o pipefail

git init -q

echo 1.0 > 1
git add 1
git commit -m 1.0 1

echo 1.1 > 1
git commit -m 1.1 1

echo 1.2 > 1
git commit -m 1.2 1
touch 2
git add 2
git commit -m 2 2

# This should fail and leave us in a revert + sequencer state
git revert --no-commit HEAD HEAD~2 || true
67 changes: 53 additions & 14 deletions git-repository/tests/repository/state.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,60 @@
use crate::{named_repo, Result};
use git_repository as git;

#[test]
fn apply_mailbox() -> Result {
let repo = named_repo("make_am_repo.sh")?;

assert_eq!(repo.head_name()?.unwrap().shorten(), "main");
assert_eq!(repo.state(), Some(git::state::InProgress::ApplyMailbox));
Ok(())
}

#[test]
fn bisect() -> Result {
let repo = named_repo("make_bisect_repo.sh")?;

assert_eq!(repo.head_name()?.unwrap().shorten(), "main");
assert_eq!(repo.state(), Some(git::state::InProgress::Bisect));

Ok(())
}

#[test]
fn cherry_pick() -> Result {
let repo = named_repo("make_cherry_pick_repo.sh")?;

let head = repo.head()?;
let head_name = head.referent_name().expect("no detached head").shorten();
assert_eq!(head_name, "main");
assert_eq!(repo.head_name()?.unwrap().shorten(), "main");
assert_eq!(repo.state(), Some(git::state::InProgress::CherryPick));
Ok(())
}

#[test]
fn cherry_pick_sequence() -> Result {
let repo = named_repo("make_cherry_pick_sequence_repo.sh")?;

assert_eq!(repo.head_name()?.unwrap().shorten(), "main");
assert_eq!(repo.state(), Some(git::state::InProgress::CherryPickSequence));

Ok(())
}

#[test]
fn merge() -> Result {
let repo = named_repo("make_merge_repo.sh")?;

assert_eq!(repo.head_name()?.unwrap().shorten(), "main");
assert_eq!(repo.state(), Some(git::state::InProgress::Merge));

assert_eq!(repo.in_progress_operation(), Some(git::state::InProgress::CherryPick));
Ok(())
}

#[test]
fn rebase_interactive() -> Result {
let repo = named_repo("make_rebase_i_repo.sh")?;

let head = repo.head()?;
assert!(head.is_detached());
assert_eq!(
repo.in_progress_operation(),
Some(git::state::InProgress::RebaseInteractive)
);
assert!(repo.head()?.is_detached());
assert_eq!(repo.state(), Some(git::state::InProgress::RebaseInteractive));

Ok(())
}
Expand All @@ -31,11 +63,18 @@ fn rebase_interactive() -> Result {
fn revert() -> Result {
let repo = named_repo("make_revert_repo.sh")?;

let head = repo.head()?;
let head_name = head.referent_name().expect("no detached head").shorten();
assert_eq!(head_name, "main");
assert_eq!(repo.head_name()?.unwrap().shorten(), "main");
assert_eq!(repo.state(), Some(git::state::InProgress::Revert));

Ok(())
}

#[test]
fn revert_sequence() -> Result {
let repo = named_repo("make_revert_sequence_repo.sh")?;

assert_eq!(repo.in_progress_operation(), Some(git::state::InProgress::Revert));
assert_eq!(repo.head_name()?.unwrap().shorten(), "main");
assert_eq!(repo.state(), Some(git::state::InProgress::RevertSequence));

Ok(())
}