Exemple #1
0
        static int Main(string[] args)
        {
            bool forceDelete = false;

#if DEBUG
            OldLogger.Level = OldLogger.LevelValue.Verbose;
            OldLogger.AnnounceStartStopActions = true;
#endif
            for (int ii = 0; ii < args.Length; ii++)
            {
                string arg = args[ii].ToLower();
                switch (arg)
                {
                case "/v":
                case "/verbose":
                    OldLogger.AnnounceStartStopActions = true;
                    OldLogger.Level = OldLogger.LevelValue.Verbose;
                    break;

                case "/log":
                    OldLogger.AddLogFile(args[++ii]);
                    break;

                case "/html":
                    OldLogger.AddHTMLLogFile(args[++ii]);
                    break;

                case "/vlog":
                    OldLogger.AnnounceStartStopActions = true;
                    OldLogger.AddLogFile(args[++ii], OldLogger.LevelValue.Verbose);
                    break;

                case "/fd":
                    forceDelete = true;
                    break;

                default:
                    Console.WriteLine("Unknown argument: " + arg);
                    PrintUsage();
                    OldLogger.FlushLogs();
                    return((int)OldLogger.WarningCount);
                }
            }

            string verboseLogFile = OldLogger.VerboseLogPath;
            if (!string.IsNullOrEmpty(verboseLogFile))
            {
                OldLogger.LogLine("Verbose log path: " + verboseLogFile);
            }

            OldLogger.Log("Fetching... ");
            GitOperations.FetchAll();
            OldLogger.LogLine("done");

            OldLogger.LogLine(@"Examining local branches...");

            List <BranchInfo> localBranches      = GitOperations.GetLocalBranches().Select(x => new BranchInfo(x)).ToList();
            string            defaultBranchName  = GitOperations.GetDefaultBranch();
            BranchInfo        localDefaultBranch = localBranches.FirstOrDefault(x => x.Name == defaultBranchName);
            if (localDefaultBranch == null)
            {
                defaultBranchName = "origin/" + defaultBranchName;
            }
            else
            {
                localDefaultBranch.IsDefault = true;
            }

            List <string> remoteBranches = new List <string>(GitOperations.GetRemoteBranches());
            foreach (BranchInfo branch in localBranches)
            {
                branch.HasRemoteBranch = remoteBranches.Contains(branch.Name) || remoteBranches.Contains("origin/" + branch.Name) || branch.Name.Contains("HEAD");
                if (!branch.HasRemoteBranch)
                {
                    OldLogger.LogLine($"Remote branch is gone for {branch.Name}", OldLogger.LevelValue.Warning);
                }
            }

            List <BranchInfo> unparentedBranches = localBranches.Where(x => !x.IsParented && !x.IsDefault).ToList();
            if (unparentedBranches.Count > 0)
            {
                foreach (BranchInfo branch in unparentedBranches)
                {
                    OldLogger.LogLine($"{branch.Name} has no parent branch");
                }

                string exampleBranchName = "<branch name>";
                if (unparentedBranches.Count == 1)
                {
                    exampleBranchName = unparentedBranches.First().Name;
                }
                OldLogger.LogLine($"To set {defaultBranchName} as the parent branch, run the following command:");
                OldLogger.LogLine($"\tgit config branch.{exampleBranchName}.basedon {defaultBranchName}");
            }

            GitStatus originalStatus     = GitOperations.GetStatus();
            string    originalBranchName = originalStatus.Branch;
            OldLogger.LogLine("\r\nStarted in " + originalBranchName);
            if (originalStatus.AnyChanges)
            {
                if (!GitOperations.Stash())
                {
                    OldLogger.LogLine("Unable to stash the current work.  Cannot continue.", OldLogger.LevelValue.Error);
                    OldLogger.FlushLogs();
                    return((int)OldLogger.WarningCount);
                }
            }

            //
            // Delete local branches that don't have a remote branch
            //
            ProcessHelper failureProc = null;
            foreach (BranchInfo branch in localBranches.Where(x => !x.HasRemoteBranch && !x.IsDefault))
            {
                if (GitOperations.GetCurrentBranchName() == branch.Name)
                {
                    if (!GitOperations.SwitchBranch(defaultBranchName, out failureProc))
                    {
                        OldLogger.LogLine("Unable to switch branches", OldLogger.LevelValue.Warning);
                        OldLogger.LogLine(failureProc.AllOutput, OldLogger.LevelValue.Normal);
                        continue;
                    }
                }

                if (GitOperations.DeleteBranch(branch.Name, force: forceDelete))
                {
                    if (branch.Name == originalBranchName)
                    {
                        originalBranchName = defaultBranchName;
                    }
                    branch.IsDeleted = true;
                }
            }
            localBranches = localBranches.Where(x => !x.IsDeleted).ToList();

            foreach (BranchInfo branch in SortParentBranchesFirst(localBranches))
            {
                OldLogger.LogLine(string.Empty);

                UpdateBranch(branch.Name);

                if (branch.IsParented)
                {
                    MergeBranch(branch.Name, branch.ParentBranchName);
                }
            }
            OldLogger.LogLine(string.Empty);

            if (!GitOperations.SwitchBranch(originalBranchName, out failureProc))
            {
                OldLogger.LogLine("Unable to switch branches", OldLogger.LevelValue.Error);
                OldLogger.LogLine(failureProc.AllOutput, OldLogger.LevelValue.Warning);
                OldLogger.FlushLogs();
                return((int)OldLogger.WarningCount);
            }

            if (originalStatus.AnyChanges)
            {
                GitOperations.StashPop();
            }

            OldLogger.FlushLogs();
            return((int)OldLogger.WarningCount);
        }
