|
3 | 3 | #include "dir.h"
|
4 | 4 | #include "parse-options.h"
|
5 | 5 | #include "argv-array.h"
|
| 6 | +#include "branch.h" |
| 7 | +#include "refs.h" |
6 | 8 | #include "run-command.h"
|
7 | 9 | #include "sigchain.h"
|
8 | 10 |
|
@@ -188,11 +190,26 @@ static int add_worktree(const char *path, const char *refname,
|
188 | 190 | struct child_process cp;
|
189 | 191 | struct argv_array child_env = ARGV_ARRAY_INIT;
|
190 | 192 | int counter = 0, len, ret;
|
| 193 | + struct strbuf symref = STRBUF_INIT; |
| 194 | + struct commit *commit = NULL; |
191 | 195 | unsigned char rev[20];
|
192 | 196 |
|
193 | 197 | if (file_exists(path) && !is_empty_dir(path))
|
194 | 198 | die(_("'%s' already exists"), path);
|
195 | 199 |
|
| 200 | + /* is 'refname' a branch or commit? */ |
| 201 | + if (opts->force_new_branch) /* definitely a branch */ |
| 202 | + ; |
| 203 | + else if (!opts->detach && !strbuf_check_branch_ref(&symref, refname) && |
| 204 | + ref_exists(symref.buf)) { /* it's a branch */ |
| 205 | + if (!opts->force) |
| 206 | + die_if_checked_out(symref.buf); |
| 207 | + } else { /* must be a commit */ |
| 208 | + commit = lookup_commit_reference_by_name(refname); |
| 209 | + if (!commit) |
| 210 | + die(_("invalid reference: %s"), refname); |
| 211 | + } |
| 212 | + |
196 | 213 | name = worktree_basename(path, &len);
|
197 | 214 | strbuf_addstr(&sb_repo,
|
198 | 215 | git_path("worktrees/%.*s", (int)(path + len - name), name));
|
@@ -281,6 +298,7 @@ static int add_worktree(const char *path, const char *refname,
|
281 | 298 | unlink_or_warn(sb.buf);
|
282 | 299 | argv_array_clear(&child_env);
|
283 | 300 | strbuf_release(&sb);
|
| 301 | + strbuf_release(&symref); |
284 | 302 | strbuf_release(&sb_repo);
|
285 | 303 | strbuf_release(&sb_git);
|
286 | 304 | return ret;
|
|
0 commit comments