-1

So, basically I have 2 versions of a project and for some users, I want to use the latest version while for others, I want to use older version. Both of them have same file names and multiple users will use it simultaneously. To accomplish this, I want to call function from different git branch without actually switching the branch. Is there a way to do so?

for eg., when my current branch is v1 and the other branch is v2; depending on the value of variable flag, call the function

if flag == 1:
    # import function f1() from branch v2
    return f1()
else:
    # use current branch v1
proprius
  • 502
  • 9
  • 20
  • 2
    It might be helpful if you can explain why you need to do this e.g. why you can't have all the code you need on a single branch. This seems like an XY problem. http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem – Tom Dalton Sep 02 '16 at 10:51
  • @TomDalton i have updated the question to explain the details – proprius Sep 02 '16 at 11:12
  • why is this question downvoted? – proprius Sep 02 '16 at 12:25

2 Answers2

1

Without commenting on why you need to do that, you can simply checkout your repo twice: once for branch1, and one for branch2 (without cloning twice).
See "git working on two branches simultaneously".

You can then make your script aware of its current path (/path/to/branch1), and relative path to the other branch (../branch2/...)

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I cannot do this because the code is used parallely by multiple users. If I switch the branch, users for which flag is not 1 might also get the result of v2 branch – proprius Sep 02 '16 at 11:07
  • 1
    @proprius my answer is bout *not* switching the branch. The two branches are already there. If your code knows it, no branch switching of any kind is needed. – VonC Sep 02 '16 at 11:08
0

You must have both versions of the code present / accessible in order to invoke both versions of the code dynamically.

The by-far-simplest way to accomplish this is to have both versions of the code present in different locations, as in VonC's answer.

Since Python is what it is, though, you could dynamically extract specific versions of specific source files, compile them on the fly (using dynamic imports and temporary files, or exec and internal strings), and hence run code that does not show up in casual perusal of the program source. I do not encourage this approach: it is difficult (though not very difficult) and error-prone, tends towards security holes, and is overall a terrible way to work unless you're writing something like a Python debugger or IDE. But if this is what you want to do, you simply decompose the problem into:

  • examine and/or extract specific files from specific commits (git show, git cat-file -p, etc.), and
  • dynamically load or execute code from file in file system or from string in memory.

The first is a Git programming exercise (and is pretty trivial, git show 1234567:foo.py or git show branch:foo.py: you can redirect the output to a file using either shell redirection or Python's subprocess module), and when done with files, the second is a Python programming exercise of moderate difficulty: see the documentation, paying particularly close attention to importlib.

Community
  • 1
  • 1
torek
  • 448,244
  • 59
  • 642
  • 775