Skip to content

Instantly share code, notes, and snippets.

@bobmaerten
Created June 17, 2013 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bobmaerten/5797403 to your computer and use it in GitHub Desktop.
Save bobmaerten/5797403 to your computer and use it in GitHub Desktop.
Utilisation de diff pour obtenir la liste des fichiers/répertoire modifiés par un ou plusieurs commits. À la demande de @nicosomb dans le cadre d'une utilisation d'un hook git.

Voici mon dépôt git contenant la dernière version de oh_my_zsh, avec quelques commits locaux.

$ cd $ZSH
$ git branch -a
* local
  master
  remotes/origin/HEAD -> origin/master
$ git log --pretty=format:"%h %d %s" | head -4
0649746  (HEAD, local) rebase instead of git pull while upgrading
d02f898  Fixed autostart of zsh plugin to my needs
0edb46d  Fixed regression bug to upgrade
b88b5b2  (origin/master, origin/HEAD) Merge pull request #1885

Si je veux obtenir la liste des fichiers modifiés depuis le dernier commit de master, je peux faire un :

$ git diff --name-only master
README.textile
lib/functions.zsh
lib/termsupport.zsh
plugins/debian/debian.plugin.zsh
plugins/jira/jira.plugin.zsh
plugins/postgres/postgres.plugin.zsh
plugins/tmux/tmux.plugin.zsh
plugins/virtualenv/virtualenv.plugin.zsh
plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh
tools/check_for_upgrade.sh
tools/upgrade.sh

Par extension, récupérer la liste des répertoires :

$ for d in $(git diff --name-only master); do dirname $d; done | sort | uniq
.
lib
plugins/debian
plugins/jira
plugins/postgres
plugins/tmux
plugins/virtualenv
plugins/virtualenvwrapper
tools

pour l'utiliser dans un hook git, on peut par exemple de reprendre cette commande pour vérifier qu'un fichier dans un répertoire spécifique n'a pas été commité (voir fichier suivant du gist).

#!/bin/sh
if git-rev-parse --verify HEAD >/dev/null 2>&1; then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
for FILE in `git diff --name-only $against --` ; do
if [ "$(dirname $FILE)" = "lib" ]
then
echo 'Attention, répertoire lib modifié'
exit 1
fi
done
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment