I'm trying to get a list of files in my repository that have not been touched since a certain date.
I was trying to make use of git log --before="X months ago" --name-only
and then comparing that list against git log --after="X months ago" --name-only
but I'm not sure if there's an easier way?
That or is there a way to get the HEAD commit's date of each file and filter the list? I am unsure of what the best approach is for this.
You can iterate through each file and display its modification time, i.e.:
git ls-tree -r --name-only HEAD | while read filename; do
echo "$(git log -1 --format="%ad" -- $filename) $filename"
done
This will yield output like so:
Fri Dec 23 19:01:01 2011 +0000 Config
Fri Dec 23 19:01:01 2011 +0000 Makefile
PS: You can also use: --format="%ai" (my preference) instead of --format="%ad" for better sortable time stamps.
With --format="%ai", the output will come as:
2015-05-03 21:47:26 -0500 v1/ansible/runner/lookup_plugins/consul_kv.py
2015-05-03 21:47:26 -0500 v1/ansible/runner/lookup_plugins/csvfile.py
2015-05-03 21:47:26 -0500 v1/ansible/runner/lookup_plugins/dict.py
2015-05-03 21:47:26 -0500 v1/ansible/runner/lookup_plugins/dig.py
2015-05-04 01:33:10 -0500 lib/ansible/plugins/filter/__init__.py
2015-05-04 01:33:10 -0500 lib/ansible/plugins/filter/mathstuff.py
2015-05-05 15:24:36 +0200 docs/man/man1/ansible-playbook.1
2015-05-05 15:24:36 +0200 docs/man/man1/ansible-pull.1
2015-05-07 12:22:08 +0100 docsite/rst/playbooks_tags.rst
2015-05-07 22:24:33 +0000 ISSUE_TEMPLATE.md
2015-05-07 23:56:33 -0500 lib/ansible/plugins/cache/redis.py
2015-05-08 13:49:10 -0700 test/units/playbook/test_block.py
2015-05-11 08:06:21 -0400 lib/ansible/module_utils/openstack.py
2015-05-11 22:56:56 -0400 docsite/rst/playbooks_roles.rst
2015-05-12 12:18:55 -0400 v1/ansible/utils/module_docs_fragments
2015-05-12 12:24:57 -0500 test/units/executor/test_play_iterator.py
2015-05-12 12:24:57 -0500 test/units/playbook/test_play.py
2015-05-13 11:04:12 -0700 test/integration/roles/test_bad_parsing/tasks/scenario1.yml
2015-05-14 12:24:36 +0200 docsite/rst/intro_patterns.rst
2015-05-16 15:45:01 -0500 test/units/template/test_safe_eval.py
2015-05-16 15:45:01 -0500 test/units/template/test_vars.py
2015-05-17 01:29:40 -0500 test/units/module_utils/__init__.py
2015-05-17 01:29:40 -0500 test/units/module_utils/test_database.py
2015-05-18 17:26:59 -0700 test/units/plugins/action/__init__.py
2015-05-18 17:26:59 -0700 test/units/plugins/cache/__init__.py
2015-05-18 17:26:59 -0700 test/units/plugins/cache/test_cache.py
2015-05-18 22:44:29 +0200 test/integration/roles/test_cs_firewall/tasks/main.yml
2015-05-19 08:58:27 -0400 test/integration/cloudstack.yml
2015-05-19 15:37:47 -0500 test/integration/roles/test_rax_scaling_group/files/test.txt
2015-05-19 15:37:47 -0500 test/integration/roles/test_rax_scaling_group/meta/main.yml
2015-05-21 12:24:41 -0700 test/integration/roles/test_uri/files/testserver.py
2015-05-23 21:37:12 +0200 test/units/inventory_test_data/group_vars/noparse/all.yml~
2015-05-23 21:37:12 +0200 test/units/inventory_test_data/group_vars/noparse/file.txt
2015-05-23 21:37:12 +0200 test/units/inventory_test_data/group_vars/parse/all.yml
2015-05-27 13:39:09 -0700 test/integration/roles/test_mysql_db/tasks/main.yml
2015-05-28 15:26:03 -0500 hacking/module_formatter.py
2015-05-28 15:26:03 -0500 v1/tests/inventory_test_data/simple_hosts
2015-05-28 15:26:03 -0500 v1/tests/TestInventory.py
2015-05-29 21:49:52 -0400 docsite/rst/guide_aws.rst
2015-05-30 11:35:55 +0200 lib/ansible/utils/module_docs_fragments/cloudstack.py
2015-05-31 23:15:28 -0400 test/integration/roles/prepare_win_tests/meta/main.yml
2015-05-31 23:16:45 -0400 test/integration/test_winrm.yml
2015-06-01 16:41:52 -0500 lib/ansible/parsing/yaml/dumper.py
2015-06-01 21:53:49 +0100 test/integration/roles/test_win_copy/tasks/main.yml
2015-06-02 11:43:35 -0700 packaging/rpm/ansible.spec
2015-06-02 23:39:57 -0400 lib/ansible/utils/path.py
2015-06-02 23:42:00 -0400 lib/ansible/plugins/lookup/password.py
2015-06-03 13:27:31 -0700 lib/ansible/plugins/inventory/__init__.py
2015-06-03 21:19:11 +0200 lib/ansible/module_utils/f5.py
2015-06-03 22:19:26 -0400 hacking/templates/rst.j2
2015-06-04 11:34:56 -0500 v1/ansible/utils/module_docs.py
2015-06-04 15:43:07 -0400 lib/ansible/executor/module_common.py
2015-06-05 07:25:37 -0400 test/integration/roles/test_failed_when/tasks/main.yml
2015-06-05 15:34:37 -0500 test/units/module_utils/test_basic.py
2015-06-06 09:13:14 -0500 lib/ansible/playbook/helpers.py
2015-06-08 19:32:44 -0400 docsite/rst/playbooks_intro.rst
2015-06-09 11:45:53 -0400 lib/ansible/plugins/action/package.py
2015-06-10 15:42:30 +0200 README.md
2015-06-11 13:03:25 -0400 test/units/mock/loader.py
2015-06-11 13:43:47 -0400 lib/ansible/plugins/lookup/file.py
2015-06-12 12:32:02 -0700 lib/ansible/plugins/lookup/url.py
2015-06-15 17:42:40 -0400 test/integration/roles/test_lookups/vars/main.yml
2015-06-16 11:00:03 -0400 docsite/_themes/srtd/layout.html
2015-06-16 14:20:34 +0000 v1/ansible/module_utils/powershell.ps1
2015-06-17 23:31:31 -0400 lib/ansible/plugins/connections/paramiko_ssh.py
2015-06-19 23:04:35 -0400 lib/ansible/template/vars.py
2015-06-21 01:24:35 -0400 lib/ansible/plugins/connections/local.py
2015-06-21 01:51:28 -0400 lib/ansible/plugins/shell/sh.py
2015-06-22 01:17:09 -0400 lib/ansible/playbook/block.py
2015-06-22 02:06:07 -0400 lib/ansible/inventory/group.py
2015-06-23 08:52:57 -0700 docsite/rst/quickstart.rst
2015-06-23 10:19:50 -0400 lib/ansible/playbook/role/include.py
2015-06-23 12:12:38 -0400 lib/ansible/plugins/connections/ssh.py
2015-06-23 13:00:32 -0700 docsite/rst/intro_configuration.rst
2015-06-23 20:48:13 -0400 docsite/rst/faq.rst
2015-06-24 01:00:22 -0700 lib/ansible/plugins/connections/chroot.py
2015-06-26 23:38:06 -0700 docsite/rst/test_strategies.rst
2015-06-27 00:58:03 -0400 lib/ansible/playbook/task.py
2015-06-27 01:01:08 -0400 lib/ansible/playbook/attribute.py
2015-06-28 00:30:27 -0400 lib/ansible/playbook/conditional.py
2015-06-29 11:13:17 -0700 docsite/rst/playbooks_best_practices.rst
2015-06-30 09:38:12 -0500 test/integration/roles/test_rax_cbs_attachments/tasks/main.yml
2015-06-30 09:38:12 -0500 test/integration/roles/test_rax_cbs/tasks/main.yml
2015-06-30 16:08:46 -0400 lib/ansible/playbook/playbook_include.py
2015-07-01 01:48:19 -0400 test/units/playbook/test_playbook.py
2015-07-01 01:48:19 -0400 test/units/playbook/test_taggable.py
2015-07-01 07:23:26 -0700 .travis.yml
2015-07-01 15:10:25 -0400 lib/ansible/plugins/action/patch.py
2015-07-01 18:34:44 -0400 lib/ansible/plugins/filter/ipaddr.py
2015-07-02 14:37:51 +0100 v1/ansible/runner/filter_plugins/core.py
2015-07-02 18:59:58 +0000 hacking/test-module
2015-07-03 13:54:31 -0400 lib/ansible/plugins/lookup/sequence.py
2015-07-03 22:27:49 +0200 lib/ansible/template/__init__.py
2015-07-04 10:23:30 -0400 lib/ansible/cli/galaxy.py
2015-07-05 17:23:22 +0100 lib/ansible/module_utils/facts.py
2015-07-06 10:21:40 +0300 docsite/rst/intro_installation.rst
2015-07-06 13:15:11 -0700 lib/ansible/parsing/splitter.py
2015-07-07 11:28:20 +0200 docsite/rst/index.rst
2015-07-08 09:45:02 -0700 lib/ansible/module_utils/basic.py
2015-07-08 10:11:43 -0400 ticket_stubs/needs_template.md
2015-07-08 20:39:16 -0400 contrib/inventory/rax.py
2015-07-09 08:23:43 -0400 lib/ansible/inventory/host.py
2015-07-09 10:50:31 -0400 test/integration/credentials.template
2015-07-09 10:50:31 -0400 test/integration/roles/test_azure/tasks/main.yml
2015-07-10 01:53:59 -0400 test/integration/non_destructive.yml
2015-07-10 23:48:12 -0400 lib/ansible/plugins/callback/timer.py
2015-07-11 00:47:59 -0400 lib/ansible/plugins/callback/syslog_json.py
2015-07-11 15:01:50 -0400 lib/ansible/plugins/callback/default.py
2015-07-11 21:49:35 -0400 lib/ansible/playbook/role/__init__.py
2015-07-12 16:10:34 -0400 lib/ansible/plugins/callback/mail.py
2015-07-12 16:39:27 -0400 lib/ansible/parsing/__init__.py
2015-07-12 16:39:27 -0400 lib/ansible/playbook/role/definition.py
2015-07-12 16:39:27 -0400 lib/ansible/plugins/__init__.py
2015-07-13 10:34:44 -0400 lib/ansible/plugins/action/assemble.py
2015-07-13 11:06:03 -0400 lib/ansible/plugins/cache/jsonfile.py
2015-07-13 15:42:47 -0400 lib/ansible/cli/adhoc.py
2015-07-13 20:42:09 -0400 lib/ansible/plugins/action/raw.py
2015-07-14 00:23:17 -0400 test/integration/roles/test_command_shell/tasks/main.yml
2015-07-14 07:28:32 -0400 lib/ansible/module_utils/powershell.ps1
2015-07-14 08:05:57 -0400 test/integration/roles/test_good_parsing/tasks/main.yml
2015-07-14 10:07:46 -0400 lib/ansible/utils/module_docs.py
2015-07-14 15:59:00 -0400 test/units/executor/test_task_executor.py
2015-07-14 16:47:47 -0400 lib/ansible/modules/extras
2015-07-14 17:03:57 -0400 lib/ansible/modules/core
2015-07-14 17:33:27 -0400 lib/ansible/cli/doc.py
2015-07-15 01:55:45 -0400 lib/ansible/plugins/strategies/__init__.py
2015-07-15 15:11:46 -0400 lib/ansible/executor/task_executor.py
git command's ls-tree has the advantage of listing only the files stored in the repository, skipping the .git folder and ignored files.
If you send the output of the above operation into a .txt file, then you can sort (Linux sort, see if -n option can help) and then have a BASH command / one-liner to get all rows which are older than a given date/time stamp (as shown in the output above).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With