14 static int find_tracked_branch(struct remote
*remote
, void *priv
)
16 struct tracking
*tracking
= priv
;
18 if (!remote_find_tracking(remote
, &tracking
->spec
)) {
19 if (++tracking
->matches
== 1) {
20 tracking
->src
= tracking
->spec
.src
;
21 tracking
->remote
= remote
->name
;
23 free(tracking
->spec
.src
);
29 tracking
->spec
.src
= NULL
;
36 * This is called when new_ref is branched off of orig_ref, and tries
37 * to infer the settings for branch.<new_ref>.{remote,merge} from the
40 static int setup_tracking(const char *new_ref
, const char *orig_ref
)
43 struct tracking tracking
;
45 if (strlen(new_ref
) > 1024 - 7 - 7 - 1)
46 return error("Tracking not set up: name too long: %s",
49 memset(&tracking
, 0, sizeof(tracking
));
50 tracking
.spec
.dst
= (char *)orig_ref
;
51 if (for_each_remote(find_tracked_branch
, &tracking
) ||
55 if (tracking
.matches
> 1)
56 return error("Not tracking: ambiguous information for ref %s",
59 if (tracking
.matches
== 1) {
60 sprintf(key
, "branch.%s.remote", new_ref
);
61 git_config_set(key
, tracking
.remote
? tracking
.remote
: ".");
62 sprintf(key
, "branch.%s.merge", new_ref
);
63 git_config_set(key
, tracking
.src
);
65 printf("Branch %s set up to track remote branch %s.\n",
72 void create_branch(const char *head
,
73 const char *name
, const char *start_name
,
74 int force
, int reflog
, int track
)
76 struct ref_lock
*lock
;
77 struct commit
*commit
;
78 unsigned char sha1
[20];
79 char *real_ref
, ref
[PATH_MAX
], msg
[PATH_MAX
+ 20];
82 snprintf(ref
, sizeof ref
, "refs/heads/%s", name
);
83 if (check_ref_format(ref
))
84 die("'%s' is not a valid branch name.", name
);
86 if (resolve_ref(ref
, sha1
, 1, NULL
)) {
88 die("A branch named '%s' already exists.", name
);
89 else if (!is_bare_repository() && !strcmp(head
, name
))
90 die("Cannot force update the current branch.");
95 if (get_sha1(start_name
, sha1
))
96 die("Not a valid object name: '%s'.", start_name
);
98 switch (dwim_ref(start_name
, strlen(start_name
), sha1
, &real_ref
)) {
100 /* Not branching from any existing branch */
104 /* Unique completion -- good */
107 die("Ambiguous object name: '%s'.", start_name
);
111 if ((commit
= lookup_commit_reference(sha1
)) == NULL
)
112 die("Not a valid branch point: '%s'.", start_name
);
113 hashcpy(sha1
, commit
->object
.sha1
);
115 lock
= lock_any_ref_for_update(ref
, NULL
, 0);
117 die("Failed to lock ref for update: %s.", strerror(errno
));
120 log_all_ref_updates
= 1;
123 snprintf(msg
, sizeof msg
, "branch: Reset from %s",
126 snprintf(msg
, sizeof msg
, "branch: Created from %s",
129 /* When branching off a remote branch, set up so that git-pull
130 automatically merges from there. So far, this is only done for
131 remotes registered via .git/config. */
132 if (real_ref
&& track
)
133 setup_tracking(name
, real_ref
);
135 if (write_ref_sha1(lock
, sha1
, msg
) < 0)
136 die("Failed to write ref: %s.", strerror(errno
));
142 void remove_branch_state(void)
144 unlink(git_path("MERGE_HEAD"));
145 unlink(git_path("rr-cache/MERGE_RR"));
146 unlink(git_path("MERGE_MSG"));
147 unlink(git_path("SQUASH_MSG"));