Ejemplo n.º 1
0
        private async Task <R <IReadOnlyList <string> > > UndoAndCleanFolderAsync(
            string cleanArgs, CancellationToken ct)
        {
            R <CmdResult2> result = await gitCmdService.RunAsync("reset --hard", ct);

            if (result.IsFaulted)
            {
                return(R.Error("Reset failed.", result.Exception));
            }

            CmdResult2 cleanResult = await gitCmdService.RunCmdAsync($"clean {cleanArgs}", ct);

            if (cleanResult.IsFaulted)
            {
                if (IsFailedToRemoveSomeFiles(cleanResult, out IReadOnlyList <string> failedFiles))
                {
                    Log.Warn($"Failed to clean {failedFiles.Count} files");
                    return(R.From(failedFiles));
                }

                return(R.Error(cleanResult.AsException()));
            }

            return(R.From(EmptyFileList));
        }
Ejemplo n.º 2
0
        public async Task <R> FetchRefsAsync(IEnumerable <string> refspecs, CancellationToken ct)
        {
            string refsText = string.Join(" ", refspecs);
            string args     = $"{FetchRefsArgs} {refsText}";

            Log.Debug($"Fetching {refsText} ...");

            void Progress(string text)
            {
                Log.Debug($"Progress: {text}");
            }

            CmdResult2 result = await gitCmdService.RunCmdWitProgressAsync(args, Progress, ct);

            if (result.IsFaulted)
            {
                if (IsNoRemote(result))
                {
                    return(R.Ok);
                }
                return(R.Error("Failed to fetch", result.AsException()));
            }

            return(R.Ok);
        }
Ejemplo n.º 3
0
        public async Task <R> UndoUncommittedFileAsync(string path, CancellationToken ct)
        {
            CmdResult2 result = await gitCmdService.RunCmdAsync(
                $"checkout --force -- \"{path}\"", CancellationToken.None);

            if (result.IsFaulted)
            {
                if (IsFileUnkwon(result, path))
                {
                    R deleteResult = DeleteFile(path, result);
                    if (deleteResult.IsFaulted)
                    {
                        return(R.Error($"Failed to delete {path}", deleteResult.Exception));
                    }

                    Log.Info($"Undid file {path}");
                    return(R.Ok);
                }

                return(R.Error($"Failed to undo file {path}", result.AsException()));
            }

            Log.Info($"Undid file {path}");
            return(R.Ok);
        }
Ejemplo n.º 4
0
        public async Task <R <string> > GetFileDiffAsync(string sha, string path, CancellationToken ct)
        {
            CmdResult2 result = await gitCmdService.RunCmdAsync(
                $"diff --patch --root --find-renames --unified=100000  {sha}^..{sha} -- \"{path}\" ", ct);

            if (result.IsFaulted)
            {
                if (result.Error.StartsWith("fatal: ambiguous argument"))
                {
                    // Failed to get diff for sha, might be root commit, so try again
                    CmdResult2 showRootResult = await gitCmdService.RunCmdAsync(
                        $"show --patch --root {sha} -- \"{path}\"", ct);

                    if (showRootResult.IsOk)
                    {
                        Log.Info($"Got file diff patch for root {sha}");
                        return(R.From(showRootResult.Output));
                    }
                }

                return(R.Error($"Failed to get file diff for {sha}", result.AsException()));
            }

            Log.Info($"Got path for {sha}");
            return(R.From(result.Output));
        }
Ejemplo n.º 5
0
        private R <CmdResult2> AsR(CmdResult2 result)
        {
            if (result.IsFaulted)
            {
                Error error = R.Error(result.AsException());
                return(error);
            }

            return(result);
        }
Ejemplo n.º 6
0
        public async Task <R> RemoveAsync(string path, CancellationToken ct)
        {
            CmdResult2 result = await gitCmdService.RunCmdAsync($"rm \"{path}\"", ct);

            if (result.IsFaulted)
            {
                return(R.Error($"Failed remove {path}", result.AsException()));
            }

            return(R.Ok);
        }
Ejemplo n.º 7
0
        public async Task <R <GitTag> > AddTagAsync(string sha, string tagName, CancellationToken ct)
        {
            CmdResult2 result = await gitCmdService.RunCmdAsync($"tag {tagName} {sha}", ct);

            if (result.IsFaulted)
            {
                return(R.Error($"Failed to add tag {tagName} at {sha}", result.AsException()));
            }

            Log.Info($"Added {tagName} at {sha}");
            return(new GitTag(sha, tagName));
        }
Ejemplo n.º 8
0
        public async Task <R> RemoveNoteAsync(string sha, string notesRef, CancellationToken ct)
        {
            CmdResult2 result = await gitCmdService.RunCmdAsync(
                $"-c core.notesRef={notesRef} notes remove --ignore-missing {sha}", ct);

            if (result.IsFaulted)
            {
                return(R.Error("Failed to remove note", result.AsException()));
            }

            Log.Info($"Removed note");
            return(R.Ok);
        }
