Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix problem with incorrect building command line arguments #3551

Conversation

azarkevich
Copy link
Contributor

Situation:

Branch name contains word push (for example pr/fix-cmdline-in-retry-push-with-force)
and remote reject push due to non-fast-forward
and user select retry with force

then word push in branch name was replaced with 'push --force-with-lease' by .Replace("push", "push --force-with-lease")

@amaiorano
Copy link
Contributor

@jbialobr is encouraging us to review each other's changes so if you don't mind, I'll review yours. Perhaps you can take a look at my change as well.

@@ -502,15 +502,15 @@ private bool HandlePushOnExit(ref bool isError, FormProcess form)

if (forcePush)
{
if (!form.ProcessArguments.Contains(" -f ") && !form.ProcessArguments.Contains(" --force"))
if (!form.ProcessArguments.Contains(" -f ") && !form.ProcessArguments.Contains(" --force") && !form.ProcessArguments.EndsWith(" -f"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why you're checking if the args ends with -f here. Even if it were necessary, wouldn't you want to also check if it's --force?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not actual anymore, but answer is: I add -f to end of arguments, so check .Contains(" -f ") will not work because of trailing space.

So this condition should cover all possible cases:

push -f origin
push --force origin
push --force-with-lease origin

push origin ... -f
push origin ... --force
push origin ... --force-with-lease

{
if (GitCommandHelpers.VersionInUse.SupportPushForceWithLease)
{
form.ProcessArguments = form.ProcessArguments.Replace("push", "push --force-with-lease");
form.ProcessArguments = form.ProcessArguments + " --force-with-lease";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think appending to the end of the args is the right thing to do. At this point, args will look something like:

push --recurse-submodules=check --progress "origin" refs/heads/test-force-push:refs/heads/test-force-push

We usually want to avoid adding args after we specify the remote and name of branch. I'm surprised this works.

I tried something locally, and this seems to work:

                if (forcePush)
                {
                    if (!form.ProcessArguments.Contains(" -f ") && !form.ProcessArguments.Contains(" --force"))
                    {
                        string forceArg = GitCommandHelpers.VersionInUse.SupportPushForceWithLease? " --force-with-lease" : " -f";
                        form.ProcessArguments = form.ProcessArguments.Insert(form.ProcessArguments.IndexOf("push") + "push".Length, forceArg);
                    }

Or if we can assume "force" is always the first value in the args list, it can be simplified to:

                if (forcePush)
                {
                    if (!form.ProcessArguments.Contains(" -f ") && !form.ProcessArguments.Contains(" --force"))
                    {
                        string forceArg = GitCommandHelpers.VersionInUse.SupportPushForceWithLease? " --force-with-lease" : " -f";
                        form.ProcessArguments = form.ProcessArguments.Insert("push".Length, forceArg);
                    }

@azarkevich azarkevich force-pushed the pr/fix-cmdline-in-retry-push-with-force branch from fd30312 to 3c80f98 Compare March 2, 2017 12:41
@azarkevich
Copy link
Contributor Author

@amaiorano, I update fix to your variant. I made it slightly more strict.

@@ -504,13 +504,14 @@ private bool HandlePushOnExit(ref bool isError, FormProcess form)
{
if (!form.ProcessArguments.Contains(" -f ") && !form.ProcessArguments.Contains(" --force"))
{
if (GitCommandHelpers.VersionInUse.SupportPushForceWithLease)
if(form.ProcessArguments.StartsWith("push ", StringComparison.OrdinalIgnoreCase))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is better, but a few things:

  • Missing a space between if and the open parenthesis.
  • No need to check with ignore case for "push", Git is case sensitive so if it were anything but "push" it wouldn't work.
  • In the few places in this code base where Debug.Assert is used, System.Diagnostics is imported at the top.
  • I'm not a fan of Debug.Assert(false, ...) as we don't see the condition for the failure. In this case, it is extremely unlikely that "push" will be missing, so I would rather we just assert that it starts with "push", then just assume it, so like:
if (!form.ProcessArguments.Contains(" -f ") && !form.ProcessArguments.Contains(" --force"))
{
    Debug.Assert(form.ProcessArguments.StartsWith("push"), "Arguments should start with 'push' command");
    string forceArg = GitCommandHelpers.VersionInUse.SupportPushForceWithLease ? " --force-with-lease" : " -f";
    form.ProcessArguments = form.ProcessArguments.Insert("push".Length, forceArg);
}

I personally prefer this over having the "safe code" with the else asserting false, but perhaps we should ask @jbialobr to weigh in on this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally prefer this over having the "safe code" with the else asserting false, but perhaps we should ask @jbialobr to weigh in on this.

First of all, I want to thank you for reviewing PRs. This is a community project and I don't want to force my coding preferences. Let's wait for @azarkevich to see his opinion (pros and cons). I will have to decide only If you won't find an agreement.

Copy link
Contributor Author

@azarkevich azarkevich Mar 3, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with your proposals and remarks. Pushed new version.
Though I use Trace.Assert instead of Debug.Assert, so this condition will be verified even in Release.

@azarkevich azarkevich force-pushed the pr/fix-cmdline-in-retry-push-with-force branch from 3c80f98 to 915563d Compare March 3, 2017 09:50
Branch name contains word 'push' (for example my-push-implementation)
and remote reject push due to non-fast-forward
and user select 'retry with force'

then 'push' in branch name was replaced with 'push --force-with-lease'
@azarkevich azarkevich force-pushed the pr/fix-cmdline-in-retry-push-with-force branch from 915563d to d87d91a Compare March 3, 2017 09:55
Copy link
Contributor

@amaiorano amaiorano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me!

@jbialobr jbialobr merged commit ca42b5e into gitextensions:master Mar 5, 2017
@azarkevich azarkevich deleted the pr/fix-cmdline-in-retry-push-with-force branch March 5, 2017 08:05
jbialobr added a commit that referenced this pull request Mar 16, 2017
…-with-force

Fix problem with incorrect building command line arguments
(cherry picked from commit ca42b5e)

# Conflicts:
#	GitUI/CommandsDialogs/FormPush.cs
@jbialobr jbialobr added this to the 2.49.2 milestone Mar 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants