public void CreateDirectory_CreateTrunk() { SvnSandBox sbox = new SvnSandBox(this); Uri ReposUrl = sbox.CreateRepository(SandBoxRepository.Empty); using (SvnClient client = NewSvnClient(true, false)) { Uri trunkUri = new Uri(ReposUrl, "trunk/"); client.RemoteCreateDirectory(trunkUri); string trunkPath = sbox.Wc; client.CheckOut(trunkUri, trunkPath); TouchFile(Path.Combine(trunkPath, "test.txt")); Assert.That(SvnTools.IsManagedPath(trunkPath)); Assert.That(SvnTools.IsBelowManagedPath(trunkPath)); Assert.That(SvnTools.IsBelowManagedPath(Path.Combine(trunkPath, "q/r/s/t/u/v/test.txt"))); client.Add(Path.Combine(trunkPath, "test.txt")); Directory.CreateDirectory(Path.Combine(trunkPath, "dir")); TouchFile(Path.Combine(trunkPath, "dir/test.txt")); SvnAddArgs aa = new SvnAddArgs(); aa.AddParents = true; client.Add(Path.Combine(trunkPath, "dir/test.txt"), aa); client.Commit(trunkPath); client.RemoteDelete(trunkUri, new SvnDeleteArgs()); } }
/// <summary> /// 初始化 如果没有被管理,则签出 /// </summary> /// <returns></returns> public bool Init() { ShowInfo("正在初始化...."); bool result = true; HandEvents(); if(!SvnTools.IsManagedPath(GetAppLoc()))//查看某目录是否是受svn管理的状态, 即是否为工作副本 { notiny = "正在检出文件."; ShowInfo(); result = SC.CheckOut(new SvnUriTarget(svnurl), GetAppLoc()); ShowInfo("文件检出完成."); } ShowInfo("初始化完毕."); return result; }
void RefreshTo(SvnStatusData status) { if (status == null) { throw new ArgumentNullException("status"); } if (status.LocalNodeStatus == SvnStatus.External) { // When iterating the status of an external in it's parent directory // We get an external status and no really usefull information SetState(SvnItemState.Exists | SvnItemState.Versionable | SvnItemState.IsDiskFolder, SvnItemState.IsDiskFile | SvnItemState.ReadOnly | SvnItemState.MustLock | SvnItemState.IsTextFile); if (_statusDirty != XBool.False) { _statusDirty = XBool.True; // Walk the path itself to get the data you want } return; } else if (MightBeNestedWorkingCopy(status) && IsDirectory) { // A not versioned directory might be a working copy by itself! if (_statusDirty == XBool.False) { return; // No need to remove valid cache entries } if (SvnTools.IsManagedPath(FullPath)) { _statusDirty = XBool.True; // Walk the path itself to get the data // Extract useful information we got anyway SetState(SvnItemState.Exists | SvnItemState.Versionable | SvnItemState.Versioned | SvnItemState.IsWCRoot | SvnItemState.IsDiskFolder, SvnItemState.IsDiskFile | SvnItemState.ReadOnly | SvnItemState.MustLock | SvnItemState.IsTextFile); return; } else { SetState(SvnItemState.None, SvnItemState.IsWCRoot); } // Fall through } _cookie = NextCookie(); _statusDirty = XBool.False; _status = status; const SvnItemState unset = SvnItemState.Modified | SvnItemState.Added | SvnItemState.HasCopyOrigin | SvnItemState.Deleted | SvnItemState.ContentConflicted | SvnItemState.Ignored | SvnItemState.Obstructed | SvnItemState.Replaced | SvnItemState.MovedHere; const SvnItemState managed = SvnItemState.Versioned; // Let's assume status is more recent than our internal property cache // Set all caching properties we can bool svnDirty = true; bool exists = true; bool provideDiskInfo = true; switch (status.LocalNodeStatus) { case SvnStatus.None: SetState(SvnItemState.None, managed | unset); svnDirty = false; exists = false; provideDiskInfo = false; break; case SvnStatus.NotVersioned: // Node exists but is not managed by us in this directory // (Might be from an other location as in the nested case) SetState(SvnItemState.None, unset | managed); svnDirty = false; break; case SvnStatus.Ignored: // Node exists but is not managed by us in this directory // (Might be from an other location as in the nested case) SetState(SvnItemState.Ignored, unset | managed); svnDirty = false; break; case SvnStatus.Added: if (status.IsMoved && status.IsCopied) { SetState(managed | SvnItemState.Added | SvnItemState.HasCopyOrigin | SvnItemState.MovedHere, unset); } else if (status.IsCopied) { SetState(managed | SvnItemState.Added | SvnItemState.HasCopyOrigin, unset); } else { SetState(managed | SvnItemState.Added, unset); } if (status.LocalTextStatus == SvnStatus.Modified) { SetState(SvnItemState.Modified, SvnItemState.None); } else if (status.LocalTextStatus == SvnStatus.Normal) { SetState(SvnItemState.None, SvnItemState.Modified); } break; case SvnStatus.Replaced: if (status.IsMoved && status.IsCopied) { SetState(managed | SvnItemState.Replaced | SvnItemState.HasCopyOrigin | SvnItemState.MovedHere, unset); } else if (status.IsCopied) { SetState(managed | SvnItemState.Replaced | SvnItemState.HasCopyOrigin, unset); } else { SetState(managed | SvnItemState.Replaced, unset); } if (status.LocalTextStatus == SvnStatus.Modified) { SetState(SvnItemState.Modified, SvnItemState.None); } else if (status.LocalTextStatus == SvnStatus.Normal) { SetState(SvnItemState.None, SvnItemState.Modified); } break; case SvnStatus.Modified: case SvnStatus.Conflicted: { bool done = false; switch (status.LocalTextStatus) { case SvnStatus.Modified: SetState(managed | SvnItemState.Modified, unset); done = true; break; case SvnStatus.Conflicted: SetState(managed | SvnItemState.ContentConflicted | SvnItemState.Conflicted, unset); done = true; break; } if (!done) { goto case SvnStatus.Normal; } break; } case SvnStatus.Obstructed: // node exists but is of the wrong type SetState(SvnItemState.None, managed | unset); provideDiskInfo = false; // Info is wrong break; case SvnStatus.Missing: exists = false; provideDiskInfo = false; // Info is wrong SetState(managed, unset); break; case SvnStatus.Deleted: SetState(managed | SvnItemState.Deleted, unset); if (status.LocalFileExists) { exists = provideDiskInfo = true; } else { exists = false; provideDiskInfo = false; // Folder might still exist } break; case SvnStatus.External: // Should be handled above throw new InvalidOperationException(); case SvnStatus.Incomplete: SetState(managed, unset); break; default: Trace.WriteLine(string.Format("Ignoring undefined status {0} in SvnItem.Refresh()", status.LocalNodeStatus)); provideDiskInfo = false; // Can't trust an unknown status goto case SvnStatus.Normal; case SvnStatus.Normal: SetState(managed | SvnItemState.Exists, unset); svnDirty = false; break; } if (exists) { SetState(SvnItemState.Versionable, SvnItemState.None); } else { SetState(SvnItemState.None, SvnItemState.Versionable); } if (status.Conflicted) { SetState(SvnItemState.Conflicted, SvnItemState.None); } else { SetState(SvnItemState.None, SvnItemState.Conflicted); } bool hasProperties = true; switch (status.LocalPropertyStatus) { case SvnStatus.None: hasProperties = false; SetState(SvnItemState.None, SvnItemState.PropertiesConflicted | SvnItemState.PropertyModified | SvnItemState.HasProperties); break; case SvnStatus.Modified: SetState(SvnItemState.PropertyModified | SvnItemState.HasProperties, SvnItemState.PropertiesConflicted); svnDirty = true; break; case SvnStatus.Conflicted: SetState(SvnItemState.PropertyModified | SvnItemState.PropertiesConflicted | SvnItemState.HasProperties, SvnItemState.None); svnDirty = true; break; case SvnStatus.Normal: default: SetState(SvnItemState.HasProperties, SvnItemState.PropertiesConflicted | SvnItemState.PropertyModified); break; } if (svnDirty) { SetState(SvnItemState.SvnDirty, SvnItemState.None); } else { SetState(SvnItemState.None, SvnItemState.SvnDirty); } if (!hasProperties) { SetState(SvnItemState.None, SvnItemState.MustLock); } if (provideDiskInfo) { if (exists) // Behaviour must match updating from UpdateAttributeInfo() { switch (status.NodeKind) { case SvnNodeKind.Directory: SetState(SvnItemState.IsDiskFolder | SvnItemState.Exists, SvnItemState.ReadOnly | SvnItemState.MustLock | SvnItemState.IsTextFile | SvnItemState.IsDiskFile); break; case SvnNodeKind.File: SetState(SvnItemState.IsDiskFile | SvnItemState.Exists, SvnItemState.IsDiskFolder); break; default: // Handle direct replacement without an additional stat if (status.LocalFileExists) { goto case SvnNodeKind.File; } break; } } else { SetState(SvnItemState.None, SvnItemState.Exists); } } if (status.IsLockedLocal) { SetState(SvnItemState.HasLockToken, SvnItemState.None); } else { SetState(SvnItemState.None, SvnItemState.HasLockToken); } }
static void Main(string[] args) { client.Authentication.Clear(); client.Authentication.UserNamePasswordHandlers += (sender, e) => { e.UserName = "******"; e.Password = "******"; }; client.Conflict += (sender, e) => { Console.WriteLine(" {0}", "there is conflict in your local, please dealing with it first!"); }; while (true) { Console.Write(">"); var cmd_str = Console.ReadLine(); if (String.IsNullOrEmpty(cmd_str)) { continue; } var arr_cmd = cmd_str.Split(new String[] { " " }, StringSplitOptions.RemoveEmptyEntries); var cmd = arr_cmd[0]; var cmd_param = String.Empty; if (arr_cmd.Length >= 2) { cmd_param = arr_cmd[1]; } if (cmd == "getinfo") { if (cmd_param == String.Empty || cmd_param == "remote") { string repoUrl = "svn://118.25.197.165/test_szp"; Console.WriteLine(" to getinfo {0} ...", repoUrl); SvnInfoEventArgs info; client.GetInfo(new Uri(repoUrl), out info); Console.WriteLine(" Reversion:{0} lastChangedReversion:{1} lastChangedTime:{2} islocked:{3}" , info.Revision , info.LastChangeRevision , info.LastChangeTime , info.Lock != null ); } else { var local_path = String.Format(@"E:\SVN_BASE\{0}", cmd_param); Console.WriteLine(" to getinfo {0} ...", local_path); try { SvnInfoEventArgs info; client.GetInfo(new SvnPathTarget(local_path), out info); Console.WriteLine(" Reversion:{0} lastChangedReversion:{1} lastChangedTime:{2} islocked:{3}" , info.Revision , info.LastChangeRevision , info.LastChangeTime , info.Lock != null ); } catch (SvnException svnEx) { if (svnEx.SvnErrorCode == SvnErrorCode.SVN_ERR_WC_PATH_NOT_FOUND) { Console.WriteLine(" {0}", "it is not a svn path! your should add it into svn."); } else if (svnEx.SvnErrorCode == SvnErrorCode.SVN_ERR_WC_NOT_WORKING_COPY) { Console.WriteLine(" {0}", "it is not a svn dir! your should checkout this dir."); } else { Console.WriteLine(" {0}({1})", svnEx.Message, svnEx.SvnErrorCode); } } } } else if (cmd == "checkout") { var local_path = String.Format(@"E:\SVN_BASE\{0}\", cmd_param); Console.WriteLine(" to checkout {0} ...", local_path); var repo_url = "svn://118.25.197.165/test_szp"; var r = client.CheckOut(new SvnUriTarget(repo_url), local_path); if (r) { Console.WriteLine(" {0}", "check out successfully!"); } else { Console.WriteLine(" {0}", "check out faild!"); } } else if (cmd == "getstate") { var local_path = String.Format(@"E:\SVN_BASE\{0}", cmd_param); Console.WriteLine(" to getstate {0} ...", local_path); var r = SvnTools.IsManagedPath(local_path); if (r) { Console.WriteLine(" {0} is svn local path", local_path); } else { Console.WriteLine(" {0} is not svn local path", local_path); } } else if (cmd == "geturi") { var local_path = String.Format(@"E:\SVN_BASE\{0}", cmd_param); Console.WriteLine(" to geturi {0} ...", local_path); var uri = client.GetUriFromWorkingCopy(local_path); Console.WriteLine(" {0} ====svn==== {1}", local_path, uri); } else if (cmd == "add") { var local_path = String.Format(@"E:\SVN_BASE\{0}", cmd_param); Console.WriteLine(" to add {0} ...", local_path); var r = client.Add(local_path); if (r) { Console.WriteLine(" add {0} successfully!", local_path); } else { Console.WriteLine(" faild to add {0} !", local_path); } } else if (cmd == "commit") { var local_path = String.Format(@"E:\SVN_BASE\{0}", cmd_param); Console.WriteLine(" to commit {0} ...", local_path); var arg = new SvnCommitArgs(); arg.LogMessage = String.Format("test {0:MM:dd HH:mm}", DateTime.Now); SvnCommitResult result; try { var r = client.Commit(local_path, arg, out result); if (r) { Console.WriteLine(" {0}", "commit successfully!"); } else { Console.WriteLine(" {0}", "faild to commit!"); } } catch (SvnException svnEx) { if (svnEx.SvnErrorCode == SvnErrorCode.SVN_ERR_FS_BAD_LOCK_TOKEN) { Console.WriteLine(" {0} ({1})", "faild to commit!", "another locked this node!"); } else if (svnEx.SvnErrorCode == SvnErrorCode.SVN_ERR_FS_TXN_OUT_OF_DATE) { Console.WriteLine(" {0} ({1})", "faild to commit!", "please first update!"); } else if (svnEx.SvnErrorCode == SvnErrorCode.SVN_ERR_WC_FOUND_CONFLICT) { Console.WriteLine(" {0} ({1})", "faild to commit!", "please dealing with conflict!"); } else { Console.WriteLine(" {0} ({1})", svnEx.Message, svnEx.SvnErrorCode); } } } else if (cmd == "update") { var local_path = String.Format(@"E:\SVN_BASE\{0}", cmd_param); Console.WriteLine(" to update {0} ...", local_path); var r = client.Update(local_path); if (r) { Console.WriteLine(" {0}", "Update successfully!"); } else { Console.WriteLine(" {0}", "Update faild!"); } } else if (cmd == "remotelock") { var local_path = String.Format(@"E:\SVN_BASE\{0}", cmd_param); Console.WriteLine(" to lock {0} ...", local_path); var uri = client.GetUriFromWorkingCopy(local_path); var r = client.RemoteLock(uri, String.Format("test lock {0:MM-dd HH:mm}", DateTime.Now)); if (r) { Console.WriteLine(" remote lock {0} successfully!", local_path); } else { Console.WriteLine(" faild to remote lock {0}!", local_path); } } else if (cmd == "remoteunlock") { var local_path = String.Format(@"E:\SVN_BASE\{0}", cmd_param); Console.WriteLine(" to unlock {0} ...", local_path); var uri = client.GetUriFromWorkingCopy(local_path); var r = client.RemoteUnlock(uri); if (r) { Console.WriteLine(" remote unlock {0} successfully!", local_path); } else { Console.WriteLine(" faild to remote lock {0}!", local_path); } } else if (cmd == "lock") { var local_path = String.Format(@"E:\SVN_BASE\{0}", cmd_param); Console.WriteLine(" to lock {0} ...", local_path); //var uri = client.GetUriFromWorkingCopy(local_path); var r = client.Lock(local_path, String.Format("test lock {0:MM-dd HH:mm}", DateTime.Now)); if (r) { Console.WriteLine(" lock {0} successfully!", local_path); } else { Console.WriteLine(" faild to lock {0}!", local_path); } } else if (cmd == "unlock") { var local_path = String.Format(@"E:\SVN_BASE\{0}", cmd_param); Console.WriteLine(" to unlock {0} ...", local_path); //var uri = client.GetUriFromWorkingCopy(local_path); var r = client.Unlock(local_path); if (r) { Console.WriteLine(" unlock {0} successfully!", local_path); } else { Console.WriteLine(" faild to lock {0}!", local_path); } } else if (cmd == "export") { var local_path = String.Format(@"E:\SVN_BASE\{0}", cmd_param); var dest_path = arr_cmd[2]; Console.WriteLine(" to unlock {0} ...", local_path); var uri = client.GetUriFromWorkingCopy(local_path); try { var r = client.Export(new SvnUriTarget(uri), dest_path); if (r) { Console.WriteLine(" export {0} to {1} successfully!", uri, dest_path); } else { Console.WriteLine(" faild to export {0} to {1}!", uri, dest_path); } } catch (SvnException svnEx) { Console.WriteLine(" {0}(1)", svnEx.Message, svnEx.SvnErrorCode); } } else if (cmd == "help" || cmd == "?") { Console.WriteLine(" 1.getinfo [localpath] or [null] or [remotepath]"); Console.WriteLine(" 2.getstate [localpath]"); Console.WriteLine(" 2.geturi [localpath]"); Console.WriteLine(" 3.checkout [localpath]"); Console.WriteLine(" 4.add [localpath]"); Console.WriteLine(" 5.commit [localpath]"); Console.WriteLine(" 6.update [localpath]"); Console.WriteLine(" 7.lock [localpath]"); Console.WriteLine(" 8.unlock [localpath]"); Console.WriteLine(" 9.remotelock [localpath]"); Console.WriteLine(" 10.remoteunlock [localpath]"); Console.WriteLine(" 10.export [localpath] [localpath]"); } } }
private void Window_Loaded(object sender, RoutedEventArgs e) { lbInfo.Content = "一切正常"; String curDir = Directory.GetCurrentDirectory(); String curExe = System.Reflection.Assembly.GetExecutingAssembly().Location; SvnClient = new SvnClient(); SvnTarget svnTargetCurDir = SvnTarget.FromString(curDir); SvnTarget snvTargetCurExe = SvnTarget.FromString(curExe); // 未使用 - 获取目录下的文件状态 Collection <SvnStatusEventArgs> svnStatusEventArgsCollection; SvnClient.GetStatus(Directory.GetCurrentDirectory(), out svnStatusEventArgsCollection); // 未使用 - 空的 Collection <SvnPropertyListEventArgs> svnPropertyListEventArgsCollection; SvnClient.GetPropertyList(Directory.GetCurrentDirectory(), out svnPropertyListEventArgsCollection); // 未使用 - 获取一个文件的状态 Collection <SvnFileVersionEventArgs> svnFileVersionEventArgsCollection; try { SvnClient.GetFileVersions(snvTargetCurExe, out svnFileVersionEventArgsCollection); } catch (SvnWorkingCopyPathNotFoundException ex) { // 如果查询的文件未加入Repo,会抛此异常 } // 未使用 - 一个好像没什么用的信息列表,目录是第一个项,没有版本号 Collection <SvnListEventArgs> svnListEventArgsCollection; SvnClient.GetList(svnTargetCurDir, out svnListEventArgsCollection); // 未使用 - 工作目录全路径 String workingCopyRoot = SvnClient.GetWorkingCopyRoot(Directory.GetCurrentDirectory()); // 未使用 - 整个仓库的最新版本 long revision = 0; SvnClient.Youngest(Directory.GetCurrentDirectory(), out revision); // 此目录的相关变更 和 GUI中的ShowLog一样 是此目录的相关变更 Collection <SvnLogEventArgs> logList; try { SvnClient.GetLog(Directory.GetCurrentDirectory(), out logList); } catch (SvnInvalidNodeKindException ex) { lbInfo.Content = "当前目录不是SVN目录,停用更新检查功能"; btnUpdateAndLaunch.Visibility = Visibility.Hidden; return; } // 获取本地目录信息,当前版本和修改版本都是本地的 SvnInfoEventArgs svnInfoEventArgs; SvnClient.GetInfo(svnTargetCurDir, out svnInfoEventArgs); long curRevision = svnInfoEventArgs.Revision; // 当前的SVN版本 long changeRevision = logList[0].Revision; // 当前目录的最新远程变更版本 lbVersionChange.Content = changeRevision; lbVersionCur.Content = svnInfoEventArgs.Revision; if (curRevision < changeRevision) { btnUpdateAndLaunch.Visibility = Visibility.Visible; lbInfo.Content = "发现新版本"; } else { btnUpdateAndLaunch.Visibility = Visibility.Hidden; lbInfo.Content = "已经是最新版"; } // -------------------------------------------------------- // SvnWorkingCopyClient // -------------------------------------------------------- SvnWorkingCopyClient svnWorkingCopyClient = new SvnWorkingCopyClient(); // 未使用 只有一个值IsText 没看出有什么用处 SvnWorkingCopyState svnWorkingCopyState; svnWorkingCopyClient.GetState(Directory.GetCurrentDirectory(), out svnWorkingCopyState); // 未使用 返回仅本地存在的所有修改版本 SvnWorkingCopyVersion svnWorkingCopyVersion; svnWorkingCopyClient.GetVersion(curDir, out svnWorkingCopyVersion); // -------------------------------------------------------- // SvnTools // -------------------------------------------------------- // 未使用 传入正确的目录却返回false???? bool isCurDirInWorkingCopy = SvnTools.IsManagedPath(curDir); }