Skip to content

Commit

Permalink
Adding some ways of asking for a password when git asks for it in net…
Browse files Browse the repository at this point in the history
…work operations

SSH_ASKPASS/GIT_ASKPASS will ask for a password if there isn't one. If the command
line asks for a password, use a custom UI to ask for it.
  • Loading branch information
shana committed May 29, 2018
1 parent 0aa40cb commit edb884f
Show file tree
Hide file tree
Showing 32 changed files with 348 additions and 125 deletions.
4 changes: 3 additions & 1 deletion src/GitHub.Api/Application/ApplicationManagerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,12 +295,14 @@ public void RestartRepository()

repositoryManager = Unity.RepositoryManager.CreateInstance(Platform, TaskManager, GitClient, Environment.RepositoryPath);
repositoryManager.Initialize();
Environment.Repository.Initialize(repositoryManager, TaskManager);
Environment.Repository.Initialize(repositoryManager, TaskManager, this);
repositoryManager.Start();
Environment.Repository.Start();
Logger.Trace($"Got a repository? {(Environment.Repository != null ? Environment.Repository.LocalPath : "null")}");
}

public abstract void OpenPopupWindow(PopupViewType viewType, object data, Action<bool, object> onClose);

protected void SetupMetrics(string unityVersion)
{
string userId = null;
Expand Down
9 changes: 9 additions & 0 deletions src/GitHub.Api/Application/IApplicationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@

namespace GitHub.Unity
{
public enum PopupViewType
{
None,
PublishView,
AuthenticationView,
PasswordView,
}

public interface IApplicationManager : IDisposable
{
IEnvironment Environment { get; }
Expand All @@ -22,5 +30,6 @@ public interface IApplicationManager : IDisposable
event Action<IProgress> OnProgress;
void SetupGit(GitInstaller.GitInstallationState state);
void RestartRepository();
void OpenPopupWindow(PopupViewType viewType, object data, Action<bool, object> onClose);
}
}
12 changes: 6 additions & 6 deletions src/GitHub.Api/Git/GitClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ public interface IGitClient
ITask<string> SetConfig(string key, string value, GitConfigSource configSource, IOutputProcessor<string> processor = null);
ITask<GitUser> GetConfigUserAndEmail();
ITask<List<GitLock>> ListLocks(bool local, BaseOutputListProcessor<GitLock> processor = null);
ITask<string> Pull(string remote, string branch, IOutputProcessor<string> processor = null);
ITask<string> Push(string remote, string branch, IOutputProcessor<string> processor = null);
IProcessTask<string> Pull(string remote, string branch, IOutputProcessor<string> processor = null);
IProcessTask<string> Push(string remote, string branch, IOutputProcessor<string> processor = null);
ITask<string> Revert(string changeset, IOutputProcessor<string> processor = null);
ITask<string> Fetch(string remote, IOutputProcessor<string> processor = null);
IProcessTask<string> Fetch(string remote, IOutputProcessor<string> processor = null);
ITask<string> SwitchBranch(string branch, IOutputProcessor<string> processor = null);
ITask<string> DeleteBranch(string branch, bool deleteUnmerged = false, IOutputProcessor<string> processor = null);
ITask<string> CreateBranch(string branch, string baseBranch, IOutputProcessor<string> processor = null);
Expand Down Expand Up @@ -146,13 +146,13 @@ public ITask<List<GitLock>> ListLocks(bool local, BaseOutputListProcessor<GitLoc
.Configure(processManager);
}

public ITask<string> Pull(string remote, string branch, IOutputProcessor<string> processor = null)
public IProcessTask<string> Pull(string remote, string branch, IOutputProcessor<string> processor = null)
{
return new GitPullTask(remote, branch, cancellationToken, processor)
.Configure(processManager);
}

public ITask<string> Push(string remote, string branch,
public IProcessTask<string> Push(string remote, string branch,
IOutputProcessor<string> processor = null)
{
return new GitPushTask(remote, branch, true, cancellationToken, processor)
Expand All @@ -165,7 +165,7 @@ public ITask<string> Revert(string changeset, IOutputProcessor<string> processor
.Configure(processManager);
}

public ITask<string> Fetch(string remote,
public IProcessTask<string> Fetch(string remote,
IOutputProcessor<string> processor = null)
{
return new GitFetchTask(remote, cancellationToken, processor: processor)
Expand Down
2 changes: 1 addition & 1 deletion src/GitHub.Api/Git/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace GitHub.Unity
/// </summary>
public interface IRepository : IEquatable<IRepository>, IDisposable, IBackedByCache
{
void Initialize(IRepositoryManager theRepositoryManager, ITaskManager theTaskManager);
void Initialize(IRepositoryManager theRepositoryManager, ITaskManager theTaskManager, IApplicationManager appManager);
void Start();

ITask CommitAllFiles(string message, string body);
Expand Down
53 changes: 48 additions & 5 deletions src/GitHub.Api/Git/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ sealed class Repository : IEquatable<Repository>, IRepository

private IRepositoryManager repositoryManager;
private ITaskManager taskManager;
private IApplicationManager appManager;
private ICacheContainer cacheContainer;
private UriString cloneUrl;
private string name;
Expand Down Expand Up @@ -80,13 +81,16 @@ public Repository(NPath localPath, ICacheContainer container)
};
}

public void Initialize(IRepositoryManager theRepositoryManager, ITaskManager theTaskManager)
public void Initialize(IRepositoryManager theRepositoryManager, ITaskManager theTaskManager, IApplicationManager theAppManager)
{
Guard.ArgumentNotNull(theRepositoryManager, nameof(theRepositoryManager));
Guard.ArgumentNotNull(theTaskManager, nameof(theTaskManager));
Guard.ArgumentNotNull(theAppManager, nameof(theAppManager));

this.taskManager = theTaskManager;
this.repositoryManager = theRepositoryManager;
this.appManager = theAppManager;

this.repositoryManager.CurrentBranchUpdated += RepositoryManagerOnCurrentBranchUpdated;
this.repositoryManager.GitStatusUpdated += RepositoryManagerOnGitStatusUpdated;
this.repositoryManager.GitAheadBehindStatusUpdated += RepositoryManagerOnGitAheadBehindStatusUpdated;
Expand Down Expand Up @@ -130,10 +134,33 @@ public ITask SetupRemote(string remote, string remoteUrl)

public ITask CommitAllFiles(string message, string body) => repositoryManager.CommitAllFiles(message, body);
public ITask CommitFiles(List<string> files, string message, string body) => repositoryManager.CommitFiles(files, message, body);
public ITask Pull() => repositoryManager.Pull(CurrentRemote.Value.Name, CurrentBranch?.Name);
public ITask Push(string remote) => repositoryManager.Push(remote, CurrentBranch?.Name);
public ITask Push() => repositoryManager.Push(CurrentRemote.Value.Name, CurrentBranch?.Name);
public ITask Fetch() => repositoryManager.Fetch(CurrentRemote.Value.Name);
public ITask Pull()
{
var task = repositoryManager.Pull(CurrentRemote.Value.Name, CurrentBranch?.Name);
task.OnInput += HandleGitProcessInput;
return task.GetEndOfChain();
}
public ITask Push(string remote)
{
var task = repositoryManager.Push(remote, CurrentBranch?.Name);
task.OnInput += HandleGitProcessInput;
return task.GetEndOfChain();
}

public ITask Push()
{
var task = repositoryManager.Push(CurrentRemote.Value.Name, CurrentBranch?.Name);
task.OnInput += HandleGitProcessInput;
return task.GetEndOfChain();
}

public ITask Fetch()
{
var task = repositoryManager.Fetch(CurrentRemote.Value.Name);
task.OnInput += HandleGitProcessInput;
return task.GetEndOfChain();
}

public ITask Revert(string changeset) => repositoryManager.Revert(changeset);
public ITask RequestLock(NPath file) => repositoryManager.LockFile(file);
public ITask ReleaseLock(NPath file, bool force) => repositoryManager.UnlockFile(file, force);
Expand Down Expand Up @@ -179,6 +206,22 @@ public bool Equals(IRepository other)
return other != null && object.Equals(LocalPath, other.LocalPath);
}

private void HandleGitProcessInput(IProcess proc, string input)
{
taskManager.RunInUI(() => {
appManager.OpenPopupWindow(PopupViewType.PasswordView, input, (success, output) => {
if (success)
{
proc.StandardInput.WriteLine(output);
}
else
{
proc.Stop();
}
});
});
}

private void RefreshCache(CacheType cacheType)
{
taskManager.RunInUI(() => Refresh(cacheType));
Expand Down
21 changes: 12 additions & 9 deletions src/GitHub.Api/Git/RepositoryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public interface IRepositoryManager : IDisposable
void Stop();
ITask CommitAllFiles(string message, string body);
ITask CommitFiles(List<string> files, string message, string body);
ITask Fetch(string remote);
ITask Pull(string remote, string branch);
ITask Push(string remote, string branch);
IProcessTask<string> Fetch(string remote);
IProcessTask<string> Pull(string remote, string branch);
IProcessTask<string> Push(string remote, string branch);
ITask Revert(string changeset);
ITask RemoteAdd(string remote, string url);
ITask RemoteRemove(string remote);
Expand Down Expand Up @@ -192,22 +192,25 @@ public ITask CommitFiles(List<string> files, string message, string body)
return HookupHandlers(task, true);
}

public ITask Fetch(string remote)
public IProcessTask<string> Fetch(string remote)
{
var task = GitClient.Fetch(remote);
return HookupHandlers(task, false);
HookupHandlers(task, false);
return task;
}

public ITask Pull(string remote, string branch)
public IProcessTask<string> Pull(string remote, string branch)
{
var task = GitClient.Pull(remote, branch);
return HookupHandlers(task, true);
HookupHandlers(task, true);
return task;
}

public ITask Push(string remote, string branch)
public IProcessTask<string> Push(string remote, string branch)
{
var task = GitClient.Push(remote, branch);
return HookupHandlers(task, false);
HookupHandlers(task, false);
return task;
}

public ITask Revert(string changeset)
Expand Down
2 changes: 1 addition & 1 deletion src/GitHub.Api/Git/Tasks/GitFetchTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class GitFetchTask : ProcessTask<string>

public GitFetchTask(string remote,
CancellationToken token, bool prune = true, bool tags = true, IOutputProcessor<string> processor = null)
: base(token, processor ?? new SimpleOutputProcessor())
: base(token, processor ?? new GitNetworkOperationOutputProcessor())
{
Name = TaskName;
var args = new List<string> { "fetch" };
Expand Down
2 changes: 1 addition & 1 deletion src/GitHub.Api/Git/Tasks/GitPullTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class GitPullTask : ProcessTask<string>

public GitPullTask(string remote, string branch,
CancellationToken token, IOutputProcessor<string> processor = null)
: base(token, processor ?? new SimpleOutputProcessor())
: base(token, processor ?? new GitNetworkOperationOutputProcessor())
{
Name = TaskName;
var stringBuilder = new StringBuilder();
Expand Down
4 changes: 2 additions & 2 deletions src/GitHub.Api/Git/Tasks/GitPushTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ class GitPushTask : ProcessTask<string>
private readonly string arguments;

public GitPushTask(CancellationToken token, IOutputProcessor<string> processor = null)
: base(token, processor ?? new SimpleOutputProcessor())
: base(token, processor ?? new GitNetworkOperationOutputProcessor())
{
Name = TaskName;
arguments = "push";
}

public GitPushTask(string remote, string branch, bool setUpstream,
CancellationToken token, IOutputProcessor<string> processor = null)
: base(token, processor ?? new SimpleOutputProcessor())
: base(token, processor ?? new GitNetworkOperationOutputProcessor())
{
Guard.ArgumentNotNullOrWhiteSpace(remote, "remote");
Guard.ArgumentNotNullOrWhiteSpace(branch, "branch");
Expand Down
7 changes: 4 additions & 3 deletions src/GitHub.Api/OutputProcessors/BranchListOutputProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ class BranchListOutputProcessor : BaseOutputListProcessor<GitBranch>
{
private static readonly Regex trackingBranchRegex = new Regex(@"\[[\w]+\/.*\]");

public override void LineReceived(string line)
public override bool LineReceived(string line)
{
if (line == null)
return;
return false;

var proc = new LineParser(line);
if (proc.IsAtEnd)
return;
return false;

var active = proc.Matches('*');
proc.SkipWhitespace();
Expand All @@ -39,6 +39,7 @@ public override void LineReceived(string line)
var branch = new GitBranch(name, trackingName);

RaiseOnEntry(branch);
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@ namespace GitHub.Unity
{
class GitAheadBehindStatusOutputProcessor : BaseOutputProcessor<GitAheadBehindStatus>
{
public override void LineReceived(string line)
public override bool LineReceived(string line)
{
if (line == null)
{
return;
}
return false;

var proc = new LineParser(line);

var ahead = int.Parse(proc.ReadUntilWhitespace());
var behind = int.Parse(proc.ReadToEnd());

RaiseOnEntry(new GitAheadBehindStatus(ahead, behind));
return false;
}
}
}
5 changes: 3 additions & 2 deletions src/GitHub.Api/OutputProcessors/LfsVersionOutputProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ namespace GitHub.Unity
{
class LfsVersionOutputProcessor : BaseOutputProcessor<TheVersion>
{
public override void LineReceived(string line)
public override bool LineReceived(string line)
{
if (String.IsNullOrEmpty(line))
return;
return false;

var parts = line.Split('/', ' ');
if (parts.Length > 1)
{
var version = TheVersion.Parse(parts[1]);
RaiseOnEntry(version);
}
return false;
}
}
}
5 changes: 3 additions & 2 deletions src/GitHub.Api/OutputProcessors/LocksOutputProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ namespace GitHub.Unity
{
class LocksOutputProcessor : BaseOutputListProcessor<GitLock>
{
public override void LineReceived(string line)
public override bool LineReceived(string line)
{
if (string.IsNullOrEmpty(line))
{
//Do Nothing
return;
return false;
}

try
Expand All @@ -24,6 +24,7 @@ public override void LineReceived(string line)
{
Logger.Error(ex, $"Failed to parse lock line {line}");
}
return false;
}
}
}
Loading

0 comments on commit edb884f

Please sign in to comment.