optimize<InteractiveRebase>: remove commit message editor from InteractiveRebase panel (as a result of my bad implementation)

This commit is contained in:
leo 2020-12-23 20:40:12 +08:00
parent c816dcf039
commit 567fad495b
3 changed files with 94 additions and 196 deletions

View file

@ -35,69 +35,6 @@ namespace SourceGit {
});
}
/// <summary>
/// Startup event.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnAppStartup(object sender, StartupEventArgs e) {
// Use this app as a sequence editor?
var args = e.Args;
if (args.Length > 1) {
if (args[0] == "--interactive-rebase") {
if (args.Length < 3) {
Environment.Exit(1);
return;
}
File.WriteAllText(args[2], File.ReadAllText(args[1]));
}
Environment.Exit(0);
return;
}
// Load settings.
var settingFile = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"SourceGit",
"preference.json");
if (!File.Exists(settingFile)) {
Setting = new Preference();
} else {
Setting = JsonSerializer.Deserialize<Preference>(File.ReadAllText(settingFile));
}
// Try auto configure git via registry.
if (!IsGitConfigured) {
var root = RegistryKey.OpenBaseKey(
RegistryHive.LocalMachine,
Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32);
var git = root.OpenSubKey("SOFTWARE\\GitForWindows");
if (git != null) {
Setting.Tools.GitExecutable = Path.Combine(
git.GetValue("InstallPath") as string,
"bin",
"git.exe");
}
}
// Apply themes
if (Setting.UI.UseLightTheme) {
foreach (var rs in Current.Resources.MergedDictionaries) {
if (rs.Source != null && rs.Source.OriginalString.StartsWith("pack://application:,,,/Resources/Themes/")) {
rs.Source = new Uri("pack://application:,,,/Resources/Themes/Light.xaml", UriKind.Absolute);
break;
}
}
}
// Show main window
Current.MainWindow = new UI.Launcher();
Current.MainWindow.Show();
}
/// <summary>
/// Open repository.
/// </summary>
@ -122,6 +59,56 @@ namespace SourceGit {
File.WriteAllText(settingFile, data);
}
/// <summary>
/// Startup event.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnAppStartup(object sender, StartupEventArgs e) {
// Use this app as a sequence editor?
if (OpenAsEditor(e)) return;
// Load settings.
var settingFile = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"SourceGit",
"preference.json");
if (!File.Exists(settingFile)) {
Setting = new Preference();
} else {
Setting = JsonSerializer.Deserialize<Preference>(File.ReadAllText(settingFile));
}
// Try auto configure git via registry.
if (!IsGitConfigured) {
var root = RegistryKey.OpenBaseKey(
RegistryHive.LocalMachine,
Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32);
var git = root.OpenSubKey("SOFTWARE\\GitForWindows");
if (git != null) {
Setting.Tools.GitExecutable = Path.Combine(
git.GetValue("InstallPath") as string,
"bin",
"git.exe");
}
}
// Apply themes
if (Setting.UI.UseLightTheme) {
foreach (var rs in Current.Resources.MergedDictionaries) {
if (rs.Source != null && rs.Source.OriginalString.StartsWith("pack://application:,,,/Resources/Themes/")) {
rs.Source = new Uri("pack://application:,,,/Resources/Themes/Light.xaml", UriKind.Absolute);
break;
}
}
}
// Show main window
Current.MainWindow = new UI.Launcher();
Current.MainWindow.Show();
}
/// <summary>
/// Deactivated event.
/// </summary>
@ -131,5 +118,29 @@ namespace SourceGit {
GC.Collect();
SaveSetting();
}
/// <summary>
/// Try to open app as git editor
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
private bool OpenAsEditor(StartupEventArgs e) {
if (e.Args.Length < 3) return false;
switch (e.Args[0]) {
case "--sequence":
var output = File.CreateText(e.Args[2]);
output.Write(File.ReadAllText(e.Args[1]));
output.Flush();
output.Close();
Environment.Exit(0);
break;
default:
return false;
}
return true;
}
}
}

View file

@ -139,40 +139,11 @@
</ComboBox.ItemTemplate>
</ComboBox>
<Grid Grid.Column="1">
<ContentControl x:Name="MessageEditorAnchor" MouseDoubleClick="PopupMessageEditor">
<TextBlock Text="{Binding Subject, Mode=TwoWay}" VerticalAlignment="Center" Foreground="{StaticResource Brush.FG}"/>
</ContentControl>
<Popup x:Name="MessageEditor" Placement="Bottom" IsOpen="{Binding IsEditorOpened}" VerticalOffset="1" Height="150" Width="400" PlacementTarget="{Binding ElementName=MessageEditorAnchor}">
<Border BorderBrush="{StaticResource Brush.Accent1}" BorderThickness="1" Background="{StaticResource Brush.BG4}">
<Grid Margin="8">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="32"/>
</Grid.RowDefinitions>
<Border Grid.Row="0" BorderBrush="{StaticResource Brush.Border1}" BorderThickness="1">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="24"/>
<RowDefinition Height="1"/>
<RowDefinition Height="79"/>
</Grid.RowDefinitions>
<TextBox Grid.Row="0" Text="{Binding EditSubject, Mode=TwoWay}" Height="32" Padding="2,0" helpers:TextBoxHelper.Placeholder="Enter commit subject" BorderThickness="0"/>
<Rectangle Grid.Row="1" Height="1" Fill="{StaticResource Brush.FG}" Opacity="0.1"/>
<TextBox Grid.Row="2" Text="{Binding EditMessage, Mode=TwoWay}" TextChanged="CommitMessageChanged" Height="79" ScrollViewer.VerticalScrollBarVisibility="Auto" Padding="2" helpers:TextBoxHelper.Placeholder="Enter commit description. Optional" helpers:TextBoxHelper.PlaceholderBaseline="Top" BorderThickness="0" AcceptsReturn="True" AcceptsTab="True" TextWrapping="Wrap"/>
</Grid>
</Border>
<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,8,0,0">
<Button Click="ApplyMessageEdit" Height="24" Style="{StaticResource Style.Button.AccentBordered}" BorderThickness="1" Content="APPLY" Margin="8,0"/>
<Button Click="HideMessageEditor" Height="24" Style="{StaticResource Style.Button.Bordered}" BorderThickness="1" Content="CANCEL"/>
</StackPanel>
</Grid>
</Border>
</Popup>
</Grid>
<TextBlock
Grid.Column="1"
Text="{Binding Commit.Subject}"
VerticalAlignment="Center"
Foreground="{StaticResource Brush.FG}"/>
<TextBlock
Grid.Column="2"

View file

@ -51,64 +51,17 @@ namespace SourceGit.UI {
/// <summary>
/// Rebase item.
/// </summary>
public class InteractiveRebaseItem : INotifyPropertyChanged {
private InteractiveRebaseMode mode = InteractiveRebaseMode.Pick;
private bool isEditorOpened = false;
private string editSubject = null;
private string editMsg = null;
public class InteractiveRebaseItem {
private InteractiveRebaseMode mode = InteractiveRebaseMode.Pick;
public event PropertyChangedEventHandler PropertyChanged;
public Git.Commit Commit { get; set; }
public Git.Commit Commit {
get;
set;
}
public int Mode {
get { return (int)mode; }
set {
if (value != (int)mode) {
mode = (InteractiveRebaseMode)value;
PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Mode"));
}
}
}
public bool IsEditorOpened {
get { return isEditorOpened; }
set {
if (value != isEditorOpened) {
isEditorOpened = value;
PropertyChanged.Invoke(this, new PropertyChangedEventArgs("IsEditorOpened"));
}
}
}
public string Subject {
get { return Commit.Subject; }
set {
if (value != Commit.Subject) {
Commit.Subject = value;
PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Subject"));
}
}
}
public string EditSubject {
get { return editSubject; }
set {
if (value != editMsg) {
editSubject = value;
PropertyChanged.Invoke(this, new PropertyChangedEventArgs("EditSubject"));
}
}
}
public string EditMessage {
get { return editMsg; }
set {
if (value != editMsg) {
editMsg = value;
PropertyChanged.Invoke(this, new PropertyChangedEventArgs("EditMessage"));
}
}
get { return (int)mode; }
set { mode = (InteractiveRebaseMode)value; }
}
}
@ -181,46 +134,12 @@ namespace SourceGit.UI {
#endregion
private void CommitSelectionChanged(object sender, SelectionChangedEventArgs e) {
foreach (var obj in e.RemovedItems) {
var item = obj as InteractiveRebaseItem;
if (item != null) item.IsEditorOpened = false;
}
if (e.AddedItems.Count == 1) {
var item = e.AddedItems[0] as InteractiveRebaseItem;
if (item != null) commitViewer.SetData(repo, item.Commit);
}
}
private void PopupMessageEditor(object sender, MouseButtonEventArgs e) {
var item = (sender as Control).DataContext as InteractiveRebaseItem;
if (item == null) return;
item.EditSubject = item.Commit.Subject;
item.EditMessage = item.Commit.Message;
item.IsEditorOpened = true;
}
private void HideMessageEditor(object sender, RoutedEventArgs e) {
var item = (sender as Button).DataContext as InteractiveRebaseItem;
if (item == null) return;
item.IsEditorOpened = false;
}
private void ApplyMessageEdit(object sender, RoutedEventArgs e) {
var item = (sender as Button).DataContext as InteractiveRebaseItem;
if (item == null) return;
item.Subject = item.EditSubject;
item.Commit.Message = item.EditMessage;
item.Mode = (int)InteractiveRebaseMode.Reword;
item.IsEditorOpened = false;
}
private void CommitMessageChanged(object sender, TextChangedEventArgs e) {
(sender as TextBox).ScrollToEnd();
}
private void MoveUp(object sender, RoutedEventArgs e) {
var item = (sender as Button).DataContext as InteractiveRebaseItem;
if (item == null) return;
@ -259,39 +178,36 @@ namespace SourceGit.UI {
private void Start(object sender, RoutedEventArgs e) {
var temp = Path.GetTempFileName();
var stream = new FileStream(temp, FileMode.Create);
var writer = new StreamWriter(stream);
var writer = File.CreateText(temp);
for (int i = Items.Count - 1; i >= 0; i--) {
var item = Items[i];
switch ((InteractiveRebaseMode)item.Mode) {
case InteractiveRebaseMode.Pick:
writer.WriteLine($"p {item.Commit.ShortSHA} {item.Subject}");
writer.WriteLine($"p {item.Commit.ShortSHA} {item.Commit.Subject}");
break;
case InteractiveRebaseMode.Reword:
writer.WriteLine($"r {item.Commit.ShortSHA} {item.Subject}");
writer.WriteLine($"r {item.Commit.ShortSHA} {item.Commit.Subject}");
break;
case InteractiveRebaseMode.Squash:
writer.WriteLine($"s {item.Commit.ShortSHA} {item.Subject}");
writer.WriteLine($"s {item.Commit.ShortSHA} {item.Commit.Subject}");
break;
case InteractiveRebaseMode.Fixup:
writer.WriteLine($"f {item.Commit.ShortSHA} {item.Subject}");
writer.WriteLine($"f {item.Commit.ShortSHA} {item.Commit.Subject}");
break;
case InteractiveRebaseMode.Drop:
writer.WriteLine($"d {item.Commit.ShortSHA} {item.Subject}");
writer.WriteLine($"d {item.Commit.ShortSHA} {item.Commit.Subject}");
break;
}
}
writer.Flush();
stream.Flush();
writer.Close();
stream.Close();
repo.SetWatcherEnabled(false);
var editor = Process.GetCurrentProcess().MainModule.FileName;
var errs = repo.RunCommand($"-c sequence.editor=\"\\\"{editor}\\\" --interactive-rebase \\\"{temp}\\\"\" rebase -i {from}", null);
var errs = repo.RunCommand($"-c sequence.editor=\"\\\"{editor}\\\" --sequence \\\"{temp}\\\"\" rebase -i {from}", null);
repo.AssertCommand(errs);
Close();