@@ -1906,6 +1906,23 @@ static const char *tracking_for_push_dest(struct remote *remote,
return ret;
}
+static const char *default_missing_upstream(struct remote *remote,
+ struct branch *branch,
+ struct strbuf *err)
+{
+ int autosetupremote = 0;
+
+ if (branch && (!branch->merge || !branch->merge[0])) {
+ repo_config_get_bool(the_repository,
+ "push.autosetupremote",
+ &autosetupremote);
+ if (autosetupremote)
+ return tracking_for_push_dest(remote, branch->refname, err);
+ }
+
+ return NULL;
+}
+
static const char *branch_get_push_1(struct remote_state *remote_state,
struct branch *branch, struct strbuf *err)
{
@@ -1946,13 +1963,22 @@ static const char *branch_get_push_1(struct remote_state *remote_state,
return tracking_for_push_dest(remote, branch->refname, err);
case PUSH_DEFAULT_UPSTREAM:
- return branch_get_upstream(branch, err);
-
+ {
+ const char *up;
+ up = default_missing_upstream(remote, branch, err);
+ if (up)
+ return up;
+ return branch_get_upstream(branch, err);
+ }
case PUSH_DEFAULT_UNSPECIFIED:
case PUSH_DEFAULT_SIMPLE:
{
const char *up, *cur;
+ up = default_missing_upstream(remote, branch, err);
+ if (up)
+ return up;
+
up = branch_get_upstream(branch, err);
if (!up)
return NULL;
@@ -21,7 +21,10 @@ test_expect_success 'setup' '
git push origin HEAD &&
git branch --set-upstream-to=origin/main main &&
git branch --track topic origin/main &&
+ git branch --no-track indie_topic origin/main &&
+ git branch --no-track new_topic origin/main &&
git push origin topic &&
+ git push origin indie_topic &&
git push other topic
'
@@ -73,4 +76,21 @@ test_expect_success 'resolving @{push} fails with a detached HEAD' '
test_must_fail git rev-parse @{push}
'
+test_expect_success '@{push} with default=simple without tracking' '
+ test_config push.default simple &&
+ test_must_fail git rev-parse indie_topic@{push}
+'
+
+test_expect_success '@{push} with default=simple with autosetupremote' '
+ test_config push.default simple &&
+ test_config push.autosetupremote true &&
+ resolve indie_topic@{push} refs/remotes/origin/indie_topic
+'
+
+test_expect_success '@{push} with default=simple with autosetupremote, new branch' '
+ test_config push.default simple &&
+ test_config push.autosetupremote true &&
+ test_must_fail git rev-parse new_topic@{push}
+'
+
test_done