예제 #1
0
        public Changeset Commit(ICommitScmFileArgs args)
        {
            Reset(args.RepositoryPath);

            if (!args.All)
            {
                foreach (var file in args.Files)
                {
                    AddFileToIndex(args.RepositoryPath, file);
                }
            }
            var ab = new ArgBuilder();

            ab.Add("commit");
            ab.AddIf(args.All, "--all");

            using (var msgFile = new TempFile("msg"))
            {
                File.WriteAllText(msgFile.FilePath, args.Message);
                ab.Add($"--file=\"{msgFile}\"");
                RunCommand(ab, args);
            }

            return(null); // todo
        }
예제 #2
0
        public Changeset Commit(ICommitScmFileArgs args)
        {
            var      ab       = new ArgBuilder();
            TempFile fileList = null;

            ab.Add("commit");
            if (args.All)
            {
                ab.Add("--addremove");
            }
            else
            if (args.Files?.Any() == true)
            {
                fileList = CreateFileList(args.Files);
                ab.Add($"--include listfile:\"{fileList}\"");
            }

            try
            {
                using (var msgFile = new TempFile("msg"))
                {
                    File.WriteAllText(msgFile.FilePath, args.Message);
                    ab.Add($"--logfile \"{msgFile}\"");
                    RunCommand(ab, args);
                }
            }
            finally
            {
                fileList?.Dispose();
            }

            return(null); // todo
        }
예제 #3
0
        public Changeset[] UploadChangesets(IUploadChangesetArgs args)
        {
            var currentBranch = GetBranches(new GetBranchesArgs
            {
                Current        = true,
                RepositoryPath = args.RepositoryPath
            }).FirstOrDefault();

            var targetRev   = args.TargetRev.WhenNullOrEmpty(currentBranch);
            var destination = args.Destination.WhenNullOrEmpty("origin");
            var query       = new RevisionQuery
            {
                FromRevisionId = targetRev,
                ToRevisionId   = $"{destination}/{targetRev}"
            };

            var changeSets =
                GetChangesets(new GetChangesetsArgs
            {
                RepositoryPath = args.RepositoryPath,
                Query          = query
            });

            if (!args.CheckOnly)
            {
                var ab = new ArgBuilder();
                ab.Add("push");
                ab.Add(args.Destination);
                ab.Add(args.TargetRev);
            }

            return(changeSets);
        }
예제 #4
0
        public FileDiff[] GetDiff(IGetDiffArgs args)
        {
            var ab = new ArgBuilder();

            ab.Add("diff");
            TempFile fileList = null;

            if (args.Files != null)
            {
                if (args.Files.Any())
                {
                    fileList = CreateFileList(args.Files);
                    ab.Add($"--include listfile:\"{fileList}\"");
                }
            }

            try
            {
                if (args.Query != null)
                {
                    ab.Add($"--rev \"{FormatQuery(args.Query)}\"");
                }
                var lines = RunCommand(ab, args);
                return(FileDiff.FromLines(lines));
            }
            finally
            {
                fileList?.Dispose();
            }
        }
예제 #5
0
        public string GetRepositoryRoot(string folder)
        {
            var ab = new ArgBuilder();

            ab.Add("rev-parse");
            ab.Add("--show-toplevel");
            return(RunCommand(ab, null, folder).LastOrDefault());
        }
예제 #6
0
        private void AddFileToIndex(string repositoryPath, string filename)
        {
            var ab = new ArgBuilder();

            ab.Add("add");
            ab.Add(filename);
            RunCommand(ab, repositoryPath);
        }
예제 #7
0
        public void CreateRepository(INewRepositoryArgs args)
        {
            Directory.CreateDirectory(args.RepositoryPath);
            var ab = new ArgBuilder();

            ab.Add("init");
            ab.Add(args.RepositoryPath.Enclose('"'));
            RunCommand(ab, args);
        }
예제 #8
0
        public void CloneRepository(ICloneRepositoryArgs args)
        {
            var ab = new ArgBuilder();

            ab.Add("clone");
            ab.Add(args.Source.Enclose('"'));
            ab.Add(args.RepositoryPath.Enclose('"'));
            RunCommand(ab, args);
        }
