]> git.ipfire.org Git - thirdparty/git.git/commitdiff
Allow tracking branches to set up rebase by default.
authorDustin Sallings <dustin@spy.net>
Sat, 10 May 2008 22:36:29 +0000 (15:36 -0700)
committerJunio C Hamano <gitster@pobox.com>
Sun, 11 May 2008 16:28:52 +0000 (09:28 -0700)
Change cd67e4d4 introduced a new configuration parameter that told
pull to automatically perform a rebase instead of a merge.  This
change provides a configuration option to enable this feature
automatically when creating a new branch.

If the variable branch.autosetuprebase applies for a branch that's
being created, that branch will have branch.<name>.rebase set to true.

Signed-off-by: Dustin Sallings <dustin@spy.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/config.txt
branch.c
cache.h
config.c
environment.c
t/t3200-branch.sh

index a6fc5a2cfd96bc4d26f42637721f5f9eb2f73e7e..217980f48d9d9ac71860d61313e9afa9c6b7c332 100644 (file)
@@ -399,6 +399,21 @@ branch.autosetupmerge::
        done when the starting point is either a local branch or remote
        branch. This option defaults to true.
 
+branch.autosetuprebase::
+       When a new branch is created with `git-branch` or `git-checkout`
+       that tracks another branch, this variable tells git to set
+       up pull to rebase instead of merge (see "branch.<name>.rebase").
+       When `never`, rebase is never automatically set to true.
+       When `local`, rebase is set to true for tracked branches of
+       other local branches.
+       When `remote`, rebase is set to true for tracked branches of
+       remote branches.
+       When `always`, rebase will be set to true for all tracking
+       branches.
+       See "branch.autosetupmerge" for details on how to set up a
+       branch to track another branch.
+       This option defaults to never.
+
 branch.<name>.remote::
        When in branch <name>, it tells `git fetch` which remote to fetch.
        If this option is not given, `git fetch` defaults to remote "origin".
index daf862e728622f9a59a2721e5cd297394be6c9da..56e949232cac1fe9dbe23aa93b50fe2f7c55c07d 100644 (file)
--- a/branch.c
+++ b/branch.c
@@ -32,6 +32,21 @@ static int find_tracked_branch(struct remote *remote, void *priv)
        return 0;
 }
 
+static int should_setup_rebase(const struct tracking *tracking)
+{
+       switch (autorebase) {
+       case AUTOREBASE_NEVER:
+               return 0;
+       case AUTOREBASE_LOCAL:
+               return tracking->remote == NULL;
+       case AUTOREBASE_REMOTE:
+               return tracking->remote != NULL;
+       case AUTOREBASE_ALWAYS:
+               return 1;
+       }
+       return 0;
+}
+
 /*
  * This is called when new_ref is branched off of orig_ref, and tries
  * to infer the settings for branch.<new_ref>.{remote,merge} from the
@@ -69,9 +84,14 @@ static int setup_tracking(const char *new_ref, const char *orig_ref,
        git_config_set(key, tracking.remote ?  tracking.remote : ".");
        sprintf(key, "branch.%s.merge", new_ref);
        git_config_set(key, tracking.src ? tracking.src : orig_ref);
-       free(tracking.src);
        printf("Branch %s set up to track %s branch %s.\n", new_ref,
                tracking.remote ? "remote" : "local", orig_ref);
+       if (should_setup_rebase(&tracking)) {
+               sprintf(key, "branch.%s.rebase", new_ref);
+               git_config_set(key, "true");
+               printf("This branch will rebase on pull.\n");
+       }
+       free(tracking.src);
 
        return 0;
 }
diff --git a/cache.h b/cache.h
index 7fb8f3359dddbb8f93aa827947c3e5d9de04f639..3442130a59fb7c41e16cab1cb8cd20bec5f71a3d 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -434,7 +434,15 @@ enum branch_track {
        BRANCH_TRACK_EXPLICIT,
 };
 
+enum rebase_setup_type {
+       AUTOREBASE_NEVER = 0,
+       AUTOREBASE_LOCAL,
+       AUTOREBASE_REMOTE,
+       AUTOREBASE_ALWAYS,
+};
+
 extern enum branch_track git_branch_track;
+extern enum rebase_setup_type autorebase;
 
 #define GIT_REPO_VERSION 0
 extern int repository_format_version;
index b0ada515b9d839fc8691bc9af320353ff323b251..cf2bfd35c8394579beed8fa565bfcfe33dbd7540 100644 (file)
--- a/config.c
+++ b/config.c
@@ -487,6 +487,21 @@ int git_default_config(const char *var, const char *value)
                git_branch_track = git_config_bool(var, value);
                return 0;
        }
+       if (!strcmp(var, "branch.autosetuprebase")) {
+               if (!value)
+                       return config_error_nonbool(var);
+               else if (!strcmp(value, "never"))
+                       autorebase = AUTOREBASE_NEVER;
+               else if (!strcmp(value, "local"))
+                       autorebase = AUTOREBASE_LOCAL;
+               else if (!strcmp(value, "remote"))
+                       autorebase = AUTOREBASE_REMOTE;
+               else if (!strcmp(value, "always"))
+                       autorebase = AUTOREBASE_ALWAYS;
+               else
+                       return error("Malformed value for %s", var);
+               return 0;
+       }
 
        /* Add other config variables here and to Documentation/config.txt. */
        return 0;
