Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 437ae09

Browse files
committed
Only enable Open file when branch checked out.
In the Pull Request Details view, disable the Open File context menu for files unless the PR branch is checked out and when opening a file, open the file in the working directory. Fixes #801 Fixes #817
1 parent 2c8a718 commit 437ae09

File tree

5 files changed

+38
-31
lines changed

5 files changed

+38
-31
lines changed

src/GitHub.App/SampleData/PullRequestDetailViewModelDesigner.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,12 @@ public PullRequestDetailViewModelDesigner()
8484
public ReactiveCommand<object> OpenFile { get; }
8585
public ReactiveCommand<object> DiffFile { get; }
8686

87-
public Task<string> ExtractFile(IPullRequestFileNode file)
87+
public Task<Tuple<string, string>> ExtractDiffFiles(IPullRequestFileNode file)
8888
{
8989
return null;
9090
}
9191

92-
public Task<Tuple<string, string>> ExtractDiffFiles(IPullRequestFileNode file)
92+
public string GetLocalFilePath(IPullRequestFileNode file)
9393
{
9494
return null;
9595
}

src/GitHub.App/ViewModels/PullRequestDetailViewModel.cs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class PullRequestDetailViewModel : BaseViewModel, IPullRequestDetailViewM
3939
IPullRequestCheckoutState checkoutState;
4040
IPullRequestUpdateState updateState;
4141
string operationError;
42+
bool isCheckedOut;
4243
bool isFromFork;
4344
bool isInCheckout;
4445

@@ -103,7 +104,7 @@ public PullRequestDetailViewModel(
103104
SubscribeOperationError(Push);
104105

105106
OpenOnGitHub = ReactiveCommand.Create();
106-
OpenFile = ReactiveCommand.Create();
107+
OpenFile = ReactiveCommand.Create(this.WhenAnyValue(x => x.IsCheckedOut));
107108
DiffFile = ReactiveCommand.Create();
108109
}
109110

@@ -145,6 +146,15 @@ public string TargetBranchDisplayName
145146
private set { this.RaiseAndSetIfChanged(ref targetBranchDisplayName, value); }
146147
}
147148

149+
/// <summary>
150+
/// Gets a value indicating whether the pull request branch is checked out.
151+
/// </summary>
152+
public bool IsCheckedOut
153+
{
154+
get { return isCheckedOut; }
155+
private set { this.RaiseAndSetIfChanged(ref isCheckedOut, value); }
156+
}
157+
148158
/// <summary>
149159
/// Gets a value indicating whether the pull request comes from a fork.
150160
/// </summary>
@@ -269,9 +279,10 @@ public async Task Load(IPullRequestModel pullRequest)
269279
}
270280

271281
var localBranches = await pullRequestsService.GetLocalBranches(repository, pullRequest).ToList();
272-
var isCheckedOut = localBranches.Contains(repository.CurrentBranch);
273282

274-
if (isCheckedOut)
283+
IsCheckedOut = localBranches.Contains(repository.CurrentBranch);
284+
285+
if (IsCheckedOut)
275286
{
276287
var divergence = await pullRequestsService.CalculateHistoryDivergence(repository, Model.Number);
277288
var pullEnabled = divergence.BehindBy > 0;
@@ -339,17 +350,6 @@ public async Task Load(IPullRequestModel pullRequest)
339350
}
340351
}
341352

342-
/// <summary>
343-
/// Gets the specified file as it appears in the pull request.
344-
/// </summary>
345-
/// <param name="file">The file or directory node.</param>
346-
/// <returns>The path to the extracted file.</returns>
347-
public Task<string> ExtractFile(IPullRequestFileNode file)
348-
{
349-
var path = Path.Combine(file.DirectoryPath, file.FileName);
350-
return pullRequestsService.ExtractFile(repository, modelService, model.Head.Sha, path, file.Sha).ToTask();
351-
}
352-
353353
/// <summary>
354354
/// Gets the before and after files needed for viewing a diff.
355355
/// </summary>
@@ -361,6 +361,16 @@ public Task<Tuple<string, string>> ExtractDiffFiles(IPullRequestFileNode file)
361361
return pullRequestsService.ExtractDiffFiles(repository, modelService, model, path, file.Sha).ToTask();
362362
}
363363

