Skip to content

Instantly share code, notes, and snippets.

@davidfowl
Last active December 12, 2024 09:11
Show Gist options
  • Save davidfowl/f116c515cd977a8c96670abcd1cc2263 to your computer and use it in GitHub Desktop.
Save davidfowl/f116c515cd977a8c96670abcd1cc2263 to your computer and use it in GitHub Desktop.

Revisions

  1. davidfowl revised this gist Dec 10, 2024. 1 changed file with 54 additions and 55 deletions.
    109 changes: 54 additions & 55 deletions git.cs
    Original file line number Diff line number Diff line change
    @@ -2,79 +2,78 @@
    using System.Collections.Generic;
    using CliWrap;

    namespace GitCLIWrapper
    namespace GitCLIWrapper;

    public static class Git
    {
    public static class Git
    public static Command Clone(
    string repository,
    string? directory = null,
    string? branch = null,
    int? depth = null)
    {
    public static Command Clone(
    string repository,
    string? directory = null,
    string? branch = null,
    int? depth = null)
    {
    var args = new List<string> { "clone", repository };
    var args = new List<string> { "clone", repository };

    if (!string.IsNullOrEmpty(directory))
    args.Add(directory);
    if (!string.IsNullOrEmpty(directory))
    args.Add(directory);

    if (!string.IsNullOrEmpty(branch))
    args.Add($"--branch {branch}");
    if (!string.IsNullOrEmpty(branch))
    args.Add($"--branch {branch}");

    if (depth.HasValue)
    args.Add($"--depth {depth}");
    if (depth.HasValue)
    args.Add($"--depth {depth}");

    return Cli.Wrap("git").WithArguments(string.Join(" ", args));
    }
    return Cli.Wrap("git").WithArguments(string.Join(" ", args));
    }

    public static Command Add(
    string pathspec,
    bool update = false,
    bool verbose = false)
    {
    var args = new List<string> { "add", pathspec };
    public static Command Add(
    string pathspec,
    bool update = false,
    bool verbose = false)
    {
    var args = new List<string> { "add", pathspec };

    if (update)
    args.Add("--update");
    if (update)
    args.Add("--update");

    if (verbose)
    args.Add("--verbose");
    if (verbose)
    args.Add("--verbose");

    return Cli.Wrap("git").WithArguments(string.Join(" ", args));
    }
    return Cli.Wrap("git").WithArguments(string.Join(" ", args));
    }

    public static Command Commit(
    string message,
    bool all = false,
    bool amend = false)
    {
    var args = new List<string> { "commit", $"-m \"{message}\"" };
    public static Command Commit(
    string message,
    bool all = false,
    bool amend = false)
    {
    var args = new List<string> { "commit", $"-m \"{message}\"" };

    if (all)
    args.Add("--all");
    if (all)
    args.Add("--all");

    if (amend)
    args.Add("--amend");
    if (amend)
    args.Add("--amend");

    return Cli.Wrap("git").WithArguments(string.Join(" ", args));
    }
    return Cli.Wrap("git").WithArguments(string.Join(" ", args));
    }

    public static Command Clean(
    bool dryRun = false,
    bool force = false,
    bool directories = false)
    {
    var args = new List<string> { "clean" };
    public static Command Clean(
    bool dryRun = false,
    bool force = false,
    bool directories = false)
    {
    var args = new List<string> { "clean" };

    if (dryRun)
    args.Add("--dry-run");
    if (dryRun)
    args.Add("--dry-run");

    if (force)
    args.Add("--force");
    if (force)
    args.Add("--force");

    if (directories)
    args.Add("--directories");
    if (directories)
    args.Add("--directories");

    return Cli.Wrap("git").WithArguments(string.Join(" ", args));
    }
    return Cli.Wrap("git").WithArguments(string.Join(" ", args));
    }
    }
  2. davidfowl revised this gist Dec 10, 2024. 1 changed file with 80 additions and 0 deletions.
    80 changes: 80 additions & 0 deletions git.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,80 @@
    using System;
    using System.Collections.Generic;
    using CliWrap;

    namespace GitCLIWrapper
    {
    public static class Git
    {
    public static Command Clone(
    string repository,
    string? directory = null,
    string? branch = null,
    int? depth = null)
    {
    var args = new List<string> { "clone", repository };

    if (!string.IsNullOrEmpty(directory))
    args.Add(directory);

    if (!string.IsNullOrEmpty(branch))
    args.Add($"--branch {branch}");

    if (depth.HasValue)
    args.Add($"--depth {depth}");

    return Cli.Wrap("git").WithArguments(string.Join(" ", args));
    }

    public static Command Add(
    string pathspec,
    bool update = false,
    bool verbose = false)
    {
    var args = new List<string> { "add", pathspec };

    if (update)
    args.Add("--update");

    if (verbose)
    args.Add("--verbose");

    return Cli.Wrap("git").WithArguments(string.Join(" ", args));
    }

    public static Command Commit(
    string message,
    bool all = false,
    bool amend = false)
    {
    var args = new List<string> { "commit", $"-m \"{message}\"" };

    if (all)
    args.Add("--all");

    if (amend)
    args.Add("--amend");

    return Cli.Wrap("git").WithArguments(string.Join(" ", args));
    }

    public static Command Clean(
    bool dryRun = false,
    bool force = false,
    bool directories = false)
    {
    var args = new List<string> { "clean" };

    if (dryRun)
    args.Add("--dry-run");

    if (force)
    args.Add("--force");

    if (directories)
    args.Add("--directories");

    return Cli.Wrap("git").WithArguments(string.Join(" ", args));
    }
    }
    }
  3. davidfowl created this gist Dec 10, 2024.
    109 changes: 109 additions & 0 deletions git.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,109 @@
    {
    "name": "git",
    "description": "A distributed version control system.",
    "commands": {
    "clone": {
    "description": "Clone a repository into a new directory.",
    "arguments": [
    {
    "name": "repository",
    "type": "string",
    "description": "The URL of the repository to clone."
    },
    {
    "name": "directory",
    "type": "string",
    "description": "The name of a directory to clone into.",
    "required": false
    }
    ],
    "options": [
    {
    "name": "branch",
    "type": "string",
    "short": "b",
    "description": "Checkout a specific branch after cloning.",
    "required": false
    },
    {
    "name": "depth",
    "type": "number",
    "description": "Create a shallow clone with a history truncated to the specified number of commits.",
    "required": false
    }
    ]
    },
    "add": {
    "description": "Add file contents to the index.",
    "arguments": [
    {
    "name": "pathspec",
    "type": "string",
    "description": "Files to add to the index. Can be a file, directory, or pattern."
    }
    ],
    "options": [
    {
    "name": "update",
    "type": "boolean",
    "short": "u",
    "description": "Only update tracked files."
    },
    {
    "name": "verbose",
    "type": "boolean",
    "short": "v",
    "description": "Be verbose."
    }
    ]
    },
    "commit": {
    "description": "Record changes to the repository.",
    "arguments": [],
    "options": [
    {
    "name": "message",
    "type": "string",
    "short": "m",
    "description": "Use the given message as the commit message.",
    "required": true
    },
    {
    "name": "all",
    "type": "boolean",
    "short": "a",
    "description": "Stage all modified and deleted files before committing."
    },
    {
    "name": "amend",
    "type": "boolean",
    "description": "Amend the tip of the current branch."
    }
    ]
    },
    "clean": {
    "description": "Remove untracked files from the working directory.",
    "arguments": [],
    "options": [
    {
    "name": "dry-run",
    "type": "boolean",
    "short": "n",
    "description": "Show what would be done without actually doing it."
    },
    {
    "name": "force",
    "type": "boolean",
    "short": "f",
    "description": "Force the clean operation."
    },
    {
    "name": "directories",
    "type": "boolean",
    "short": "d",
    "description": "Remove untracked directories in addition to untracked files."
    }
    ]
    }
    }
    }