3
#include "builtin.h"
4
#include "config.h"
5
#include "gettext.h"
6
+#include "hex.h"
7
+#include "object-store.h"
8
#include "revision.h"
9
#include "reachable.h"
10
#include "wildmatch.h"
22
#define BUILTIN_REFLOG_EXISTS_USAGE \
23
N_("git reflog exists <ref>")
24
25
+#define BUILTIN_REFLOG_WRITE_USAGE \
26
+ N_("git reflog write <ref> <old-oid> <new-oid> <message>")
27
+
28
#define BUILTIN_REFLOG_DELETE_USAGE \
29
N_("git reflog delete [--rewrite] [--updateref]\n" \
30
" [--dry-run | -n] [--verbose] <ref>@{<specifier>}...")
52
NULL,
53
};
54
55
+static const char *const reflog_write_usage[] = {
56
+ BUILTIN_REFLOG_WRITE_USAGE,
57
+ NULL,
58
+};
59
+
60
static const char *const reflog_delete_usage[] = {
61
BUILTIN_REFLOG_DELETE_USAGE,
62
NULL
76
BUILTIN_REFLOG_SHOW_USAGE,
77
BUILTIN_REFLOG_LIST_USAGE,
78
BUILTIN_REFLOG_EXISTS_USAGE,
79
+ BUILTIN_REFLOG_WRITE_USAGE,
80
BUILTIN_REFLOG_DELETE_USAGE,
81
BUILTIN_REFLOG_DROP_USAGE,
82
BUILTIN_REFLOG_EXPIRE_USAGE,
403
return ret;
404
}
405
406
+static int cmd_reflog_write(int argc, const char **argv, const char *prefix,
407
+ struct repository *repo)
408
+{
409
+ const struct option options[] = {
410
+ OPT_END()
411
+ };
412
+ struct object_id old_oid, new_oid;
413
+ struct strbuf err = STRBUF_INIT;
414
+ struct ref_transaction *tx;
415
+ const char *ref, *message;
416
+ int ret;
417
+
418
+ argc = parse_options(argc, argv, prefix, options, reflog_write_usage, 0);
419
+ if (argc != 4)
420
+ usage_with_options(reflog_write_usage, options);
421
+
422
+ ref = argv[0];
423
+ if (!is_root_ref(ref) && check_refname_format(ref, 0))
424
+ die(_("invalid reference name: %s"), ref);
425
+
426
+ ret = get_oid_hex_algop(argv[1], &old_oid, repo->hash_algo);
427
+ if (ret)
428
+ die(_("invalid old object ID: '%s'"), argv[1]);
429
+ if (!is_null_oid(&old_oid) && !has_object(the_repository, &old_oid, 0))
430
+ die(_("old object '%s' does not exist"), argv[1]);
431
+
432
+ ret = get_oid_hex_algop(argv[2], &new_oid, repo->hash_algo);
433
+ if (ret)
434
+ die(_("invalid new object ID: '%s'"), argv[2]);
435
+ if (!is_null_oid(&new_oid) && !has_object(the_repository, &new_oid, 0))
436
+ die(_("new object '%s' does not exist"), argv[2]);
437
+
438
+ message = argv[3];
439
+
440
+ tx = ref_store_transaction_begin(get_main_ref_store(repo), 0, &err);
441
+ if (!tx)
442
+ die(_("cannot start transaction: %s"), err.buf);
443
+
444
+ ret = ref_transaction_update_reflog(tx, ref, &new_oid, &old_oid,
445
+ git_committer_info(0),
446
+ message, 0, &err);
447
+ if (ret)
448
+ die(_("cannot queue reflog update: %s"), err.buf);
449
+
450
+ ret = ref_transaction_commit(tx, &err);
451
+ if (ret)
452
+ die(_("cannot commit reflog update: %s"), err.buf);
453
+
454
+ ref_transaction_free(tx);
455
+ strbuf_release(&err);
456
+ return 0;
457
+}
458
+
459
/*
460
* main "reflog"
461
*/
469
OPT_SUBCOMMAND("show", &fn, cmd_reflog_show),
470
OPT_SUBCOMMAND("list", &fn, cmd_reflog_list),
471
OPT_SUBCOMMAND("exists", &fn, cmd_reflog_exists),
472
+ OPT_SUBCOMMAND("write", &fn, cmd_reflog_write),
473
OPT_SUBCOMMAND("delete", &fn, cmd_reflog_delete),
474
OPT_SUBCOMMAND("drop", &fn, cmd_reflog_drop),
475
OPT_SUBCOMMAND("expire", &fn, cmd_reflog_expire),