From: Charles Bailey <charles@hashpling.org>
To: Junio C Hamano <gitster@pobox.com>, git@vger.kernel.org
Subject: [PATCH] Add list-all-objects command
Date: Sun, 21 Jun 2015 20:20:31 +0100 [thread overview]
Message-ID: <1434914431-7745-2-git-send-email-charles@hashpling.org> (raw)
In-Reply-To: <1434914431-7745-1-git-send-email-charles@hashpling.org>
From: Charles Bailey <cbailey32@bloomberg.net>
list-all-objects is a command to print the ids of all objects in the
object database of a repository. It is designed as a low overhead
interface for scripts that want to analyse all objects but don't require
the ordering implied by a revision walk.
It will list all objects, loose and packed, and will include unreachable
objects.
list-all-objects is faster that "rev-list --all --objects" but there is
no guarantee as to the order in which objects will be listed.
Signed-off-by: Charles Bailey <cbailey32@bloomberg.net>
---
Documentation/git-list-all-objects.txt | 29 +++++++++++++++
Makefile | 1 +
builtin.h | 1 +
builtin/list-all-objects.c | 64 ++++++++++++++++++++++++++++++++++
git.c | 1 +
t/t8100-list-all-objects.sh | 45 ++++++++++++++++++++++++
6 files changed, 141 insertions(+)
create mode 100644 Documentation/git-list-all-objects.txt
create mode 100644 builtin/list-all-objects.c
create mode 100755 t/t8100-list-all-objects.sh
diff --git a/Documentation/git-list-all-objects.txt b/Documentation/git-list-all-objects.txt
new file mode 100644
index 0000000..5f28d41
--- /dev/null
+++ b/Documentation/git-list-all-objects.txt
@@ -0,0 +1,29 @@
+git-list-all-objects(1)
+=======================
+
+NAME
+----
+git-list-all-objects - List all objects in the repository.
+
+SYNOPSIS
+--------
+[verse]
+'git list-all-objects' [-v|--verbose]
+
+DESCRIPTION
+-----------
+List the ids of all objects in a repository, including any unreachable objects.
+If `--verbose` is specified then the object's type and size is printed out as
+well as its id.
+
+OPTIONS
+-------
+
+-v::
+--verbose::
+ Output in the followin format instead of just printing object ids:
+ <sha1> SP <type> SP <size>
+
+GIT
+---
+Part of the linkgit:git[1] suite
diff --git a/Makefile b/Makefile
index 149f1c7..cf4f0c3 100644
--- a/Makefile
+++ b/Makefile
@@ -853,6 +853,7 @@ BUILTIN_OBJS += builtin/help.o
BUILTIN_OBJS += builtin/index-pack.o
BUILTIN_OBJS += builtin/init-db.o
BUILTIN_OBJS += builtin/interpret-trailers.o
+BUILTIN_OBJS += builtin/list-all-objects.o
BUILTIN_OBJS += builtin/log.o
BUILTIN_OBJS += builtin/ls-files.o
BUILTIN_OBJS += builtin/ls-remote.o
diff --git a/builtin.h b/builtin.h
index b87df70..112bafb 100644
--- a/builtin.h
+++ b/builtin.h
@@ -74,6 +74,7 @@ extern int cmd_help(int argc, const char **argv, const char *prefix);
extern int cmd_index_pack(int argc, const char **argv, const char *prefix);
extern int cmd_init_db(int argc, const char **argv, const char *prefix);
extern int cmd_interpret_trailers(int argc, const char **argv, const char *prefix);
+extern int cmd_list_all_objects(int argc, const char **argv, const char *prefix);
extern int cmd_log(int argc, const char **argv, const char *prefix);
extern int cmd_log_reflog(int argc, const char **argv, const char *prefix);
extern int cmd_ls_files(int argc, const char **argv, const char *prefix);
diff --git a/builtin/list-all-objects.c b/builtin/list-all-objects.c
new file mode 100644
index 0000000..3b43b02
--- /dev/null
+++ b/builtin/list-all-objects.c
@@ -0,0 +1,64 @@
+#include "cache.h"
+#include "builtin.h"
+#include "revision.h"
+#include "parse-options.h"
+
+#include <stdio.h>
+
+static int verbose;
+
+static int print_object(const unsigned char *sha1)
+{
+ if (verbose) {
+ unsigned long size;
+ int type = sha1_object_info(sha1, &size);
+
+ if (type < 0)
+ return -1;
+
+ printf("%s %s %lu\n", sha1_to_hex(sha1), typename(type), size);
+ }
+ else
+ printf("%s\n", sha1_to_hex(sha1));
+
+ return 0;
+}
+
+static int check_loose_object(const unsigned char *sha1,
+ const char *path,
+ void *data)
+{
+ return print_object(sha1);
+}
+
+static int check_packed_object(const unsigned char *sha1,
+ struct packed_git *pack,
+ uint32_t pos,
+ void *data)
+{
+ return print_object(sha1);
+}
+
+static struct option builtin_filter_objects_options[] = {
+ OPT__VERBOSE(&verbose, "show object type and size"),
+ OPT_END()
+};
+
+int cmd_list_all_objects(int argc, const char **argv, const char *prefix)
+{
+ struct packed_git *p;
+
+ argc = parse_options(argc, argv, prefix, builtin_filter_objects_options,
+ NULL, 0);
+
+ for_each_loose_object(check_loose_object, NULL, 0);
+
+ prepare_packed_git();
+ for (p = packed_git; p; p = p->next) {
+ open_pack_index(p);
+ }
+
+ for_each_packed_object(check_packed_object, NULL, 0);
+
+ return 0;
+}
diff --git a/git.c b/git.c
index 44374b1..81e8ae4 100644
--- a/git.c
+++ b/git.c
@@ -417,6 +417,7 @@ static struct cmd_struct commands[] = {
{ "init", cmd_init_db, NO_SETUP },
{ "init-db", cmd_init_db, NO_SETUP },
{ "interpret-trailers", cmd_interpret_trailers, RUN_SETUP },
+ { "list-all-objects", cmd_list_all_objects, RUN_SETUP },
{ "log", cmd_log, RUN_SETUP },
{ "ls-files", cmd_ls_files, RUN_SETUP },
{ "ls-remote", cmd_ls_remote, RUN_SETUP_GENTLY },
diff --git a/t/t8100-list-all-objects.sh b/t/t8100-list-all-objects.sh
new file mode 100755
index 0000000..a7b51ce
--- /dev/null
+++ b/t/t8100-list-all-objects.sh
@@ -0,0 +1,45 @@
+#!/bin/sh
+
+test_description='git list-all-objects'
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ echo hello, world >file &&
+ git add file &&
+ git commit -m "initial"
+'
+
+test_basic_repo_objects () {
+ git cat-file --batch-check="%(objectname)" <<-EOF >expected.unsorted &&
+ HEAD
+ HEAD:file
+ HEAD^{tree}
+ EOF
+ git list-all-objects >all-objects.unsorted &&
+ sort expected.unsorted >expected &&
+ sort all-objects.unsorted >all-objects &&
+ test_cmp all-objects expected
+}
+
+test_expect_success 'list all objects' '
+ test_basic_repo_objects
+'
+test_expect_success 'list all objects after pack' '
+ git repack -Ad &&
+ test_basic_repo_objects
+'
+
+test_expect_success 'verbose output' '
+ git cat-file --batch-check="%(objectname) %(objecttype) %(objectsize)" \
+ <<-EOF >expected.unsorted &&
+ HEAD
+ HEAD:file
+ HEAD^{tree}
+ EOF
+ git list-all-objects -v >all-objects.unsorted &&
+ sort expected.unsorted >expected &&
+ sort all-objects.unsorted >all-objects &&
+ test_cmp all-objects expected
+'
+
+test_done
--
2.4.0.53.g8440f74
next prev parent reply other threads:[~2015-06-21 19:21 UTC|newest]
Thread overview: 51+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-06-19 9:10 Improvements to parse-options and a new filter-objects command Charles Bailey
2015-06-19 9:10 ` [PATCH 1/3] Correct test-parse-options to handle negative ints Charles Bailey
2015-06-19 18:28 ` Junio C Hamano
2015-06-19 9:10 ` [PATCH 2/3] Move unsigned long option parsing out of pack-objects.c Charles Bailey
2015-06-19 11:03 ` Remi Galan Alfonso
2015-06-19 11:06 ` Charles Bailey
2015-06-19 17:58 ` Junio C Hamano
2015-06-19 18:39 ` Junio C Hamano
2015-06-20 15:31 ` Jakub Narębski
2015-06-19 18:47 ` Jakub Narębski
2015-06-20 16:51 ` Charles Bailey
2015-06-20 17:47 ` Junio C Hamano
2015-06-19 9:10 ` [PATCH 3/3] Add filter-objects command Charles Bailey
2015-06-19 10:10 ` Jeff King
2015-06-19 10:33 ` Charles Bailey
2015-06-19 10:52 ` Jeff King
2015-06-19 18:28 ` Junio C Hamano
2015-06-19 10:52 ` John Keeping
2015-06-19 11:04 ` Charles Bailey
2015-06-21 18:25 ` Improvements to integer option parsing Charles Bailey
2015-06-21 18:25 ` [PATCH 1/2] Correct test-parse-options to handle negative ints Charles Bailey
2015-06-21 18:25 ` [PATCH 2/2] Move unsigned long option parsing out of pack-objects.c Charles Bailey
2015-06-21 18:30 ` Charles Bailey
2015-06-22 22:03 ` Junio C Hamano
2015-06-22 22:08 ` Junio C Hamano
2015-06-22 22:09 ` Improvements to integer option parsing Junio C Hamano
2015-06-22 22:42 ` Charles Bailey
2015-06-21 19:20 ` Fast enumeration of objects Charles Bailey
2015-06-21 19:20 ` Charles Bailey [this message]
2015-06-22 8:38 ` [PATCH] Add list-all-objects command Jeff King
2015-06-22 10:33 ` Jeff King
2015-06-22 10:40 ` [PATCH 1/7] for_each_packed_object: automatically open pack index Jeff King
2015-06-22 10:40 ` [PATCH 2/7] cat-file: minor style fix in options list Jeff King
2015-06-22 10:41 ` [PATCH 3/7] cat-file: move batch_options definition to top of file Jeff King
2015-06-22 10:45 ` [PATCH 4/7] cat-file: add --buffer option Jeff King
2015-06-22 10:45 ` [PATCH 5/7] cat-file: stop returning value from batch_one_object Jeff King
2015-06-22 10:45 ` [PATCH 6/7] cat-file: split batch_one_object into two stages Jeff King
2015-06-22 10:45 ` [PATCH 7/7] cat-file: add --batch-all-objects option Jeff King
2015-06-26 6:56 ` Eric Sunshine
2015-06-26 15:48 ` Jeff King
2015-06-22 11:06 ` [PATCH 8/7] cat-file: sort and de-dup output of --batch-all-objects Jeff King
2015-06-22 22:03 ` Charles Bailey
2015-06-22 23:46 ` Jeff King
2015-06-22 21:48 ` [PATCH] Add list-all-objects command Charles Bailey
2015-06-22 21:50 ` Junio C Hamano
2015-06-22 23:50 ` Jeff King
2015-06-22 11:38 ` Charles Bailey
2015-06-22 9:57 ` Duy Nguyen
2015-06-22 10:24 ` Jeff King
2015-06-22 8:35 ` Fast enumeration of objects Jeff King
2015-06-22 19:44 ` Junio C Hamano
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=1434914431-7745-2-git-send-email-charles@hashpling.org \
--to=charles@hashpling.org \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
/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.