| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,7 +16,7 @@ Requirements | |||
| 16 | 16 | ||
| 17 | 17 | * Git_ tested with 1.5.3.7 | |
| 18 | 18 | * `Python Nose`_ - used for running the tests | |
| 19 | - * `Mock by Michael Foord`_ used for tests. Requires 0.4 | ||
| 19 | + * `Mock by Michael Foord`_ used for tests. Requires 0.5 | ||
| 20 | 20 | ||
| 21 | 21 | .. _Git: http://git-scm.com/ | |
| 22 | 22 | .. _Python Nose: http://code.google.com/p/python-nose/ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -24,68 +24,68 @@ initialize GitPython with a bare repository. | |||
| 24 | 24 | A repo object provides high-level access to your data, it allows you to create | |
| 25 | 25 | and delete heads, tags and remotes and access the configuration of the | |
| 26 | 26 | repository. | |
| 27 | - | ||
| 28 | - >>> repo.config_reader() # get a config reader for read-only access | ||
| 29 | - >>> repo.config_writer() # get a config writer to change configuration | ||
| 27 | + | ||
| 28 | + >>> repo.config_reader() # get a config reader for read-only access | ||
| 29 | + >>> repo.config_writer() # get a config writer to change configuration | ||
| 30 | 30 | ||
| 31 | 31 | Query the active branch, query untracked files or whether the repository data | |
| 32 | 32 | has been modified. | |
| 33 | - | ||
| 34 | - >>> repo.is_dirty() | ||
| 35 | - False | ||
| 36 | - >>> repo.untracked_files() | ||
| 37 | - ['my_untracked_file'] | ||
| 38 | - | ||
| 33 | + | ||
| 34 | + >>> repo.is_dirty() | ||
| 35 | + False | ||
| 36 | + >>> repo.untracked_files() | ||
| 37 | + ['my_untracked_file'] | ||
| 38 | + | ||
| 39 | 39 | Clone from existing repositories or initialize new empty ones. | |
| 40 | 40 | ||
| 41 | - >>> cloned_repo = repo.clone("to/this/path") | ||
| 42 | - >>> new_repo = repo.init("path/for/new/repo") | ||
| 43 | - | ||
| 41 | + >>> cloned_repo = repo.clone("to/this/path") | ||
| 42 | + >>> new_repo = repo.init("path/for/new/repo") | ||
| 43 | + | ||
| 44 | 44 | Archive the repository contents to a tar file. | |
| 45 | 45 | ||
| 46 | - >>> repo.archive(open("repo.tar",'w')) | ||
| 47 | - | ||
| 46 | + >>> repo.archive(open("repo.tar",'w')) | ||
| 47 | + | ||
| 48 | 48 | Examining References | |
| 49 | 49 | ******************** | |
| 50 | 50 | ||
| 51 | 51 | References are the tips of your commit graph from which you can easily examine | |
| 52 | 52 | the history of your project. | |
| 53 | 53 | ||
| 54 | 54 | >>> heads = repo.heads | |
| 55 | - >>> master = heads.master # lists can be accessed by name for convenience | ||
| 56 | - >>> master.commit # the commit pointed to by head called master | ||
| 57 | - >>> master.rename("new_name") # rename individual heads or | ||
| 55 | + >>> master = heads.master # lists can be accessed by name for convenience | ||
| 56 | + >>> master.commit # the commit pointed to by head called master | ||
| 57 | + >>> master.rename("new_name") # rename individual heads or | ||
| 58 | 58 | ||
| 59 | 59 | Tags are (usually immutable) references to a commit and/or a tag object. | |
| 60 | 60 | ||
| 61 | - >>> tags = repo.tags | ||
| 62 | - >>> tagref = tags[0] | ||
| 63 | - >>> tagref.tag # tags may have tag objects carrying additional information | ||
| 64 | - >>> tagref.commit # but they always point to commits | ||
| 65 | - >>> repo.delete_tag(tagref) # delete or | ||
| 66 | - >>> repo.create_tag("my_tag") # create tags using the repo | ||
| 67 | - | ||
| 61 | + >>> tags = repo.tags | ||
| 62 | + >>> tagref = tags[0] | ||
| 63 | + >>> tagref.tag # tags may have tag objects carrying additional information | ||
| 64 | + >>> tagref.commit # but they always point to commits | ||
| 65 | + >>> repo.delete_tag(tagref) # delete or | ||
| 66 | + >>> repo.create_tag("my_tag") # create tags using the repo | ||
| 67 | + | ||
| 68 | 68 | A symbolic reference is a special case of a reference as it points to another | |
| 69 | 69 | reference instead of a commit | |
| 70 | 70 | ||
| 71 | 71 | Modifying References | |
| 72 | 72 | ******************** | |
| 73 | 73 | You can easily create and delete reference types or modify where they point to. | |
| 74 | 74 | ||
| 75 | - >>> repo.delete_head('master') | ||
| 76 | - >>> master = repo.create_head('master') | ||
| 77 | - >>> master.commit = 'HEAD~10' # set another commit without changing index or working tree | ||
| 75 | + >>> repo.delete_head('master') | ||
| 76 | + >>> master = repo.create_head('master') | ||
| 77 | + >>> master.commit = 'HEAD~10' # set another commit without changing index or working tree | ||
| 78 | 78 | ||
| 79 | 79 | Create or delete tags the same way except you may not change them afterwards | |
| 80 | 80 | ||
| 81 | - >>> new_tag = repo.create_tag('my_tag', 'my message') | ||
| 82 | - >>> repo.delete_tag(new_tag) | ||
| 83 | - | ||
| 81 | + >>> new_tag = repo.create_tag('my_tag', 'my message') | ||
| 82 | + >>> repo.delete_tag(new_tag) | ||
| 83 | + | ||
| 84 | 84 | Change the symbolic reference to switch branches cheaply ( without adjusting the index | |
| 85 | 85 | or the working copy ) | |
| 86 | 86 | ||
| 87 | - >>> new_branch = repo.create_head('new_branch') | ||
| 88 | - >>> repo.head.reference = new_branch | ||
| 87 | + >>> new_branch = repo.create_head('new_branch') | ||
| 88 | + >>> repo.head.reference = new_branch | ||
| 89 | 89 | ||
| 90 | 90 | Understanding Objects | |
| 91 | 91 | ********************* | |
@@ -98,43 +98,43 @@ Git only knows 4 distinct object types being Blobs, Trees, Commits and Tags. | |||
| 98 | 98 | In Git-Pyhton, all objects can be accessed through their common base, compared | |
| 99 | 99 | and hashed, as shown in the following example. | |
| 100 | 100 | ||
| 101 | - >>> hc = repo.head.commit | ||
| 102 | - >>> hct = hc.tree | ||
| 103 | - >>> hc != hct | ||
| 104 | - >>> hc != repo.tags[0] | ||
| 105 | - >>> hc == repo.head.reference.commit | ||
| 106 | - | ||
| 101 | + >>> hc = repo.head.commit | ||
| 102 | + >>> hct = hc.tree | ||
| 103 | + >>> hc != hct | ||
| 104 | + >>> hc != repo.tags[0] | ||
| 105 | + >>> hc == repo.head.reference.commit | ||
| 106 | + | ||
| 107 | 107 | Basic fields are | |
| 108 | 108 | ||
| 109 | - >>> hct.type | ||
| 110 | - 'tree' | ||
| 111 | - >>> hct.size | ||
| 112 | - 166 | ||
| 113 | - >>> hct.sha | ||
| 114 | - 'a95eeb2a7082212c197cabbf2539185ec74ed0e8' | ||
| 115 | - >>> hct.data # returns string with pure uncompressed data | ||
| 116 | - '...' | ||
| 117 | - >>> len(hct.data) == hct.size | ||
| 118 | - | ||
| 109 | + >>> hct.type | ||
| 110 | + 'tree' | ||
| 111 | + >>> hct.size | ||
| 112 | + 166 | ||
| 113 | + >>> hct.sha | ||
| 114 | + 'a95eeb2a7082212c197cabbf2539185ec74ed0e8' | ||
| 115 | + >>> hct.data # returns string with pure uncompressed data | ||
| 116 | + '...' | ||
| 117 | + >>> len(hct.data) == hct.size | ||
| 118 | + | ||
| 119 | 119 | Index Objects are objects that can be put into gits index. These objects are trees | |
| 120 | 120 | and blobs which additionally know about their path in the filesystem as well as their | |
| 121 | 121 | mode. | |
| 122 | 122 | ||
| 123 | - >>> hct.path # root tree has no path | ||
| 124 | - '' | ||
| 125 | - >>> hct.trees[0].path # the first subdirectory has one though | ||
| 126 | - 'dir' | ||
| 127 | - >>> htc.mode # trees have mode 0 | ||
| 128 | - 0 | ||
| 129 | - >>> '%o' % htc.blobs[0].mode # blobs have a specific mode though comparable to a standard linux fs | ||
| 130 | - 100644 | ||
| 131 | - | ||
| 123 | + >>> hct.path # root tree has no path | ||
| 124 | + '' | ||
| 125 | + >>> hct.trees[0].path # the first subdirectory has one though | ||
| 126 | + 'dir' | ||
| 127 | + >>> htc.mode # trees have mode 0 | ||
| 128 | + 0 | ||
| 129 | + >>> '%o' % htc.blobs[0].mode # blobs have a specific mode though comparable to a standard linux fs | ||
| 130 | + 100644 | ||
| 131 | + | ||
| 132 | 132 | Access blob data (or any object data) directly or using streams. | |
| 133 | - >>> htc.data # binary tree data | ||
| 134 | - >>> htc.blobs[0].data_stream # stream object to read data from | ||
| 135 | - >>> htc.blobs[0].stream_data(my_stream) # write data to given stream | ||
| 136 | - | ||
| 137 | - | ||
| 133 | + >>> htc.data # binary tree data | ||
| 134 | + >>> htc.blobs[0].data_stream # stream object to read data from | ||
| 135 | + >>> htc.blobs[0].stream_data(my_stream) # write data to given stream | ||
| 136 | + | ||
| 137 | + | ||
| 138 | 138 | The Commit object | |
| 139 | 139 | ***************** | |
| 140 | 140 | ||
@@ -171,7 +171,7 @@ The above will return commits 21-30 from the commit list. | |||
| 171 | 171 | >>> headcommit.author | |
| 172 | 172 | <git.Actor "Michael Trier <mtrier@gmail.com>"> | |
| 173 | 173 | ||
| 174 | - >>> headcommit.authored_date # seconds since epoch | ||
| 174 | + >>> headcommit.authored_date # seconds since epoch | ||
| 175 | 175 | 1256291446 | |
| 176 | 176 | ||
| 177 | 177 | >>> headcommit.committer | |
@@ -184,7 +184,7 @@ The above will return commits 21-30 from the commit list. | |||
| 184 | 184 | 'cleaned up a lot of test information. Fixed escaping so it works with | |
| 185 | 185 | subprocess.' | |
| 186 | 186 | ||
| 187 | - Note: date time is represented in a `seconds since epock`_ format. Conversion to | ||
| 187 | + Note: date time is represented in a ``seconds since epock`` format. Conversion to | ||
| 188 | 188 | human readable form can be accomplished with the various time module methods. | |
| 189 | 189 | ||
| 190 | 190 | >>> import time | |
@@ -216,14 +216,14 @@ the root tree of the latest commit on the master branch. | |||
| 216 | 216 | ||
| 217 | 217 | Once you have a tree, you can get the contents. | |
| 218 | 218 | ||
| 219 | - >>> tree.trees # trees are subdirectories | ||
| 219 | + >>> tree.trees # trees are subdirectories | ||
| 220 | 220 | [<git.Tree "f7eb5df2e465ab621b1db3f5714850d6732cfed2">] | |
| 221 | 221 | ||
| 222 | - >>> tree.blobs # blobs are files | ||
| 222 | + >>> tree.blobs # blobs are files | ||
| 223 | 223 | [<git.Blob "a871e79d59cf8488cac4af0c8f990b7a989e2b53">, | |
| 224 | - <git.Blob "3594e94c04db171e2767224db355f514b13715c5">, | ||
| 225 | - <git.Blob "e79b05161e4836e5fbf197aeb52515753e8d6ab6">, | ||
| 226 | - <git.Blob "94954abda49de8615a048f8d2e64b5de848e27a1">] | ||
| 224 | + <git.Blob "3594e94c04db171e2767224db355f514b13715c5">, | ||
| 225 | + <git.Blob "e79b05161e4836e5fbf197aeb52515753e8d6ab6">, | ||
| 226 | + <git.Blob "94954abda49de8615a048f8d2e64b5de848e27a1">] | ||
| 227 | 227 | ||
| 228 | 228 | Its useful to know that a tree behaves like a list with the ability to | |
| 229 | 229 | query entries by name. | |
@@ -260,60 +260,60 @@ You can also get a tree directly from the repository if you know its name. | |||
| 260 | 260 | As trees only allow direct access to their direct entries, use the traverse | |
| 261 | 261 | method to obtain an iterator to access entries recursively. | |
| 262 | 262 | ||
| 263 | - >>> tree.traverse() | ||
| 264 | - <generator object at 0x7f6598bd65a8> | ||
| 265 | - >>> for entry in traverse(): do_something(entry) | ||
| 263 | + >>> tree.traverse() | ||
| 264 | + <generator object at 0x7f6598bd65a8> | ||
| 265 | + >>> for entry in traverse(): do_something(entry) | ||
| 266 | 266 | ||
| 267 | - | ||
| 267 | + | ||
| 268 | 268 | The Index Object | |
| 269 | 269 | **************** | |
| 270 | 270 | The git index is the stage containing changes to be written to the next commit | |
| 271 | 271 | or where merges finally have to take place. You may freely access and manipulate | |
| 272 | 272 | this information using the Index Object. | |
| 273 | 273 | ||
| 274 | - >>> index = repo.index | ||
| 275 | - | ||
| 274 | + >>> index = repo.index | ||
| 275 | + | ||
| 276 | 276 | Access objects and add/remove entries. Commit the changes. | |
| 277 | 277 | ||
| 278 | - >>> for stage,blob in index.iter_blobs(): do_something(...) | ||
| 279 | - Access blob objects | ||
| 280 | - >>> for (path,stage),entry in index.entries.iteritems: pass | ||
| 281 | - Access the entries directly | ||
| 282 | - >>> index.add(['my_new_file']) # add a new file to the index | ||
| 283 | - >>> index.remove(['dir/existing_file']) | ||
| 284 | - >>> new_commit = index.commit("my commit message") | ||
| 285 | - | ||
| 278 | + >>> for stage,blob in index.iter_blobs(): do_something(...) | ||
| 279 | + Access blob objects | ||
| 280 | + >>> for (path,stage),entry in index.entries.iteritems: pass | ||
| 281 | + Access the entries directly | ||
| 282 | + >>> index.add(['my_new_file']) # add a new file to the index | ||
| 283 | + >>> index.remove(['dir/existing_file']) | ||
| 284 | + >>> new_commit = index.commit("my commit message") | ||
| 285 | + | ||
| 286 | 286 | Create new indices from other trees or as result of a merge. Write that result to | |
| 287 | 287 | a new index. | |
| 288 | 288 | ||
| 289 | - >>> tmp_index = Index.from_tree(repo, 'HEAD~1') # load a tree into a temporary index | ||
| 290 | - >>> merge_index = Index.from_tree(repo, 'HEAD', 'some_branch') # merge two trees | ||
| 291 | - >>> merge_index.write("merged_index") | ||
| 292 | - | ||
| 289 | + >>> tmp_index = Index.from_tree(repo, 'HEAD~1') # load a tree into a temporary index | ||
| 290 | + >>> merge_index = Index.from_tree(repo, 'HEAD', 'some_branch') # merge two trees | ||
| 291 | + >>> merge_index.write("merged_index") | ||
| 292 | + | ||
| 293 | 293 | Handling Remotes | |
| 294 | 294 | **************** | |
| 295 | 295 | ||
| 296 | 296 | Remotes are used as alias for a foreign repository to ease pushing to and fetching | |
| 297 | 297 | from them. | |
| 298 | 298 | ||
| 299 | - >>> test_remote = repo.create_remote('test', 'git@server:repo.git') | ||
| 300 | - >>> repo.delete_remote(test_remote) # create and delete remotes | ||
| 301 | - >>> origin = repo.remotes.origin # get default remote by name | ||
| 302 | - >>> origin.refs # local remote references | ||
| 303 | - >>> o = origin.rename('new_origin') # rename remotes | ||
| 304 | - >>> o.fetch() # fetch, pull and push from and to the remote | ||
| 305 | - >>> o.pull() | ||
| 306 | - >>> o.push() | ||
| 299 | + >>> test_remote = repo.create_remote('test', 'git@server:repo.git') | ||
| 300 | + >>> repo.delete_remote(test_remote) # create and delete remotes | ||
| 301 | + >>> origin = repo.remotes.origin # get default remote by name | ||
| 302 | + >>> origin.refs # local remote references | ||
| 303 | + >>> o = origin.rename('new_origin') # rename remotes | ||
| 304 | + >>> o.fetch() # fetch, pull and push from and to the remote | ||
| 305 | + >>> o.pull() | ||
| 306 | + >>> o.push() | ||
| 307 | 307 | ||
| 308 | 308 | You can easily access configuration information for a remote by accessing options | |
| 309 | 309 | as if they where attributes. | |
| 310 | 310 | ||
| 311 | - >>> o.url | ||
| 312 | - 'git@server:dummy_repo.git' | ||
| 313 | - | ||
| 311 | + >>> o.url | ||
| 312 | + 'git@server:dummy_repo.git' | ||
| 313 | + | ||
| 314 | 314 | Change configuration for a specific remote only | |
| 315 | - >>> o.config_writer.set("url", "other_url") | ||
| 316 | - | ||
| 315 | + >>> o.config_writer.set("url", "other_url") | ||
| 316 | + | ||
| 317 | 317 | Obtaining Diff Information | |
| 318 | 318 | ************************** | |
| 319 | 319 | ||
@@ -325,30 +325,30 @@ Diffs can be made between Index and Trees, Index and the working tree, trees and | |||
| 325 | 325 | trees as well as trees and the working copy. If commits are involved, their tree | |
| 326 | 326 | will be used implicitly. | |
| 327 | 327 | ||
| 328 | - >>> hcommit = repo.head.commit | ||
| 329 | - >>> idiff = hcommit.diff() # diff tree against index | ||
| 330 | - >>> tdiff = hcommit.diff('HEAD~1') # diff tree against previous tree | ||
| 331 | - >>> wdiff = hcommit.diff(None) # diff tree against working tree | ||
| 332 | - | ||
| 333 | - >>> index = repo.index | ||
| 334 | - >>> index.diff() # diff index against itself yielding empty diff | ||
| 335 | - >>> index.diff(None) # diff index against working copy | ||
| 336 | - >>> index.diff('HEAD') # diff index against current HEAD tree | ||
| 328 | + >>> hcommit = repo.head.commit | ||
| 329 | + >>> idiff = hcommit.diff() # diff tree against index | ||
| 330 | + >>> tdiff = hcommit.diff('HEAD~1') # diff tree against previous tree | ||
| 331 | + >>> wdiff = hcommit.diff(None) # diff tree against working tree | ||
| 332 | + | ||
| 333 | + >>> index = repo.index | ||
| 334 | + >>> index.diff() # diff index against itself yielding empty diff | ||
| 335 | + >>> index.diff(None) # diff index against working copy | ||
| 336 | + >>> index.diff('HEAD') # diff index against current HEAD tree | ||
| 337 | 337 | ||
| 338 | 338 | The item returned is a DiffIndex which is essentially a list of Diff objects. It | |
| 339 | 339 | provides additional filtering to find what you might be looking for | |
| 340 | 340 | ||
| 341 | - >>> for diff_added in wdiff.iter_change_type('A'): do_something(diff_added) | ||
| 341 | + >>> for diff_added in wdiff.iter_change_type('A'): do_something(diff_added) | ||
| 342 | 342 | ||
| 343 | 343 | Switching Branches | |
| 344 | 344 | ****************** | |
| 345 | 345 | To switch between branches, you effectively need to point your HEAD to the new branch | |
| 346 | 346 | head and reset your index and working copy to match. A simple manual way to do it | |
| 347 | 347 | is the following one. | |
| 348 | 348 | ||
| 349 | - >>> repo.head.reference = repo.heads.other_branch | ||
| 350 | - >>> repo.head.reset(index=True, working_tree=True | ||
| 351 | - | ||
| 349 | + >>> repo.head.reference = repo.heads.other_branch | ||
| 350 | + >>> repo.head.reset(index=True, working_tree=True | ||
| 351 | + | ||
| 352 | 352 | The previous approach would brutally overwrite the user's changes in the working copy | |
| 353 | 353 | and index though and is less sophisticated than a git-checkout for instance which | |
| 354 | 354 | generally prevents you from destroying your work. | |
@@ -358,15 +358,15 @@ Using git directly | |||
| 358 | 358 | In case you are missing functionality as it has not been wrapped, you may conveniently | |
| 359 | 359 | use the git command directly. It is owned by each repository instance. | |
| 360 | 360 | ||
| 361 | - >>> git = repo.git | ||
| 362 | - >>> git.checkout('head', b="my_new_branch") # default command | ||
| 363 | - >>> git.for_each_ref() # '-' becomes '_' when calling it | ||
| 364 | - | ||
| 361 | + >>> git = repo.git | ||
| 362 | + >>> git.checkout('head', b="my_new_branch") # default command | ||
| 363 | + >>> git.for_each_ref() # '-' becomes '_' when calling it | ||
| 364 | + | ||
| 365 | 365 | The return value will by default be a string of the standard output channel produced | |
| 366 | 366 | by the command. | |
| 367 | 367 | ||
| 368 | 368 | Keyword arguments translate to short and long keyword arguments on the commandline. | |
| 369 | - The special notion `git.command(flag=True)`_ will create a flag without value like | ||
| 369 | + The special notion ``git.command(flag=True)`` will create a flag without value like | ||
| 370 | 370 | ``command --flag``. | |
| 371 | 371 | ||
| 372 | 372 | If ``None`` is found in the arguments, it will be dropped silently. Lists and tuples | |
| Back | FazBrowse Home | New Git URL |
0 commit comments