Exemple #1
0
        static void Main(string[] args)
        {
#if DEBUG
            OldLogger.AnnounceStartStopActions = true;
            OldLogger.Level = OldLogger.LevelValue.Verbose;
#endif

            string        currentBranch = GitOperations.GetCurrentBranchName();
            List <string> branches      = new List <string>(GitOperations.GetLocalBranches());
            branches.Remove(currentBranch);

            OldLogger.LogLine(@"Currently on " + currentBranch);
            OldLogger.LogLine(@"Select from the following:");
            for (int ii = 0; ii < Math.Min(9, branches.Count); ii++)
            {
                OldLogger.LogLine("\t" + (ii + 1) + " : " + branches[ii]);
            }
            OldLogger.LogLine("\tn : Make a new branch");
            OldLogger.LogLine("\tf : Follow an existing remote branch");
            string input = Console.ReadKey().KeyChar.ToString().Trim().ToLower();
            OldLogger.LogLine(string.Empty);
            if (string.IsNullOrEmpty(input) || (input.ToLower() == "q"))
            {
                return;
            }

            if (input.ToLower() == "n")
            {
                MakeNewBranch();
                return;
            }

            if (input.ToLower() == "f")
            {
                FollowExistingRemoteBranch();
                return;
            }

            if ((input == "-") || (input == "--"))
            {
                SwitchBranch("-");
                OldLogger.LogLine("Branch is now : " + GitOperations.GetCurrentBranchName());
                return;
            }

            int index = -1;
            if (!int.TryParse(input, out index))
            {
                OldLogger.LogLine(@"Not a valid number: " + input, OldLogger.LevelValue.Error);
                return;
            }

            if ((index <= 0) || (index > branches.Count))
            {
                OldLogger.LogLine(@"Invalid index: " + index, OldLogger.LevelValue.Error);
                return;
            }

            SwitchBranch(branches[index - 1]);
        }
Exemple #2
0
        private static void MakeNewBranch()
        {
            string suggestedPrefix = "u/" + Environment.GetEnvironmentVariable("USERNAME") + "/";

            OldLogger.Log("\r\nPrefix? [" + suggestedPrefix + "] : ");
            string prefix = Console.ReadLine().Trim().ToLower().Replace('_', '-').Replace(' ', '-');

            if (string.IsNullOrEmpty(prefix))
            {
                prefix = suggestedPrefix;
            }
            OldLogger.Log("Short name : ");
            string shortName = Console.ReadLine().Trim().ToLower().Replace('_', '-').Replace(' ', '-');

            if (string.IsNullOrEmpty(shortName))
            {
                OldLogger.LogLine("Short name must be provided!", OldLogger.LevelValue.Error);
                return;
            }
            string branchName = string.Join("/", string.Join("/", new string[] { prefix, shortName }).Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries));

            string suggestedBasedOn = GitOperations.GetBranchBase(GitOperations.GetCurrentBranchName());

            if (string.IsNullOrEmpty(suggestedBasedOn))
            {
                suggestedBasedOn = GetDefaultBranch();
            }
            OldLogger.Log("Based on what branch? [" + suggestedBasedOn + "] : ");
            string basedOn = Console.ReadLine().Trim();

            if (string.IsNullOrEmpty(basedOn))
            {
                basedOn = suggestedBasedOn;
            }

            string remoteBranchName = "origin/" + branchName;

            OldLogger.LogLine("Confirming new branch called " + branchName + " based on " + basedOn);
            OldLogger.LogLine("This will also be tracking " + remoteBranchName);
            OldLogger.Log("That look ok? [y] ");
            string prompt = Console.ReadKey().KeyChar.ToString().Trim().ToLower();

            OldLogger.LogLine(string.Empty);
            if (string.IsNullOrEmpty(prompt))
            {
                prompt = "y";
            }
            if (prompt == "y")
            {
                CreateBranch(branchName, basedOn);
            }
        }
Exemple #3
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);
        }