Skip to content

Commit

Permalink
add git blame function
Browse files Browse the repository at this point in the history
Just display the file git blame results. Blame file highlighting and searching are to be developed.
  • Loading branch information
yysun committed Jun 29, 2012
1 parent 1125295 commit 6530153
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 52 deletions.
8 changes: 6 additions & 2 deletions GitApi/DataServices/GitTreeObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class GitTreeObject : INotifyPropertyChanged
{
public string Id { get; internal set; }
public string Name { get; internal set; }
public string FullName { get; internal set; }
public string Type { get; set; }
public string Mode { get; set; }
public string Repository { get; internal set; }
Expand Down Expand Up @@ -66,14 +67,17 @@ private GitTreeObject ParseString(string itemsString)
return null;

var guidStart = itemsString.IndexOf(' ', 7);
var name = itemsString.Substring(guidStart + 42).Trim();
var fullName = this.FullName.Length == 0 ? name : this.FullName + "/" + name;

return new GitTreeObject
{
Mode = itemsString.Substring(0, 6),
Type = itemsString.Substring(7, guidStart - 7).ToLower(),
Id = itemsString.Substring(guidStart + 1, 40),
Name = itemsString.Substring(guidStart + 42).Trim(),
Repository = this.Repository
Name = name,
FullName = fullName,
Repository = this.Repository,
};
}

Expand Down
5 changes: 3 additions & 2 deletions GitApi/DataServices/RepositoryGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,9 @@ public GitTreeObject GetTree(string commitId)

return new GitTreeObject
{
Id = commitId, Name = "", Type="tree", IsExpanded= true,
Repository = this.workingDirectory
Id = commitId, Name = "", FullName = "",
Type="tree", IsExpanded= true,
Repository = this.workingDirectory,
};
}

Expand Down
14 changes: 14 additions & 0 deletions GitApi/GitFileStatusTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,20 @@ public IDictionary<string, string> Configs
return configs ?? new Dictionary<string, string>();
}
}

public string Blame(string fileName)
{
if (!this.HasGitRepository) return "";

var tmpFileName = Path.ChangeExtension(Path.GetTempFileName(), ".blame");

if (GitBash.Exists)
{
var fileNameRel = GetRelativeFileName(fileName);
GitBash.RunCmd(string.Format("blame -M -w -- \"{0}\" > \"{1}\"", fileNameRel, tmpFileName), this.GitWorkingDirectory);
}
return tmpFileName;
}
}

public abstract class Log
Expand Down
51 changes: 26 additions & 25 deletions GitUI/UI/CommitDetails.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -180,39 +180,40 @@
<GradientStop Color="White" Offset="1" />
</LinearGradientBrush>
</Grid.Background>
<TextBlock x:Name="txtFileName" Text="" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="10,0,0,0" />
<TextBlock x:Name="txtFileName" Text="" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10,0,0" />
<CheckBox Content="Blame" Height="16" HorizontalAlignment="Right" Margin="0,10,20,0" Name="chkBlame" VerticalAlignment="Top" Click="chkBlame_Click" />
</Grid>

<Grid Grid.Column="2" Background="White" Margin="0,38,0,10" HorizontalAlignment="Stretch">
<!--<ContentControl x:Name="editor" />-->
<avalonEdit:TextEditor Name="editor" FontFamily="Consolas" FontSize="10pt"/>
<avalonEdit:TextEditor Name="editor" FontFamily="Consolas" FontSize="10pt"/>
</Grid>

<Grid Grid.Column="4" Height="36" HorizontalAlignment="Stretch" VerticalAlignment="Top" >
<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFCBCBCB" Offset="0" />
<GradientStop Color="White" Offset="1" />
</LinearGradientBrush>
</Grid.Background>
<TextBlock Text="Comments" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="10,0,0,0" />
</Grid>
<RichTextBox Grid.Column="4" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0,38,10,10"
Name="txtComments" IsReadOnly="True"
VerticalScrollBarVisibility="Visible">
<RichTextBox.Resources>
<Style TargetType="{x:Type Paragraph}">
<Setter Property="Margin" Value="0"/>
</Style>
</RichTextBox.Resources>
</RichTextBox>

