blob: bfc78bb3f6eff2f8e39649b9649ae7263f143ad9 [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[] = {
33 OPT__QUIET(&quiet),
34 OPT_STRING('m', NULL, &msg, "reason", "reason of the update"),
35 OPT_END(),
36 };
Junio C Hamanoa0f42802007-01-15 13:56:05 -080037
Johannes Schindelinef90d6d2008-05-14 18:46:53 +010038 git_config(git_default_config, NULL);
Pierre Habouzit78558612007-10-07 23:18:23 +020039 argc = parse_options(argc, argv, options, git_symbolic_ref_usage, 0);
40 if (msg &&!*msg)
41 die("Refusing to perform update with empty message");
Junio C Hamano8098a172005-09-30 14:26:57 -070042 switch (argc) {
Pierre Habouzit78558612007-10-07 23:18:23 +020043 case 1:
44 check_symref(argv[0], quiet);
Junio C Hamano8098a172005-09-30 14:26:57 -070045 break;
Pierre Habouzit78558612007-10-07 23:18:23 +020046 case 2:
47 create_symref(argv[0], argv[1], msg);
Junio C Hamano8098a172005-09-30 14:26:57 -070048 break;
49 default:
Pierre Habouzit78558612007-10-07 23:18:23 +020050 usage_with_options(git_symbolic_ref_usage, options);
Junio C Hamano8098a172005-09-30 14:26:57 -070051 }
52 return 0;
53}