Ejemplo n.º 9
0
        public async Task <R> AppendNoteAsync(
            string sha, string notesRef, string note, CancellationToken ct)
        {
            CmdResult2 result = await gitCmdService.RunCmdAsync(
                $"-c core.notesRef={notesRef} notes append --allow-empty -m\"{note}\" {sha}", ct);

            if (result.IsFaulted)
            {
                return(R.Error("Failed to add note", result.AsException()));
            }


            Log.Info($"Added note {note.Length} length");
            return(R.Ok);
        }
Ejemplo n.º 10
0
        public async Task <R> PushAsync(CancellationToken ct)
        {
            CmdResult2 result = await gitCmdService.RunCmdAsync(PushArgs, ct);

            if (result.IsFaulted)
            {
                if (IsNoRemoteBranch(result))
                {
                    return(R.Ok);
                }
                return(R.Error("Failed to push", result.AsException()));
            }

            return(R.Ok);
        }
Ejemplo n.º 11
0
        public async Task <R> PushDeleteRemoteBranchAsync(string branchName, CancellationToken ct)
        {
            Log.Debug($"Pushing delete branch {branchName} ...");
            CmdResult2 result = await gitCmdService.RunCmdAsync($"{PushArgs} --delete {branchName}", ct);

            if (result.IsFaulted)
            {
                if (IsNoRemote(result))
                {
                    return(R.Ok);
                }
                return(R.Error("Failed to push", result.AsException()));
            }

            return(R.Ok);
        }
Ejemplo n.º 12
0
        public async Task <R <IReadOnlyList <GitTag> > > GetAllTagsAsync(CancellationToken ct)
        {
            CmdResult2 result = await gitCmdService.RunCmdAsync("show-ref -d --tags", ct);

            if (result.IsFaulted)
            {
                if (!(result.ExitCode == 1 && string.IsNullOrEmpty(result.Output)))
                {
                    return(R.Error("Failed to list tags", result.AsException()));
                }
            }

            IReadOnlyList <GitTag> tags = ParseTags(result);

            Log.Info($"Got {tags.Count} tags");
            return(R.From(tags));
        }
Ejemplo n.º 13
0
        public async Task <R> PushTagAsync(string tagName, CancellationToken ct)
        {
            Log.Debug($"Pushing tag {tagName} ...");

            CmdResult2 result = await gitCmdService.RunCmdAsync($"{PushArgs} {tagName}", ct);

            if (result.IsFaulted)
            {
                if (IsNoRemote(result))
                {
                    return(R.Ok);
                }
                return(R.Error("Failed to push tag", result.AsException()));
            }

            return(R.Ok);
        }
Ejemplo n.º 14
0
        public async Task <R <IReadOnlyList <string> > > GetRefsIdsAsync(CancellationToken ct)
        {
            CmdResult2 result = await gitCmdService.RunCmdAsync("show-ref", ct);

            if (result.IsFaulted)
            {
                if (result.ExitCode != 1)
                {
                    return(R.Error("Failed to get refs", result.AsException()));
                }
            }

            IReadOnlyList <string> refs = result.OutputLines.ToList();

            Log.Info($"Got {refs.Count} refs");
            return(R.From(refs));
        }
Ejemplo n.º 15
0
        public async Task <R> PushBranchAsync(string branchName, CancellationToken ct)
        {
            Log.Debug($"Pushing branch {branchName} ...");
            string args = $"{PushArgs} -u refs/heads/{branchName}:refs/heads/{branchName}";

            CmdResult2 result = await gitCmdService.RunCmdAsync(args, ct);

            if (result.IsFaulted)
            {
                if (IsNoRemote(result))
                {
                    return(R.Ok);
                }
                return(R.Error("Failed to push", result.AsException()));
            }

            return(R.Ok);
        }
Ejemplo n.º 16
0
        public async Task <R <bool> > TryCheckoutAsync(string name, CancellationToken ct)
        {
            CmdResult2 result = await gitCmdService.RunCmdAsync($"checkout --progress {name}", ct);

            if (result.IsFaulted)
            {
                if (IsUnknownName(result, name))
                {
                    Log.Info($"Unknown name: {name}");
                    return(false);
                }

                return(R.Error($"Failed to checkout {name}", result.AsException()));
            }

            Log.Info($"Checked out {name}");
            return(true);
        }
Ejemplo n.º 17
0
        public async Task <R> MergeAsync(string name, CancellationToken ct)
        {
            CmdResult2 result = await gitCmdService.RunCmdAsync(
                $"merge --no-ff --no-commit --stat --progress {name}", ct);

            if (result.IsFaulted)
            {
                if (result.ExitCode == 1 && IsConflicts(result))
                {
                    Log.Info($"Merge of {name} resulted in conflict(s)");
                    return(R.Ok);
                }

                return(R.Error($"Failed to merge branch {name}", result.AsException()));
            }

            Log.Info($"Merge of {name} was OK");
            return(R.Ok);
        }
