blob: dea849c3c5ec0c0a7ee0ac98e1ae62a6dacf5806 [file] [log] [blame]
Matthias Kestenholz640ce102006-08-03 17:24:38 +02001#include "builtin.h"
Junio C Hamano8098a172005-09-30 14:26:57 -07002#include "cache.h"
Junio C Hamano8da19772006-09-20 22:02:01 -07003#include "refs.h"
Pierre Habouzit78558612007-10-07 23:18:23 +02004#include "parse-options.h"
Junio C Hamano8098a172005-09-30 14:26:57 -07005
Pierre Habouzit78558612007-10-07 23:18:23 +02006static const char * const git_symbolic_ref_usage[] = {
Stephan Beyer1b1dd232008-07-13 15:36:15 +02007 "git symbolic-ref [options] name [ref]",
Pierre Habouzit78558612007-10-07 23:18:23 +02008 NULL
9};
Junio C Hamano8098a172005-09-30 14:26:57 -070010
Junio C Hamanoa0f42802007-01-15 13:56:05 -080011static void check_symref(const char *HEAD, int quiet)
Junio C Hamano8098a172005-09-30 14:26:57 -070012{
13 unsigned char sha1[20];
Junio C Hamano8da19772006-09-20 22:02:01 -070014 int flag;
15 const char *refs_heads_master = resolve_ref(HEAD, sha1, 0, &flag);
Linus Torvaldsed378ec2006-09-11 20:17:35 -070016
17 if (!refs_heads_master)
Junio C Hamano8098a172005-09-30 14:26:57 -070018 die("No such ref: %s", HEAD);
Junio C Hamanoa0f42802007-01-15 13:56:05 -080019 else if (!(flag & REF_ISSYMREF)) {
20 if (!quiet)
21 die("ref %s is not a symbolic ref", HEAD);
22 else
23 exit(1);
24 }
Linus Torvaldsed378ec2006-09-11 20:17:35 -070025 puts(refs_heads_master);
Junio C Hamano8098a172005-09-30 14:26:57 -070026}
27
Matthias Kestenholz640ce102006-08-03 17:24:38 +020028int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
Junio C Hamano8098a172005-09-30 14:26:57 -070029{
Junio C Hamanoa0f42802007-01-15 13:56:05 -080030 int quiet = 0;
Nicolas Pitre8b5157e2007-01-26 17:26:10 -050031 const char *msg = NULL;
Pierre Habouzit78558612007-10-07 23:18:23 +020032 struct option options[] = {
Jonathan Nieder8c839682010-11-08 13:54:48 -060033 OPT__QUIET(&quiet,
34 "suppress error message for non-symbolic (detached) refs"),
Pierre Habouzit78558612007-10-07 23:18:23 +020035 OPT_STRING('m', NULL, &msg, "reason", "reason of the update"),
36 OPT_END(),
37 };
Junio C Hamanoa0f42802007-01-15 13:56:05 -080038
Johannes Schindelinef90d6d2008-05-14 18:46:53 +010039 git_config(git_default_config, NULL);
Stephen Boyd37782922009-05-23 11:53:12 -070040 argc = parse_options(argc, argv, prefix, options,
41 git_symbolic_ref_usage, 0);
Pierre Habouzit78558612007-10-07 23:18:23 +020042 if (msg &&!*msg)
43 die("Refusing to perform update with empty message");
Junio C Hamano8098a172005-09-30 14:26:57 -070044 switch (argc) {
Pierre Habouzit78558612007-10-07 23:18:23 +020045 case 1:
46 check_symref(argv[0], quiet);
Junio C Hamano8098a172005-09-30 14:26:57 -070047 break;
Pierre Habouzit78558612007-10-07 23:18:23 +020048 case 2:
Jeff Kingafe5d3d2009-01-29 03:33:02 -050049 if (!strcmp(argv[0], "HEAD") &&
Jeff Kinge9cc02f2009-02-13 13:26:09 -050050 prefixcmp(argv[1], "refs/"))
51 die("Refusing to point HEAD outside of refs/");
Pierre Habouzit78558612007-10-07 23:18:23 +020052 create_symref(argv[0], argv[1], msg);
Junio C Hamano8098a172005-09-30 14:26:57 -070053 break;
54 default:
Pierre Habouzit78558612007-10-07 23:18:23 +020055 usage_with_options(git_symbolic_ref_usage, options);
Junio C Hamano8098a172005-09-30 14:26:57 -070056 }
57 return 0;
58}