-
-
Notifications
You must be signed in to change notification settings - Fork 354
Add Repository::in_progress_operation() #382
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
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
c2f66e4
First pass at Repository::in_progress_state()
56038ed
Tweak the naming and comments a bit
172b464
in_progress_operation now returns an Option
499c811
Add a few tests for good measure.
4086335
Merge branch 'main' into repo-status
bb18a13
Pass version appropriate rebase flags to git.
a5406b5
Make clippy happier.
5c162e0
Fix the GNU sed detection so it works where /usr/bin/sed is GNU.
ac3c8c7
Take a couple more steps to appease the CI gods.
475e7d1
Let's try not parsing the git version.
53c22ee
repo_path -> git_dir
0eb2372
Merge branch 'main' into repo-status
cf19a18
Print out some human readable text if GNU sed cannot be found.
463a705
Clean up the error message and comments.
8122c5c
Merge branch 'main' into repo-status
da8059c
RepositoryState: remove unused variant
9679d6b
Merge branch 'main' into repo-status
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -136,6 +136,8 @@ mod location; | |
|
||
mod snapshots; | ||
|
||
mod state; | ||
|
||
mod impls; | ||
|
||
mod cache; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use crate::RepositoryState; | ||
|
||
impl crate::Repository { | ||
/// Returns the status of an in progress operation on a repository or [`None`] | ||
/// if nothing is happening. | ||
pub fn in_progress_operation(&self) -> Option<RepositoryState> { | ||
let git_dir = self.path(); | ||
|
||
// This is modeled on the logic from wt_status_get_state in git's wt-status.c and | ||
// ps1 from git-prompt.sh. | ||
|
||
if git_dir.join("rebase-apply/applying").is_file() { | ||
Some(RepositoryState::ApplyMailbox) | ||
} else if git_dir.join("rebase-apply/rebasing").is_file() { | ||
Some(RepositoryState::Rebase) | ||
} else if git_dir.join("rebase-apply").is_dir() { | ||
Some(RepositoryState::ApplyMailboxRebase) | ||
} else if git_dir.join("rebase-merge/interactive").is_file() { | ||
Some(RepositoryState::RebaseInteractive) | ||
} else if git_dir.join("rebase-merge").is_dir() { | ||
Some(RepositoryState::Rebase) | ||
} else if git_dir.join("CHERRY_PICK_HEAD").is_file() { | ||
if git_dir.join("todo").is_file() { | ||
Some(RepositoryState::CherryPickSequence) | ||
} else { | ||
Some(RepositoryState::CherryPick) | ||
} | ||
} else if git_dir.join("MERGE_HEAD").is_file() { | ||
Some(RepositoryState::Merge) | ||
} else if git_dir.join("BISECT_LOG").is_file() { | ||
Some(RepositoryState::Bisect) | ||
} else if git_dir.join("REVERT_HEAD").is_file() { | ||
if git_dir.join("todo").is_file() { | ||
Some(RepositoryState::RevertSequence) | ||
} else { | ||
Some(RepositoryState::Revert) | ||
} | ||
} else { | ||
None | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#!/bin/bash | ||
set -eu -o pipefail | ||
|
||
git init -q | ||
|
||
git config commit.gpgsign false | ||
|
||
git config advice.statusHints false | ||
git config advice.resolveConflict false | ||
git config advice.commitBeforeMerge false | ||
git config advice.skippedCherryPicks false | ||
|
||
git config init.defaultBranch master | ||
|
||
unset GIT_AUTHOR_DATE | ||
unset GIT_COMMITTER_DATE | ||
|
||
touch 1 | ||
git add 1 | ||
git commit -m 1 1 | ||
git checkout -b other-branch | ||
echo other-branch > 1 | ||
git add 1 | ||
git commit -m 1.other 1 | ||
git checkout master | ||
echo master > 1 | ||
git add 1 | ||
git commit -m 1.master 1 | ||
|
||
# This should fail and leave us in a cherry-pick state | ||
git cherry-pick other-branch || true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#!/bin/bash | ||
set -eu -o pipefail | ||
|
||
git init -q | ||
|
||
git config commit.gpgsign false | ||
|
||
git config advice.statusHints false | ||
git config advice.resolveConflict false | ||
git config advice.commitBeforeMerge false | ||
git config advice.skippedCherryPicks false | ||
|
||
git config init.defaultBranch master | ||
|
||
unset GIT_AUTHOR_DATE | ||
unset GIT_COMMITTER_DATE | ||
|
||
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 | ||
|
||
# NOTE: This relies on GNU sed behavior and will fail on *BSDs (including macOS) without GNU | ||
# sed installed. | ||
sed=$(which gsed sed | head -1 || true) | ||
|
||
# GNU sed recognizes long arguments, BSD sed does not | ||
# NOTE: We can't rely on $? because set -e guarantees the script will terminate on a non-zero exit | ||
${sed} --version 2&>/dev/null && sed_exit_code=success || sed_exit_code=fail | ||
if [ "${sed_exit_code}" = "fail" ]; then | ||
printf "\n** GNU sed is required for this test but was not found **\n" | ||
exit 1 | ||
fi | ||
unset sed_exit_code | ||
|
||
# NOTE: Starting around git 2.35.0 --preserve-merges was renamed to --rebase-merges | ||
# however --preserve-merges first appeared in git 2.18. That should cover most use cases. | ||
EDITOR="${sed} -i.bak -z 's/pick/edit/2'" git rebase --rebase-merges --interactive HEAD~2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#!/bin/bash | ||
set -eu -o pipefail | ||
|
||
git init -q | ||
|
||
git config commit.gpgsign false | ||
|
||
git config advice.statusHints false | ||
git config advice.resolveConflict false | ||
git config advice.commitBeforeMerge false | ||
git config advice.skippedCherryPicks false | ||
|
||
git config init.defaultBranch master | ||
|
||
unset GIT_AUTHOR_DATE | ||
unset GIT_COMMITTER_DATE | ||
|
||
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 | ||
git revert --no-commit HEAD~1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,3 +27,4 @@ mod discover; | |
mod easy; | ||
mod init; | ||
mod reference; | ||
mod state; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use crate::{repo, Result}; | ||
use anyhow::anyhow; | ||
use git_repository::{bstr::ByteSlice, RepositoryState}; | ||
|
||
// Can we identify that a cherry pick operation is in progress | ||
#[test] | ||
fn cherry_pick() -> Result { | ||
let repo = repo("make_cherry_pick_repo.sh").map(|r| r.to_thread_local())?; | ||
|
||
let head = repo.head()?; | ||
let head_name = head | ||
.referent_name() | ||
.ok_or_else(|| anyhow!("detached head?"))? | ||
.shorten() | ||
.to_str()?; | ||
assert_eq!("master", head_name); | ||
|
||
assert_eq!(Some(RepositoryState::CherryPick), repo.in_progress_operation()); | ||
|
||
Ok(()) | ||
} | ||
|
||
// Can we identify that we're in the middle of an interactive rebase? | ||
#[test] | ||
fn rebase_interactive() -> Result { | ||
let repo = repo("make_rebase_i_repo.sh").map(|r| r.to_thread_local())?; | ||
|
||
let head = repo.head()?; | ||
// TODO: Get rebase head/target | ||
let head_name = head.referent_name(); | ||
assert!(head_name.is_none()); | ||
|
||
assert_eq!(Some(RepositoryState::RebaseInteractive), repo.in_progress_operation()); | ||
|
||
Ok(()) | ||
} | ||
|
||
// Can we identify a revert operation when we see it? | ||
#[test] | ||
fn revert() -> Result { | ||
let repo = repo("make_revert_repo.sh").map(|r| r.to_thread_local())?; | ||
|
||
let head = repo.head()?; | ||
let head_name = head | ||
.referent_name() | ||
.ok_or_else(|| anyhow!("detached head?"))? | ||
.shorten() | ||
.to_str()?; | ||
assert_eq!("master", head_name); | ||
|
||
assert_eq!(Some(RepositoryState::Revert), repo.in_progress_operation()); | ||
|
||
Ok(()) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.