Ejemplo n.º 18
0
        public async Task <R> PushRefsAsync(IEnumerable <string> refspecs, CancellationToken ct)
        {
            string refsText = string.Join(" ", refspecs);

            Log.Debug($"Pushing refs {refsText} ...");

            CmdResult2 result = await gitCmdService.RunCmdAsync($"{PushArgs} {refsText}", ct);

            if (result.IsFaulted)
            {
                if (IsNoRemote(result))
                {
                    return(R.Ok);
                }
                return(R.Error("Failed to push", result.AsException()));
            }

            return(R.Ok);
        }
Ejemplo n.º 19
0
        public async Task <R <bool> > TryMergeFastForwardAsync(string name, CancellationToken ct)
        {
            CmdResult2 result = await gitCmdService.RunCmdAsync(
                $"merge --ff-only --stat --progress {name}", ct);

            if (result.IsFaulted)
            {
                if (result.Error.StartsWith("fatal: Not possible to fast-forward"))
                {
                    Log.Info($"Merge of {name} could not be fast forward merged");
                    return(false);
                }

                return(R.Error($"Failed to ff merge branch {name}", result.AsException()));
            }

            Log.Info($"Merge of {name} was OK");
            return(true);
        }
Ejemplo n.º 20
0
        public async Task <R <string> > GetNoteAsync(string sha, string notesRef, CancellationToken ct)
        {
            CmdResult2 result = await gitCmdService.RunCmdAsync(
                $"-c core.notesRef={notesRef} notes show {sha}", ct);

            if (result.IsFaulted)
            {
                if (result.ExitCode == 1 && result.Error.StartsWith($"error: no note found for object {sha}"))
                {
                    return(R.NoValue);
                }

                return(R.Error("Failed to get note", result.AsException()));
            }

            string notes = result.Output;

            Log.Info($"Got note {notes.Length} length");
            return(notes);
        }
Ejemplo n.º 21
0
        public async Task <R> AddNoteAsync(
            string sha, string notesRef, string note, CancellationToken ct)
        {
            Log.Debug($"Adding {note.Length}chars on {sha} {notesRef} ...");

            string filePath = Path.GetTempFileName();

            File.WriteAllText(filePath, note);
            CmdResult2 result = await gitCmdService.RunCmdAsync(
                $"-c core.notesRef={notesRef} notes add -f --allow-empty -F \"{filePath}\" {sha}", ct);

            DeleteNotesFile(filePath);

            if (result.IsFaulted)
            {
                return(R.Error("Failed to add note", result.AsException()));
            }


            Log.Info($"Added note {note.Length} length");
            return(R.Ok);
        }
Ejemplo n.º 22
0
        public async Task <R> FetchAsync(CancellationToken ct)
        {
            Log.Debug("Fetching ...");

            void Progress(string text)
            {
                Log.Debug($"Progress: {text}");
            }

            CmdResult2 result = await gitCmdService.RunCmdWitProgressAsync(FetchArgs, Progress, ct);

            if (result.IsFaulted)
            {
                if (IsNoRemote(result))
                {
                    return(R.Ok);
                }
                return(R.Error("Failed to fetch", result.AsException()));
            }

            return(R.Ok);
        }
Ejemplo n.º 23
0
        public async Task <R> GetLogAsync(Action <GitCommit> commits, CancellationToken ct)
        {
            int count = 0;

            void OutputLines(string line)
            {
                if (!ct.IsCancellationRequested)
                {
                    count++;
                    commits(Parse(line));
                }
            }

            CmdResult2 result = await gitCmdService.RunCmdAsync($"log --all --pretty=\"{LogFormat}\"", OutputLines, ct);

            if (result.IsFaulted && !ct.IsCancellationRequested)
            {
                return(R.Error("Failed to get log", result.AsException()));
            }

            Log.Info($"Got log for {count} commits");
            return(R.Ok);
        }
Ejemplo n.º 24
0
        public async Task <R> FetchPruneTagsAsync(CancellationToken ct)
        {
            Log.Debug("Fetching tags ...");
            string args = "fetch --prune origin \"+refs/tags/*:refs/tags/*\"";

            void Progress(string text)
            {
                Log.Debug($"Progress: {text}");
            }

            CmdResult2 result = await gitCmdService.RunCmdWitProgressAsync(args, Progress, ct);

            if (result.IsFaulted)
            {
                if (IsNoRemote(result))
                {
                    return(R.Ok);
                }
                return(R.Error("Failed to fetch", result.AsException()));
            }

            return(R.Ok);
        }