return 0;
}
-static void set_branch_defaults(const char *name, const char *real_ref)
+static void set_branch_merge(const char *name, const char *config_remote,
+ const char *config_repo)
{
char key[1024];
+ if (sizeof(key) <=
+ snprintf(key, sizeof(key), "branch.%s.remote", name))
+ die("what a long branch name you have!");
+ git_config_set(key, config_remote);
+
+ /*
+ * We do not have to check if we have enough space for
+ * the 'merge' key, since it's shorter than the
+ * previous 'remote' key, which we already checked.
+ */
+ snprintf(key, sizeof(key), "branch.%s.merge", name);
+ git_config_set(key, config_repo);
+}
+
+static void set_branch_defaults(const char *name, const char *real_ref)
+{
const char *slash = strrchr(real_ref, '/');
if (!slash)
start_len = strlen(real_ref);
base_len = slash - real_ref;
git_config(get_remote_config);
+ if (!config_repo && !config_remote &&
+ !prefixcmp(real_ref, "refs/heads/")) {
+ set_branch_merge(name, ".", real_ref);
+ printf("Branch %s set up to track local branch %s.\n",
+ name, real_ref);
+ }
if (config_repo && config_remote) {
- if (sizeof(key) <=
- snprintf(key, sizeof(key), "branch.%s.remote", name))
- die("what a long branch name you have!");
- git_config_set(key, config_remote);
-
- /*
- * We do not have to check if we have enough space for
- * the 'merge' key, since it's shorter than the
- * previous 'remote' key, which we already checked.
- */
- snprintf(key, sizeof(key), "branch.%s.merge", name);
- git_config_set(key, config_repo);
-
+ set_branch_merge(name, config_remote, config_repo);
printf("Branch %s set up to track remote branch %s.\n",
name, real_ref);
}
}
static void create_branch(const char *name, const char *start_name,
- unsigned char *start_sha1,
int force, int reflog, int track)
{
struct ref_lock *lock;
forcing = 1;
}
- if (start_sha1) {
- /* detached HEAD */
- hashcpy(sha1, start_sha1);
+ real_ref = NULL;
+ if (get_sha1(start_name, sha1))
+ die("Not a valid object name: '%s'.", start_name);
+
+ switch (dwim_ref(start_name, strlen(start_name), sha1, &real_ref)) {
+ case 0:
+ /* Not branching from any existing branch */
real_ref = NULL;
- }
- else if (dwim_ref(start_name, strlen(start_name), sha1, &real_ref) > 1)
+ break;
+ case 1:
+ /* Unique completion -- good */
+ break;
+ default:
die("Ambiguous object name: '%s'.", start_name);
- else if (real_ref == NULL)
- die("Not a valid object name: '%s'.", start_name);
+ break;
+ }
if ((commit = lookup_commit_reference(sha1)) == NULL)
die("Not a valid branch point: '%s'.", start_name);
{
char oldref[PATH_MAX], newref[PATH_MAX], logmsg[PATH_MAX*2 + 100];
unsigned char sha1[20];
+ char oldsection[PATH_MAX], newsection[PATH_MAX];
if (!oldname)
die("cannot rename the current branch while not on any.");
/* no need to pass logmsg here as HEAD didn't really move */
if (!strcmp(oldname, head) && create_symref("HEAD", newref, NULL))
die("Branch renamed to %s, but HEAD is not updated!", newname);
+
+ snprintf(oldsection, sizeof(oldsection), "branch.%s", oldref + 11);
+ snprintf(newsection, sizeof(newsection), "branch.%s", newref + 11);
+ if (git_config_rename_section(oldsection, newsection) < 0)
+ die("Branch is renamed, but update of config-file failed");
}
int cmd_branch(int argc, const char **argv, const char *prefix)
rename_branch(head, argv[i], force_rename);
else if (rename && (i == argc - 2))
rename_branch(argv[i], argv[i + 1], force_rename);
- else if (i == argc - 1)
- create_branch(argv[i], head, head_sha1, force_create, reflog,
- track);
- else if (i == argc - 2)
- create_branch(argv[i], argv[i+1], NULL, force_create, reflog,
- track);
+ else if (i == argc - 1 || i == argc - 2)
+ create_branch(argv[i], (i == argc - 2) ? argv[i+1] : head,
+ force_create, reflog, track);
else
usage(builtin_branch_usage);