Pushing all submodules recursively

前端 未结 4 1874
终归单人心
终归单人心 2021-02-06 05:53

I wrote the below script to push all the changes in the workspace, both in the submodules and the superproject. However, it sounds a little odd that, it is this complex to do wh

4条回答
  •  忘掉有多难
    2021-02-06 06:23

    This c# console app prompts you for each submodule with changes and allows you to easily add/commit/push or run custom git commands:

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    
    static class Program
    {
        static readonly Process Proc = new Process();
    
    static void Main(string[] args)
    {
        Console.WindowHeight = (int) (Console.LargestWindowHeight * .8f);
        var dir = Path.GetFullPath(args.Length == 1 ? args[0] : Directory.GetCurrentDirectory());
    
        foreach (var module in GetModules(dir))
        {
            Console.WriteLine("Processing: " + module);
            PrintDiff(module, out var untracked, out var mods);
    
            if (!untracked && !mods)
                continue;
    
            while (true)
            {
                Console.WriteLine("Use \"c \" to commit and push" + (untracked ? ", \"a \" to also add untracked files" : "") + ", r to refresh.\nTo fix issues use git as normal, or cmd to enter prompt (exit to leave)" + ":");
    
                var cmd = Console.ReadLine();
    
                if (cmd.StartsWith("a "))
                    Run("git", "add .", module);
    
                if (cmd.StartsWith("a ") || cmd.StartsWith("c "))
                {
                    Run("git", "commit -a -m \"" + cmd.Substring(2) + "\"", module);
                    Run("git", "push", module);
                    break;
                }
    
                if (cmd.Trim() == "r")
                {
                    PrintDiff(module, out untracked, out mods);
                    if (!untracked && !mods)
                        break;
                    continue;
                }
    
                try
                {
                    var parts = cmd.Split(' ');
                    Run(parts[0], cmd.Substring(parts[0].Length), module);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    Console.WriteLine();
                }
            }
    
            Console.WriteLine();
        }
    
        Console.WriteLine("Press any key to continue...");
        Console.ReadKey();
    }
    
    static void PrintDiff(string module, out bool untracked, out bool mods)
    {
        var u = false;
    
        Run("git", "ls-files . --exclude-standard --others", module, s =>
        {
            u = true;
            Console.WriteLine("Untracked file: " + s);
        });
    
        var m = false;
        Run("git", "diff --staged", module, t =>
        {
            m = true;
            if (t.StartsWith("diff --git"))
                Console.WriteLine();
            Console.WriteLine(t);
        });
    
        Run("git", "diff", module, t =>
        {
            m = true;
            if (t.StartsWith("diff --git"))
                Console.WriteLine();
            Console.WriteLine(t);
        });
    
        if (m)
            Console.WriteLine();
    
        if (!u && !m)
        {
            Console.WriteLine("No changes");
            Console.WriteLine();
        }
    
        untracked = u;
        mods = m;
    }
    
    static List GetModules(string dir)
    {
        var l = new List();
        var paths = new Queue();
        var modules = new List();
        paths.Enqueue("");
    
        while (paths.Count > 0)
        {
            var d = Path.Combine(dir, paths.Dequeue());
            if (!File.Exists(Path.Combine(d, ".gitmodules")))
                continue;
    
            Run("git", "config --file .gitmodules -l", d, t =>
            {
                var parts = t.Split('=');
                if (parts[0].EndsWith(".path"))
                    l.Add(Path.Combine(d, parts[1]));
            });
    
            foreach (var s in l)
                paths.Enqueue(s);
            modules.AddRange(l);
            l.Clear();
        }
    
        modules.Reverse();
        return modules;
    }
    
    static void Run(string fileName, string arguments, string directory)
    {
        Run(fileName, arguments, directory, Console.WriteLine);
        Console.WriteLine();
    }
    
    static void Run(string fileName, string arguments, string directory, Action a)
    {
        Proc.StartInfo = new ProcessStartInfo
        {
            FileName = fileName,
            Arguments = arguments,
            RedirectStandardOutput = true,
            WorkingDirectory = directory,
            UseShellExecute = false
        };
        Proc.Start();
        string t;
        while ((t = Proc.StandardOutput.ReadLine()) != null)
            a(t);
    }
    }
    

提交回复
热议问题