blob: 71286b4fae4792dce73ab1533204e803e2a61334 [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[] = {
Nguyễn Thái Ngọc Duyb10bf3f2012-08-20 19:32:46 +07007 N_("git symbolic-ref [options] name [ref]"),
Johan Herland9ab55da2012-10-21 13:32:33 +02008 N_("git symbolic-ref -d [-q] name"),
Pierre Habouzit78558612007-10-07 23:18:23 +02009 NULL
10};
Junio C Hamano8098a172005-09-30 14:26:57 -070011
Johan Herland9ab55da2012-10-21 13:32:33 +020012static int check_symref(const char *HEAD, int quiet, int shorten, int print)
Junio C Hamano8098a172005-09-30 14:26:57 -070013{
14 unsigned char sha1[20];
Junio C Hamano8da19772006-09-20 22:02:01 -070015 int flag;
Jan Krüger42b00592012-02-27 23:10:38 +010016 const char *refname = resolve_ref_unsafe(HEAD, sha1, 0, &flag);
Linus Torvaldsed378ec2006-09-11 20:17:35 -070017
Jan Krüger42b00592012-02-27 23:10:38 +010018 if (!refname)
Junio C Hamano8098a172005-09-30 14:26:57 -070019 die("No such ref: %s", HEAD);
Junio C Hamanoa0f42802007-01-15 13:56:05 -080020 else if (!(flag & REF_ISSYMREF)) {
21 if (!quiet)
22 die("ref %s is not a symbolic ref", HEAD);
23 else
Johan Herland9ab55da2012-10-21 13:32:33 +020024 return 1;
Junio C Hamanoa0f42802007-01-15 13:56:05 -080025 }
Johan Herland9ab55da2012-10-21 13:32:33 +020026 if (print) {
27 if (shorten)
28 refname = shorten_unambiguous_ref(refname, 0);
29 puts(refname);
30 }
31 return 0;
Junio C Hamano8098a172005-09-30 14:26:57 -070032}
33
Matthias Kestenholz640ce102006-08-03 17:24:38 +020034int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
Junio C Hamano8098a172005-09-30 14:26:57 -070035{
Johan Herland9ab55da2012-10-21 13:32:33 +020036 int quiet = 0, delete = 0, shorten = 0, ret = 0;
Nicolas Pitre8b5157e2007-01-26 17:26:10 -050037 const char *msg = NULL;
Pierre Habouzit78558612007-10-07 23:18:23 +020038 struct option options[] = {
Jonathan Nieder8c839682010-11-08 13:54:48 -060039 OPT__QUIET(&quiet,
Nguyễn Thái Ngọc Duyb10bf3f2012-08-20 19:32:46 +070040 N_("suppress error message for non-symbolic (detached) refs")),
Johan Herland9ab55da2012-10-21 13:32:33 +020041 OPT_BOOL('d', "delete", &delete, N_("delete symbolic ref")),
Nguyễn Thái Ngọc Duyb10bf3f2012-08-20 19:32:46 +070042 OPT_BOOL(0, "short", &shorten, N_("shorten ref output")),
43 OPT_STRING('m', NULL, &msg, N_("reason"), N_("reason of the update")),
Pierre Habouzit78558612007-10-07 23:18:23 +020044 OPT_END(),
45 };
Junio C Hamanoa0f42802007-01-15 13:56:05 -080046
Johannes Schindelinef90d6d2008-05-14 18:46:53 +010047 git_config(git_default_config, NULL);
Stephen Boyd37782922009-05-23 11:53:12 -070048 argc = parse_options(argc, argv, prefix, options,
49 git_symbolic_ref_usage, 0);
Junio C Hamanoc01499e2013-10-16 10:26:39 -070050 if (msg && !*msg)
Pierre Habouzit78558612007-10-07 23:18:23 +020051 die("Refusing to perform update with empty message");
Johan Herland9ab55da2012-10-21 13:32:33 +020052
53 if (delete) {
54 if (argc != 1)
55 usage_with_options(git_symbolic_ref_usage, options);
56 ret = check_symref(argv[0], 1, 0, 0);
57 if (ret)
58 die("Cannot delete %s, not a symbolic ref", argv[0]);
59 return delete_ref(argv[0], NULL, REF_NODEREF);
60 }
61
Junio C Hamano8098a172005-09-30 14:26:57 -070062 switch (argc) {
Pierre Habouzit78558612007-10-07 23:18:23 +020063 case 1:
Johan Herland9ab55da2012-10-21 13:32:33 +020064 ret = check_symref(argv[0], quiet, shorten, 1);
Junio C Hamano8098a172005-09-30 14:26:57 -070065 break;
Pierre Habouzit78558612007-10-07 23:18:23 +020066 case 2:
Jeff Kingafe5d3d2009-01-29 03:33:02 -050067 if (!strcmp(argv[0], "HEAD") &&
Jeff Kinge9cc02f2009-02-13 13:26:09 -050068 prefixcmp(argv[1], "refs/"))
69 die("Refusing to point HEAD outside of refs/");
Pierre Habouzit78558612007-10-07 23:18:23 +020070 create_symref(argv[0], argv[1], msg);
Junio C Hamano8098a172005-09-30 14:26:57 -070071 break;
72 default:
Pierre Habouzit78558612007-10-07 23:18:23 +020073 usage_with_options(git_symbolic_ref_usage, options);
Junio C Hamano8098a172005-09-30 14:26:57 -070074 }
Johan Herland9ab55da2012-10-21 13:32:33 +020075 return ret;
Junio C Hamano8098a172005-09-30 14:26:57 -070076}