如何监视目录的变化并自动推送到Git服务器
在进行软件工程推进的管理活动中,总会遇到一些传统的开发者(大多数是软硬结合的数字电路开发人员),不愿意使用版本管理工具,就其原因,可能无外乎就是如下几个方面:
- 怕麻烦
- 有学习成本
- 出于自己的技术保密心理
除了最后一点需要组织层面介入解决之外,其他的两个方面都我觉的可以通过软件来解决。大体思路如下:
- 配置管理人员或者其他相关人员辅助配置好GIT登录信息和Server端的分支信息,以及本地仓库的初始化;
- 利用windows的相关API监视目录变化,如果发生变化则自动执行Git的备份过程(为了降低使用难度,可以让配置管理人员将上传备份过程写成一个脚本或者批处理,如果发生变化则让脚本自动完成备份操作)
别的不说了,核心代码如下(代码很烂):
//当变化发生则执行外部程序
string dateAndTime = DateTime.Now.ToString();
if(commitstr==null)
{
commitstr = dateAndTime;
}
//this code shows how to execute the BAT file in windows
Process process = new Process();
process.StartInfo.UseShellExecute =false;
process.StartInfo.RedirectStandardOutput =true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = AppDomain.CurrentDomain.BaseDirectory + "\\execute.bat";
process.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
{
if(!String.IsNullOrEmpty(e.Data))
{
Form1.curForm1.ShowMessages(null, e.Data);
}
}); //show the output information to the other controller
process.StartInfo.Arguments = commitstr;
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
Console.WriteLine("OK");
process.Close();
//这段代码展示了如何监测目录文件的变化
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DirectoryWatcher
{
enum NotifyKind {NONE, CREATE,ADD,CHANGE,DELETE }; //变更的类型
class DirectoryChangeNotify
{
/*
static void Main()
{
Run(@"D:\TEMP\");
}*/
public static void Run(string directory)
{
FileSystemWatcher watcher = new FileSystemWatcher(directory);
watcher.NotifyFilter = NotifyFilters.Attributes
| NotifyFilters.CreationTime
| NotifyFilters.DirectoryName
| NotifyFilters.FileName
| NotifyFilters.LastAccess
| NotifyFilters.LastWrite
| NotifyFilters.Security
| NotifyFilters.Size;
watcher.Changed += OnChanged;
watcher.Created += OnCreated;
watcher.Deleted += OnDeleted;
watcher.Renamed += OnRenamed;
watcher.Error += OnError;
watcher.Filter = "*.*";
watcher.IncludeSubdirectories = true;
watcher.EnableRaisingEvents = true;
Console.WriteLine("Press enter to exit.");
Console.ReadLine();
}
private static void OnChanged(object sender, FileSystemEventArgs e)
{
if(e.ChangeType != WatcherChangeTypes.Changed)
{
return;
}
Console.WriteLine($"Changed:{e.FullPath}");
DirectoryOperation.GetInstance().Changed();
}
private static void OnCreated(object sender, FileSystemEventArgs e)
{
string value = $"Create:{e.FullPath}";
Console.WriteLine(value);
DirectoryOperation.GetInstance().Created();
}
private static void OnDeleted(object sender, FileSystemEventArgs e)
{
DirectoryOperation.GetInstance().Deleted();
}
private static void OnRenamed(object sender, RenamedEventArgs e)
{
Console.WriteLine($"Renamed:");
Console.WriteLine($" Old:{e.OldFullPath}");
Console.WriteLine($" New:{e.FullPath}");
}
private static void OnError(object sender, ErrorEventArgs e) =>
PrintException(e.GetException());
private static void PrintException(Exception ex)
{
if(ex!=null)
{
Console.WriteLine($"Message:{ex.Message}");
Console.WriteLine("Stacktrace:");
Console.WriteLine(ex.StackTrace);
Console.WriteLine();
PrintException(ex.InnerException);
}
}
}
}
结束