Browse Source

Making GitHead immutable.

LibGit2Sharp
Steffen Forkmann 15 years ago
parent
commit
e537aa5ab8
  1. 38
      GitCommands/Git/GitCommands.cs
  2. 127
      GitCommands/Git/GitHead.cs
  3. 14
      GitCommands/Git/IGitItem.cs
  4. 5
      GitUI/FormAddSubmodule.cs
  5. 11
      GitUI/FormPull.cs
  6. 3
      GitUI/FormPush.cs

38
GitCommands/Git/GitCommands.cs

@ -1945,45 +1945,15 @@ namespace GitCommands
{
if (itemsString.Length <= 42) continue;
var head = new GitHead
{
Guid = itemsString.Substring(0, 40),
Name = itemsString.Substring(41).Trim()
};
if (head.Name.Length > 0 && head.Name.LastIndexOf("/") > 1)
{
if (head.Name.Contains("refs/tags/"))
{
//we need the one containing ^{}, because it contains the reference
if (head.Name.Contains("^{}"))
{
head.Name = head.Name.Substring(0, head.Name.Length - 3);
}
head.Name = head.Name.Substring(head.Name.LastIndexOf("/") + 1);
head.IsHead = false;
head.IsTag = true;
}
else
{
head.IsHead = head.Name.Contains("refs/heads/");
head.IsRemote = head.Name.Contains("refs/remotes/");
head.IsTag = false;
head.IsOther = !head.IsHead && !head.IsRemote && !head.IsTag;
if (head.IsHead)
head.Name = head.Name.Substring(head.Name.LastIndexOf("heads/") + 6);
else if (head.IsRemote)
head.Name = head.Name.Substring(head.Name.LastIndexOf("remotes/") + 8);
else
head.Name = head.Name.Substring(head.Name.LastIndexOf("/") + 1);
}
}
heads.Add(head);
var guid = itemsString.Substring(0, 40);
var completeName = itemsString.Substring(41).Trim();
heads.Add(new GitHead(guid, completeName));
}
return heads;
}
public static List<string> GetBranches(bool remotes, string filterRemote)
{
var tree =

127
GitCommands/Git/GitHead.cs

@ -1,42 +1,51 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace GitCommands
{
public class GitHead : IGitItem
{
public string Guid { get; set; }
public string Name { get; set; }
public string HeadType { get; set; }
public bool Selected { get; set; }
private readonly string _mergeSettingName;
private readonly string _remoteSettingName;
private List<IGitItem> _subItems;
public GitHead()
public GitHead(string guid, string completeName)
{
Guid = guid;
Selected = false;
CompleteName = completeName;
ParseName();
IsTag = CompleteName.Contains("refs/tags/");
IsHead = CompleteName.Contains("refs/heads/");
IsRemote = CompleteName.Contains("refs/remotes/");
_remoteSettingName = String.Format("branch.{0}.remote", Name);
_mergeSettingName = String.Format("branch.{0}.merge", Name);
}
public bool IsHead { get; set; }
public bool IsTag { get; set; }
public bool IsRemote { get; set; }
public bool IsOther { get; set; }
public string CompleteName { get; private set; }
public bool Selected { get; set; }
public bool IsTag { get; private set; }
public bool IsHead { get; private set; }
public bool IsRemote { get; private set; }
public string Remote
public bool IsOther
{
get
{
return GitCommands.GetSetting("branch." + Name + ".remote");
}
get { return !IsHead && !IsRemote && !IsTag; }
}
public string Remote
{
get { return GitCommands.GetSetting(_remoteSettingName); }
set
{
if (string.IsNullOrEmpty(value))
{
GitCommands.UnSetSetting("branch." + Name + ".remote");
}
if (String.IsNullOrEmpty(value))
GitCommands.UnSetSetting(_remoteSettingName);
else
{
GitCommands.SetSetting("branch." + Name + ".remote", value);
GitCommands.SetSetting(_remoteSettingName, value);
if (MergeWith == "")
MergeWith = Name;
@ -48,37 +57,73 @@ namespace GitCommands
{
get
{
string merge = GitCommands.GetSetting("branch." + Name + ".merge");
if (merge.StartsWith("refs/heads/"))
return merge.Substring(11);
return merge;
var merge = GitCommands.GetSetting(_mergeSettingName);
return merge.StartsWith("refs/heads/") ? merge.Substring(11) : merge;
}
set
{
if (string.IsNullOrEmpty(value))
{
GitCommands.UnSetSetting("branch." + Name + ".merge");
}
if (String.IsNullOrEmpty(value))
GitCommands.UnSetSetting(_mergeSettingName);
else
{
GitCommands.SetSetting("branch." + Name + ".merge", "refs/heads/" + value);
}
GitCommands.SetSetting(_mergeSettingName, "refs/heads/" + value);
}
}
protected List<IGitItem> subItems;
public static GitHead NoHead
{
get { return new GitHead(null, ""); }
}
public static GitHead AllHeads
{
get { return new GitHead(null, "*"); }
}
#region IGitItem Members
public string Guid { get; private set; }
public string Name { get; private set; }
public List<IGitItem> SubItems
{
get
get { return _subItems ?? (_subItems = GitCommands.GetTree(Guid)); }
}
#endregion
public override string ToString()
{
return CompleteName;
}
private void ParseName()
{
if (CompleteName.Length == 0 || !CompleteName.Contains("/"))
{
if (subItems == null)
{
subItems = GitCommands.GetTree(Guid);
}
Name = CompleteName;
return;
}
return subItems;
if (IsTag)
{
// we need the one containing ^{}, because it contains the reference
if (CompleteName.Contains("^{}"))
Name = CompleteName.Substring(0, CompleteName.Length - 3);
Name = CompleteName.Substring(CompleteName.LastIndexOf("/") + 1);
return;
}
if (IsHead)
{
Name = CompleteName.Substring(CompleteName.LastIndexOf("heads/") + 6);
return;
}
if (IsRemote)
{
Name = CompleteName.Substring(CompleteName.LastIndexOf("remotes/") + 8);
return;
}
Name = CompleteName.Substring(CompleteName.LastIndexOf("/") + 1);
}
}
}
}

14
GitCommands/Git/IGitItem.cs

@ -1,16 +1,12 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
namespace GitCommands
{
public interface IGitItem
{
string Guid { get; set; }
string Name { get; set; }
string Guid { get; }
string Name { get; }
List<IGitItem> SubItems
{
get;
}
List<IGitItem> SubItems { get; }
}
}
}

5
GitUI/FormAddSubmodule.cs

@ -5,6 +5,7 @@ using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using GitCommands;
namespace GitUI
{
@ -62,9 +63,7 @@ namespace GitUI
List<GitCommands.GitHead> heads = GitCommands.GitCommands.GetHeads(false);
GitCommands.GitHead noHead = new GitCommands.GitHead();
noHead.Name = "";
heads.Insert(0, noHead);
heads.Insert(0, GitHead.NoHead);
Branch.DisplayMember = "Name";
Branch.DataSource = heads;

11
GitUI/FormPull.cs

@ -98,8 +98,7 @@ namespace GitUI
{
if (head.IsRemote && head.Name.StartsWith(Remotes.Text, StringComparison.CurrentCultureIgnoreCase))
{
GitCommands.GitHead remoteHead = new GitCommands.GitHead();
remoteHead.Name = head.Name.Substring(head.Name.LastIndexOf("/") + 1);
var remoteHead = new GitHead(null,head.Name.Substring(head.Name.LastIndexOf("/") + 1));
Heads.Insert(0, remoteHead);
}
@ -108,12 +107,8 @@ namespace GitUI
}
Branches.DisplayMember = "Name";
GitCommands.GitHead allHead = new GitCommands.GitHead();
allHead.Name = "*";
Heads.Insert(0, allHead);
GitCommands.GitHead noHead = new GitCommands.GitHead();
noHead.Name = "";
Heads.Insert(0, noHead);
Heads.Insert(0, GitHead.AllHeads);
Heads.Insert(0, GitHead.NoHead);
Branches.DataSource = Heads;
}
finally

3
GitUI/FormPush.cs

@ -257,7 +257,8 @@ namespace GitUI
private void TagDropDown(object sender, EventArgs e)
{
TagComboBox.DisplayMember = "Name";
TagComboBox.DataSource = GitCommands.GitCommands.GetHeads(true, false);
var tags = GitCommands.GitCommands.GetHeads(true, false);
TagComboBox.DataSource = tags;
}
private void ForcePushBranchesCheckedChanged(object sender, EventArgs e)

Loading…
Cancel
Save