Linus Torvalds | 66bf85a | 2005-09-25 11:43:05 -0700 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "refs.h" |
Lukas Sandström | 854b462 | 2006-06-13 22:22:00 +0200 | [diff] [blame] | 3 | #include "builtin.h" |
Linus Torvalds | 66bf85a | 2005-09-25 11:43:05 -0700 | [diff] [blame] | 4 | |
Shawn Pearce | 5b16b09 | 2006-05-17 05:55:19 -0400 | [diff] [blame] | 5 | static const char git_update_ref_usage[] = |
Sven Verdoolaege | 68db31c | 2007-05-09 12:33:20 +0200 | [diff] [blame] | 6 | "git-update-ref [-m <reason>] (-d <refname> <value> | [--no-deref] <refname> <value> [<oldval>])"; |
Junio C Hamano | 152da3d | 2005-09-25 16:28:51 -0700 | [diff] [blame] | 7 | |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 8 | int cmd_update_ref(int argc, const char **argv, const char *prefix) |
Linus Torvalds | 66bf85a | 2005-09-25 11:43:05 -0700 | [diff] [blame] | 9 | { |
Shawn Pearce | 5b16b09 | 2006-05-17 05:55:19 -0400 | [diff] [blame] | 10 | const char *refname=NULL, *value=NULL, *oldval=NULL, *msg=NULL; |
| 11 | struct ref_lock *lock; |
| 12 | unsigned char sha1[20], oldsha1[20]; |
Sven Verdoolaege | 68db31c | 2007-05-09 12:33:20 +0200 | [diff] [blame] | 13 | int i, delete, ref_flags; |
Linus Torvalds | 66bf85a | 2005-09-25 11:43:05 -0700 | [diff] [blame] | 14 | |
Junio C Hamano | ac5409e | 2006-09-27 01:58:57 -0700 | [diff] [blame] | 15 | delete = 0; |
Sven Verdoolaege | 68db31c | 2007-05-09 12:33:20 +0200 | [diff] [blame] | 16 | ref_flags = 0; |
Junio C Hamano | 84a9b58 | 2006-03-23 23:41:18 -0800 | [diff] [blame] | 17 | git_config(git_default_config); |
Shawn Pearce | 5b16b09 | 2006-05-17 05:55:19 -0400 | [diff] [blame] | 18 | |
| 19 | for (i = 1; i < argc; i++) { |
| 20 | if (!strcmp("-m", argv[i])) { |
| 21 | if (i+1 >= argc) |
| 22 | usage(git_update_ref_usage); |
| 23 | msg = argv[++i]; |
| 24 | if (!*msg) |
| 25 | die("Refusing to perform update with empty message."); |
Shawn Pearce | 5b16b09 | 2006-05-17 05:55:19 -0400 | [diff] [blame] | 26 | continue; |
| 27 | } |
Junio C Hamano | ac5409e | 2006-09-27 01:58:57 -0700 | [diff] [blame] | 28 | if (!strcmp("-d", argv[i])) { |
| 29 | delete = 1; |
| 30 | continue; |
| 31 | } |
Sven Verdoolaege | 68db31c | 2007-05-09 12:33:20 +0200 | [diff] [blame] | 32 | if (!strcmp("--no-deref", argv[i])) { |
| 33 | ref_flags |= REF_NODEREF; |
| 34 | continue; |
| 35 | } |
Shawn Pearce | 5b16b09 | 2006-05-17 05:55:19 -0400 | [diff] [blame] | 36 | if (!refname) { |
| 37 | refname = argv[i]; |
| 38 | continue; |
| 39 | } |
| 40 | if (!value) { |
| 41 | value = argv[i]; |
| 42 | continue; |
| 43 | } |
| 44 | if (!oldval) { |
| 45 | oldval = argv[i]; |
| 46 | continue; |
| 47 | } |
| 48 | } |
| 49 | if (!refname || !value) |
Linus Torvalds | 66bf85a | 2005-09-25 11:43:05 -0700 | [diff] [blame] | 50 | usage(git_update_ref_usage); |
| 51 | |
Dmitry V. Levin | 31fff30 | 2006-05-09 01:43:38 +0400 | [diff] [blame] | 52 | if (get_sha1(value, sha1)) |
Linus Torvalds | 66bf85a | 2005-09-25 11:43:05 -0700 | [diff] [blame] | 53 | die("%s: not a valid SHA1", value); |
Junio C Hamano | ac5409e | 2006-09-27 01:58:57 -0700 | [diff] [blame] | 54 | |
| 55 | if (delete) { |
| 56 | if (oldval) |
| 57 | usage(git_update_ref_usage); |
| 58 | return delete_ref(refname, sha1); |
| 59 | } |
| 60 | |
Junio C Hamano | a8e0d16 | 2006-08-23 13:57:23 -0700 | [diff] [blame] | 61 | hashclr(oldsha1); |
Junio C Hamano | ac5409e | 2006-09-27 01:58:57 -0700 | [diff] [blame] | 62 | if (oldval && *oldval && get_sha1(oldval, oldsha1)) |
Linus Torvalds | 66bf85a | 2005-09-25 11:43:05 -0700 | [diff] [blame] | 63 | die("%s: not a valid old SHA1", oldval); |
| 64 | |
Sven Verdoolaege | 68db31c | 2007-05-09 12:33:20 +0200 | [diff] [blame] | 65 | lock = lock_any_ref_for_update(refname, oldval ? oldsha1 : NULL, ref_flags); |
Shawn Pearce | 5b16b09 | 2006-05-17 05:55:19 -0400 | [diff] [blame] | 66 | if (!lock) |
Junio C Hamano | a2f9fe9 | 2007-01-29 00:57:07 -0800 | [diff] [blame] | 67 | die("%s: cannot lock the ref", refname); |
Shawn Pearce | 5b16b09 | 2006-05-17 05:55:19 -0400 | [diff] [blame] | 68 | if (write_ref_sha1(lock, sha1, msg) < 0) |
Junio C Hamano | a2f9fe9 | 2007-01-29 00:57:07 -0800 | [diff] [blame] | 69 | die("%s: cannot update the ref", refname); |
Linus Torvalds | 66bf85a | 2005-09-25 11:43:05 -0700 | [diff] [blame] | 70 | return 0; |
| 71 | } |