<GridSplitter Grid.Column="3"
<Grid Grid.Column="4" Height="36" HorizontalAlignment="Stretch" VerticalAlignment="Top" >
<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFCBCBCB" Offset="0" />
<GradientStop Color="White" Offset="1" />
</LinearGradientBrush>
</Grid.Background>
<TextBlock Text="Comments" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="10,0,0,0" />
</Grid>
<RichTextBox Grid.Column="4" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0,38,10,10"
Name="txtComments" IsReadOnly="True"
VerticalScrollBarVisibility="Visible">
<RichTextBox.Resources>
<Style TargetType="{x:Type Paragraph}">
<Setter Property="Margin" Value="0"/>
</Style>
</RichTextBox.Resources>
</RichTextBox>

<GridSplitter Grid.Column="3"
VerticalAlignment="Stretch" HorizontalAlignment="Center" Width="4"
ResizeDirection="Columns" />
ResizeDirection="Columns" />

</Grid>
</Grid>
</Grid>

</UserControl>
76 changes: 53 additions & 23 deletions GitUI/UI/CommitDetails.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,23 +230,27 @@ private void fileTree_SelectedItemChanged(object sender, RoutedPropertyChangedEv
var selection = this.fileTree.SelectedValue as GitTreeObject;
if (selection != null)
{
txtFileName.Text = "Content: " + selection.Name;
var dispatcher = Dispatcher.CurrentDispatcher;
Action act = () =>
if (this.chkBlame.IsChecked == true) ShowBlame(selection.Name);
else
{
try
txtFileName.Text = "Content: " + selection.Name;
var dispatcher = Dispatcher.CurrentDispatcher;
Action act = () =>
{
var content = selection.Content;
if (content != null)
try
{
var tmpFileName = Path.ChangeExtension(Path.GetTempFileName(), Path.GetExtension(selection.Name));
File.WriteAllBytes(tmpFileName, content);
ShowFile(tmpFileName);
var content = selection.Content;
if (content != null)
{
var tmpFileName = Path.ChangeExtension(Path.GetTempFileName(), Path.GetExtension(selection.Name));
File.WriteAllBytes(tmpFileName, content);
ShowFile(tmpFileName);
}
}
}
catch { }
};
dispatcher.BeginInvoke(act, DispatcherPriority.ApplicationIdle);
catch { }
};
dispatcher.BeginInvoke(act, DispatcherPriority.ApplicationIdle);
}
}
}

Expand All @@ -257,22 +261,42 @@ private void patchList_SelectionChanged(object sender, SelectionChangedEventArgs
var selection = (this.patchList.SelectedItem) as Change;
if (selection != null)
{
txtFileName.Text = "Diff: " + selection.Name;
var dispatcher = Dispatcher.CurrentDispatcher;
Action act = () =>
if (this.chkBlame.IsChecked == true) ShowBlame(selection.Name);
else
{
try
txtFileName.Text = "Diff: " + selection.Name;
var dispatcher = Dispatcher.CurrentDispatcher;
Action act = () =>
{
var tmpFileName = this.tracker.DiffFile(selection.Name, commitId1, commitId2);
ShowFile(tmpFileName);
}
catch { }
};
try
{
var tmpFileName = this.tracker.DiffFile(selection.Name, commitId1, commitId2);
ShowFile(tmpFileName);
}
catch { }
};

dispatcher.BeginInvoke(act, DispatcherPriority.ApplicationIdle);
dispatcher.BeginInvoke(act, DispatcherPriority.ApplicationIdle);
}
}
}

private void ShowBlame(string fileName)
{
txtFileName.Text = "Blame: " + fileName;
var dispatcher = Dispatcher.CurrentDispatcher;
Action act = () =>
{
try
{
var tmpFileName = this.tracker.Blame(fileName);
ShowFile(tmpFileName);
}
catch { }
};
dispatcher.BeginInvoke(act, DispatcherPriority.ApplicationIdle);

}
private void btnSwitch_Click(object sender, RoutedEventArgs e)
{
var selected = patchList.SelectedValue;
Expand Down Expand Up @@ -343,5 +367,11 @@ private void btnSave_Click(object sender, RoutedEventArgs e)
}
}
}

private void chkBlame_Click(object sender, RoutedEventArgs e)
{
fileTree_SelectedItemChanged(this, null);
patchList_SelectionChanged(this, null);
}
}
}

0 comments on commit 6530153

Please sign in to comment.