364+
/// <summary>
365+
/// Gets the full path to a file in the working directory.
366+
/// </summary>
367+
/// <param name="file">The file.</param>
368+
/// <returns>The full path to the file in the working directory.</returns>
369+
public string GetLocalFilePath(IPullRequestFileNode file)
370+
{
371+
return Path.Combine(repository.LocalPath, file.DirectoryPath, file.FileName);
372+
}
373+
364374
void SubscribeOperationError(ReactiveCommand<Unit> command)
365375
{
366376
command.ThrownExceptions.Subscribe(x => OperationError = x.Message);

src/GitHub.Exports.Reactive/ViewModels/IPullRequestDetailViewModel.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,18 +139,18 @@ public interface IPullRequestDetailViewModel : IViewModel, IHasBusy
139139
/// </summary>
140140
ReactiveCommand<object> DiffFile { get; }
141141

142-
/// <summary>
143-
/// Gets the specified file as it appears in the pull request.
144-
/// </summary>
145-
/// <param name="file">The file or directory node.</param>
146-
/// <returns>The path to the extracted file.</returns>
147-
Task<string> ExtractFile(IPullRequestFileNode file);
148-
149142
/// <summary>
150143
/// Gets the before and after files needed for viewing a diff.
151144
/// </summary>
152145
/// <param name="file">The changed file.</param>
153146
/// <returns>A tuple containing the full path to the before and after files.</returns>
154147
Task<Tuple<string, string>> ExtractDiffFiles(IPullRequestFileNode file);
148+
149+
/// <summary>
150+
/// Gets the full path to a file in the working directory.
151+
/// </summary>
152+
/// <param name="file">The file.</param>
153+
/// <returns>The full path to the file in the working directory.</returns>
154+
string GetLocalFilePath(IPullRequestFileNode file);
155155
}
156156
}

src/GitHub.VisualStudio/UI/Views/PullRequestDetailView.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,8 @@
286286
<!-- When invoked from the TreeView and the ListView we programmatically bind the
287287
DataContext and update the CommandParameter to the changed file -->
288288
<ContextMenu x:Key="FileContextMenu">
289-
<MenuItem Header="{x:Static prop:Resources.OpenFile}" Command="{Binding OpenFile}"/>
290289
<MenuItem Header="{x:Static prop:Resources.CompareFile}" Command="{Binding DiffFile}"/>
290+
<MenuItem Header="{x:Static prop:Resources.OpenFile}" Command="{Binding OpenFile}"/>
291291
</ContextMenu>
292292
</Grid.Resources>
293293

src/GitHub.VisualStudio/UI/Views/PullRequestDetailView.xaml.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public PullRequestDetailView()
4141
this.WhenActivated(d =>
4242
{
4343
d(ViewModel.OpenOnGitHub.Subscribe(_ => DoOpenOnGitHub()));
44-
d(ViewModel.OpenFile.Subscribe(x => DoOpenFile((IPullRequestFileNode)x).Forget()));
44+
d(ViewModel.OpenFile.Subscribe(x => DoOpenFile((IPullRequestFileNode)x)));
4545
d(ViewModel.DiffFile.Subscribe(x => DoDiffFile((IPullRequestFileNode)x).Forget()));
4646
});
4747
}
@@ -65,15 +65,12 @@ void DoOpenOnGitHub()
6565
browser.OpenUrl(url);
6666
}
6767

68-
async Task DoOpenFile(IPullRequestFileNode file)
68+
void DoOpenFile(IPullRequestFileNode file)
6969
{
7070
try
7171
{
72-
var fileName = await ViewModel.ExtractFile(file);
73-
var window = Services.Dte.ItemOperations.OpenFile(fileName);
74-
75-
// If the file we extracted isn't the current file on disk, make the window read-only.
76-
window.Document.ReadOnly = fileName != file.DirectoryPath;
72+
var fileName = ViewModel.GetLocalFilePath(file);
73+
Services.Dte.ItemOperations.OpenFile(fileName);
7774
}
7875
catch (Exception e)
7976
{

0 commit comments

Comments
 (0)