예제 #9
0
        public Changeset[] GetChangesets(IGetChangesetsArgs args)
        {
            var ab = new ArgBuilder();

            ab.Add("log");
            ab.Add(args.Current ? "-n 1" : FormatQuery(args.Query));
            ab.Add($"--pretty=\"{XmlLogTemplate}\"");
            return(Changeset.FromXml(RunCommand(ab, args)));
        }
예제 #10
0
        public void CheckoutChangeset(ICheckoutChangesetArgs args)
        {
            var ab = new ArgBuilder();

            ab.Add("checkout");
            ab.Add(args.TargetRev);
            ab.AddIf(args.Force, "--force");
            ab.AddIf(!String.IsNullOrEmpty(args.NewBranchName), $"-b \"{args.NewBranchName}\"");
            RunCommand(ab, args);
        }
예제 #11
0
        public void Revert(IResetScmFilesArgs args)
        {
            var ab = new ArgBuilder();

            ab.Add("revert");
            var fileList = CreateFileList(args.Files);

            ab.Add($"--include listfile:\"{fileList}\"");
            RunCommand(ab, args);
        }
예제 #12
0
 public byte[] GetFileContents(IGetScmFileContentArgs args)
 {
     using (var tempFile = new TempFile())
     {
         var ab = new ArgBuilder();
         ab.Add("cat");
         ab.Add(args.File);
         ab.Add($"--output \"{tempFile}\"");
         RunCommand(ab, args);
         return(File.ReadAllBytes(tempFile.FilePath));
     }
 }
예제 #13
0
        public void RenameScmFile(IRenameScmFileArgs args)
        {
            var ab = new ArgBuilder();

            ab.Add("rename");
            if (args.MarkOnly)
            {
                ab.Add("--after");
            }
            ab.Add(args.Filename.Enclose('"'));
            ab.Add(args.NewFilename.Enclose('"'));
            RunCommand(ab, args);
        }
예제 #14
0
        private void CreateBookmark(INewBranchArgs args)
        {
            var ab = new ArgBuilder();

            ab.Add("bookmarks");
            if (!String.IsNullOrEmpty(args.TargetChangeset))
            {
                ab.Add($"--rev \"{args.TargetChangeset}\"");
            }
            ab.AddIf(args.Force, "--force");
            ab.Add(args.Name);
            RunCommand(ab, args);
        }
예제 #15
0
        public ScmFile[] GetFiles(IGetScmFileArgs args)
        {
            var command = args.All ? "files" : "status";

            var ab = new ArgBuilder();

            ab.Add(command);
            TempFile fileList = null;

            if (args.Files != null)
            {
                if (args.Files.Any())
                {
                    fileList = CreateFileList(args.Files);
                    ab.Add($"--include listfile:\"{fileList}\"");
                }
            }

            string[] files;
            try
            {
                if (args.Query != null)
                {
                    ab.Add($"--rev \"{FormatQuery(args.Query)}\"");
                }
                files = RunCommand(ab, args);
            }
            finally
            {
                fileList?.Dispose();
            }

            var dict = new Dictionary <char, ScmFileStatus>
            {
                { 'C', ScmFileStatus.Clean },
                { 'M', ScmFileStatus.Modified },
                { 'A', ScmFileStatus.Added },
                { 'R', ScmFileStatus.Removed },
                { '?', ScmFileStatus.Unknown },
                { '!', ScmFileStatus.Missing },
            };

            return(files.Select(file =>
            {
                var status = dict[file[0]];
                var localFilename = file.Substring(2);
                return new ScmFile(args.RepositoryPath, localFilename, status);
            }).ToArray());
        }
예제 #16
0
        public Changeset[] UploadChangesets(IUploadChangesetArgs args)
        {
            var ab = new ArgBuilder();

            ab.Add(args.CheckOnly ? "outgoing" : "push");
            ab.Add(args.Destination);
            if (args.TargetRev != null)
            {
                ab.Add($"--rev {FormatQuery(args.TargetRev)}");
            }

            var r = RunCommand(ab, args);

            return(args.CheckOnly ? ParseChangesets(r) : new Changeset[] { });
        }
예제 #17
0
        public Changeset[] DownloadChangesets(IDownloadChangesetArgs args)
        {
            var ab = new ArgBuilder();

            ab.Add("fetch");
            ab.Add(args.Source);
            ab.Add(args.TargetRev);
            RunCommand(ab, args);

            return(GetChangesets(new GetChangesetsArgs
            {
                RepositoryPath = args.RepositoryPath,
                Query = "..@{u}"
            }));
        }
