Terence Eden
Terence Eden

Reputation: 14324

git - show filenames and filesize of changed files?

Is there any way to see the new filesize on a diff?

If I run git diff --name-only I get

path/to/changed/file.1
path/to/changed/file.2
path/to/changed/file.3

What I want is to see:

path/to/changed/file.1 1.7KB
path/to/changed/file.2 300Bytes
path/to/changed/file.3 9MB

I can run git diff --name-only | xargs ls -l but that also gives me file permissions, dates, etc.

Is there a built in git diff command to show file size?

Upvotes: 4

Views: 1038

Answers (2)

محسن عباسی
محسن عباسی

Reputation: 2434

By this:

git diff --name-only

You should get:

path/to/changed/file.1
path/to/changed/file.2
path/to/changed/file.3

Since this command shows changed files relative to the project's root directory, you must change directory to root of the project (e.g. where file .gitignore exists).
Now you can get also size of each changed file besides by running this:

git diff --name-only  | xargs du -hs

A sample output is:

5.0K    path/to/changed/file.1
15.0K   path/to/changed/file.2
14.0K   path/to/changed/file.3

If you want exactly your favorite result, run this:

git diff --name-only  | xargs du -hs | awk '{ for (i=NF; i>1; i--) printf("%s ",$i); print $1; }'

And the output:

path/to/changed/file.1  5.0K
path/to/changed/file.2  15.0K
path/to/changed/file.3  14.0K

Upvotes: 5

torek
torek

Reputation: 488213

Is there a built in git diff command to show file size?

No.

Think of Git as a set of tools (because it is). One of the tools is "compare two commits, or one commit and the files I have in my work-tree right now, or similar"; that tool is git diff.1 Adding --name-only gets you just the file names, which is useful as input to another tool.

That other tool might be a Git command, if you want to check on the size of some file inside a commit for instance. Or it might be a non-Git command, as in these comments.

This is more flexible than a built in solution: the tools plug together so that you can construct new solutions to new problems, rather than merely solving one single problem someone else anticipated earlier. If you have to do it very often, though, you might wish to write your own tool. Git allows you to do so: simply write any program, call it git-xyzzy, make it executable, and place it in your $PATH (assuming sh/bash style command line environment; it's %PATH% on Windows). Then, git xyzzy will invoke your git-xyzzy command, with the path augmented to have access to various Git back-end scripts, useful when writing your own sh/bash scripts. For instance, you can use . git-sh-setup to obtain extra tools that Git itself uses internally in its own shell scripts.


1Technically, git diff is more a front end to three separate tools: git diff-tree, git diff-index, and git diff-files. Those three tools are the ones meant for use by other programs. Git calls these plumbing commands.

If you use front-end or porcelain commands like git diff, these front-end commands tend to obey user configuration. For instance, they might color their output, and the set of colors chosen, and when those colors are seen at all, are at the whim of each user. A program that tries to read the output from git diff must compensate for all of these options. In general, that's the wrong way to go about it: instead, use the plumbing command, whose output is predictable and does not depend on user configuration so that you can reliably take the output apart and do useful stuff with it.

There are some specific cases where the user configuration is what you want anyway, or for some reason the plumbing equivalent command is missing (the latter is true often with git log). In this case, go ahead and use the porcelain command in your script.

Upvotes: 2

Related Questions