IDEA-83755 GitCommand locking made modifiable

Some commands don't need write locks in certain cases: for example, git stash list.
For them let create an instance of GitCommand with read lock instead of write lock.
Apply this locking policy to 'git stash list' and 'git checkout -b <branch>'
This commit is contained in:
Kirill Likhodedov
2012-03-30 16:15:35 +04:00
parent 27ae8a9a44
commit 030279297c
4 changed files with 21 additions and 4 deletions
@@ -89,6 +89,18 @@ public class GitCommand {
@NotNull @NonNls private final String myName; // command name passed to git
@NotNull private final LockingPolicy myLocking; // Locking policy for the command
/**
* Creates a git command with LockingPolicy different from the default one.
* Use this constructor with care: specifying read-policy on a write operation may result in a conflict during simultaneous
* modification of index.
* @param command Original command.
* @param lockingPolicy Locking policy overriding default locking policy of the original command.
*/
private GitCommand(@NotNull GitCommand command, @NotNull LockingPolicy lockingPolicy) {
myName = command.name();
myLocking = lockingPolicy;
}
private GitCommand(@NonNls @NotNull String name, @NotNull LockingPolicy locking) {
myLocking = locking;
myName = name;
@@ -119,4 +131,9 @@ public class GitCommand {
return myLocking;
}
@NotNull
public GitCommand readLockingCommand() {
return new GitCommand(this, LockingPolicy.READ);
}
}
@@ -182,7 +182,7 @@ public class GitImpl implements Git {
@Override
public GitCommandResult checkoutNewBranch(@NotNull GitRepository repository, @NotNull String branchName,
@Nullable GitLineHandlerListener listener) {
final GitLineHandler h = new GitLineHandler(repository.getProject(), repository.getRoot(), GitCommand.CHECKOUT);
final GitLineHandler h = new GitLineHandler(repository.getProject(), repository.getRoot(), GitCommand.CHECKOUT.readLockingCommand());
h.setSilent(false);
h.addParameters("-b");
h.addParameters(branchName);
@@ -728,7 +728,7 @@ public class GitHistoryUtils {
@Nullable
public static Pair<AbstractHash, AbstractHash> getStashTop(@NotNull Project project, @NotNull VirtualFile root) throws VcsException {
GitSimpleHandler h = new GitSimpleHandler(project, root, GitCommand.STASH);
GitSimpleHandler h = new GitSimpleHandler(project, root, GitCommand.STASH.readLockingCommand());
GitLogParser parser = new GitLogParser(project, SHORT_HASH, SHORT_PARENTS);
h.setSilent(true);
h.setNoSSH(true);
@@ -776,7 +776,7 @@ public class GitHistoryUtils {
@Nullable
public static List<Pair<String, GitCommit>> loadStashStackAsCommits(@NotNull Project project, @NotNull VirtualFile root,
SymbolicRefsI refs, final String... parameters) throws VcsException {
GitSimpleHandler h = new GitSimpleHandler(project, root, GitCommand.STASH);
GitSimpleHandler h = new GitSimpleHandler(project, root, GitCommand.STASH.readLockingCommand());
GitLogParser parser = new GitLogParser(project, GitLogParser.NameStatus.STATUS, SHORT_HASH, HASH, COMMIT_TIME, AUTHOR_NAME, AUTHOR_TIME, AUTHOR_EMAIL, COMMITTER_NAME,
COMMITTER_EMAIL, SHORT_PARENTS, REF_NAMES, SHORT_REF_LOG_SELECTOR, SUBJECT, BODY, RAW_BODY);
h.setSilent(true);
@@ -64,7 +64,7 @@ public class GitStashUtils {
public static void loadStashStack(@NotNull Project project, @NotNull VirtualFile root, final Charset charset,
final Consumer<StashInfo> consumer) {
GitSimpleHandler h = new GitSimpleHandler(project, root, GitCommand.STASH);
GitSimpleHandler h = new GitSimpleHandler(project, root, GitCommand.STASH.readLockingCommand());
h.setSilent(true);
h.setNoSSH(true);
h.addParameters("list");