@matheusbsbs / matheus-bagliano / commits / a9f2c13685
Fork 0

Make git-clone respect branch.autosetuprebase

When git-clone creates an initial branch it was not checking the branch.autosetuprebase configuration option (which may exist in ~/.gitconfig). Refactor the code used by "git branch" to create a new branch, and use it instead of the insufficiently duplicated code in builtin-clone. Changes are partly, and the test is mostly, based on the previous work by Pat Notz. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Mar 3, 2009 at 22:29 UTC a9f2c13685ae9040d52d53cd719a18040f1dd123
4 files changed +58 -31
branch.c
+33 -16
@@ -32,21 +32,48 @@ static int find_tracked_branch(struct remote *remote, void *priv)
32 return 0;
33 }
34
35 -static int should_setup_rebase(const struct tracking *tracking)
35 +static int should_setup_rebase(const char *origin)
36 {
37 switch (autorebase) {
38 case AUTOREBASE_NEVER:
39 return 0;
40 case AUTOREBASE_LOCAL:
41 - return tracking->remote == NULL;
41 + return origin == NULL;
42 case AUTOREBASE_REMOTE:
43 - return tracking->remote != NULL;
43 + return origin != NULL;
44 case AUTOREBASE_ALWAYS:
45 return 1;
46 }
47 return 0;
48 }
49
50 +void install_branch_config(int flag, const char *local, const char *origin, const char *remote)
51 +{
52 + struct strbuf key = STRBUF_INIT;
53 + int rebasing = should_setup_rebase(origin);
54 +
55 + strbuf_addf(&key, "branch.%s.remote", local);
56 + git_config_set(key.buf, origin ? origin : ".");
57 +
58 + strbuf_reset(&key);
59 + strbuf_addf(&key, "branch.%s.merge", local);
60 + git_config_set(key.buf, remote);
61 +
62 + if (rebasing) {
63 + strbuf_reset(&key);
64 + strbuf_addf(&key, "branch.%s.rebase", local);
65 + git_config_set(key.buf, "true");
66 + }
67 +
68 + if (flag & BRANCH_CONFIG_VERBOSE)
69 + printf("Branch %s set up to track %s branch %s %s.\n",
70 + local,
71 + origin ? "remote" : "local",
72 + remote,
73 + rebasing ? "by rebasing" : "by merging");
74 + strbuf_release(&key);
75 +}
76 +
77 /*
78 * This is called when new_ref is branched off of orig_ref, and tries
79 * to infer the settings for branch.<new_ref>.{remote,merge} from the
@@ -55,7 +82,6 @@ static int should_setup_rebase(const struct tracking *tracking)
82 static int setup_tracking(const char *new_ref, const char *orig_ref,
83 enum branch_track track)
84 {
58 - char key[1024];
85 struct tracking tracking;
86
87 if (strlen(new_ref) > 1024 - 7 - 7 - 1)
@@ -80,19 +106,10 @@ static int setup_tracking(const char *new_ref, const char *orig_ref,
106 return error("Not tracking: ambiguous information for ref %s",
107 orig_ref);
108
83 - sprintf(key, "branch.%s.remote", new_ref);
84 - git_config_set(key, tracking.remote ? tracking.remote : ".");
85 - sprintf(key, "branch.%s.merge", new_ref);
86 - git_config_set(key, tracking.src ? tracking.src : orig_ref);
87 - printf("Branch %s set up to track %s branch %s.\n", new_ref,
88 - tracking.remote ? "remote" : "local", orig_ref);
89 - if (should_setup_rebase(&tracking)) {
90 - sprintf(key, "branch.%s.rebase", new_ref);
91 - git_config_set(key, "true");
92 - printf("This branch will rebase on pull.\n");
93 - }
94 - free(tracking.src);
109 + install_branch_config(BRANCH_CONFIG_VERBOSE, new_ref, tracking.remote,
110 + tracking.src ? tracking.src : orig_ref);
111
112 + free(tracking.src);
113 return 0;
114 }
115
branch.h
+7
@@ -21,4 +21,11 @@ void create_branch(const char *head, const char *name, const char *start_name,
21 */
22 void remove_branch_state(void);
23
24 +/*
25 + * Configure local branch "local" to merge remote branch "remote"
26 + * taken from origin "origin".
27 + */
28 +#define BRANCH_CONFIG_VERBOSE 01
29 +extern void install_branch_config(int flag, const char *local, const char *origin, const char *remote);
30 +
31 #endif
builtin-clone.c
+3 -15
@@ -20,6 +20,7 @@
20 #include "dir.h"
21 #include "pack-refs.h"
22 #include "sigchain.h"
23 +#include "branch.h"
24
25 /*
26 * Overall FIXMEs:
@@ -350,19 +351,6 @@ static struct ref *write_remote_refs(const struct ref *refs,
351 return local_refs;
352 }
353
353 -static void install_branch_config(const char *local,
354 - const char *origin,
355 - const char *remote)
356 -{
357 - struct strbuf key = STRBUF_INIT;
358 - strbuf_addf(&key, "branch.%s.remote", local);
359 - git_config_set(key.buf, origin);
360 - strbuf_reset(&key);
361 - strbuf_addf(&key, "branch.%s.merge", local);
362 - git_config_set(key.buf, remote);
363 - strbuf_release(&key);
364 -}
365 -
354 int cmd_clone(int argc, const char **argv, const char *prefix)
355 {
356 int use_local_hardlinks = 1;
@@ -553,7 +541,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
541 remote_head = NULL;
542 option_no_checkout = 1;
543 if (!option_bare)
556 - install_branch_config("master", option_origin,
544 + install_branch_config(0, "master", option_origin,
545 "refs/heads/master");
546 }
547
@@ -583,7 +571,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
571 head_points_at->peer_ref->name,
572 reflog_msg.buf);
573
586 - install_branch_config(head, option_origin,
574 + install_branch_config(0, head, option_origin,
575 head_points_at->name);
576 }
577 } else if (remote_head) {
t/t5601-clone.sh
+15
@@ -159,4 +159,19 @@ test_expect_success 'clone a void' '
159 test_cmp target-6/.git/config target-7/.git/config
160 '
161
162 +test_expect_success 'clone respects global branch.autosetuprebase' '
163 + (
164 + HOME=$(pwd) &&
165 + export HOME &&
166 + test_config="$HOME/.gitconfig" &&
167 + unset GIT_CONFIG_NOGLOBAL &&
168 + git config -f "$test_config" branch.autosetuprebase remote &&
169 + rm -fr dst &&
170 + git clone src dst &&
171 + cd dst &&
172 + actual="z$(git config branch.master.rebase)" &&
173 + test ztrue = $actual
174 + )
175 +'
176 +
177 test_done