Exemple #2
0
        private static void FollowExistingRemoteBranch()
        {
            string searchTerm = Environment.GetEnvironmentVariable("USERNAME").ToLower();

#if DEBUG
            searchTerm = "personal";
#endif
            string[]      localBranches  = StringHelper.ToLower(GitOperations.GetLocalBranches());
            List <string> remoteBranches = new List <string>(GitOperations.GetRemoteBranches("--sort committerdate"));
            for (int ii = 0; ii < remoteBranches.Count; ii++)
            {
                string lowerRemoteBranch = StringHelper.TrimStart(remoteBranches[ii].ToLower(), "origin/");
                if (localBranches.Contains(lowerRemoteBranch))
                {
                    remoteBranches.RemoveAt(ii--);
                }
            }

            List <string> matchingBranches = new List <string>();
            foreach (string remoteBranch in remoteBranches)
            {
                if (remoteBranch.ToLower().Contains(searchTerm))
                {
                    matchingBranches.Add(remoteBranch);
                }
            }
            if (matchingBranches.Count > 0)
            {
                OldLogger.LogLine(@"Select from the following:");
                for (int ii = 0; ii < matchingBranches.Count; ii++)
                {
                    OldLogger.LogLine("\t" + (ii + 1) + " : " + matchingBranches[ii]);
                }
                OldLogger.Log("Or");
            }
            OldLogger.LogLine("Enter another branch name");
            string prompt = Console.ReadLine().Trim();
            if (string.IsNullOrEmpty(prompt) || (prompt.ToLower() == "q"))
            {
                return;
            }

            string basedOn = string.Empty;
            if (remoteBranches.Contains(prompt))
            {
                basedOn = prompt;
            }
            else if (remoteBranches.Contains("origin/" + prompt))
            {
                basedOn = "origin/" + prompt;
            }
            else
            {
                int index = -1;
                if (int.TryParse(prompt, out index) && (index > 0) && (index <= matchingBranches.Count))
                {
                    basedOn = matchingBranches[index - 1];
                }
            }

            if (string.IsNullOrEmpty(basedOn))
            {
                OldLogger.LogLine("Unable to find the given branch: " + prompt, OldLogger.LevelValue.Error);
                return;
            }

            CreateBranch(StringHelper.TrimStart(basedOn, "origin/"), basedOn);
        }
Exemple #3
0
        static void Main(string[] args)
        {
#if DEBUG
            //Logger.AnnounceStartStopActions = true;
            OldLogger.Level = OldLogger.LevelValue.Verbose;
#endif

            bool     localOnly       = false;
            DateTime aMonthAgo       = DateTime.Now.AddMonths(-1);
            string   aMonthAgoString = aMonthAgo.ToShortDateString();

            string[] branches;
            if (localOnly)
            {
                branches = GitOperations.GetLocalBranches();
            }
            else
            {
                branches = GitOperations.GetRemoteBranches();
            }

            List <string> lines = new List <string>();
            foreach (string branch in branches)
            {
                if (!branch.Contains(" -> "))
                {
                    OldLogger.Log(".", OldLogger.LevelValue.Verbose);
                    ProcessHelper proc = new ProcessHelper("git.exe", "log -n 1 --since=" + aMonthAgoString + " " + branch);
                    if (proc.Go().Length == 0)
                    {
                        string[] changeDescription = (new ProcessHelper("git.exe", "log --date=short --pretty=format:\"%an,%ae," + StringHelper.TrimStart(branch, "origin/") + ",%ad\" -n 1 " + branch)).Go();
                        Debug.Assert(changeDescription.Length == 1);
                        lines.Add(changeDescription[0]);
                    }
                }
            }
            OldLogger.LogLine("", OldLogger.LevelValue.Verbose);
            lines.Sort();
            OldLogger.LogLine(lines.ToArray());

            OldLogger.LogLine(string.Empty);
            lines = new List <string>();

            foreach (string branch in branches)
            {
                if (branch.ToLower().StartsWith(@"origin/u/"))
                {
                    continue;
                }
                if (branch.ToLower().StartsWith(@"origin/user/"))
                {
                    continue;
                }
                if (branch.ToLower().StartsWith(@"origin/users/"))
                {
                    continue;
                }
                if (branch.ToLower().StartsWith(@"origin/feature/"))
                {
                    continue;
                }
                if (branch.ToLower().StartsWith(@"origin/features/"))
                {
                    continue;
                }
                if (branch.ToLower().StartsWith(@"origin/release/"))
                {
                    continue;
                }
                if (branch.Contains(" -> "))
                {
                    continue;
                }

                OldLogger.LogLine(branch);

                string[] changeDescription = (new ProcessHelper("git.exe", "log --date=short --pretty=format:\"%an (%ae) was the last person to touch " + StringHelper.TrimStart(branch, "origin/") + " on %ad\" -n 1 " + branch)).Go();
                Debug.Assert(changeDescription.Length == 1);
                lines.Add(changeDescription[0]);
            }

            OldLogger.LogLine("", OldLogger.LevelValue.Verbose);
            lines.Sort();
            OldLogger.LogLine(lines.ToArray());
        }