예제 #18
0
        private void Reset(string repositoryPath)
        {
            var ab = new ArgBuilder();

            ab.Add("reset");
            RunCommand(ab, repositoryPath);
        }
예제 #19
0
        public string GetRepositoryRoot(string path)
        {
            var ab = new ArgBuilder();

            ab.Add("root");
            return(RunCommand(ab, null, path).LastOrDefault());
        }
예제 #20
0
        public Changeset[] DownloadChangesets(IDownloadChangesetArgs args)
        {
            if (String.IsNullOrEmpty(args.Source))
            {
                throw new ArgumentNullException(nameof(args.Source));
            }

            var ab = new ArgBuilder();

            ab.Add(args.CheckOnly ? "incoming" : "pull");
            ab.Add(args.Source);
            if (args.TargetRev != null)
            {
                ab.Add($"--rev {FormatQuery(args.TargetRev)}");
            }

            var r = RunCommand(ab, args);

            return(args.CheckOnly ? ParseChangesets(r) : new Changeset[] { });
        }
예제 #21
0
        public Changeset[] GetChangesets(IGetChangesetsArgs args)
        {
            var ab = new ArgBuilder();

            if (args.Current)
            {
                ab.Add("parent");
            }
            else
            {
                ab.Add("log");
                if (args.Query != null)
                {
                    ab.Add($"--rev \"{FormatQuery(args.Query)}\"");
                }
            }
            ab.Add($"--template \"{XmlLogTemplate}\"");
            var lines = RunCommand(ab, args);

            return(Changeset.FromXml(lines));
        }
예제 #22
0
        private void CreatePermanentBranch(INewBranchArgs args)
        {
            if (!String.IsNullOrEmpty(args.TargetChangeset))
            {
                throw new NotSupportedException($"'{nameof(args.TargetChangeset)}' is not supported for permanent branches.");
            }

            var ab = new ArgBuilder();

            ab.Add("branch");
            ab.Add($"\"{args.Name}\"");
            ab.AddIf(args.Force, "--force");
            RunCommand(ab, args);

            if (args.Commit)
            {
                Commit(new CommitScmFileArgs
                {
                    RepositoryPath = args.RepositoryPath,
                    Message        = $"Opened branch '{args.Name}'"
                });
            }
        }
예제 #23
0
        public void CheckoutChangeset(ICheckoutChangesetArgs args)
        {
            var ab = new ArgBuilder();

            ab.Add("update");
            if (args.Clean)
            {
                ab.Add("--clean");
            }
            if (args.TargetRev != null)
            {
                ab.Add(args.TargetRev);
            }
            RunCommand(ab, args);

            if (!String.IsNullOrEmpty(args.NewBranchName))
            {
                CreateBranch(new NewBranchArgs
                {
                    Name           = args.NewBranchName,
                    RepositoryPath = args.RepositoryPath
                });
            }
        }
예제 #24
0
        public string[] GetBranches(IGetBranchesArgs args)
        {
            var ab = new ArgBuilder();

            ab.Add(args.Current ? "branch" : "branches");

            IEnumerable <string> result = RunCommand(ab, args);

            if (!args.Current)
            {
                result = result.Select(r =>
                {
                    var m = Regex.Match(r, @"^(?<name>.*?)\s+[0-9]+:[a-f0-9]+(?<inactive> \(inactive\))?");
                    return(!m.Success ? null : m.Groups["name"].Value);
                });
            }
            return(result.Where(r => !String.IsNullOrEmpty(r)).ToArray());
        }
예제 #25
0
        public string[] GetBranches(IGetBranchesArgs args)
        {
            if (args.Permanent)
            {
                ThrowPermanentBranchException();
            }

            var ab = new ArgBuilder();

            ab.Add("branch");
            var branches = RunCommand(ab, args)
                           .Select(line => Regex.Match(line, @"^(?<active>\*)? *(?<name>.*)$"));

            if (args.Current)
            {
                branches = branches.Where(b => b.Groups["active"].Value == "*");
            }
            return(branches.Select(b => b.Groups["name"].Value).ToArray());
        }