Skip to content

Commit 4d35924

Browse files
committed
Merge branch 'rr/triangle'
Support "pull from one place, push to another place" workflow better by introducing remote.pushdefault (overrides the "origin" thing) and branch.*.pushremote (overrides the branch.*.remote). * rr/triangle: remote.c: introduce branch.<name>.pushremote remote.c: introduce remote.pushdefault remote.c: introduce a way to have different remotes for fetch/push t5516 (fetch-push): drop implicit arguments from helper functions t5516 (fetch-push): update test description remote.c: simplify a bit of code using git_config_string()
2 parents e64734b + 9f765ce commit 4d35924

File tree

5 files changed

+241
-149
lines changed

5 files changed

+241
-149
lines changed

Documentation/config.txt

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -727,9 +727,22 @@ branch.autosetuprebase::
727727
This option defaults to never.
728728

729729
branch.<name>.remote::
730-
When in branch <name>, it tells 'git fetch' and 'git push' which
731-
remote to fetch from/push to. It defaults to `origin` if no remote is
732-
configured. `origin` is also used if you are not on any branch.
730+
When on branch <name>, it tells 'git fetch' and 'git push'
731+
which remote to fetch from/push to. The remote to push to
732+
may be overridden with `remote.pushdefault` (for all branches).
733+
The remote to push to, for the current branch, may be further
734+
overridden by `branch.<name>.pushremote`. If no remote is
735+
configured, or if you are not on any branch, it defaults to
736+
`origin` for fetching and `remote.pushdefault` for pushing.
737+
738+
branch.<name>.pushremote::
739+
When on branch <name>, it overrides `branch.<name>.remote` for
740+
pushing. It also overrides `remote.pushdefault` for pushing
741+
from branch <name>. When you pull from one place (e.g. your
742+
upstream) and push to another place (e.g. your own publishing
743+
repository), you would want to set `remote.pushdefault` to
744+
specify the remote to push to for all branches, and use this
745+
option to override it for a specific branch.
733746

734747
branch.<name>.merge::
735748
Defines, together with branch.<name>.remote, the upstream branch
@@ -1898,6 +1911,11 @@ receive.updateserverinfo::
18981911
If set to true, git-receive-pack will run git-update-server-info
18991912
after receiving data from git-push and updating refs.
19001913

1914+
remote.pushdefault::
1915+
The remote to push to by default. Overrides
1916+
`branch.<name>.remote` for all branches, and is overridden by
1917+
`branch.<name>.pushremote` for specific branches.
1918+
19011919
remote.<name>.url::
19021920
The URL of a remote repository. See linkgit:git-fetch[1] or
19031921
linkgit:git-push[1].

builtin/push.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ static int push_with_options(struct transport *transport, int flags)
322322
static int do_push(const char *repo, int flags)
323323
{
324324
int i, errs;
325-
struct remote *remote = remote_get(repo);
325+
struct remote *remote = pushremote_get(repo);
326326
const char **url;
327327
int url_nr;
328328

remote.c

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ static int branches_nr;
4949

5050
static struct branch *current_branch;
5151
static const char *default_remote_name;
52+
static const char *pushremote_name;
5253
static int explicit_default_remote_name;
5354

5455
static struct rewrites rewrites;
@@ -357,13 +358,16 @@ static int handle_config(const char *key, const char *value, void *cb)
357358
return 0;
358359
branch = make_branch(name, subkey - name);
359360
if (!strcmp(subkey, ".remote")) {
360-
if (!value)
361-
return config_error_nonbool(key);
362-
branch->remote_name = xstrdup(value);
361+
if (git_config_string(&branch->remote_name, key, value))
362+
return -1;
363363
if (branch == current_branch) {
364364
default_remote_name = branch->remote_name;
365365
explicit_default_remote_name = 1;
366366
}
367+
} else if (!strcmp(subkey, ".pushremote")) {
368+
if (branch == current_branch)
369+
if (git_config_string(&pushremote_name, key, value))
370+
return -1;
367371
} else if (!strcmp(subkey, ".merge")) {
368372
if (!value)
369373
return config_error_nonbool(key);
@@ -389,9 +393,16 @@ static int handle_config(const char *key, const char *value, void *cb)
389393
add_instead_of(rewrite, xstrdup(value));
390394
}
391395
}
396+
392397
if (prefixcmp(key, "remote."))
393398
return 0;
394399
name = key + 7;
400+
401+
/* Handle remote.* variables */
402+
if (!strcmp(name, "pushdefault"))
403+
return git_config_string(&pushremote_name, key, value);
404+
405+
/* Handle remote.<name>.* variables */
395406
if (*name == '/') {
396407
warning("Config remote shorthand cannot begin with '/': %s",
397408
name);
@@ -671,17 +682,21 @@ static int valid_remote_nick(const char *name)
671682
return !strchr(name, '/'); /* no slash */
672683
}
673684

674-
struct remote *remote_get(const char *name)
685+
static struct remote *remote_get_1(const char *name, const char *pushremote_name)
675686
{
676687
struct remote *ret;
677688
int name_given = 0;
678689

679-
read_config();
680690
if (name)
681691
name_given = 1;
682692
else {
683-
name = default_remote_name;
684-
name_given = explicit_default_remote_name;
693+
if (pushremote_name) {
694+
name = pushremote_name;
695+
name_given = 1;
696+
} else {
697+
name = default_remote_name;
698+
name_given = explicit_default_remote_name;
699+
}
685700
}
686701

687702
ret = make_remote(name, 0);
@@ -700,6 +715,18 @@ struct remote *remote_get(const char *name)
700715
return ret;
701716
}
702717

718+
struct remote *remote_get(const char *name)
719+
{
720+
read_config();
721+
return remote_get_1(name, NULL);
722+
}
723+
724+
struct remote *pushremote_get(const char *name)
725+
{
726+
read_config();
727+
return remote_get_1(name, pushremote_name);
728+
}
729+
703730
int remote_is_configured(const char *name)
704731
{
705732
int i;

remote.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ struct remote {
5151
};
5252

5353
struct remote *remote_get(const char *name);
54+
struct remote *pushremote_get(const char *name);
5455
int remote_is_configured(const char *name);
5556

5657
typedef int each_remote_fn(struct remote *remote, void *priv);

0 commit comments

Comments
 (0)