index fcd1ee5ef8c0999fce0095a2d6a792e5d221c50a..4892a302bd77c038a79a11935e21d556ede494cb 100644 (file)
@@ -38,6 +38,7 @@ int auto_crlf = 0;    /* 1: both ways, -1: only when adding git objects */
 enum safe_crlf safe_crlf = SAFE_CRLF_WARN;
 unsigned whitespace_rule_cfg = WS_DEFAULT_RULE;
 enum branch_track git_branch_track = BRANCH_TRACK_REMOTE;
+enum rebase_setup_type autorebase = AUTOREBASE_NEVER;
 
 /* This is set by setup_git_dir_gently() and/or git_default_config() */
 char *git_work_tree_cfg;
index cb5f7a444175938c8a9cc4c029b41b7325b91618..8d8768688d8dde3637a50ece28fb8720b7612ddd 100755 (executable)
@@ -224,4 +224,238 @@ test_expect_success 'avoid ambiguous track' '
        test -z "$(git config branch.all1.merge)"
 '
 
+test_expect_success 'autosetuprebase local on a tracked local branch' '
+       git config remote.local.url . &&
+       git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+       git config branch.autosetuprebase local &&
+       (git show-ref -q refs/remotes/local/o || git-fetch local) &&
+       git branch mybase &&
+       git branch --track myr1 mybase &&
+       test "$(git config branch.myr1.remote)" = . &&
+       test "$(git config branch.myr1.merge)" = refs/heads/mybase &&
+       test "$(git config branch.myr1.rebase)" = true
+'
+
+test_expect_success 'autosetuprebase always on a tracked local branch' '
+       git config remote.local.url . &&
+       git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+       git config branch.autosetuprebase always &&
+       (git show-ref -q refs/remotes/local/o || git-fetch local) &&
+       git branch mybase2 &&
+       git branch --track myr2 mybase &&
+       test "$(git config branch.myr2.remote)" = . &&
+       test "$(git config branch.myr2.merge)" = refs/heads/mybase &&
+       test "$(git config branch.myr2.rebase)" = true
+'
+
+test_expect_success 'autosetuprebase remote on a tracked local branch' '
+       git config remote.local.url . &&
+       git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+       git config branch.autosetuprebase remote &&
+       (git show-ref -q refs/remotes/local/o || git-fetch local) &&
+       git branch mybase3 &&
+       git branch --track myr3 mybase2 &&
+       test "$(git config branch.myr3.remote)" = . &&
+       test "$(git config branch.myr3.merge)" = refs/heads/mybase2 &&
+       ! test "$(git config branch.myr3.rebase)" = true
+'
+
+test_expect_success 'autosetuprebase never on a tracked local branch' '
+       git config remote.local.url . &&
+       git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+       git config branch.autosetuprebase never &&
+       (git show-ref -q refs/remotes/local/o || git-fetch local) &&
+       git branch mybase4 &&
+       git branch --track myr4 mybase2 &&
+       test "$(git config branch.myr4.remote)" = . &&
+       test "$(git config branch.myr4.merge)" = refs/heads/mybase2 &&
+       ! test "$(git config branch.myr4.rebase)" = true
+'
+
+test_expect_success 'autosetuprebase local on a tracked remote branch' '
+       git config remote.local.url . &&
+       git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+       git config branch.autosetuprebase local &&
+       (git show-ref -q refs/remotes/local/master || git-fetch local) &&
+       git branch --track myr5 local/master &&
+       test "$(git config branch.myr5.remote)" = local &&
+       test "$(git config branch.myr5.merge)" = refs/heads/master &&
+       ! test "$(git config branch.myr5.rebase)" = true
+'
+
+test_expect_success 'autosetuprebase never on a tracked remote branch' '
+       git config remote.local.url . &&
+       git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+       git config branch.autosetuprebase never &&
+       (git show-ref -q refs/remotes/local/master || git-fetch local) &&
+       git branch --track myr6 local/master &&
+       test "$(git config branch.myr6.remote)" = local &&
+       test "$(git config branch.myr6.merge)" = refs/heads/master &&
+       ! test "$(git config branch.myr6.rebase)" = true
+'
+
+test_expect_success 'autosetuprebase remote on a tracked remote branch' '
+       git config remote.local.url . &&
+       git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+       git config branch.autosetuprebase remote &&
+       (git show-ref -q refs/remotes/local/master || git-fetch local) &&
+       git branch --track myr7 local/master &&
+       test "$(git config branch.myr7.remote)" = local &&
+       test "$(git config branch.myr7.merge)" = refs/heads/master &&
+       test "$(git config branch.myr7.rebase)" = true
+'
+
+test_expect_success 'autosetuprebase always on a tracked remote branch' '
+       git config remote.local.url . &&
+       git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+       git config branch.autosetuprebase remote &&
+       (git show-ref -q refs/remotes/local/master || git-fetch local) &&
+       git branch --track myr8 local/master &&
+       test "$(git config branch.myr8.remote)" = local &&
+       test "$(git config branch.myr8.merge)" = refs/heads/master &&
+       test "$(git config branch.myr8.rebase)" = true
+'
+
+test_expect_success 'autosetuprebase unconfigured on a tracked remote branch' '
+       git config --unset branch.autosetuprebase &&
+       git config remote.local.url . &&
+       git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+       (git show-ref -q refs/remotes/local/master || git-fetch local) &&
+       git branch --track myr9 local/master &&
+       test "$(git config branch.myr9.remote)" = local &&
+       test "$(git config branch.myr9.merge)" = refs/heads/master &&
+       test "z$(git config branch.myr9.rebase)" = z
+'
+
+test_expect_success 'autosetuprebase unconfigured on a tracked local branch' '
+       git config remote.local.url . &&
+       git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+       (git show-ref -q refs/remotes/local/o || git-fetch local) &&
+       git branch mybase10 &&
+       git branch --track myr10 mybase2 &&
+       test "$(git config branch.myr10.remote)" = . &&
+       test "$(git config branch.myr10.merge)" = refs/heads/mybase2 &&
+       test "z$(git config branch.myr10.rebase)" = z
+'
+
+test_expect_success 'autosetuprebase unconfigured on untracked local branch' '
+       git config remote.local.url . &&
+       git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+       (git show-ref -q refs/remotes/local/master || git-fetch local) &&
+       git branch --no-track myr11 mybase2 &&
+       test "z$(git config branch.myr11.remote)" = z &&
+       test "z$(git config branch.myr11.merge)" = z &&
+       test "z$(git config branch.myr11.rebase)" = z
+'
+
+test_expect_success 'autosetuprebase unconfigured on untracked remote branch' '
+       git config remote.local.url . &&
+       git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+       (git show-ref -q refs/remotes/local/master || git-fetch local) &&
+       git branch --no-track myr12 local/master &&
+       test "z$(git config branch.myr12.remote)" = z &&
+       test "z$(git config branch.myr12.merge)" = z &&
+       test "z$(git config branch.myr12.rebase)" = z
+'
+
+test_expect_success 'autosetuprebase never on an untracked local branch' '
+       git config branch.autosetuprebase never &&
+       git config remote.local.url . &&
+       git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+       (git show-ref -q refs/remotes/local/master || git-fetch local) &&
+       git branch --no-track myr13 mybase2 &&
+       test "z$(git config branch.myr13.remote)" = z &&
+       test "z$(git config branch.myr13.merge)" = z &&
+       test "z$(git config branch.myr13.rebase)" = z
+'
+
+test_expect_success 'autosetuprebase local on an untracked local branch' '
+       git config branch.autosetuprebase local &&
+       git config remote.local.url . &&
+       git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+       (git show-ref -q refs/remotes/local/master || git-fetch local) &&
+       git branch --no-track myr14 mybase2 &&
+       test "z$(git config branch.myr14.remote)" = z &&
+       test "z$(git config branch.myr14.merge)" = z &&
+       test "z$(git config branch.myr14.rebase)" = z
+'
+
+test_expect_success 'autosetuprebase remote on an untracked local branch' '
+       git config branch.autosetuprebase remote &&
+       git config remote.local.url . &&
+       git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+       (git show-ref -q refs/remotes/local/master || git-fetch local) &&
+       git branch --no-track myr15 mybase2 &&
+       test "z$(git config branch.myr15.remote)" = z &&
+       test "z$(git config branch.myr15.merge)" = z &&
+       test "z$(git config branch.myr15.rebase)" = z
+'
+
+test_expect_success 'autosetuprebase always on an untracked local branch' '
+       git config branch.autosetuprebase always &&
+       git config remote.local.url . &&
+       git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+       (git show-ref -q refs/remotes/local/master || git-fetch local) &&
+       git branch --no-track myr16 mybase2 &&
+       test "z$(git config branch.myr16.remote)" = z &&
+       test "z$(git config branch.myr16.merge)" = z &&
+       test "z$(git config branch.myr16.rebase)" = z
+'
+
+test_expect_success 'autosetuprebase never on an untracked remote branch' '
+       git config branch.autosetuprebase never &&
+       git config remote.local.url . &&
+       git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+       (git show-ref -q refs/remotes/local/master || git-fetch local) &&
+       git branch --no-track myr17 local/master &&
+       test "z$(git config branch.myr17.remote)" = z &&
+       test "z$(git config branch.myr17.merge)" = z &&
+       test "z$(git config branch.myr17.rebase)" = z
+'
+
+test_expect_success 'autosetuprebase local on an untracked remote branch' '
+       git config branch.autosetuprebase local &&
+       git config remote.local.url . &&
+       git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+       (git show-ref -q refs/remotes/local/master || git-fetch local) &&
+       git branch --no-track myr18 local/master &&
+       test "z$(git config branch.myr18.remote)" = z &&
+       test "z$(git config branch.myr18.merge)" = z &&
+       test "z$(git config branch.myr18.rebase)" = z
+'
+
+test_expect_success 'autosetuprebase remote on an untracked remote branch' '
+       git config branch.autosetuprebase remote &&
+       git config remote.local.url . &&
+       git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+       (git show-ref -q refs/remotes/local/master || git-fetch local) &&
+       git branch --no-track myr19 local/master &&
+       test "z$(git config branch.myr19.remote)" = z &&
+       test "z$(git config branch.myr19.merge)" = z &&
+       test "z$(git config branch.myr19.rebase)" = z
+'
+
+test_expect_success 'autosetuprebase always on an untracked remote branch' '
+       git config branch.autosetuprebase always &&
+       git config remote.local.url . &&
+       git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+       (git show-ref -q refs/remotes/local/master || git-fetch local) &&
+       git branch --no-track myr20 local/master &&
+       test "z$(git config branch.myr20.remote)" = z &&
+       test "z$(git config branch.myr20.merge)" = z &&
+       test "z$(git config branch.myr20.rebase)" = z
+'
+
+test_expect_success 'detect misconfigured autosetuprebase (bad value)' '
+       git config branch.autosetuprebase garbage &&
+       test_must_fail git branch
+'
+
+test_expect_success 'detect misconfigured autosetuprebase (no value)' '
+       git config --unset branch.autosetuprebase &&
+       echo "[branch] autosetuprebase" >> .git/config &&
+       test_must_fail git branch &&
+       git config --unset branch.autosetuprebase
+'
+
 test_done