Skip to content

Commit

Permalink
Merge branch 'dt/refs-backend-preamble'
Browse files Browse the repository at this point in the history
In preparation for allowing different "backends" to store the refs
in a way different from the traditional "one ref per file in $GIT_DIR
or in a $GIT_DIR/packed-refs file" filesystem storage, reduce
direct filesystem access to ref-like things like CHERRY_PICK_HEAD
from scripts and programs.

* dt/refs-backend-preamble:
  git-stash: use update-ref --create-reflog instead of creating files
  update-ref and tag: add --create-reflog arg
  refs: add REF_FORCE_CREATE_REFLOG flag
  git-reflog: add exists command
  refs: new public ref function: safe_create_reflog
  refs: break out check for reflog autocreation
  refs.c: add err arguments to reflog functions
  • Loading branch information
Junio C Hamano committed Aug 3, 2015
2 parents 8348bf1 + 89dea97 commit b6d323f
Show file tree
Hide file tree
Showing 13 changed files with 239 additions and 86 deletions.
4 changes: 4 additions & 0 deletions Documentation/git-reflog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ depending on the subcommand:
[--dry-run] [--verbose] [--all | <refs>...]
'git reflog delete' [--rewrite] [--updateref]
[--dry-run] [--verbose] ref@\{specifier\}...
'git reflog exists' <ref>

Reference logs, or "reflogs", record when the tips of branches and
other references were updated in the local repository. Reflogs are
Expand Down Expand Up @@ -52,6 +53,9 @@ argument must be an _exact_ entry (e.g. "`git reflog delete
master@{2}`"). This subcommand is also typically not used directly by
end users.

The "exists" subcommand checks whether a ref has a reflog. It exits
with zero status if the reflog exists, and non-zero status if it does
not.

OPTIONS
-------
Expand Down
5 changes: 4 additions & 1 deletion Documentation/git-tag.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ SYNOPSIS
<tagname> [<commit> | <object>]
'git tag' -d <tagname>...
'git tag' [-n[<num>]] -l [--contains <commit>] [--points-at <object>]
[--column[=<options>] | --no-column] [<pattern>...]
[--column[=<options>] | --no-column] [--create-reflog] [<pattern>...]
'git tag' -v <tagname>...

DESCRIPTION
Expand Down Expand Up @@ -142,6 +142,9 @@ This option is only applicable when listing tags without annotation lines.
all, 'whitespace' removes just leading/trailing whitespace lines and
'strip' removes both whitespace and commentary.

--create-reflog::
Create a reflog for the tag.

<tagname>::
The name of the tag to create, delete, or describe.
The new tag name must pass all checks defined by
Expand Down
5 changes: 4 additions & 1 deletion Documentation/git-update-ref.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ git-update-ref - Update the object name stored in a ref safely
SYNOPSIS
--------
[verse]
'git update-ref' [-m <reason>] (-d <ref> [<oldvalue>] | [--no-deref] <ref> <newvalue> [<oldvalue>] | --stdin [-z])
'git update-ref' [-m <reason>] (-d <ref> [<oldvalue>] | [--no-deref] [--create-reflog] <ref> <newvalue> [<oldvalue>] | --stdin [-z])

DESCRIPTION
-----------
Expand Down Expand Up @@ -67,6 +67,9 @@ performs all modifications together. Specify commands of the form:
verify SP <ref> [SP <oldvalue>] LF
option SP <opt> LF

With `--create-reflog`, update-ref will create a reflog for each ref
even if one would not ordinarily be created.

Quote fields containing whitespace as if they were strings in C source
code; i.e., surrounded by double-quotes and with backslash escapes.
Use 40 "0" characters or the empty string to specify a zero value. To
Expand Down
22 changes: 10 additions & 12 deletions builtin/checkout.c
Original file line number Diff line number Diff line change
Expand Up @@ -612,22 +612,20 @@ static void update_refs_for_switch(const struct checkout_opts *opts,
if (opts->new_branch) {
if (opts->new_orphan_branch) {
if (opts->new_branch_log && !log_all_ref_updates) {
int temp;
struct strbuf log_file = STRBUF_INIT;
int ret;
const char *ref_name;

ref_name = mkpath("refs/heads/%s", opts->new_orphan_branch);
temp = log_all_ref_updates;
log_all_ref_updates = 1;
ret = log_ref_setup(ref_name, &log_file);
log_all_ref_updates = temp;
strbuf_release(&log_file);
char *refname;
struct strbuf err = STRBUF_INIT;

refname = mkpathdup("refs/heads/%s", opts->new_orphan_branch);
ret = safe_create_reflog(refname, 1, &err);
free(refname);
if (ret) {
fprintf(stderr, _("Can not do reflog for '%s'\n"),
opts->new_orphan_branch);
fprintf(stderr, _("Can not do reflog for '%s': %s\n"),
opts->new_orphan_branch, err.buf);
strbuf_release(&err);
return;
}
strbuf_release(&err);
}
}
else
Expand Down
33 changes: 32 additions & 1 deletion builtin/reflog.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ static const char reflog_expire_usage[] =
"git reflog expire [--expire=<time>] [--expire-unreachable=<time>] [--rewrite] [--updateref] [--stale-fix] [--dry-run | -n] [--verbose] [--all] <refs>...";
static const char reflog_delete_usage[] =
"git reflog delete [--rewrite] [--updateref] [--dry-run | -n] [--verbose] <refs>...";
static const char reflog_exists_usage[] =
"git reflog exists <ref>";

static unsigned long default_reflog_expire;
static unsigned long default_reflog_expire_unreachable;
Expand Down Expand Up @@ -699,12 +701,38 @@ static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
return status;
}

static int cmd_reflog_exists(int argc, const char **argv, const char *prefix)
{
int i, start = 0;

for (i = 1; i < argc; i++) {
const char *arg = argv[i];
if (!strcmp(arg, "--")) {
i++;
break;
}
else if (arg[0] == '-')
usage(reflog_exists_usage);
else
break;
}

start = i;

if (argc - start != 1)
usage(reflog_exists_usage);

if (check_refname_format(argv[start], REFNAME_ALLOW_ONELEVEL))
die("invalid ref format: %s", argv[start]);
return !reflog_exists(argv[start]);
}

/*
* main "reflog"
*/

static const char reflog_usage[] =
"git reflog [ show | expire | delete ]";
"git reflog [ show | expire | delete | exists ]";

int cmd_reflog(int argc, const char **argv, const char *prefix)
{
Expand All @@ -724,5 +752,8 @@ int cmd_reflog(int argc, const char **argv, const char *prefix)
if (!strcmp(argv[1], "delete"))
return cmd_reflog_delete(argc - 1, argv + 1, prefix);

if (!strcmp(argv[1], "exists"))
return cmd_reflog_exists(argc - 1, argv + 1, prefix);

return cmd_log_reflog(argc, argv, prefix);
}
5 changes: 4 additions & 1 deletion builtin/tag.c
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
struct create_tag_options opt;
char *cleanup_arg = NULL;
int annotate = 0, force = 0, lines = -1;
int create_reflog = 0;
int cmdmode = 0;
const char *msgfile = NULL, *keyid = NULL;
struct msg_arg msg = { 0, STRBUF_INIT };
Expand All @@ -605,6 +606,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
OPT_STRING('u', "local-user", &keyid, N_("key-id"),
N_("use another key to sign the tag")),
OPT__FORCE(&force, N_("replace the tag if exists")),
OPT_BOOL(0, "create-reflog", &create_reflog, N_("create_reflog")),

OPT_GROUP(N_("Tag listing options")),
OPT_COLUMN(0, "column", &colopts, N_("show tag list in columns")),
Expand Down Expand Up @@ -733,7 +735,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
transaction = ref_transaction_begin(&err);
if (!transaction ||
ref_transaction_update(transaction, ref.buf, object, prev,
0, NULL, &err) ||
create_reflog ? REF_FORCE_CREATE_REFLOG : 0,
NULL, &err) ||
ref_transaction_commit(transaction, &err))
die("%s", err.buf);
ref_transaction_free(transaction);
Expand Down
14 changes: 11 additions & 3 deletions builtin/update-ref.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ static const char * const git_update_ref_usage[] = {

static char line_termination = '\n';
static int update_flags;
static unsigned create_reflog_flag;
static const char *msg;

/*
Expand Down Expand Up @@ -200,7 +201,8 @@ static const char *parse_cmd_update(struct ref_transaction *transaction,

if (ref_transaction_update(transaction, refname,
new_sha1, have_old ? old_sha1 : NULL,
update_flags, msg, &err))
update_flags | create_reflog_flag,
msg, &err))
die("%s", err.buf);

update_flags = 0;
Expand Down Expand Up @@ -231,7 +233,8 @@ static const char *parse_cmd_create(struct ref_transaction *transaction,
die("create %s: extra input: %s", refname, next);

if (ref_transaction_create(transaction, refname, new_sha1,
update_flags, msg, &err))
update_flags | create_reflog_flag,
msg, &err))
die("%s", err.buf);

update_flags = 0;
Expand Down Expand Up @@ -354,13 +357,15 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
unsigned char sha1[20], oldsha1[20];
int delete = 0, no_deref = 0, read_stdin = 0, end_null = 0;
unsigned int flags = 0;
int create_reflog = 0;
struct option options[] = {
OPT_STRING( 'm', NULL, &msg, N_("reason"), N_("reason of the update")),
OPT_BOOL('d', NULL, &delete, N_("delete the reference")),
OPT_BOOL( 0 , "no-deref", &no_deref,
N_("update <refname> not the one it points to")),
OPT_BOOL('z', NULL, &end_null, N_("stdin has NUL-terminated arguments")),
OPT_BOOL( 0 , "stdin", &read_stdin, N_("read updates from stdin")),
OPT_BOOL( 0 , "create-reflog", &create_reflog, N_("create_reflog")),
OPT_END(),
};

Expand All @@ -370,6 +375,8 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
if (msg && !*msg)
die("Refusing to perform update with empty message.");

create_reflog_flag = create_reflog ? REF_FORCE_CREATE_REFLOG : 0;

if (read_stdin) {
struct strbuf err = STRBUF_INIT;
struct ref_transaction *transaction;
Expand Down Expand Up @@ -431,5 +438,6 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
flags);
else
return update_ref(msg, refname, sha1, oldval ? oldsha1 : NULL,
flags, UPDATE_REFS_DIE_ON_ERR);
flags | create_reflog_flag,
UPDATE_REFS_DIE_ON_ERR);
}
6 changes: 2 additions & 4 deletions git-stash.sh
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,7 @@ store_stash () {
stash_msg="Created via \"git stash store\"."
fi

# Make sure the reflog for stash is kept.
: >>"$(git rev-parse --git-path logs/$ref_stash)"
git update-ref -m "$stash_msg" $ref_stash $w_commit
git update-ref --create-reflog -m "$stash_msg" $ref_stash $w_commit
ret=$?
test $ret != 0 && test -z $quiet &&
die "$(eval_gettext "Cannot update \$ref_stash with \$w_commit")"
Expand Down Expand Up @@ -262,7 +260,7 @@ save_stash () {
say "$(gettext "No local changes to save")"
exit 0
fi
test -f "$(git rev-parse --git-path logs/$ref_stash)" ||
git reflog exists $ref_stash ||
clear_stash || die "$(gettext "Cannot initialize stash")"

create_stash "$stash_msg" $untracked
Expand Down
Loading

0 comments on commit b6d323f

Please sign in to comment.