public bool OpenFileLocation(string filePath) { try { string path = PlatformInfo.ConvertPathToPlatform(string.Format("{0}\\{1}", repository.repoPath, filePath)); if (!File.Exists(path)) { return(false); } if (PlatformInfo.platform == Platforms.Windows) { Process.Start("explorer.exe", "/select, " + path); return(true); } else { throw new Exception("Unsuported platform: " + PlatformInfo.platform); } } catch (Exception ex) { DebugLog.LogError("Failed to open folder location: " + ex.Message); } return(false); }
public bool UpdateSignature(string name, string email, SignatureLocations location) { lock (this) { try { // remove local sig if (signatureIsLocal && location == SignatureLocations.Global) { repository.RemoveSettings(SignatureLocations.Local, "user"); } // update sig if (!repository.SetSignature(location, name, email)) { throw new Exception(repository.lastError); } signatureName = name; signatureEmail = email; signatureIsLocal = location == SignatureLocations.Local; return(true); } catch (Exception e) { DebugLog.LogError("Update Signature: " + e.Message); return(false); } } }
public static bool OpenFolderLocation(string folderPath) { try { if (!Directory.Exists(folderPath)) { return(false); } if (PlatformInfo.platform == Platforms.Windows) { Process.Start("explorer.exe", PlatformInfo.ConvertPathToPlatform(folderPath)); return(true); } else { throw new Exception("Unsuported platform: " + PlatformInfo.platform); } } catch (Exception ex) { DebugLog.LogError("Failed to open file: " + ex.Message); } return(false); }
public static bool Save <T>(string filename, T settings) where T : new() { string path = Path.GetDirectoryName(filename); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } try { var xml = new XmlSerializer(typeof(T)); using (var stream = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None)) { xml.Serialize(stream, settings); } } catch (Exception e) { DebugLog.LogError("Save Settings Error: " + e.Message); return(false); } return(true); }
public bool AddFirstAutoCommit() { lock (this) { try { // clone if (!repository.Stage(".gitignore")) { throw new Exception(repository.lastError); } if (!repository.Commit("First Commit! (Auto generated by Git-It-GUI)")) { throw new Exception(repository.lastError); } } catch (Exception e) { DebugLog.LogError("Clone error: " + e.Message); return(false); } Refresh(); return(true); } }
public bool RemoveTracking() { lock (this) { if (activeBranch == null) { return(false); } if (!activeBranch.isTracking) { return(true); } bool success = true; try { if (!repository.RemoveActiveBranchTracking()) { throw new Exception(repository.lastError); } } catch (Exception e) { DebugLog.LogError("Remove Branch Error: " + e.Message); success = false; } Refresh(); return(success); } }
public bool IsUpToDateWithRemote(out bool yes) { lock (this) { if (activeBranch == null) { yes = false; return(false); } if (!activeBranch.isTracking) { yes = true; return(true); } try { if (!repository.IsUpToDateWithRemote(activeBranch.tracking.remoteState.name, activeBranch.tracking.name, out yes)) { throw new Exception(repository.lastError); } } catch (Exception e) { DebugLog.LogError("Remove Branch Error: " + e.Message); yes = false; return(false); } return(true); } }
public MergeResults MergeBranchIntoActive(BranchState srcBranch) { lock (this) { MergeResults mergeResult; try { if (!repository.MergeBranchIntoActive(srcBranch.fullname)) { throw new Exception(repository.lastError); } bool yes; if (!repository.ConflitedExist(out yes)) { throw new Exception(repository.lastError); } mergeResult = yes ? MergeResults.Conflicts : MergeResults.Succeeded; } catch (Exception e) { DebugLog.LogError("BranchManager.Merge Failed: " + e.Message); mergeResult = MergeResults.Error; } Refresh(); return(mergeResult); } }
private bool RefreshBranches(bool isRefreshMode) { isEmpty = false; try { // gather branches BranchState[] bStates; if (!repository.GetBrancheStates(out bStates)) { throw new Exception(repository.lastError); } branchStates = bStates; // check for new repo state if (branchStates.Length == 0) { isEmpty = true; activeBranch = null; branchStates = null; remoteStates = null; DebugLog.LogWarning("New branch, nothing commit!"); return(true); } // find active branch activeBranch = Array.Find <BranchState>(branchStates, x => x.isActive); if (activeBranch.isRemote) { DebugLog.LogError("Active repo branch cannot be a remote: " + activeBranch.fullname); if (isRefreshMode) { Environment.Exit(0); } else { return(false); } } // gather remotes RemoteState[] rStates; if (!repository.GetRemoteStates(out rStates)) { throw new Exception(repository.lastError); } remoteStates = rStates; } catch (Exception e) { DebugLog.LogError("BranchManager.Refresh Failed: " + e.Message); return(false); } return(true); }
public void PruneLFSFiles() { lock (this) { try { if (!repository.lfs.Prune()) { throw new Exception(repository.lastError); } } catch (Exception e) { DebugLog.LogError("Failed to prune lfs files: " + e.Message); } } }
public void Optimize() { lock (this) { try { if (!repository.GarbageCollect()) { throw new Exception(repository.lastError); } } catch (Exception e) { DebugLog.LogError("Failed to gc: " + e.Message); } } }
public void OpenGitk(string filename = null) { lock (this) { try { // open gitk using (var process = new Process()) { if (PlatformInfo.platform == Platforms.Windows) { string programFilesx86, programFilesx64; PlatformInfo.GetWindowsProgramFilesPath(out programFilesx86, out programFilesx64); process.StartInfo.FileName = Path.Combine(programFilesx64, "Git", "cmd", "gitk.exe"); } else { throw new Exception("Unsported platform: " + PlatformInfo.platform); } process.StartInfo.WorkingDirectory = repository.repoPath; if (filename != null) { process.StartInfo.Arguments = string.Format("\"{0}\"", filename); } process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized; if (!process.Start()) { DebugLog.LogError("Failed to start history tool (is it installed?)"); return; } process.WaitForExit(); } } catch (Exception e) { DebugLog.LogError("Failed to start history tool: " + e.Message); return; } Refresh(); } }
/*private static string platformName = "Unknown"; * static AppManager() * { * switch (PlatformInfo.platform) * { * case Platforms.Windows: platformName = "Windows"; break; * case Platforms.Mac: platformName = "Mac"; break; * case Platforms.Linux: platformName = "Linux"; break; * default: throw new Exception("Unsupported platform: " + PlatformInfo.platform); * } * }*/ /// <summary> /// Must be called before using any other API feature /// </summary> /// <returns>True if succeeded</returns> public static bool Init() { try { // load settings settings = Settings.Load <XML.AppSettings>(Path.Combine(PlatformInfo.appDataPath, Settings.appSettingsFolderName, Settings.appSettingsFilename)); // apply default lfs ignore types var lowerCase = new List <string>() { ".jpg", ".jpeg", ".png", ".bmp", ".tga", ".tif", // image types ".psd", // image binary types ".ai", ".svg", ".dwg", // vector binary types ".ae", // video binary types ".mpeg", ".mov", ".avi", ".mp4", ".wmv", // video types ".wav", ".mp3", ".ogg", ".wma", ".acc", // audio types ".zip", ".7z", ".rar", ".tar", ".gz", // compression types ".fbx", ".obj", ".3ds", ".blend", ".ma", ".mb", ".dae", ".daz", ".stl", ".wrl", ".spp", ".sbs", ".sppr", ".sbsar", ".ztl", ".zpr", ".obg", // 3d formats ".pdf", ".doc", ".docx", // doc types ".unity", ".unitypackage", ".uasset", ".asset", ".exr", // known binary types ".bin", ".data", ".raw", ".hex", // unknown binary types }; defaultGitLFS_Exts = new List <string>(); defaultGitLFS_Exts.AddRange(lowerCase); for (int i = 0; i != lowerCase.Count; ++i) { defaultGitLFS_Exts.Add(lowerCase[i].ToUpper()); } // load LoadMergeDiffTool(); } catch (Exception e) { DebugLog.LogError("AppManager.Init Failed: " + e.Message); Dispose(); return(false); } return(true); }
public bool PruneRemoteBranches() { lock (this) { bool success = true; try { if (!repository.PruneRemoteBranches()) { throw new Exception(repository.lastError); } } catch (Exception e) { DebugLog.LogError("Failed to prune branches: " + e.Message); success = false; } Refresh(); return(success); } }
public bool Clone(string url, string destination, out string repoPath, StdInputStreamCallbackMethod writeUsernameCallback, StdInputStreamCallbackMethod writePasswordCallback) { lock (this) { try { // clone if (!repository.Clone(url, destination, out repoPath, writeUsernameCallback, writePasswordCallback)) { throw new Exception(repository.lastError); } repoPath = Path.Combine(destination, repoPath); return(true); } catch (Exception e) { DebugLog.LogError("Clone error: " + e.Message); repoPath = null; return(false); } } }
public bool RenameActiveBranch(string newBranchName) { lock (this) { bool success = true; try { if (!repository.RenameActiveBranch(newBranchName)) { throw new Exception(repository.lastError); } } catch (Exception e) { DebugLog.LogError("Rename active Branch Error: " + e.Message); success = false; } Refresh(); return(success); } }
public bool CopyTracking(BranchState srcRemoteBranch) { lock (this) { bool success = true; try { if (!repository.SetActiveBranchTracking(srcRemoteBranch.fullname)) { throw new Exception(repository.lastError); } } catch (Exception e) { DebugLog.LogError("Add/Update tracking Branch Error: " + e.Message); success = false; } Refresh(); return(success); } }
public bool RenameNonActiveBranch(BranchState currentBranch, string newBranchName) { lock (this) { bool success = true; try { if (!repository.RenameNonActiveBranch(currentBranch.fullname, newBranchName)) { throw new Exception(repository.lastError); } } catch (Exception e) { DebugLog.LogError("Rename non-active Branch Error: " + e.Message); success = false; } Refresh(); return(success); } }
public bool DeleteNonActiveBranch(BranchState branch) { lock (this) { bool success = true; try { if (!repository.DeleteBranch(branch.fullname, branch.isRemote)) { throw new Exception(repository.lastError); } } catch (Exception e) { DebugLog.LogError("Delete new Branch Error: " + e.Message); success = false; } Refresh(); return(success); } }
public int UnusedLFSFiles(out string size) { lock (this) { try { int count; if (!repository.lfs.PruneObjectCount(out count, out size)) { throw new Exception(repository.lastError); } return(count); } catch (Exception e) { DebugLog.LogError("Failed PruneObjectCount: " + e.Message); } size = null; return(-1); } }
public static T Load <T>(string filename) where T : new() { if (!File.Exists(filename)) { var settings = new T(); Save <T>(filename, settings); return(settings); } try { var xml = new XmlSerializer(typeof(T)); using (var stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.None)) { return((T)xml.Deserialize(stream)); } } catch (Exception e) { DebugLog.LogError("Load Settings Error: " + e.Message); return(new T()); } }
public bool Create(string repoPath) { lock (this) { try { if (!Directory.Exists(repoPath)) { Directory.CreateDirectory(repoPath); } if (!repository.Init(repoPath)) { throw new Exception(repository.lastError); } return(true); } catch (Exception e) { DebugLog.LogError("Clone error: " + e.Message); repoPath = null; return(false); } } }
public bool Checkout(BranchState branch, bool useFullname = false) { lock (this) { if (activeBranch == null) { return(false); } bool success = true; try { string name = useFullname ? branch.fullname : branch.name; if (activeBranch.name != name) { if (!repository.CheckoutBranch(name)) { throw new Exception(repository.lastError); } } else { DebugLog.LogError("Already on branch: " + name); success = false; } } catch (Exception e) { DebugLog.LogError("BranchManager.Checkout Failed: " + e.Message); success = false; } Refresh(); return(success); } }
public bool CheckoutNewBranch(string branchName, string remoteName = null) { lock (this) { bool success = true; try { // create branch if (!repository.CheckoutNewBranch(branchName)) { throw new Exception(repository.lastError); } // push branch to remote if (!string.IsNullOrEmpty(remoteName)) { if (!repository.PushLocalBranchToRemote(branchName, remoteName)) { //NOTE: this ignores false positive noise/errors that come from GitLab if (!string.IsNullOrEmpty(repository.lastError) && !repository.lastError.Contains("To create a merge request for")) { throw new Exception(repository.lastError); } } } } catch (Exception e) { DebugLog.LogError("Add new Branch Error: " + e.Message); success = false; } Refresh(); return(success); } }
public static void CheckForUpdates(string url, CheckForUpdatesCallbackMethod checkForUpdatesCallback) { try { // validate git install var repository = new Repository(); // git and git-lfs versions string gitVersion = null, gitlfsVersion = null; string gitlfsRequiredGitVersion = "0.0.0.0"; const string minGitVersion = "2.11.0", minGitLFSVersion = "1.5.5"; // get git version string try { if (!repository.GetVersion(out gitVersion)) { throw new Exception(repository.lastError); } } catch { DebugLog.LogError("git is not installed correctly. (Make sure git is usable in the cmd/terminal)"); client.Dispose(); if (checkForUpdatesCallback != null) { checkForUpdatesCallback(UpdateCheckResult.GitNotInstalledError); } DownloadGit(); return; } // get git-lfs version string try { if (!repository.lfs.GetVersion(out gitlfsVersion)) { throw new Exception(repository.lastError); } } catch { DebugLog.LogError("git-lfs is not installed correctly. (Make sure git-lfs is usable in the cmd/terminal)"); client.Dispose(); if (checkForUpdatesCallback != null) { checkForUpdatesCallback(UpdateCheckResult.GitLFSNotInstalledError); } DownloadGitLFS(); return; } // grab git version value string appendix = ""; if (PlatformInfo.platform == Platforms.Windows) { appendix = @"\.windows"; } var match = Regex.Match(gitVersion, @"git version (.*)" + appendix); if (match.Success && match.Groups.Count == 2) { gitVersion = match.Groups[1].Value; } else { DebugLog.LogError("Failed to grab git version!"); client.Dispose(); if (checkForUpdatesCallback != null) { checkForUpdatesCallback(UpdateCheckResult.GitVersionCheckError); } DownloadGit(); return; } // grab lfs and required git version value if (PlatformInfo.platform == Platforms.Windows) { appendix = @"; git .*\)"; } else { appendix = @"\)"; } match = Regex.Match(gitlfsVersion, @"git-lfs/(.*) \(GitHub; (\w*) (\w*); go (.*)" + appendix); if (match.Success && match.Groups.Count == 5) { gitlfsVersion = match.Groups[1].Value; gitlfsRequiredGitVersion = match.Groups[4].Value; } else { DebugLog.LogError("Failed to grab git-lfs version!"); client.Dispose(); if (checkForUpdatesCallback != null) { checkForUpdatesCallback(UpdateCheckResult.GitLFSVersionCheckError); } DownloadGitLFS(); return; } // make sure the git version installed is supporeted by lfs if (!IsValidVersion(gitVersion, gitlfsRequiredGitVersion)) { DebugLog.LogError(string.Format("'git-lfs' version is not compatible with 'git' version installed!")); client.Dispose(); if (checkForUpdatesCallback != null) { checkForUpdatesCallback(UpdateCheckResult.GitVersionToLowForLFS); } DownloadGit(); DownloadGitLFS(); return; } // check min git versions bool gitValid = true, gitlfsValid = true; if (!IsValidVersion(gitVersion, minGitVersion)) { DebugLog.LogError("Your 'git' version is out of date.\nDownload and install with defaults!"); gitValid = false; } if (!IsValidVersion(gitlfsVersion, minGitLFSVersion)) { DebugLog.LogError("Your 'git-lfs' version is out of date.\nDownload and install with defaults!"); gitlfsValid = false; } if (!gitValid || !gitlfsValid) { if (!gitValid) { DownloadGit(); } if (!gitlfsValid) { DownloadGitLFS(); } if (checkForUpdatesCallback != null) { checkForUpdatesCallback(UpdateCheckResult.BadVersionError); } return; } // check app version client = new WebClient(); client.DownloadStringCompleted += Client_DownloadStringCompleted; client.DownloadStringAsync(new Uri(url), checkForUpdatesCallback); } catch (Exception e) { DebugLog.LogError("Failed to check for updates: " + e.Message); if (checkForUpdatesCallback != null) { checkForUpdatesCallback(UpdateCheckResult.CommonError); } } }
private void DebugLog_StdErrorCallback(string line) { DebugLog.LogError(line); }
/// <summary> /// Use to open an existing repo /// </summary> /// <param name="repoPath">Path to git repo</param> /// <returns>True if succeeded</returns> public bool Open(string repoPath, bool checkForSettingErros = false) { lock (this) { isOpen = false; // unload repo if (string.IsNullOrEmpty(repoPath)) { repository.Close(); return(true); } bool isRefreshMode = repoPath == repository.repoPath; try { // load repo if (isRefreshMode) { repository.Close(); } if (!repository.Open(repoPath)) { throw new Exception(repository.lastError); } // check for git lfs lfsEnabled = repository.lfs.isEnabled; // check for .gitignore file if (!isRefreshMode) { string gitIgnorePath = Path.Combine(repoPath, ".gitignore"); if (!File.Exists(gitIgnorePath)) { DebugLog.LogWarning("No '.gitignore' file exists.\nAuto creating one!"); File.WriteAllText(gitIgnorePath, ""); } } // add repo to history AppManager.AddRepoToHistory(repoPath); // non-refresh checks if (!isRefreshMode) { // get signature string sigName, sigEmail; if (repository.GetSignature(SignatureLocations.Local, out sigName, out sigEmail)) { signatureName = sigName; signatureEmail = sigEmail; signatureIsLocal = true; if (string.IsNullOrEmpty(sigName) || string.IsNullOrEmpty(sigEmail)) { if (repository.GetSignature(SignatureLocations.Any, out sigName, out sigEmail)) { signatureName = sigName; signatureEmail = sigEmail; signatureIsLocal = false; } else { signatureName = "<< ERROR >>"; signatureEmail = "<< ERROR >>"; signatureIsLocal = false; } } } else { signatureName = "<< ERROR >>"; signatureEmail = "<< ERROR >>"; signatureIsLocal = false; } if (checkForSettingErros) { if (string.IsNullOrEmpty(sigName) || string.IsNullOrEmpty(sigEmail)) { DebugLog.LogWarning("Credentials not set, please go to the settings tab!"); } } // submodules if (repository.hasSubmodules) { if (repository.areSubmodulesInit) { if (!repository.PullSubmodules()) { DebugLog.LogError("Failed to pull submodules: " + repository.lastError); return(false); } } else { if (!repository.InitPullSubmodules()) { DebugLog.LogError("Failed to init and pull submodules: " + repository.lastError); return(false); } } } } } catch (Exception e) { DebugLog.LogError("RepoManager.OpenRepo Failed: " + e.Message); repository.Close(); return(false); } // refesh partials if (!RefreshBranches(isRefreshMode)) { return(false); } if (!RefreshChanges()) { return(false); } // check sync if (IsUpToDateWithRemote(out bool yes)) { isInSync = yes; } else { isInSync = null; } isOpen = true; } // finish if (!disableRepoRefreshedCallback && RepoRefreshedCallback != null) { RepoRefreshedCallback(false); } return(true); }
public bool AddGitLFSSupport(bool addLFSDefaultExts) { lock (this) { // check if already init if (lfsEnabled) { DebugLog.LogWarning("Git LFS already enabled on repo"); return(false); } try { // init git lfs string lfsFolder = Path.Combine(repository.repoPath, ".git", "lfs"); if (!Directory.Exists(lfsFolder)) { if (!repository.lfs.Install()) { throw new Exception(repository.lastError); } if (!Directory.Exists(lfsFolder)) { DebugLog.LogError("Git-LFS install failed! (Try manually)"); lfsEnabled = false; return(false); } } // add attr file if it doesn't exist string gitattributesPath = Path.Combine(repository.repoPath, ".gitattributes"); if (!File.Exists(gitattributesPath)) { using (var writer = File.CreateText(gitattributesPath)) { // this will be an empty file... } } // add default ext to git lfs if (addLFSDefaultExts && AppManager.defaultGitLFS_Exts != null && AppManager.defaultGitLFS_Exts.Count != 0) { if (!repository.lfs.Track(AppManager.defaultGitLFS_Exts)) { throw new Exception(repository.lastError); } } // finish lfsEnabled = true; } catch (Exception e) { DebugLog.LogError("Add Git-LFS Error: " + e.Message); Environment.Exit(0); // quit for safety as application should restart return(false); } return(true); } }
public bool RemoveGitLFSSupport(bool rebase) { lock (this) { // check if not init if (!lfsEnabled) { DebugLog.LogWarning("Git LFS is not enabled on repo"); return(false); } try { // untrack lfs filters string gitattributesPath = Path.Combine(repository.repoPath, ".gitattributes"); if (File.Exists(gitattributesPath)) { string data = File.ReadAllText(gitattributesPath); var values = Regex.Matches(data, @"(\*\..*)? filter=lfs diff=lfs merge=lfs"); foreach (Match value in values) { if (value.Groups.Count != 2) { continue; } if (!repository.lfs.Untrack(value.Groups[1].Value)) { throw new Exception(repository.lastError); } } } // remove lfs repo files if (!repository.lfs.Uninstall()) { throw new Exception(repository.lastError); } string lfsHookFile = Path.Combine(repository.repoPath, ".git", "hooks", "pre-push"); if (File.Exists(lfsHookFile)) { File.Delete(lfsHookFile); } string lfsFolder = Path.Combine(repository.repoPath, ".git", "lfs"); if (Directory.Exists(lfsFolder)) { Directory.Delete(lfsFolder, true); } // rebase repo if (rebase) { // TODO } // finish lfsEnabled = false; } catch (Exception e) { DebugLog.LogError("Remove Git-LFS Error: " + e.Message); Environment.Exit(0); // quit for safety as application should restart return(false); } return(true); } }
private static void Client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { var checkForUpdatesCallback = (CheckForUpdatesCallbackMethod)e.UserState; if (e.Error != null) { DebugLog.LogError("Failed to check for updates: " + e.Error.Message); client.Dispose(); if (checkForUpdatesCallback != null) { checkForUpdatesCallback(UpdateCheckResult.CommonError); } return; } if (e.Cancelled) { DebugLog.LogError("Update check canceled!"); client.Dispose(); if (checkForUpdatesCallback != null) { checkForUpdatesCallback(UpdateCheckResult.CommonError); } return; } try { // check app version bool canCheckAppVersion = true; using (var reader = new StringReader(e.Result)) using (var xmlReader = new XmlTextReader(reader)) { while (xmlReader.Read()) { if (canCheckAppVersion && xmlReader.Name == "AppVersion") { canCheckAppVersion = false; if (!IsValidVersion(VersionInfo.version, xmlReader.ReadInnerXml())) { DebugLog.LogWarning("Your 'Git-It-GUI' version is out of date."); if (checkForUpdatesCallback != null) { checkForUpdatesCallback(UpdateCheckResult.AppVersionOutOfDate); } } } } } if (checkForUpdatesCallback != null) { checkForUpdatesCallback(UpdateCheckResult.Success); } } catch (Exception ex) { DebugLog.LogError("Failed to get version info!\nMake sure git and git-lfs are installed\nAlso make sure you're connected to the internet: \n\n" + ex.Message); if (checkForUpdatesCallback != null) { checkForUpdatesCallback(UpdateCheckResult.AppVersionParseError); } } client.Dispose(); }