From: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
To: git@vger.kernel.org
Cc: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: [PATCH 10/24] backup-log: add cat command
Date: Sun, 9 Dec 2018 11:44:05 +0100 [thread overview]
Message-ID: <20181209104419.12639-11-pclouds@gmail.com> (raw)
In-Reply-To: <20181209104419.12639-1-pclouds@gmail.com>
This command introduces a new concept, "change id". This is similar to
"n" in reflog sha-1 extended syntax @{n}. I'm trying to group changes of
the same second together, and this timestamp becomes "change id", so
you view roughly a snapshot of changes. Of course it's not 100%
accurate. But it works most of the time and it keeps log update low.
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
builtin/backup-log.c | 83 +++++++++++++++++++++++++++++++++++++++++++
t/t2080-backup-log.sh | 17 +++++++++
2 files changed, 100 insertions(+)
diff --git a/builtin/backup-log.c b/builtin/backup-log.c
index 75a02c8878..b370ff9d27 100644
--- a/builtin/backup-log.c
+++ b/builtin/backup-log.c
@@ -1,10 +1,13 @@
#include "cache.h"
#include "builtin.h"
#include "backup-log.h"
+#include "dir.h"
+#include "object-store.h"
#include "parse-options.h"
static char const * const backup_log_usage[] = {
N_("git backup-log [--path=<path> | --id=<id>] update <path> <old-hash> <new-hash>"),
+ N_("git backup-log [--path=<path> | --id=<id>] cat [<options>] <change-id> <path>"),
NULL
};
@@ -34,6 +37,84 @@ static int update(int argc, const char **argv,
return ret;
}
+struct cat_options {
+ timestamp_t id;
+ char *path;
+ int before;
+ int show_hash;
+};
+
+static int cat_parse(struct strbuf *line, void *data)
+{
+ struct cat_options *opts = data;
+ struct bkl_entry entry;
+ struct object_id *oid;
+ void *buf;
+ unsigned long size;
+ enum object_type type;
+
+ if (bkl_parse_entry(line, &entry))
+ return -1;
+
+ if (entry.timestamp < opts->id)
+ return 2;
+
+ if (entry.timestamp != opts->id ||
+ fspathcmp(entry.path, opts->path))
+ return 0;
+
+ if (opts->before)
+ oid = &entry.old_oid;
+ else
+ oid = &entry.new_oid;
+
+ if (opts->show_hash) {
+ puts(oid_to_hex(oid));
+ return 1;
+ }
+
+ if (is_null_oid(oid))
+ return 1; /* treat null oid like empty blob */
+
+ buf = read_object_file(oid, &type, &size);
+ if (!buf)
+ die(_("object not found: %s"), oid_to_hex(oid));
+ if (type != OBJ_BLOB)
+ die(_("not a blob: %s"), oid_to_hex(oid));
+
+ write_in_full(1, buf, size);
+ free(buf);
+
+ return 1;
+}
+
+static int cat(int argc, const char **argv,
+ const char *prefix, const char *log_path)
+{
+ struct cat_options opts;
+ struct option options[] = {
+ OPT_BOOL(0, "before", &opts.before, N_("select the version before the update")),
+ OPT_BOOL(0, "hash", &opts.show_hash, N_("show the hash instead of the content")),
+ OPT_END()
+ };
+ char *end = NULL;
+ int ret;
+
+ memset(&opts, 0, sizeof(opts));
+ argc = parse_options(argc, argv, prefix, options, backup_log_usage, 0);
+ if (argc != 2)
+ usage_with_options(backup_log_usage, options);
+ opts.id = strtol(argv[0], &end, 10);
+ if (!end || *end)
+ die(_("not a valid change id: %s"), argv[0]);
+ opts.path = prefix_path(prefix, prefix ? strlen(prefix) : 0, argv[1]);
+ setup_pager();
+ ret = bkl_parse_file_reverse(log_path, cat_parse, &opts);
+ if (ret < 0)
+ die(_("failed to parse '%s'"), log_path);
+ return ret - 1;
+}
+
static char *log_id_to_path(const char *id)
{
if (!strcmp(id, "index"))
@@ -73,6 +154,8 @@ int cmd_backup_log(int argc, const char **argv, const char *prefix)
if (!strcmp(argv[0], "update"))
return update(argc, argv, prefix, log_path);
+ else if (!strcmp(argv[0], "cat"))
+ return cat(argc, argv, prefix, log_path);
else
die(_("unknown subcommand: %s"), argv[0]);
diff --git a/t/t2080-backup-log.sh b/t/t2080-backup-log.sh
index 8d1c8c5935..9c004c9727 100755
--- a/t/t2080-backup-log.sh
+++ b/t/t2080-backup-log.sh
@@ -88,4 +88,21 @@ test_expect_success 'apply --cached writes backup log' '
test_cmp expected .git/index.bkl
'
+test_expect_success 'backup-log cat' '
+ OLD=$(git rev-parse :./initial.t) &&
+ echo update >>initial.t &&
+ test_tick &&
+ git -c core.backupLog=true add initial.t &&
+ NEW=$(git rev-parse :./initial.t) &&
+ git backup-log --id=index cat --before --hash $test_tick initial.t >actual &&
+ echo $OLD >expected &&
+ test_cmp expected actual &&
+ git backup-log --id=index cat --hash $test_tick initial.t >actual &&
+ echo $NEW >expected &&
+ test_cmp expected actual &&
+ git backup-log --id=index cat $test_tick initial.t >actual &&
+ git cat-file blob $NEW >expected &&
+ test_cmp expected actual
+'
+
test_done
--
2.20.0.rc2.486.g9832c05c3d
next prev parent reply other threads:[~2018-12-09 10:45 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-12-09 10:43 [RFC PATCH 00/24] Add backup log Nguyễn Thái Ngọc Duy
2018-12-09 10:43 ` [PATCH 01/24] doc: introduce new "backup log" concept Nguyễn Thái Ngọc Duy
2018-12-09 10:43 ` [PATCH 02/24] backup-log: add "update" subcommand Nguyễn Thái Ngọc Duy
2018-12-09 10:43 ` [PATCH 03/24] read-cache.c: new flag for add_index_entry() to write to backup log Nguyễn Thái Ngọc Duy
2018-12-09 10:43 ` [PATCH 04/24] add: support " Nguyễn Thái Ngọc Duy
2018-12-09 10:44 ` [PATCH 05/24] update-index: support backup log with --keep-backup Nguyễn Thái Ngọc Duy
2018-12-09 10:44 ` [PATCH 06/24] commit: support backup log Nguyễn Thái Ngọc Duy
2018-12-09 10:44 ` [PATCH 07/24] apply: support backup log with --keep-backup Nguyễn Thái Ngọc Duy
2018-12-09 10:44 ` [PATCH 08/24] add--interactive: support backup log Nguyễn Thái Ngọc Duy
2018-12-09 10:44 ` [PATCH 09/24] backup-log.c: add API for walking " Nguyễn Thái Ngọc Duy
2018-12-09 10:44 ` Nguyễn Thái Ngọc Duy [this message]
2018-12-09 10:44 ` [PATCH 11/24] backup-log: add diff command Nguyễn Thái Ngọc Duy
2018-12-09 10:44 ` [PATCH 12/24] backup-log: add log command Nguyễn Thái Ngọc Duy
2018-12-09 10:44 ` [PATCH 13/24] backup-log: add prune command Nguyễn Thái Ngọc Duy
2018-12-09 10:44 ` [PATCH 14/24] gc: prune backup logs Nguyễn Thái Ngọc Duy
2018-12-09 10:44 ` [PATCH 15/24] backup-log: keep all blob references around Nguyễn Thái Ngọc Duy
2018-12-09 10:44 ` [PATCH 16/24] sha1-file.c: let index_path() accept NULL istate Nguyễn Thái Ngọc Duy
2018-12-09 10:44 ` [PATCH 17/24] config --edit: support backup log Nguyễn Thái Ngọc Duy
2018-12-09 10:44 ` [PATCH 18/24] refs: keep backup of deleted reflog Nguyễn Thái Ngọc Duy
2018-12-09 10:44 ` [PATCH 19/24] unpack-trees.c: keep backup of ignored files being overwritten Nguyễn Thái Ngọc Duy
2018-12-09 10:44 ` [PATCH 20/24] reset --hard: keep backup of overwritten files Nguyễn Thái Ngọc Duy
2018-12-09 10:44 ` [PATCH 21/24] checkout -f: " Nguyễn Thái Ngọc Duy
2018-12-09 10:44 ` [PATCH 22/24] am: keep backup of overwritten files on --skip or --abort Nguyễn Thái Ngọc Duy
2018-12-09 10:44 ` [PATCH 23/24] rebase: " Nguyễn Thái Ngọc Duy
2018-12-09 10:44 ` [PATCH 24/24] FIXME Nguyễn Thái Ngọc Duy
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20181209104419.12639-11-pclouds@gmail.com \
--to=pclouds@gmail.com \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.