push: change `simple` to accommodate triangular workflows

When remote.pushdefault or branch.<name>.pushremote is set to a remote that is different from where you usually fetch from (i.e. a triangular workflow), master@{u} != origin, and push.default is set to `upstream` or `simple` would fail with this error: $ git push fatal: You are pushing to remote 'origin', which is not the upstream of your current branch 'master', without telling me what to push to update which remote branch. The very name of "upstream" indicates that it is only suitable for use in central workflows; let us not even attempt to give it a new meaning in triangular workflows, and error out as before. However, the `simple` does not have to share this error. It is poised to be the default for Git 2.0, and we would like it to do something sensible in triangular workflows. Redefine "simple" as "safer upstream" for centralized workflow as before, but work as "current" for triangular workflow. We may want to make it "safer current", but that is a separate issue. Reported-by: Leandro Lucarella <leandro.lucarella@sociomantic.com> Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ramkumar Ramachandra committed Jun 19, 2013 at 16:41 UTC ed2b18292bfeedc98c9e2b6bd8a35d8001dab2fc
2 files changed +38 -15
Documentation/config.txt
+7 -3
@@ -1848,9 +1848,13 @@ push.default::
1848 pushing to the same repository you would normally pull from
1849 (i.e. central workflow).
1850
1851 -* `simple` - like `upstream`, but refuses to push if the upstream
1852 - branch's name is different from the local one. This is the safest
1853 - option and is well-suited for beginners.
1851 +* `simple` - in centralized workflow, work like `upstream` with an
1852 + added safety to refuse to push if the upstream branch's name is
1853 + different from the local one.
1854 ++
1855 +When pushing to a remote that is different from the remote you normally
1856 +pull from, work as `current`. This is the safest option and is suited
1857 +for beginners.
1858 +
1859 This mode will become the default in Git 2.0.
1860
builtin/push.c
+31 -12
@@ -120,10 +120,11 @@ static const char message_detached_head_die[] =
120 "\n"
121 " git push %s HEAD:<name-of-remote-branch>\n");
122
123 -static void setup_push_upstream(struct remote *remote, int simple)
123 +static void setup_push_upstream(struct remote *remote, struct branch *branch,
124 + int triangular)
125 {
126 struct strbuf refspec = STRBUF_INIT;
126 - struct branch *branch = branch_get(NULL);
127 +
128 if (!branch)
129 die(_(message_detached_head_die), remote->name);
130 if (!branch->merge_nr || !branch->merge || !branch->remote_name)
@@ -137,18 +138,29 @@ static void setup_push_upstream(struct remote *remote, int simple)
138 if (branch->merge_nr != 1)
139 die(_("The current branch %s has multiple upstream branches, "
140 "refusing to push."), branch->name);
140 - if (strcmp(branch->remote_name, remote->name))
141 + if (triangular)
142 die(_("You are pushing to remote '%s', which is not the upstream of\n"
143 "your current branch '%s', without telling me what to push\n"
144 "to update which remote branch."),
145 remote->name, branch->name);
145 - if (simple && strcmp(branch->refname, branch->merge[0]->src))
146 - die_push_simple(branch, remote);
146 +
147 + if (push_default == PUSH_DEFAULT_SIMPLE) {
148 + /* Additional safety */
149 + if (strcmp(branch->refname, branch->merge[0]->src))
150 + die_push_simple(branch, remote);
151 + }
152
153 strbuf_addf(&refspec, "%s:%s", branch->name, branch->merge[0]->src);
154 add_refspec(refspec.buf);
155 }
156
157 +static void setup_push_current(struct remote *remote, struct branch *branch)
158 +{
159 + if (!branch)
160 + die(_(message_detached_head_die), remote->name);
161 + add_refspec(branch->name);
162 +}
163 +
164 static char warn_unspecified_push_default_msg[] =
165 N_("push.default is unset; its implicit value is changing in\n"
166 "Git 2.0 from 'matching' to 'simple'. To squelch this message\n"
@@ -173,9 +185,16 @@ static void warn_unspecified_push_default_configuration(void)
185 warning("%s\n", _(warn_unspecified_push_default_msg));
186 }
187
188 +static int is_workflow_triangular(struct remote *remote)
189 +{
190 + struct remote *fetch_remote = remote_get(NULL);
191 + return (fetch_remote && fetch_remote != remote);
192 +}
193 +
194 static void setup_default_push_refspecs(struct remote *remote)
195 {
178 - struct branch *branch;
196 + struct branch *branch = branch_get(NULL);
197 + int triangular = is_workflow_triangular(remote);
198
199 switch (push_default) {
200 default:
@@ -188,18 +207,18 @@ static void setup_default_push_refspecs(struct remote *remote)
207 break;
208
209 case PUSH_DEFAULT_SIMPLE:
191 - setup_push_upstream(remote, 1);
210 + if (triangular)
211 + setup_push_current(remote, branch);
212 + else
213 + setup_push_upstream(remote, branch, triangular);
214 break;
215
216 case PUSH_DEFAULT_UPSTREAM:
195 - setup_push_upstream(remote, 0);
217 + setup_push_upstream(remote, branch, triangular);
218 break;
219
220 case PUSH_DEFAULT_CURRENT:
199 - branch = branch_get(NULL);
200 - if (!branch)
201 - die(_(message_detached_head_die), remote->name);
202 - add_refspec(branch->name);
221 + setup_push_current(remote, branch);
222 break;
223
224 case PUSH_DEFAULT_NOTHING: