Menu

[38707f]: / ClassGitDirectory.cs  Maximize  Restore  History

Download this file

157 lines (134 with data), 5.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
namespace GitForce
{
public sealed class GitFileInfo
{
/// <summary>
/// Short name of a file
/// </summary>
public string Name;
/// <summary>
/// Full path name to a file
/// </summary>
public string FullName;
/// <summary>
/// Class constructor
/// </summary>
public GitFileInfo(string fullName)
{
FullName = fullName;
Name = Path.GetFileName(FullName);
}
}
public sealed class GitDirectoryInfo
{
/// <summary>
/// Sort the files returned by GetFiles() funtion by name or by their extension
/// </summary>
public enum SortBy { Name, Extension };
/// <summary>
/// Short name of a directory
/// </summary>
public string Name;
/// <summary>
/// Full path to the directory
/// </summary>
public string FullName;
/// <summary>
/// List of files and directories at this level
/// </summary>
public List<string> List;
/// <summary>
/// Class constructor
/// </summary>
public GitDirectoryInfo(string fullName, List<string> list)
{
FullName = fullName;
Name = Path.GetFileName(FullName);
List = list;
}
/// <summary>
/// Returns a list of subdirectories of the current directory
/// </summary>
public GitDirectoryInfo[] GetDirectories()
{
// As the subdirectories are being added, keep them sorted
SortedDictionary<string, GitDirectoryInfo> dict = new SortedDictionary<string, GitDirectoryInfo>();
// For every node item in our list that represents a directory, add it
// to the bucket where each represents one subdirectory node at that level
foreach (string s in List)
{
// Pick only directories from the list and create a new directory node
int index = s.IndexOf(Path.DirectorySeparatorChar);
if (index > 0)
{
string name = s.Substring(0, index);
string rest = s.Substring(index + 1);
string path = Path.Combine(FullName, name);
GitDirectoryInfo newDir = new GitDirectoryInfo(path, new List<string>());
if (dict.ContainsKey(name))
newDir = dict[name];
newDir.List.Add(rest);
dict[name] = newDir;
}
}
// Return the array of values of a sorted dictionary:
// values are GitDirectoryInfo structures
return dict.Values.ToArray();
}
/// <summary>
/// Return a list of files for the current directory
/// </summary>
public GitFileInfo[] GetFiles(SortBy sortBy)
{
List<GitFileInfo> files = (from s in List
where s.IndexOf(Path.DirectorySeparatorChar) < 0
select Path.Combine(FullName, s)
into path select new GitFileInfo(path)).ToList();
// Add all items in the list that dont have directory separator
// Those are the files residing at this level
// Sort the list by the name or extension of the files in it
switch (sortBy)
{
case SortBy.Name:
files.Sort((f1, f2) => f1.Name.CompareTo(f2.Name));
break;
case SortBy.Extension:
files.Sort((f1, f2) => Path.GetExtension(f1.Name).CompareTo(Path.GetExtension(f2.Name)));
break;
}
return files.ToArray();
}
/// <summary>
/// Recursively create a list of directories and files from the given path.
/// </summary>
public static List<string> GetFilesRecursive(string path)
{
List<string> result = new List<string>();
Stack<string> stack = new Stack<string>();
stack.Push(path);
while (stack.Count > 0)
{
string dir = stack.Pop();
try
{
result.AddRange(Directory.GetFiles(dir, "*.*"));
foreach (string d in
Directory.GetDirectories(dir).Where(d => !d.EndsWith(Path.DirectorySeparatorChar + ".git")
|| Properties.Settings.Default.ShowDotGitFolders))
{
stack.Push(d);
}
}
catch (Exception ex)
{
App.PrintLogMessage(ex.Message, MessageType.Error);
}
}
return result;
}
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.